U AT^;c@sdZddlZddlZddlZddlZddlmZmZm Z ddddgZ iZ Gdd d Z Gd dde Z Gd dde Zd d ZdddZdddZdddZd ddZddZddZddZddZedkredS)!aParse a Python module and describe its classes and functions. Parse enough of a Python file to recognize imports and class and function definitions, and to find out the superclasses of a class. The interface consists of a single function: readmodule_ex(module, path=None) 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 and functions defined in the module (including classes that are defined via the from XXX import YYY construct). The values are instances of classes Class and Function. One special key/value pair is present for packages: the key '__path__' has a list as its value which contains the package search path. Classes and Functions have a common superclass: _Object. Every instance has the following attributes: module -- name of the module; name -- name of the object; file -- file in which the object is defined; lineno -- line in the file where the object's definition starts; parent -- parent of this object, if any; children -- nested objects contained in this object. The 'children' attribute is a dictionary mapping names to objects. Instances of Function describe functions with the attributes from _Object. Instances of Class describe classes with the attributes from _Object, plus the following: super -- list of super classes (Class instances if possible); methods -- mapping of method names to beginning line numbers. 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. N)NAMEDEDENTOP readmodule readmodule_exClassFunctionc@s eZdZdZddZddZdS)_Objectz+Information about Python class or function.cCs(||_||_||_||_||_i|_dSN)modulenamefilelinenoparentchildrenselfr r r r rr/usr/lib64/python3.8/pyclbr.py__init__6s z_Object.__init__cC||j|<dSr )r)rr objrrr _addchild>z_Object._addchildN)__name__ __module__ __qualname____doc__rrrrrrr4src@seZdZdZdddZdS)rz7Information about a Python function, including methods.NcCst||||||dSr )rrrrrrrDrzFunction.__init__r )rrrrrrrrrrBscs*eZdZdZdfdd ZddZZS)rz!Information about a Python class.Ncs0t|||||||dkr gn||_i|_dSr )rrsupermethods)rr r rr r rZ __class__rrrJszClass.__init__cCrr )r)rr r rrr _addmethodOrzClass._addmethodr )rrrrrr Z __classcell__rrrrrHscCs:t|j||j||}|||t|tr6||||S)z*Return a Function after nesting within ob.)rr r r isinstancerr )ob func_namer Znewfuncrrr_nest_functionSs    r$cCs&t|j|||j||}||||S)z'Return a Class after nesting within ob.)rr r r)r" class_namer rZnewclassrrr _nest_class[s r&cCs6i}t||pgD]\}}t|tr|||<q|S)zReturn Class objects for the top-level classes in module. This is the original interface, before Functions were added. ) _readmoduleZitemsr!r)r pathZreskeyZvaluerrrras   cCst||p gS)zReturn a dictionary with all functions and classes in module. Search for module in PATH + sys.path. If possible, include imported superclasses. Do this by reading source, without importing (and executing) it. )r')r r(rrrrmsc Cs|dk rd||f}n|}|tkr*t|Si}|tjkrL|dkrL|t|<|S|d}|dkr|d|}||dd}t|||}|dk rd||f}d|krtd|t||d|Sd} |dk r|} n |tj} tj || } | dkrt d||d |t|<| j dk r$| j |d<z| j |} Wnttfk rR|YSX| dkrb|S| j |} t||| | ||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.rZ__path__zNo package named {}zno module named )r )_modulessysZbuiltin_module_namesZrfindr' ImportErrorformatr( importlibZutilZ_find_spec_from_pathZModuleNotFoundErrorZsubmodule_search_locationsZloaderZ get_sourceZAttributeErrorZ get_filename _create_tree)r r( inpackage fullmoduletreeZiZpackageZ submodulerfZ search_pathZspecsourcefnamerrrr'vsJ            r'c! CsHt|}g}t|j}z|D]\} } } } } | tkr`| \}}|r^|dd|kr^|d=qBq"| dkr| \}}|r|dd|kr|d=qpt|dd\} }} | tkrq"d}|r|dd}t|||}nt ||||}|||<| ||fq"| dkr| \}}|r(|dd|kr(|d=qt|dd\} }} | tkrJq"t|dd\} } } d}| dkrg}d}g}t|dd\} } } | d kr|dkrd |}||kr||}nL| d }t |dkr|d }|d}|tkrt|}||kr||}| |g}| dkr0|d7}nZ| d krR|d8}|dkrqn8| dkrh|dkrhn"| ttfkrz|dkrz| | qz|}|r|dd}t||||}nt|||||}|||<| ||fq"| dkrh| ddkrht|}|D]d\}}zL|dkr t||n2zt|||Wn tk rPt|gYnXWn YnXqq"| dkr"| ddkr"t|\}} |r"| dkrq"t|}zt|||}WnYq"YnX|D]X\}} ||kr|||| p|<n0|dkr|D] }|ddkr||||<qqq"Wntk r:YnX||S)aReturn the tree for a particular module. fullmodule (full module name), inpackage+module, becomes o.module. path is passed to recursive calls of _readmodule. fname becomes o.file. source is tokenized. Imports cause recursive calls to _readmodule. tree is {} or {'__path__': }. inpackage, None or string, is passed to recursive calls of _readmodule. The effect of recursive calls is mutation of global _modules. ir+ZdefriNZclassz()),Zr*ir8r9ZimportZfrom*Z_)ioZStringIOtokenizeZgenerate_tokensZreadlinernextrr$rappendjoinZsplitZlenr,rr&r _getnamelistr'r._getnameZ StopIterationZclose)!r3r(r7r6r4r2r5Zstackg tokentypetokenZstartZ_endZ_liner Z thisindentr#Zcur_funcZcur_objr%ZinheritnamesZlevelrZnZcZmZdZ cur_classZmodulesmodZ_mod2Zn2rrrr1s                             r1cCslg}t|\}}|sqh|dkr,t|\}}nd}|||f|dkr\d|kr\t|d}q>|dkrqhq|S)zReturn list of (dotted-name, as-name or None) tuples for token source g. An as-name is the name that follows 'as' in an as clause. ZasNr9Z r+)rAr>r=)rBrEr rDZname2rrrr@Es r@cCsg}t|dd\}}|tkr0|dkr0d|fS||t|dd\}}|dkrXqt|dd\}}|tkrvq||q:d||fS)zBReturn (dotted-name or None, next-token) tuple for token source g.rr:Nr*)r=rr>r?)rBZpartsrCrDrrrrA[s  rAc CsXddl}ztjd}Wnt}YnX|j|rj|j|g}|j|}| drn|dd}ng}t ||}dd}t | |dd }d }|rT| }t|trqt|d sd|_t|trt |j |dd }|D]} |j|| _q||t|tr,td d |j|j|j|jqt|trtdd |j|j|jqdS)z?Print module output (default this file) for quick visual check.rNr+z.pyicSs t|ddS)Nr r)Zgetattr)ZarrrZ|sz_main..T)r)ZreverserGindentz{}class {} {} {}z z {}def {} {})osr-ZargvZ__file__r(ZexistsZdirnameZbasenameZlowerZendswithrZsortedZvaluesZpopr!ZlistZhasattrrHrrZextendrZprintr/r rr r) rIrFr(r4Z lineno_keyZobjsZ indent_levelrZnew_objsr"rrr_mainmsL           rJZ__main__r r r r )rr;r-Zimportlib.utilr0r<rDrrrZ__all__r,rrrr$r&rrr'r1r@rArJrrrrrZs,(    @&