o $a@sdZddlZddlZddlZddlmZddlmZddlm Z ddl m Z ddl m Z ddlmZmZmZmZmZmZdd lmZdd lmZdd lmZdd lmZGd ddZdS)z sphinx.pycode ~~~~~~~~~~~~~ Utilities parsing and analyzing Python code. :copyright: Copyright 2007-2021 by the Sphinx team, see AUTHORS. :license: BSD, see LICENSE for details. N) OrderedDict) import_module) SignatureStringIO)path)IOAnyDictListOptionalTuple)ZipFile)RemovedInSphinx50Warning) PycodeError)Parserc @seZdZUeeeefefed<eeeefeefed<eeed<eeeefed<eee fed<eeeee e ffed<iZ eeeefe fed<e ded ee ee effd d Zed$d ededed dfddZededed dfddZededed dfddZeded dfddZdededed dfddZd%ddZd%ddZd eeeefeeffd d!Zd eeeee e fffd"d#ZdS)&ModuleAnalyzer annotations attr_docsfinals overloadstagordertagscachemodnamereturnc Cszt|}Wnty}ztd|||d}~wwt|dd}t|dd}|rGt|ddrGz||}|r<||fWSWn tyFYnw|duro|rot|ddroz||}Wntyn}ztd|||d}~ww|durytd|tt |}| d r|dd }t |st |d r|d 7}n.| d std |dtj |vrdttj }t||d\}}t |r|dfSt |std||dfS)zTry to find the source code for a module. Returns ('filename', 'source'). One of it can be None if no filename or source found zerror importing %rN __loader____file__ get_source get_filenamezerror getting filename for %rzno source found for module %r)z.pyoz.pycw)z.pyz.pywzsource is not a .py file: %r.egg (?<=\.egg)zsource file is not present: %r)r Exceptionrgetattrr ImportErrorrrnormpathabspathlowerendswithisfilesepreescapesplit) rmoderrloaderfilenamesourcepateggpath_r98/usr/lib/python3/dist-packages/sphinx/pycode/__init__.pyget_module_source&sT            z ModuleAnalyzer.get_module_sourcestringsrcnamecCs|t|||S)Nr)clsr=rr>r9r9r: for_stringVszModuleAnalyzer.for_stringr4c Csd|f|jvr|jd|fSz't|}||||}||jd|f<WdW|S1s.wYW|Stye}z$dtj|vrR|||}|jd|f<ntd|||WYd}~|Sd}~ww)Nfiler"error opening %r)rtokenizeopenr%rr-for_eggr)r?r4rfobjr2r9r9r:for_file[s$    zModuleAnalyzer.for_filec Csttj}td||\}}z&t|}||}||||WdWS1s/wYWdSt yJ}zt d|||d}~ww)Nr#rB) r.r/rr-r0rreaddecoder@r%r) r?r4rSEPr7relpatheggcodeexcr9r9r:rEjs   (zModuleAnalyzer.for_eggc Csd|f|jvr|jd|f}t|tr||Sz!||\}}|dur-||||p*d}n |dur7|||}WntyL}z||jd|f<d}~ww||jd|f<|S)Nmoduler<)r isinstancerr;r@rH)r?rentryr4r5rGr2r9r9r: for_moduleus&  zModuleAnalyzer.for_moduler5NcCs ||_||_||_d|_dS)NF)rr>rIrN _analyzed)selfr5rr>r9r9r:__init__s  zModuleAnalyzer.__init__cCstjdtdd|dS)zParse the source code.z%ModuleAnalyzer.parse() is deprecated.) stacklevelN)warningswarnranalyzerUr9r9r:parses zModuleAnalyzer.parsec Cs|jrdSzDt|j}|t|_|jD]\}}|r)|dg|j|<qdg|j|<q|j |_ |j |_ |j |_ |j |_ |j|_d|_WdSty_}z td|j|f|d}~ww)zAnalyze the source code.NTzparsing %r failed: %r)rTrrNr]rrcommentsitems splitlinesrrr definitionsr defordersrr%rr>)rUparserscopecommentrOr9r9r:r[s(  zModuleAnalyzer.analyzecC||jS)z?Find class and module-level attributes and their documentation.)r[rr\r9r9r:find_attr_docszModuleAnalyzer.find_attr_docscCrg)z?Find class, function and method definitions and their location.)r[rr\r9r9r: find_tagsrizModuleAnalyzer.find_tags)r<)rN)__name__ __module__ __qualname__r r str__annotations__r rintrr staticmethodr r; classmethodr@rHrErSrrVr]r[rhrjr9r9r9r:rs2  $/   "$r)__doc__r.rCrY collectionsr importlibrinspectriorosrtypingrr r r r r zipfilersphinx.deprecationr sphinx.errorsrsphinx.pycode.parserrrr9r9r9r:s