3 \4@sdZddlZddlZddlZddlZddlmZmZm Z ddddgZ iZ GdddZ Gd ddZ dd dZdd dZdd d ZddZddZddZedkredS)aParse a Python module and describe its classes and methods. Parse enough of a Python file to recognize imports and class and method definitions, and to find out the superclasses of a class. The interface consists of a single function: readmodule_ex(module [, path]) where module is the name of a Python module, and path is an optional list of directories where the module is to be searched. If present, path is prepended to the system search path sys.path. The return value is a dictionary. The keys of the dictionary are the names of the classes defined in the module (including classes that are defined via the from XXX import YYY construct). The values are class instances of the class Class defined here. One special key/value pair is present for packages: the key '__path__' has a list as its value which contains the package search path. A class is described by the class Class in this module. Instances of this class have the following instance variables: module -- the module name name -- the name of the class super -- a list of super classes (Class instances) methods -- a dictionary of methods file -- the file in which the class was defined lineno -- the line in the file on which the class statement occurred The dictionary of methods uses the method names as keys and the line numbers on which the method was defined as values. If the name of a super class is not recognized, the corresponding entry in the list of super classes is not a class instance but a string giving the name of the super class. Since import statements are recognized and imported modules are scanned as well, this shouldn't happen often. A function is described by the class Function in this module. Instances of this class have the following instance variables: module -- the module name name -- the name of the class file -- the file in which the class was defined lineno -- the line in the file on which the class statement occurred N)NAMEDEDENTOP readmodule readmodule_exClassFunctioncs(eZdZdZfddZddZZS)rz"Class to represent a Python class.cs4||_||_|dkrg}||_i|_||_||_dS)N)modulenamesupermethodsfilelineno)selfr r r r r) __class__/usr/lib64/python3.6/pyclbr.py__init__7szClass.__init__cCs||j|<dS)N)r )rr rrrr _addmethodAszClass._addmethod)__name__ __module__ __qualname____doc__rr __classcell__rr)rrr5s c@seZdZdZddZdS)rz.Class to represent a top-level Python functioncCs||_||_||_||_dS)N)r r r r)rr r r rrrrrFszFunction.__init__N)rrrrrrrrrrDscCs:i}x0t||pgjD]\}}t|tr|||<qW|S)z}Backwards compatible interface. Call readmodule_ex() and then only keep Class objects from the resulting dictionary.) _readmoduleitems isinstancer)r pathreskeyvaluerrrrLs   cCst||p gS)zRead a module file and return a dictionary of classes. Search for MODULE in PATH and sys.path, read and parse the module and return a dictionary with one entry for each class found in the module. )r)r rrrrrXsc&!Cs|dk rd||f}n|}|tkr*t|Si}|tjkrL|dkrL|t|<|S|jd}|dkr|d|}||dd}t|||}|dk rd||f}d|krtdj|t||d|Sd} |dk r|} n |tj} tj j || } |t|<| j dk r| j |d<y| j j |} | dkr$|SWnttfk r@|SX| j j|} tj| } g}tj| j}y<x4|D]*\}}}}}|tkr|\}}x$|r|dd|kr|d=qWqt|dkrl|\}}x$|r|dd|kr|d=qWt|dd \}}}|tkrqt|rH|dd}t|trZ|j||nt||| |||<|jd|fqt|d krD|\}}x$|r|dd|kr|d=qWt|dd \}}}|tkrȐqtt|dd \}}}d}|d krg}d}g}xt|dd \}}}|dkr|dkrdj|}||krD||}nL|jd}t|dkr|d} |d}| tkrt| }!||!kr|!|}|j|g}|d kr|d7}nX|d kr|d8}|dkr Pn8|d kr|dkrn"|tt fkr|dkr|j|qW|}t|||| |}|s2|||<|j||fqt|dkr|ddkrt!|}"xl|"D]d\}#}$yL|dkrt|#|n2yt|#||Wn tk rt|#gYnXWn YnXqjWn|dkrt|ddkrtt"|\}#}|# st|dkrqtt!|}yt|#||}!WnwtYnXxd|D]\\}}%||!krd|!|||%p`|<n4|dkr>x(|!D] }|ddkrt|!|||<qtWq>WqtWWnt#k rYnX| j$|S)a.Do the hard work for readmodule[_ex]. If INPACKAGE is given, it must be the dotted name of the package in which we are searching for a submodule, and then PATH must be the package search path; otherwise, we are searching for a top-level module, and PATH is combined with sys.path. Nz%s.%s.r__path__zNo package named {}defclass(),importfrom*_r0r0r0r0r0r0)r(r)r0)%_modulessysbuiltin_module_namesrfindr ImportErrorformatr importlibutil_find_spec_from_pathsubmodule_search_locationsloader get_sourceAttributeError get_filenameioStringIOtokenizegenerate_tokensreadlinernextrrrrrappendjoinsplitlenr _getnamelist_getname StopIterationclose)&r rZ inpackageZ fullmoduledictipackageZ submoduleparentfZ search_pathspecsourceZfnamestackg tokentypetokenstartZ_end_linerZ thisindentZ meth_nameZ cur_class class_nameZinheritnameslevelr ncmdmodulesmodZ_mod2Zn2rrrras                                           "rcCstg}xjt|\}}|sP|dkr.t|\}}nd}|j||fx |dkr`d|kr`t|d}qBW|dkrPqW|S)Nasr) r")rKrFrE)rVr\r rXZname2rrrrJs rJcCsg}t|dd\}}|tkr0|dkr0d|fS|j|xJt|dd\}}|dkrZPt|dd\}}|tkrxP|j|qTsz_main..)rr&r#z defr$)osoperatorrgr3argvrexistsdirnamebasenamelowerendswithrlistvaluessortrrprintr r rsortedr rr) rlrgrcrrNZobjsobjr r rrrr_mainFs*         rz__main__)N)N)N)rr@r3importlib.utilr8rBrXrrr__all__r2rrrrrrJrKrzrrrrr(s$  ?