o $a!C@sdZddlZddlZddlZddlmZddlmZmZm Z m Z m Z m Z ddl mZddlmZddlmZddlZddlmZdd lmZdd lmZdd lmZmZmZmZmZdd lm Z dd l!m"Z"ddl#m$Z$ddl%m&Z&ddl'm(Z(ddl)m*Z*e+dej,Z-dde.e/DZ0de1defddZ2de1de1defddZ3Gddde4Z5Gdd d Z6Gd!d"d"eZ7Gd#d$d$e"Z8d%e7de1fd&d'Z9d(e&d%e7ddfd)d*Z:d(e(d%e7ddfd+d,Z;d(e*d%e7ddfd-d.Zd1edee1effd2d3Z?dS)4a[ sphinx.ext.inheritance_diagram ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Defines a docutils directive for inserting inheritance diagrams. Provide the directive with one or more classes or modules (separated by whitespace). For modules, all of the classes in that module will be used. Example:: Given the following classes: class A: pass class B(A): pass class C(A): pass class D(B, C): pass class E(B): pass .. inheritance-diagram: D E Produces a graph like the following: A / \ B C / \ / E D The graph is inserted as a PNG+image map into HTML and a PDF in LaTeX. :copyright: Copyright 2007-2021 by the Sphinx team, see AUTHORS. :license: BSD, see LICENSE for details. N) import_module)AnyDictIterableListTuplecast)nodes)Node) directives)addnodes)Sphinx)BuildEnvironment)figure_wrappergraphvizrender_dot_htmlrender_dot_latexrender_dot_texinfo)md5)SphinxDirective) OptionSpec)HTMLTranslator)LaTeXTranslator)TexinfoTranslatorz^(?:([\w.]*)\.)? # module names (\w+) \s* $ # class/final module name cCsg|] }t|r|qS)inspectisclass).0objrr@/usr/lib/python3/dist-packages/sphinx/ext/inheritance_diagram.py Bs r objnamereturnc Cszt|WStyYdStyEt|}|sYdS|\}}|dur,YdSz t|}t||dWYStyDYYdSww)zImport a object or module using *name* and *currentmodule*. *name* should be a relative name from *currentmodule* or a fully-qualified name. Returns imported object or module. If failed, returns None value. N)r TypeError ImportError module_sig_rematchgroupsgetattr)r!matchedmodnameattrnamemodulerrr try_importFs$      r-name currmodulecCsd}|r t|d|}|durt|}|durtd|t|r&|gSt|rGg}|jD]}t|rD|j|jkrD| |q2|Std|)z0Import a class using its fully-qualified *name*.N.zECould not import class or module %r specified for inheritance diagramz=%r specified for inheritance diagram is not a class or module) r-InheritanceExceptionrrismodule__dict__values __module____name__append)r.r/targetclassesclsrrrimport_classescs.   r;c@s eZdZdS)r1N)r6r5 __qualname__rrrrr1sr1c@sreZdZdZddddgfdeedededed ed eeefd ee d dfd dZ deeded ee fddZ dee deded ed eeefd ee d ee eeeeeffddZ d4de d ed eeefd efddZd eefddZddddZdddd d!d"d#Zd$d%d&Zd'ed efd(d)Zd'ed efd*d+Zidiiifd,ed-ed.ed/ed0ed1ed efd2d3ZdS)5InheritanceGraphz Given a list of classes, determines the set of classes that they inherit from all the way to the root "object", and then is able to generate a graphviz dot graph from them. FrN class_namesr/ show_builtins private_basespartsaliases top_classesr"c Cs:||_|||}||||||||_|jstddS)z*class_names* is a list of child classes to show bases from. If *show_builtins* is True, then Python builtins will be shown in the graph. z(No classes found for inheritance diagramN)r>_import_classes _class_info class_infor1) selfr>r/r?r@rArBrCr9rrr__init__s zInheritanceGraph.__init__cCs"g}|D] }|t||q|S)zImport a list of classes.)extendr;)rGr>r/r9r.rrrrDsz InheritanceGraph._import_classesr9c sFidtddffdd |D]}|qtS)aReturn name and bases for all classes that are ancestors of *classes*. *parts* gives the number of dotted name parts to include in the displayed node names, from right to left. If given as a negative, the number of parts to drop from the left. A value of 0 displays the full dotted name. E.g. ``sphinx.ext.inheritance_diagram.InheritanceGraph`` with ``parts=2`` or ``parts=-2`` gets displayed as ``inheritance_diagram.InheritanceGraph``, and as ``ext.inheritance_diagram.InheritanceGraph`` with ``parts=3`` or ``parts=-1``. *top_classes* gives the name(s) of the top most ancestor class to traverse to. Multiple names can be specified separated by comma. r:r"Ncss|tvrdSs|jdrdS|}|d}d}z|jr:|jdd}|r:d|dd}Wn tyDYnwg}||||f|<|vrUdS|j D]$}sa|tvraqXsj|jdrjqX| ||vr||qXdS)N_r "%s""z\") py_builtinsr6 startswith class_name__doc__stripsplitreplace Exception __bases__r7)r:nodenamefullnametooltipdocbaselistbaserB all_classesrAr@recurserGr?rCrrr_s<    z-InheritanceGraph._class_info..recurse)rlistr4)rGr9r?r@rArBrCr:rr]rrEs $"  zInheritanceGraph._class_infor:cCsl|j}|dvr |j}nd||jf}|dkr|}n|d}d|| d}|dur4||vr4||S|S)zGiven a class object, return a fully-qualified name. This works for things I've tested in matplotlib so far, but may not be completely general. ) __builtin__builtinsz%s.%srr0N)r5r6r<rSjoin)rGr:rArBr,rXresult name_partsrrrrPs zInheritanceGraph.class_namecCsdd|jDS)z1Get all of the class names involved in the graph.cSsg|]\}}}}|qSrr)rrJrXrrrr sz8InheritanceGraph.get_all_class_names..)rF)rGrrrget_all_class_namessz$InheritanceGraph.get_all_class_namesLRz "8.0, 12.0" transparent)rankdirsizebgcolorbox g?zA"Vera Sans, DejaVu Sans, Liberation Sans, Arial, Helvetica, sans"z"setlinewidth(0.5),filled"white)shapefontsizeheightfontnamestyle fillcolorg?z"setlinewidth(0.5)") arrowsizersattrscCdddt|DS)N,cSg|]}d|qS)z%s=%srrxrrrr z7InheritanceGraph._format_node_attrs..rcsorteditemsrGrvrrr_format_node_attrs z#InheritanceGraph._format_node_attrscCrw)NcSry)z%s=%s; rrzrrrr r|z8InheritanceGraph._format_graph_attrs..r}rrrr_format_graph_attrs rz$InheritanceGraph._format_graph_attrsr.urlsenv graph_attrs node_attrs edge_attrsc Cs(|j}|j}|j} ||||| ||r5||jj||jj| |jjg} | d|| | |t |j D]>\}} } } |}| |vred|| |d<d|d<| rk| |d<| d|| |f| D]}| d||| | fqyqK| d d | S) aVGenerate a graphviz dot graph from the classes that were passed in to __init__. *name* is the name of the graph. *urls* is a dictionary mapping class names to HTTP URLs. *graph_attrs*, *node_attrs*, *edge_attrs* are dictionaries containing key/value pairs to pass on as graphviz properties. z digraph %s { rLURLz"_top"r8rYz "%s" [%s]; z "%s" -> "%s" [%s]; z} r)default_graph_attrscopydefault_node_attrsdefault_edge_attrsupdateconfiginheritance_graph_attrsinheritance_node_attrsinheritance_edge_attrsr7rr~rFrrc)rGr.rrrrrg_attrsn_attrse_attrsresrXbasesrYthis_node_attrs base_namerrr generate_dots@        zInheritanceGraph.generate_dot)rN)r6r5r<rQrstrboolintrrrHrDrrErPrfrrrrrrrrrrrr=sn    $; r=c@seZdZdZdS)inheritance_diagramzN A docutils node to use as a placeholder for the inheritance diagram. N)r6r5r<rQrrrrr@src@sPeZdZUdZdZdZdZdZee j e j e j dZ eed<deefd d Zd S) InheritanceDiagramzJ Run when the inheritance_diagram directive is first encountered. FrT)rA private-basescaption top-classes option_specr"c Csrt}|jj|_|jd}|jdd}|j dd|d<d ||d<g|d<|j ddd D]}| }|rE|d |q6zt ||jj d |dd |jv|jj|dd }Wnty}}z|jjj||jd gWYd}~Sd}~ww|D]}|dd||d|j\}} ||q||d<d|jvr|||gSt|||jd} || | gS)NrpyclassrAz, contentrrrxz py:moduler)rAr@rBrC)linez :class:`%s`graphr)rstatedocument argumentsrSr get_domainroleoptionsgetrcrRr7r= ref_contextrinheritance_aliasr1reporterwarninglinenorfrIadd_namer) rGnoder> class_roler:rerrr.refnodesr{figurerrrrunVsH  "     zInheritanceDiagram.runN)r6r5r<rQ has_contentrequired_argumentsoptional_argumentsfinal_argument_whitespacerr flag unchangedunchanged_requiredrr__annotations__rr rrrrrrGs rrcCs,|dt|d}t|ddS)NrrAi)rencoder hexdigest)rencodedrrrget_graph_hashsrrGc Cs|d}t|}d|}|jjjj}|jj|jj}i}tt t j |}|D]I} | ddurK|dkrAd| d|| d<q(| d|| d<q(| ddurq|dkrfd|d | d|| d<q(d | d|| d<q(|j |||jjd } t||| id d d |d dtj)zZ Output the graph for HTML. This will insert a PNG with clickable image map. r inheritance%srefuriNSVGz../reftitlerefid#)r inheritancezInheritance diagram of r)alt)rbuilderrrgraphviz_output_formatuppercurrent_docname out_suffixrrr pending_xrefrrrr SkipNode) rGrr graph_hashr.rcurrent_filenamer pending_xrefschilddotcoderrrhtml_visit_inheritance_diagrams,  rcCF|d}t|}d|}|j||jjddid}t|||idtj)z> Output the graph for LaTeX. This will insert a PDF. rrrj "6.0,6.0"rrr)rrrrrr rrGrrrr.rrrrlatex_visit_inheritance_diagrams rcCr)z@ Output the graph for Texinfo. This will insert a PNG. rrrjrrr)rrrrrr rrrrr!texinfo_visit_inheritance_diagrams rcCstj)N)r r)rGrrrrskipsrappcCs|d|jttdftdftdftdftdfd|dt| did| did| did| didt j d d S) Nzsphinx.ext.graphviz)latexhtmltextmantexinfozinheritance-diagramrFrrrT)versionparallel_read_safe) setup_extensionadd_noderrrrr add_directiveradd_config_valuesphinx__display_version__)rrrrsetups   r)@rQrbrre importlibrtypingrrrrrrdocutilsr docutils.nodesr docutils.parsers.rstr rr sphinx.applicationr sphinx.environmentrsphinx.ext.graphvizrrrrr sphinx.utilrsphinx.util.docutilsrsphinx.util.typingrsphinx.writers.htmlrsphinx.writers.latexrsphinx.writers.texinforcompileVERBOSEr%varsr4rNrr-r;rUr1r=rrrrrr NodeVisitorrrrrrrsN%              ;=!