home *** CD-ROM | disk | FTP | other *** search
Wrap
# Source Generated with Decompyle++ # File: in.pyc (Python 1.5) """Module to analyze Python source code; for syntax coloring tools. Interface: \ttags = fontify(pytext, searchfrom, searchto) The 'pytext' argument is a string containing Python source code. The (optional) arguments 'searchfrom' and 'searchto' may contain a slice in pytext. The returned value is a list of tuples, formatted like this: \t[('keyword', 0, 6, None), ('keyword', 11, 17, None), ('comment', 23, 53, None), etc. ] The tuple contents are always like this: \t(tag, startindex, endindex, sublist) tag is one of 'keyword', 'string', 'comment' or 'identifier' sublist is not used, hence always None. """ __version__ = '0.3.1' import string import regex import string def replace(where, what, with): return string.join(string.split(where, what), with) keywordsList = [ 'assert', 'del', 'from', 'lambda', 'return', 'and', 'elif', 'global', 'not', 'try', 'break', 'else', 'if', 'or', 'while', 'class', 'except', 'import', 'pass', 'continue', 'finally', 'in', 'print', 'def', 'for', 'is', 'raise'] commentPat = '#.*' pat = 'q[^\\q\n]*\\(\\\\[\x00-\xff][^\\q\n]*\\)*q' quotePat = replace(pat, 'q', "'") + '\\|' + replace(pat, 'q', '"') pat = '\n\tqqq\n\t[^\\q]*\n\t\\(\n\t\t\\(\t\\\\[\x00-\xff]\n\t\t\\|\tq\n\t\t\t\\(\t\\\\[\x00-\xff]\n\t\t\t\\|\t[^\\q]\n\t\t\t\\|\tq\n\t\t\t\t\\(\t\\\\[\x00-\xff]\n\t\t\t\t\\|\t[^\\q]\n\t\t\t\t\\)\n\t\t\t\\)\n\t\t\\)\n\t\t[^\\q]*\n\t\\)*\n\tqqq\n' pat = string.join(string.split(pat), '') tripleQuotePat = replace(pat, 'q', "'") + '\\|' + replace(pat, 'q', '"') nonKeyPat = '\\(^\\|[^a-zA-Z0-9_."\']\\)' keyPat = nonKeyPat + '\\(' for keyword in keywordsList: keyPat = keyPat + keyword + '\\|' keyPat = keyPat[:-2] + '\\)' + nonKeyPat matchPat = keyPat + '\\|' + commentPat + '\\|' + tripleQuotePat + '\\|' + quotePat matchRE = regex.compile(matchPat) idKeyPat = '[ \t]*[A-Za-z_][A-Za-z_0-9.]*' idRE = regex.compile(idKeyPat) def fontify(pytext, searchfrom = 0, searchto = None): if searchto is None: searchto = len(pytext) search = matchRE.search group = matchRE.group idSearch = idRE.search idGroup = idRE.group tags = [] tags_append = tags.append commentTag = 'comment' stringTag = 'string' keywordTag = 'keyword' identifierTag = 'identifier' start = 0 end = searchfrom while 1: start = search(pytext, end) if start < 0 or start >= searchto: break match = group(0) end = start + len(match) c = match[0] if c not in '#\'"': if start != searchfrom: match = match[1:-1] start = start + 1 else: match = match[:-1] end = end - 1 tags_append((keywordTag, start, end, None)) if match in [ 'def', 'class']: start = idSearch(pytext, end) if start == end: match = idGroup(0) end = start + len(match) tags_append((identifierTag, start, end, None)) elif c == '#': tags_append((commentTag, start, end, None)) else: tags_append((stringTag, start, end, None)) return tags def test(path): f = open(path) text = f.read() f.close() tags = fontify(text) for tag, start, end, sublist in tags: print tag, `text[start:end]`