Make the code a bit more pythonic.

git-svn-id: https://geany.svn.sourceforge.net/svnroot/geany/trunk@4248 ea778897-0a13-0410-b9d1-a72fbfd435f5
This commit is contained in:
Enrico Tröger 2009-09-27 16:49:18 +00:00
parent e0bf328c15
commit 76988d8aae
2 changed files with 113 additions and 227 deletions

View File

@ -1,3 +1,9 @@
2009-09-27 Enrico Tröger <enrico(dot)troeger(at)uvena(dot)de>
* scripts/create_py_tags.py:
Make the code a bit more pythonic.
2009-09-27 Nick Treleaven <nick(dot)treleaven(at)btinternet(dot)com>
* src/keybindings.c:

View File

@ -28,250 +28,130 @@ tag_filename = 'data/python.tags'
matcher = re.compile('^[ \t]*(def|class)[ \t]+([a-zA-Z0-9_]+)[ \t]*(\(.*\))[:]')
def add_tag(tags, tag):
end_pos = tag.find(TA_TYPE)
tagname = tag[0:(end_pos+1)]
# check for duplicates
if len(tagname) < 5:
# skip short tags
return
for test in tags:
if test.startswith(tagname):
# check whether we find a tag line which starts with the same name,
# include the separating TA_TYPE character to ensure we don't match
# writelines() and write()
class Parser:
#----------------------------------------------------------------------
def __init__(self):
self.tags = []
#----------------------------------------------------------------------
def add_tag(self, tag):
"""
Verify the found tag name and if it is valid, add it to the list
@param tag (str)
"""
end_pos = tag.find(TA_TYPE)
tagname = tag[0:(end_pos+1)]
# check for duplicates
if len(tagname) < 5:
# skip short tags
return
tags.append(tag)
for test in self.tags:
if test.startswith(tagname):
# check whether we find a tag line which starts with the same name,
# include the separating TA_TYPE character to ensure we don't match
# writelines() and write()
return
self.tags.append(tag)
#----------------------------------------------------------------------
def process_file(self, filename):
"""
Read the file specified by filename and look for class and function definitions
def treat_file(tags, filename):
try:
fp = open(filename, 'r')
except:
sys.stderr.write('Cannot open %s\n' % filename)
return
while 1:
line = fp.readline()
if not line:
break
m = matcher.match(line)
if m:
name = m.group(2)
if name[0] == '_':
# skip non-public tags
continue;
if m.group(1) == 'class':
type = TYPE_CLASS
else:
type = TYPE_FUNCTION
args = m.group(3).strip()
# tagnameTA_TYPEtypeTA_ARGLISTarglist\n
s = name + TA_TYPE + type + TA_ARGLIST + args + '\n'
add_tag(tags, s)
#~ # maybe for later use, with a more sophisticated algo to retrieve the API
#~ scope = ''
#~ return_value = ''
#~ # tagnameTA_TYPEtypeTA_ARGLISTarglistTA_SCOPEscopeTA_VARTYPEreturn_value\n
#~ s = name + TA_TYPE + type + TA_ARGLIST + args + TA_SCOPE + scope + TA_VARTYPE + return_value + '\n'
@param filename (str)
"""
try:
fp = open(filename, 'r')
except:
sys.stderr.write('Cannot open %s\n' % filename)
return
for line in fp:
m = matcher.match(line)
if m:
tag_type_str, name, args = m.groups()
if not name or name[0] == '_':
# skip non-public tags
continue;
if tag_type_str == 'class':
tag_type = TYPE_CLASS
else:
tag_type = TYPE_FUNCTION
args = args.strip()
# tagnameTA_TYPEtypeTA_ARGLISTarglist\n
s = name + TA_TYPE + tag_type + TA_ARGLIST + args + '\n'
self.add_tag(s)
#~ # maybe for later use, with a more sophisticated algo to retrieve the API
#~ scope = ''
#~ return_value = ''
#~ # tagnameTA_TYPEtypeTA_ARGLISTarglistTA_SCOPEscopeTA_VARTYPEreturn_value\n
#~ s = name + TA_TYPE + type + TA_ARGLIST + args + TA_SCOPE + scope + TA_VARTYPE + return_value + '\n'
#----------------------------------------------------------------------
def tags_to_file(self, filename):
"""
Sort the found tags and write them into the file specified by filename
@param filename (str)
"""
# sort the tags
self.tags.sort()
# write them
fp = open(filename, 'wb')
fp.write(
'# format=tagmanager - Automatically generated file - do not edit (created on %s)\n' % \
datetime.datetime.now().ctime())
for s in self.tags:
if not s == '\n': # skip empty lines
fp.write(s)
fp.close()
# files to include if none were specified on command line
# (this list was created manually and probably needs review for sensible input files)
default_files = [
'/usr/lib/python2.5/anydbm.py',
'/usr/lib/python2.5/asynchat.py',
'/usr/lib/python2.5/asyncore.py',
'/usr/lib/python2.5/audiodev.py',
'/usr/lib/python2.5/base64.py',
'/usr/lib/python2.5/BaseHTTPServer.py',
'/usr/lib/python2.5/Bastion.py',
'/usr/lib/python2.5/bdb.py',
'/usr/lib/python2.5/binhex.py',
'/usr/lib/python2.5/bisect.py',
'/usr/lib/python2.5/calendar.py',
'/usr/lib/python2.5/CGIHTTPServer.py',
'/usr/lib/python2.5/cgi.py',
'/usr/lib/python2.5/cgitb.py',
'/usr/lib/python2.5/chunk.py',
'/usr/lib/python2.5/cmd.py',
'/usr/lib/python2.5/codecs.py',
'/usr/lib/python2.5/codeop.py',
'/usr/lib/python2.5/code.py',
'/usr/lib/python2.5/colorsys.py',
'/usr/lib/python2.5/commands.py',
'/usr/lib/python2.5/compileall.py',
'/usr/lib/python2.5/ConfigParser.py',
'/usr/lib/python2.5/contextlib.py',
'/usr/lib/python2.5/cookielib.py',
'/usr/lib/python2.5/Cookie.py',
'/usr/lib/python2.5/copy.py',
'/usr/lib/python2.5/copy_reg.py',
'/usr/lib/python2.5/cProfile.py',
'/usr/lib/python2.5/csv.py',
'/usr/lib/python2.5/dbhash.py',
'/usr/lib/python2.5/decimal.py',
'/usr/lib/python2.5/difflib.py',
'/usr/lib/python2.5/dircache.py',
'/usr/lib/python2.5/dis.py',
'/usr/lib/python2.5/DocXMLRPCServer.py',
'/usr/lib/python2.5/filecmp.py',
'/usr/lib/python2.5/fileinput.py',
'/usr/lib/python2.5/fnmatch.py',
'/usr/lib/python2.5/formatter.py',
'/usr/lib/python2.5/fpformat.py',
'/usr/lib/python2.5/ftplib.py',
'/usr/lib/python2.5/functools.py',
'/usr/lib/python2.5/getopt.py',
'/usr/lib/python2.5/getpass.py',
'/usr/lib/python2.5/gettext.py',
'/usr/lib/python2.5/glob.py',
'/usr/lib/python2.5/gopherlib.py',
'/usr/lib/python2.5/gzip.py',
'/usr/lib/python2.5/hashlib.py',
'/usr/lib/python2.5/heapq.py',
'/usr/lib/python2.5/hmac.py',
'/usr/lib/python2.5/htmlentitydefs.py',
'/usr/lib/python2.5/htmllib.py',
'/usr/lib/python2.5/HTMLParser.py',
'/usr/lib/python2.5/httplib.py',
'/usr/lib/python2.5/ihooks.py',
'/usr/lib/python2.5/imaplib.py',
'/usr/lib/python2.5/imghdr.py',
'/usr/lib/python2.5/imputil.py',
'/usr/lib/python2.5/inspect.py',
'/usr/lib/python2.5/keyword.py',
'/usr/lib/python2.5/linecache.py',
'/usr/lib/python2.5/locale.py',
'/usr/lib/python2.5/mailbox.py',
'/usr/lib/python2.5/mailcap.py',
'/usr/lib/python2.5/markupbase.py',
'/usr/lib/python2.5/md5.py',
'/usr/lib/python2.5/mhlib.py',
'/usr/lib/python2.5/mimetools.py',
'/usr/lib/python2.5/mimetypes.py',
'/usr/lib/python2.5/MimeWriter.py',
'/usr/lib/python2.5/mimify.py',
'/usr/lib/python2.5/modulefinder.py',
'/usr/lib/python2.5/multifile.py',
'/usr/lib/python2.5/mutex.py',
'/usr/lib/python2.5/netrc.py',
'/usr/lib/python2.5/nntplib.py',
'/usr/lib/python2.5/ntpath.py',
'/usr/lib/python2.5/nturl2path.py',
'/usr/lib/python2.5/opcode.py',
'/usr/lib/python2.5/optparse.py',
'/usr/lib/python2.5/os2emxpath.py',
'/usr/lib/python2.5/os.py',
'/usr/lib/python2.5/pdb.py',
'/usr/lib/python2.5/pickle.py',
'/usr/lib/python2.5/pickletools.py',
'/usr/lib/python2.5/pipes.py',
'/usr/lib/python2.5/pkgutil.py',
'/usr/lib/python2.5/platform.py',
'/usr/lib/python2.5/plistlib.py',
'/usr/lib/python2.5/popen2.py',
'/usr/lib/python2.5/poplib.py',
'/usr/lib/python2.5/posixfile.py',
'/usr/lib/python2.5/posixpath.py',
'/usr/lib/python2.5/pprint.py',
'/usr/lib/python2.5/pty.py',
'/usr/lib/python2.5/py_compile.py',
'/usr/lib/python2.5/pydoc.py',
'/usr/lib/python2.5/Queue.py',
'/usr/lib/python2.5/quopri.py',
'/usr/lib/python2.5/random.py',
'/usr/lib/python2.5/repr.py',
'/usr/lib/python2.5/re.py',
'/usr/lib/python2.5/rexec.py',
'/usr/lib/python2.5/rfc822.py',
'/usr/lib/python2.5/rlcompleter.py',
'/usr/lib/python2.5/robotparser.py',
'/usr/lib/python2.5/runpy.py',
'/usr/lib/python2.5/sched.py',
'/usr/lib/python2.5/sets.py',
'/usr/lib/python2.5/sha.py',
'/usr/lib/python2.5/shelve.py',
'/usr/lib/python2.5/shlex.py',
'/usr/lib/python2.5/shutil.py',
'/usr/lib/python2.5/SimpleHTTPServer.py',
'/usr/lib/python2.5/SimpleXMLRPCServer.py',
'/usr/lib/python2.5/site.py',
'/usr/lib/python2.5/smtpd.py',
'/usr/lib/python2.5/smtplib.py',
'/usr/lib/python2.5/sndhdr.py',
'/usr/lib/python2.5/socket.py',
'/usr/lib/python2.5/SocketServer.py',
'/usr/lib/python2.5/stat.py',
'/usr/lib/python2.5/statvfs.py',
'/usr/lib/python2.5/StringIO.py',
'/usr/lib/python2.5/stringold.py',
'/usr/lib/python2.5/stringprep.py',
'/usr/lib/python2.5/string.py',
'/usr/lib/python2.5/_strptime.py',
'/usr/lib/python2.5/struct.py',
'/usr/lib/python2.5/subprocess.py',
'/usr/lib/python2.5/sunaudio.py',
'/usr/lib/python2.5/sunau.py',
'/usr/lib/python2.5/symbol.py',
'/usr/lib/python2.5/symtable.py',
'/usr/lib/python2.5/tabnanny.py',
'/usr/lib/python2.5/tarfile.py',
'/usr/lib/python2.5/telnetlib.py',
'/usr/lib/python2.5/tempfile.py',
'/usr/lib/python2.5/textwrap.py',
'/usr/lib/python2.5/this.py',
'/usr/lib/python2.5/threading.py',
'/usr/lib/python2.5/timeit.py',
'/usr/lib/python2.5/toaiff.py',
'/usr/lib/python2.5/tokenize.py',
'/usr/lib/python2.5/token.py',
'/usr/lib/python2.5/traceback.py',
'/usr/lib/python2.5/trace.py',
'/usr/lib/python2.5/tty.py',
'/usr/lib/python2.5/types.py',
'/usr/lib/python2.5/unittest.py',
'/usr/lib/python2.5/urllib2.py',
'/usr/lib/python2.5/urllib.py',
'/usr/lib/python2.5/urlparse.py',
'/usr/lib/python2.5/UserDict.py',
'/usr/lib/python2.5/UserList.py',
'/usr/lib/python2.5/user.py',
'/usr/lib/python2.5/UserString.py',
'/usr/lib/python2.5/uuid.py',
'/usr/lib/python2.5/uu.py',
'/usr/lib/python2.5/warnings.py',
'/usr/lib/python2.5/wave.py',
'/usr/lib/python2.5/weakref.py',
'/usr/lib/python2.5/webbrowser.py',
'/usr/lib/python2.5/whichdb.py',
'/usr/lib/python2.5/xdrlib.py',
'/usr/lib/python2.5/zipfile.py' ]
default_files = map(lambda x: '/usr/lib/python2.5/' + x,
[ 'anydbm.py', 'asynchat.py', 'asyncore.py', 'audiodev.py', 'base64.py', 'BaseHTTPServer.py',
'Bastion.py', 'bdb.py', 'binhex.py', 'bisect.py', 'calendar.py', 'CGIHTTPServer.py',
'cgi.py', 'cgitb.py', 'chunk.py', 'cmd.py', 'codecs.py', 'codeop.py', 'code.py', 'colorsys.py',
'commands.py', 'compileall.py', 'ConfigParser.py', 'contextlib.py', 'cookielib.py', 'Cookie.py',
'copy.py', 'copy_reg.py', 'cProfile.py', 'csv.py', 'dbhash.py', 'decimal.py', 'difflib.py',
'dircache.py', 'dis.py', 'DocXMLRPCServer.py', 'filecmp.py', 'fileinput.py', 'fnmatch.py',
'formatter.py', 'fpformat.py', 'ftplib.py', 'functools.py', 'getopt.py', 'getpass.py', 'gettext.py',
'glob.py', 'gopherlib.py', 'gzip.py', 'hashlib.py', 'heapq.py', 'hmac.py', 'htmlentitydefs.py',
'htmllib.py', 'HTMLParser.py', 'httplib.py', 'ihooks.py', 'imaplib.py', 'imghdr.py', 'imputil.py',
'inspect.py', 'keyword.py', 'linecache.py', 'locale.py', 'mailbox.py', 'mailcap.py', 'markupbase.py',
'md5.py', 'mhlib.py', 'mimetools.py', 'mimetypes.py', 'MimeWriter.py', 'mimify.py',
'modulefinder.py', 'multifile.py', 'mutex.py', 'netrc.py', 'nntplib.py', 'ntpath.py',
'nturl2path.py', 'opcode.py', 'optparse.py', 'os2emxpath.py', 'os.py', 'pdb.py', 'pickle.py',
'pickletools.py', 'pipes.py', 'pkgutil.py', 'platform.py', 'plistlib.py', 'popen2.py',
'poplib.py', 'posixfile.py', 'posixpath.py', 'pprint.py', 'pty.py', 'py_compile.py', 'pydoc.py',
'Queue.py', 'quopri.py', 'random.py', 'repr.py', 're.py', 'rexec.py', 'rfc822.py', 'rlcompleter.py',
'robotparser.py', 'runpy.py', 'sched.py', 'sets.py', 'sha.py', 'shelve.py', 'shlex.py', 'shutil.py',
'SimpleHTTPServer.py', 'SimpleXMLRPCServer.py', 'site.py', 'smtpd.py', 'smtplib.py', 'sndhdr.py',
'socket.py', 'SocketServer.py', 'stat.py', 'statvfs.py', 'StringIO.py', 'stringold.py',
'stringprep.py', 'string.py', '_strptime.py', 'struct.py', 'subprocess.py', 'sunaudio.py',
'sunau.py', 'symbol.py', 'symtable.py', 'tabnanny.py', 'tarfile.py', 'telnetlib.py', 'tempfile.py',
'textwrap.py', 'this.py', 'threading.py', 'timeit.py', 'toaiff.py', 'tokenize.py', 'token.py',
'traceback.py', 'trace.py', 'tty.py', 'types.py', 'unittest.py', 'urllib2.py', 'urllib.py',
'urlparse.py', 'UserDict.py', 'UserList.py', 'user.py', 'UserString.py', 'uuid.py', 'uu.py',
'warnings.py', 'wave.py', 'weakref.py', 'webbrowser.py', 'whichdb.py', 'xdrlib.py', 'zipfile.py'
])
def main():
tags = []
# process files given on command line
args = sys.argv[1:]
if not args:
args = default_files
for filename in args:
treat_file(tags, filename)
parser = Parser()
# sort the tags
tags.sort()
# write them
fp = open(tag_filename, 'wb')
fp.write(
'# format=tagmanager - Automatically generated file - do not edit (created on %s)\n' % \
datetime.datetime.now().ctime())
for s in tags:
if not s == '\n': # skip empty lines
fp.write(s)
fp.close()
for filename in args:
parser.process_file(filename)
parser.tags_to_file(tag_filename)
if __name__ == '__main__':