def load_comments(self, pkgfile):
""" Open the package and load comments if any.
Return the loaded comments """
# Note: This has to be called with a Python
# source file (.py) only!
if not os.path.exists(pkgfile):
return ""
comment = ""
try:
of = open(pkgfile,'rb')
data = of.read()
if data:
# Create code object
try:
c = compiler.compile(data,pkgfile,'exec')
# Get the position of first line of code
if c:
lno = c.co_firstlineno
lnum = 0
# Read file till this line number
of.seek(0)
for line in of:
comment = "".join((comment, line))
lnum += 1
if lnum==lno or line=="\n": break
except SyntaxError, e:
pass
except Exception, e:
pass
of.close()
except (OSError, IOError, TypeError), e:
pass
return comment
评论列表
文章目录