Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/ExeBy/smexe_ro.../usr/lib64/python2....
File: pyclbr.py
"""Parse a Python module and describe its classes and methods.
[0] Fix | Delete
[1] Fix | Delete
Parse enough of a Python file to recognize imports and class and
[2] Fix | Delete
method definitions, and to find out the superclasses of a class.
[3] Fix | Delete
[4] Fix | Delete
The interface consists of a single function:
[5] Fix | Delete
readmodule_ex(module [, path])
[6] Fix | Delete
where module is the name of a Python module, and path is an optional
[7] Fix | Delete
list of directories where the module is to be searched. If present,
[8] Fix | Delete
path is prepended to the system search path sys.path. The return
[9] Fix | Delete
value is a dictionary. The keys of the dictionary are the names of
[10] Fix | Delete
the classes defined in the module (including classes that are defined
[11] Fix | Delete
via the from XXX import YYY construct). The values are class
[12] Fix | Delete
instances of the class Class defined here. One special key/value pair
[13] Fix | Delete
is present for packages: the key '__path__' has a list as its value
[14] Fix | Delete
which contains the package search path.
[15] Fix | Delete
[16] Fix | Delete
A class is described by the class Class in this module. Instances
[17] Fix | Delete
of this class have the following instance variables:
[18] Fix | Delete
module -- the module name
[19] Fix | Delete
name -- the name of the class
[20] Fix | Delete
super -- a list of super classes (Class instances)
[21] Fix | Delete
methods -- a dictionary of methods
[22] Fix | Delete
file -- the file in which the class was defined
[23] Fix | Delete
lineno -- the line in the file on which the class statement occurred
[24] Fix | Delete
The dictionary of methods uses the method names as keys and the line
[25] Fix | Delete
numbers on which the method was defined as values.
[26] Fix | Delete
If the name of a super class is not recognized, the corresponding
[27] Fix | Delete
entry in the list of super classes is not a class instance but a
[28] Fix | Delete
string giving the name of the super class. Since import statements
[29] Fix | Delete
are recognized and imported modules are scanned as well, this
[30] Fix | Delete
shouldn't happen often.
[31] Fix | Delete
[32] Fix | Delete
A function is described by the class Function in this module.
[33] Fix | Delete
Instances of this class have the following instance variables:
[34] Fix | Delete
module -- the module name
[35] Fix | Delete
name -- the name of the class
[36] Fix | Delete
file -- the file in which the class was defined
[37] Fix | Delete
lineno -- the line in the file on which the class statement occurred
[38] Fix | Delete
"""
[39] Fix | Delete
[40] Fix | Delete
import sys
[41] Fix | Delete
import imp
[42] Fix | Delete
import tokenize
[43] Fix | Delete
from token import NAME, DEDENT, OP
[44] Fix | Delete
from operator import itemgetter
[45] Fix | Delete
[46] Fix | Delete
__all__ = ["readmodule", "readmodule_ex", "Class", "Function"]
[47] Fix | Delete
[48] Fix | Delete
_modules = {} # cache of modules we've seen
[49] Fix | Delete
[50] Fix | Delete
# each Python class is represented by an instance of this class
[51] Fix | Delete
class Class:
[52] Fix | Delete
'''Class to represent a Python class.'''
[53] Fix | Delete
def __init__(self, module, name, super, file, lineno):
[54] Fix | Delete
self.module = module
[55] Fix | Delete
self.name = name
[56] Fix | Delete
if super is None:
[57] Fix | Delete
super = []
[58] Fix | Delete
self.super = super
[59] Fix | Delete
self.methods = {}
[60] Fix | Delete
self.file = file
[61] Fix | Delete
self.lineno = lineno
[62] Fix | Delete
[63] Fix | Delete
def _addmethod(self, name, lineno):
[64] Fix | Delete
self.methods[name] = lineno
[65] Fix | Delete
[66] Fix | Delete
class Function:
[67] Fix | Delete
'''Class to represent a top-level Python function'''
[68] Fix | Delete
def __init__(self, module, name, file, lineno):
[69] Fix | Delete
self.module = module
[70] Fix | Delete
self.name = name
[71] Fix | Delete
self.file = file
[72] Fix | Delete
self.lineno = lineno
[73] Fix | Delete
[74] Fix | Delete
def readmodule(module, path=None):
[75] Fix | Delete
'''Backwards compatible interface.
[76] Fix | Delete
[77] Fix | Delete
Call readmodule_ex() and then only keep Class objects from the
[78] Fix | Delete
resulting dictionary.'''
[79] Fix | Delete
[80] Fix | Delete
res = {}
[81] Fix | Delete
for key, value in _readmodule(module, path or []).items():
[82] Fix | Delete
if isinstance(value, Class):
[83] Fix | Delete
res[key] = value
[84] Fix | Delete
return res
[85] Fix | Delete
[86] Fix | Delete
def readmodule_ex(module, path=None):
[87] Fix | Delete
'''Read a module file and return a dictionary of classes.
[88] Fix | Delete
[89] Fix | Delete
Search for MODULE in PATH and sys.path, read and parse the
[90] Fix | Delete
module and return a dictionary with one entry for each class
[91] Fix | Delete
found in the module.
[92] Fix | Delete
'''
[93] Fix | Delete
return _readmodule(module, path or [])
[94] Fix | Delete
[95] Fix | Delete
def _readmodule(module, path, inpackage=None):
[96] Fix | Delete
'''Do the hard work for readmodule[_ex].
[97] Fix | Delete
[98] Fix | Delete
If INPACKAGE is given, it must be the dotted name of the package in
[99] Fix | Delete
which we are searching for a submodule, and then PATH must be the
[100] Fix | Delete
package search path; otherwise, we are searching for a top-level
[101] Fix | Delete
module, and PATH is combined with sys.path.
[102] Fix | Delete
'''
[103] Fix | Delete
# Compute the full module name (prepending inpackage if set)
[104] Fix | Delete
if inpackage is not None:
[105] Fix | Delete
fullmodule = "%s.%s" % (inpackage, module)
[106] Fix | Delete
else:
[107] Fix | Delete
fullmodule = module
[108] Fix | Delete
[109] Fix | Delete
# Check in the cache
[110] Fix | Delete
if fullmodule in _modules:
[111] Fix | Delete
return _modules[fullmodule]
[112] Fix | Delete
[113] Fix | Delete
# Initialize the dict for this module's contents
[114] Fix | Delete
dict = {}
[115] Fix | Delete
[116] Fix | Delete
# Check if it is a built-in module; we don't do much for these
[117] Fix | Delete
if module in sys.builtin_module_names and inpackage is None:
[118] Fix | Delete
_modules[module] = dict
[119] Fix | Delete
return dict
[120] Fix | Delete
[121] Fix | Delete
# Check for a dotted module name
[122] Fix | Delete
i = module.rfind('.')
[123] Fix | Delete
if i >= 0:
[124] Fix | Delete
package = module[:i]
[125] Fix | Delete
submodule = module[i+1:]
[126] Fix | Delete
parent = _readmodule(package, path, inpackage)
[127] Fix | Delete
if inpackage is not None:
[128] Fix | Delete
package = "%s.%s" % (inpackage, package)
[129] Fix | Delete
if not '__path__' in parent:
[130] Fix | Delete
raise ImportError('No package named {}'.format(package))
[131] Fix | Delete
return _readmodule(submodule, parent['__path__'], package)
[132] Fix | Delete
[133] Fix | Delete
# Search the path for the module
[134] Fix | Delete
f = None
[135] Fix | Delete
if inpackage is not None:
[136] Fix | Delete
f, fname, (_s, _m, ty) = imp.find_module(module, path)
[137] Fix | Delete
else:
[138] Fix | Delete
f, fname, (_s, _m, ty) = imp.find_module(module, path + sys.path)
[139] Fix | Delete
if ty == imp.PKG_DIRECTORY:
[140] Fix | Delete
dict['__path__'] = [fname]
[141] Fix | Delete
path = [fname] + path
[142] Fix | Delete
f, fname, (_s, _m, ty) = imp.find_module('__init__', [fname])
[143] Fix | Delete
_modules[fullmodule] = dict
[144] Fix | Delete
if ty != imp.PY_SOURCE:
[145] Fix | Delete
# not Python source, can't do anything with this module
[146] Fix | Delete
f.close()
[147] Fix | Delete
return dict
[148] Fix | Delete
[149] Fix | Delete
stack = [] # stack of (class, indent) pairs
[150] Fix | Delete
[151] Fix | Delete
g = tokenize.generate_tokens(f.readline)
[152] Fix | Delete
try:
[153] Fix | Delete
for tokentype, token, start, _end, _line in g:
[154] Fix | Delete
if tokentype == DEDENT:
[155] Fix | Delete
lineno, thisindent = start
[156] Fix | Delete
# close nested classes and defs
[157] Fix | Delete
while stack and stack[-1][1] >= thisindent:
[158] Fix | Delete
del stack[-1]
[159] Fix | Delete
elif token == 'def':
[160] Fix | Delete
lineno, thisindent = start
[161] Fix | Delete
# close previous nested classes and defs
[162] Fix | Delete
while stack and stack[-1][1] >= thisindent:
[163] Fix | Delete
del stack[-1]
[164] Fix | Delete
tokentype, meth_name, start = g.next()[0:3]
[165] Fix | Delete
if tokentype != NAME:
[166] Fix | Delete
continue # Syntax error
[167] Fix | Delete
if stack:
[168] Fix | Delete
cur_class = stack[-1][0]
[169] Fix | Delete
if isinstance(cur_class, Class):
[170] Fix | Delete
# it's a method
[171] Fix | Delete
cur_class._addmethod(meth_name, lineno)
[172] Fix | Delete
# else it's a nested def
[173] Fix | Delete
else:
[174] Fix | Delete
# it's a function
[175] Fix | Delete
dict[meth_name] = Function(fullmodule, meth_name,
[176] Fix | Delete
fname, lineno)
[177] Fix | Delete
stack.append((None, thisindent)) # Marker for nested fns
[178] Fix | Delete
elif token == 'class':
[179] Fix | Delete
lineno, thisindent = start
[180] Fix | Delete
# close previous nested classes and defs
[181] Fix | Delete
while stack and stack[-1][1] >= thisindent:
[182] Fix | Delete
del stack[-1]
[183] Fix | Delete
tokentype, class_name, start = g.next()[0:3]
[184] Fix | Delete
if tokentype != NAME:
[185] Fix | Delete
continue # Syntax error
[186] Fix | Delete
# parse what follows the class name
[187] Fix | Delete
tokentype, token, start = g.next()[0:3]
[188] Fix | Delete
inherit = None
[189] Fix | Delete
if token == '(':
[190] Fix | Delete
names = [] # List of superclasses
[191] Fix | Delete
# there's a list of superclasses
[192] Fix | Delete
level = 1
[193] Fix | Delete
super = [] # Tokens making up current superclass
[194] Fix | Delete
while True:
[195] Fix | Delete
tokentype, token, start = g.next()[0:3]
[196] Fix | Delete
if token in (')', ',') and level == 1:
[197] Fix | Delete
n = "".join(super)
[198] Fix | Delete
if n in dict:
[199] Fix | Delete
# we know this super class
[200] Fix | Delete
n = dict[n]
[201] Fix | Delete
else:
[202] Fix | Delete
c = n.split('.')
[203] Fix | Delete
if len(c) > 1:
[204] Fix | Delete
# super class is of the form
[205] Fix | Delete
# module.class: look in module for
[206] Fix | Delete
# class
[207] Fix | Delete
m = c[-2]
[208] Fix | Delete
c = c[-1]
[209] Fix | Delete
if m in _modules:
[210] Fix | Delete
d = _modules[m]
[211] Fix | Delete
if c in d:
[212] Fix | Delete
n = d[c]
[213] Fix | Delete
names.append(n)
[214] Fix | Delete
super = []
[215] Fix | Delete
if token == '(':
[216] Fix | Delete
level += 1
[217] Fix | Delete
elif token == ')':
[218] Fix | Delete
level -= 1
[219] Fix | Delete
if level == 0:
[220] Fix | Delete
break
[221] Fix | Delete
elif token == ',' and level == 1:
[222] Fix | Delete
pass
[223] Fix | Delete
# only use NAME and OP (== dot) tokens for type name
[224] Fix | Delete
elif tokentype in (NAME, OP) and level == 1:
[225] Fix | Delete
super.append(token)
[226] Fix | Delete
# expressions in the base list are not supported
[227] Fix | Delete
inherit = names
[228] Fix | Delete
cur_class = Class(fullmodule, class_name, inherit,
[229] Fix | Delete
fname, lineno)
[230] Fix | Delete
if not stack:
[231] Fix | Delete
dict[class_name] = cur_class
[232] Fix | Delete
stack.append((cur_class, thisindent))
[233] Fix | Delete
elif token == 'import' and start[1] == 0:
[234] Fix | Delete
modules = _getnamelist(g)
[235] Fix | Delete
for mod, _mod2 in modules:
[236] Fix | Delete
try:
[237] Fix | Delete
# Recursively read the imported module
[238] Fix | Delete
if inpackage is None:
[239] Fix | Delete
_readmodule(mod, path)
[240] Fix | Delete
else:
[241] Fix | Delete
try:
[242] Fix | Delete
_readmodule(mod, path, inpackage)
[243] Fix | Delete
except ImportError:
[244] Fix | Delete
_readmodule(mod, [])
[245] Fix | Delete
except:
[246] Fix | Delete
# If we can't find or parse the imported module,
[247] Fix | Delete
# too bad -- don't die here.
[248] Fix | Delete
pass
[249] Fix | Delete
elif token == 'from' and start[1] == 0:
[250] Fix | Delete
mod, token = _getname(g)
[251] Fix | Delete
if not mod or token != "import":
[252] Fix | Delete
continue
[253] Fix | Delete
names = _getnamelist(g)
[254] Fix | Delete
try:
[255] Fix | Delete
# Recursively read the imported module
[256] Fix | Delete
d = _readmodule(mod, path, inpackage)
[257] Fix | Delete
except:
[258] Fix | Delete
# If we can't find or parse the imported module,
[259] Fix | Delete
# too bad -- don't die here.
[260] Fix | Delete
continue
[261] Fix | Delete
# add any classes that were defined in the imported module
[262] Fix | Delete
# to our name space if they were mentioned in the list
[263] Fix | Delete
for n, n2 in names:
[264] Fix | Delete
if n in d:
[265] Fix | Delete
dict[n2 or n] = d[n]
[266] Fix | Delete
elif n == '*':
[267] Fix | Delete
# don't add names that start with _
[268] Fix | Delete
for n in d:
[269] Fix | Delete
if n[0] != '_':
[270] Fix | Delete
dict[n] = d[n]
[271] Fix | Delete
except StopIteration:
[272] Fix | Delete
pass
[273] Fix | Delete
[274] Fix | Delete
f.close()
[275] Fix | Delete
return dict
[276] Fix | Delete
[277] Fix | Delete
def _getnamelist(g):
[278] Fix | Delete
# Helper to get a comma-separated list of dotted names plus 'as'
[279] Fix | Delete
# clauses. Return a list of pairs (name, name2) where name2 is
[280] Fix | Delete
# the 'as' name, or None if there is no 'as' clause.
[281] Fix | Delete
names = []
[282] Fix | Delete
while True:
[283] Fix | Delete
name, token = _getname(g)
[284] Fix | Delete
if not name:
[285] Fix | Delete
break
[286] Fix | Delete
if token == 'as':
[287] Fix | Delete
name2, token = _getname(g)
[288] Fix | Delete
else:
[289] Fix | Delete
name2 = None
[290] Fix | Delete
names.append((name, name2))
[291] Fix | Delete
while token != "," and "\n" not in token:
[292] Fix | Delete
token = g.next()[1]
[293] Fix | Delete
if token != ",":
[294] Fix | Delete
break
[295] Fix | Delete
return names
[296] Fix | Delete
[297] Fix | Delete
def _getname(g):
[298] Fix | Delete
# Helper to get a dotted name, return a pair (name, token) where
[299] Fix | Delete
# name is the dotted name, or None if there was no dotted name,
[300] Fix | Delete
# and token is the next input token.
[301] Fix | Delete
parts = []
[302] Fix | Delete
tokentype, token = g.next()[0:2]
[303] Fix | Delete
if tokentype != NAME and token != '*':
[304] Fix | Delete
return (None, token)
[305] Fix | Delete
parts.append(token)
[306] Fix | Delete
while True:
[307] Fix | Delete
tokentype, token = g.next()[0:2]
[308] Fix | Delete
if token != '.':
[309] Fix | Delete
break
[310] Fix | Delete
tokentype, token = g.next()[0:2]
[311] Fix | Delete
if tokentype != NAME:
[312] Fix | Delete
break
[313] Fix | Delete
parts.append(token)
[314] Fix | Delete
return (".".join(parts), token)
[315] Fix | Delete
[316] Fix | Delete
def _main():
[317] Fix | Delete
# Main program for testing.
[318] Fix | Delete
import os
[319] Fix | Delete
mod = sys.argv[1]
[320] Fix | Delete
if os.path.exists(mod):
[321] Fix | Delete
path = [os.path.dirname(mod)]
[322] Fix | Delete
mod = os.path.basename(mod)
[323] Fix | Delete
if mod.lower().endswith(".py"):
[324] Fix | Delete
mod = mod[:-3]
[325] Fix | Delete
else:
[326] Fix | Delete
path = []
[327] Fix | Delete
dict = readmodule_ex(mod, path)
[328] Fix | Delete
objs = dict.values()
[329] Fix | Delete
objs.sort(lambda a, b: cmp(getattr(a, 'lineno', 0),
[330] Fix | Delete
getattr(b, 'lineno', 0)))
[331] Fix | Delete
for obj in objs:
[332] Fix | Delete
if isinstance(obj, Class):
[333] Fix | Delete
print "class", obj.name, obj.super, obj.lineno
[334] Fix | Delete
methods = sorted(obj.methods.iteritems(), key=itemgetter(1))
[335] Fix | Delete
for name, lineno in methods:
[336] Fix | Delete
if name != "__path__":
[337] Fix | Delete
print " def", name, lineno
[338] Fix | Delete
elif isinstance(obj, Function):
[339] Fix | Delete
print "def", obj.name, obj.lineno
[340] Fix | Delete
[341] Fix | Delete
if __name__ == "__main__":
[342] Fix | Delete
_main()
[343] Fix | Delete
[344] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function