o ?Og,@sdZddlZddlZddlZgdZiZGdddZGdddeZGdd d eZ dd d Z dd dZ dddZ dddZ dddZGdddejZddZddZedkr^edSdS)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; end_lineno -- line in the file where the object's definition ends; 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, plus the following: is_async -- if a function is defined with an 'async' prefix 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) readmodule readmodule_exClassFunctionc@seZdZdZddZdS)_Objectz+Information about Python class or function.cCsD||_||_||_||_||_||_i|_|dur ||j|<dSdSN)modulenamefilelineno end_linenoparentchildren)selfrr r r r r r-/opt/alt/python310/lib64/python3.10/pyclbr.py__init__7sz_Object.__init__N)__name__ __module__ __qualname____doc__rrrrrr5s rcs*eZdZdZ dddfdd ZZS)rz7Information about a Python function, including methods.NFr cs8t||||||||_t|tr||j|<dSdSr)superris_async isinstancermethods)rrr r r r rr  __class__rrrFs  zFunction.__init__)NFrrrrr __classcell__rrrrrD rcs*eZdZdZ dddfdd ZZS)rz!Information about a Python class.Nrcs*t|||||||pg|_i|_dSr)rrr)rrr Zsuper_r r r r rrrrPs  zClass.__init__rrrrrrrNr rFc Cst|j||j||||dS)z*Return a Function after nesting within ob.)r rr )rrr )obZ func_namer r rrrr_nest_functionYsr"c Cst|j|||j|||dS)z'Return a Class after nesting within ob.r r )rrr )r! class_namer r rrrr _nest_class^sr%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. ) _readmoduleitemsrr)rpathreskeyvaluerrrrds  rcCst||pgS)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&)rr(rrrrpsrc Csv|dur d||f}n|}|tvrt|Si}|tjvr&|dur&|t|<|S|d}|dkr`|d|}||dd}t|||}|durMd||f}d|vrXtd|t||d|Sd} |duri|} n|tj} tj || } | durt d||d |t|<| j dur| j |d<z| j |} Wn ttfy|YSw| dur|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.r__path__zNo package named {}zno module named )r )_modulessysbuiltin_module_namesrfindr& ImportErrorformatr( importlibutil_find_spec_from_pathModuleNotFoundErrorsubmodule_search_locationsloader get_sourceAttributeError get_filename _create_tree)rr( inpackage fullmoduletreeipackageZ submoduler fZ search_pathspecsourcefnamerrrr&zsL         r&c@sBeZdZddZddZddddZd d Zd d Zd dZdS)_ModuleBrowsercCs(||_||_||_||_||_g|_dSr)r(rAr rr?stack)rrr(r rAr?rrrrs  z_ModuleBrowser.__init__c Csg}|jD];}t|}||jvr||j|qt|d}dkr;|^}}}|tvr:|t|||q||q|j rI|j dnd} t |j |j ||j |j| |jd}| dure||j|j <|j ||||j dS)Nr,r-r#)basesastZunparserAappendlensplitr/getrIrrr r r r generic_visitpop) rnoderKbaser names_rZclass_r rrrvisit_ClassDefs(        z_ModuleBrowser.visit_ClassDefFrc Csl|jr|jdnd}t|j|j|j|j|||jd}|dur$||j|j<|j|| ||j dS)NrJr) rIrrr r r r rArMrQrR)rrSrr Zfunctionrrrvisit_FunctionDefs   z _ModuleBrowser.visit_FunctionDefcCs|j|dddS)NTrX)rY)rrSrrrvisit_AsyncFunctionDefsz%_ModuleBrowser.visit_AsyncFunctionDefc Csn|jdkrdS|jD]*}zz t|j|j|jWnty't|jgYnwWq ttfy4Yq wdS)Nr) col_offsetrUr&r r(r?r3 SyntaxError)rrSrrrr visit_Imports   z_ModuleBrowser.visit_Importc Cs|jdkrdSzd|j}|jr||j7}t||j|j}Wn ttfy*YdSw|jD]-}|j |vrB||j |j |j p@|j <q.|j dkr[| D]\}}| drUqK||j |<qKq.dS)Nrr,*rV)r[levelrr&r(r?r3r\rUr rAasnamer' startswith)rrSrr Z import_nameZ import_valuerrrvisit_ImportFroms(        z_ModuleBrowser.visit_ImportFromN) rrrrrWrYrZr]rbrrrrrHs  rHcCs&t|||||}|t||jSr)rHZvisitrLparserA)r@r(rGrFrAr?Zmbrowserrrrr> sr>c CsVddl}ztjd}Wnt}Y|j|r3|j|g}|j|}| dr2|dd}ng}t ||}dd}t | |dd }d }|r| }t|trUqIt|d s]d|_t|tr|t |j |dd }|D]} |j|| _qn||t|trtd d |j|j|j|jnt|trtdd |j|j|j|sKdSdS)z?Print module output (default this file) for quick visual check.rNr-z.pycSs t|ddS)Nr r)getattr)arrr"s z_main..T)r*reverseindentz{}class {} {} {} z {}def {} {})osr0argv__file__r(existsdirnamebasenamelowerendswithrsortedvaluesrRrlisthasattrrjrrextendrprintr4r rr r) rlmodr(rAZ lineno_keyZobjsZ indent_levelobjZnew_objsr!rrr_mainsH            r|__main__)Fr)rrLr0importlib.utilr5__all__r/rrrr"r%rrr&Z NodeVisitorrHr>r|rrrrrs(+   @S&