a �DOg�;�@s�dZddlZddlZddlZddlZddlmZmZm Z gd�Z iZ Gdd�d�Z Gdd�de �Z Gd d �d e �Zd d �Zdd d�Zddd�Zd dd�Zd!dd�Zdd�Zdd�Zdd�Zdd�Zedkr�e�dS)"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)�NAME�DEDENT�OP)� readmodule� readmodule_ex�Class�Functionc@s eZdZdZdd�Zdd�ZdS)�_Objectz+Information about Python class or function.cCs(||_||_||_||_||_i|_dS�N)�module�name�file�lineno�parent�children��selfr r r rr�r�/usr/lib64/python3.9/pyclbr.py�__init__6s z_Object.__init__cCs||j|<dSr )r)rr �objrrr� _addchild>sz_Object._addchildN)�__name__� __module__� __qualname__�__doc__rrrrrrr 4sr c@seZdZdZddd�ZdS)rz7Information about a Python function, including methods.NcCst�||||||�dSr )r rrrrrrDszFunction.__init__)N)rrrrrrrrrrBsrcs*eZdZdZd�fdd� Zdd�Z�ZS)rz!Information about a Python class.Ncs0t�||||||�|dur gn||_i|_dSr )r r�super�methods)rr r rr rr�� __class__rrrJszClass.__init__cCs||j|<dSr )r)rr rrrr� _addmethodOszClass._addmethod)N)rrrrrr � __classcell__rrrrrHsrcCs:t|j||j||�}|�||�t|t�r6|�||�|S)z*Return a Function after nesting within ob.)rr r r� isinstancerr )�ob� func_namerZnewfuncrrr�_nest_functionSs    r%cCs&t|j|||j||�}|�||�|S)z'Return a Class after nesting within ob.)rr r r)r#� class_namerrZnewclassrrr� _nest_class[s r'cCs6i}t||pg���D]\}}t|t�r|||<q|S)z�Return Class objects for the top-level classes in module. This is the original interface, before Functions were added. )� _readmodule�itemsr"r)r �path�res�key�valuerrrras   rcCst||p g�S)z�Return 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*rrrrmsrc Cs~|durd||f}n|}|tvr*t|Si}|tjvrL|durL|t|<|S|�d�}|dkr�|d|�}||dd�}t|||�}|dur�d||f}d|vr�td�|���t||d|�Sd} |dur�|} n |tj} tj � || �} | du�rt d|��|d ��|t|<| j du�r$| j |d<z| j �|�} Wnttf�yP|YS0| du�r`|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 )�_modules�sys�builtin_module_names�rfindr(� ImportError�formatr*� importlib�util�_find_spec_from_path�ModuleNotFoundError�submodule_search_locations�loader� get_source�AttributeError� get_filename� _create_tree)r r*� inpackage� fullmodule�tree�i�packageZ submoduler�f� search_path�spec�source�fnamerrrr(vsJ            r(c! CsDt�|�}g}t�|j�}�z|D�]�\} } } } } | tkr`| \}}|r^|dd|kr^|d=qBq"| dkr�| \}}|r�|dd|kr�|d=qpt|�dd�\} }} | tkr�q"d}|r�|dd}t|||�}nt ||||�}|||<|� ||f�q"| dk�r�| \}}|�r(|dd|k�r(|d=�qt|�dd�\} }} | tk�rJq"t|�dd�\} } } d}| dk�r�g}d}g}t|�dd�\} } } | d v�r|dk�rd � |�}||v�r�||}nL|� d �}t |�dk�r|d }|d}|tv�rt|}||v�r||}|� |�g}| dk�r0|d7}nZ| d k�rR|d8}|dk�r��q�n8| dk�rh|dk�rhn"| ttfv�rz|dk�rz|� | ��qz|}|�r�|dd}t||||�}nt|||||�}|||<|� ||f�q"| dk�rf| ddk�rft|�}|D]b\}}zJ|du�r t||�n0zt|||�Wnt�yNt|g�Yn0Wn Yn0�qq"| dkr"| ddkr"t|�\}} |r"| dk�r�q"t|�}zt|||�}WnYq"Yn0|D]X\}} ||v�r�|||| �p�|<n0|dk�r�|D] }|ddk�r�||||<�q��q�q"Wnt�y6Yn0|��|S)a�Return 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. �����r/�defr�N�class�()�)�,�r.�����rPrQ�import�from�*�_)�io�StringIO�tokenize�generate_tokens�readliner�nextrr%r�append�join�split�lenr1rr'r� _getnamelistr(r5�_getname� StopIteration�close)!rBr*rJrIrCrArF�stack�g� tokentype�token�startZ_end�_linerZ thisindentr$Zcur_funcZcur_objr&Zinherit�names�levelr�n�c�m�dZ cur_class�modules�modZ_mod2Zn2rrrr@�s�                    ��         r@cCslg}t|�\}}|sqh|dkr,t|�\}}nd}|�||f�|dkr\d|vr\t|�d}q>|dkrqhq|S)z�Return 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. �asNrQ� r/)rcr^r])rgrlr riZname2rrrrbEs rbcCs�g}t|�dd�\}}|tkr0|dkr0d|fS|�|�t|�dd�\}}|dkrXq�t|�dd�\}}|tkrvq�|�|�q:d�|�|fS)zBReturn (dotted-name or None, next-token) tuple for token source g.r�rVNr.)r]rr^r_)rg�partsrhrirrrrc[s  rcc CsXddl}ztjd}Wnt}Yn0|j�|�rj|j�|�g}|j�|�}|��� d�rn|dd�}ng}t ||�}dd�}t |� �|dd �}d }|�rT|� �}t|t�r�q�t|d �s�d|_t|t�r�t |j� �|dd �}|D]} |j|| _q�|�|�t|t��r,td �d |j|j|j|j��q�t|t�r�td�d |j|j|j��q�dS)z?Print module output (default this file) for quick visual check.rNr/z.py�����cSs t|dd�S)Nrr)�getattr)�arrr�|�z_main..T)r,�reverserv�indentz{}class {} {} {}� z {}def {} {})�osr2�argv�__file__r*�exists�dirname�basename�lower�endswithr�sorted�values�popr"�list�hasattrr~r r�extendr�printr6r rrr) r�rsr*rCZ lineno_keyZobjsZ indent_levelrZnew_objsr#rrr�_mainmsD        �  � r��__main__)N)N)N)N)rrXr2�importlib.utilr7rZrirrr�__all__r1r rrr%r'rrr(r@rbrcr�rrrrr�s,(   @&