U
    pdo                    @   s  d Z dgZdZdZdZddlZddlZddlZddl	Zddl
ZddlZddlZddlZddlZddlZddlZddlZddlZddlZddlZddlZddlZddlmZ ddlmZ dd	lmZ d
d Zdd Zdd Z dd Z!dd Z"dd Z#dd Z$e%dej&Z'dd Z(dd Z)dd Z*dd  Z+dxd!d"Z,d#d$ Z-d%d& Z.d'd( Z/d)d* Z0i fd+d,Z1G d-d. d.e2Z3d/d0 Z4di fd1d2Z5G d3d4 d4Z6G d5d6 d6eZ7G d7d8 d8e6Z8G d9d: d:eZ9G d;d< d<e6Z:G d=d> d>e:Z;d?d@ a<dAdB Z=dCdD Z>dEdF Z?dGdH Z@dIdJ ZAdKdL ZBdMdN ZCdOdP ZDdydQdRZEe: ZFe; ZGe8 ZHdzdSdTZId{dVdWZJd|dXdYZKd}dZd[ZLd~d]d^ZMG d_d` d`ZNeN ZOG dadb dbZPdcdd ZQdedf ZRddhdiZSddjdkdldmdnZTdodp ZUdqdr ZVdsdt ZWdudv ZXeYdwkreX  dS )a^  Generate Python documentation in HTML or text for interactive use.

At the Python interactive prompt, calling help(thing) on a Python object
documents the object, and calling help() starts up an interactive
help session.

Or, at the shell command line outside of Python:

Run "pydoc <name>" to show documentation on something.  <name> may be
the name of a function, module, package, or a dotted reference to a
class or function within a module or module in a package.  If the
argument contains a path segment delimiter (e.g. slash on Unix,
backslash on Windows) it is treated as the path to a Python source file.

Run "pydoc -k <keyword>" to search for a keyword in the synopsis lines
of all available modules.

Run "pydoc -n <hostname>" to start an HTTP server with the given
hostname (default: localhost) on the local machine.

Run "pydoc -p <port>" to start an HTTP server on the given port on the
local machine.  Port number 0 can be used to get an arbitrary unused port.

Run "pydoc -b" to start an HTTP server on an arbitrary unused port and
open a Web browser to interactively browse documentation.  Combine with
the -n and -p options to control the hostname and port used.

Run "pydoc -w <name>" to write out the HTML documentation for a module
to a file named "<name>.html".

Module docs for core modules are assumed to be in

    /usr/share/doc/pythonX.Y/html/library

if the pythonX.Y-doc package is installed or in

    https://docs.python.org/X.Y/library/

This can be overridden by setting the PYTHONDOCS environment variable
to a different URL or to a local directory containing the Library
Reference Manual pages.
helpzKa-Ping Yee <ping@lfw.org>z26 February 2001zGuido van Rossum, for an excellent programming language.
Tommy Burnette, the original creator of manpy.
Paul Prescod, for all his work on onlinehelp.
Richard Chamberlain, for the first implementation of textdoc.
    N)deque)Repr)format_exception_onlyc                  C   s\   g } g }t jD ]H}tj|pd}tj|}||krtj|r| | || q| S )zAConvert sys.path into a list of absolute, existing, unique paths..)syspathosabspathnormcaseisdirappend)dirsZnormdirsdirZnormdir r   /usr/lib/python3.8/pydoc.pypathdirsU   s    

r   c                 C   s.   t | pt | }|r*tdd| p,dS )z-Get the doc string or comments for an object.z^ *
 )inspectgetdocZgetcommentsresubrstrip)objectresultr   r   r   r   a   s    r   c                 C   sf   |   d}t|dkr&|d dfS t|dkrX|d  sX|d d|dd fS dd|fS )z>Split a doc string into a synopsis line (if any) and the rest.
   r   r      N)stripsplitlenr   join)doclinesr   r   r   splitdocf   s    r$   c                 C   s"   | j }| j|kr| jd | }|S )z@Get a class name and qualify it with a module name if necessary.r   )__name__
__module__)r   modnamenamer   r   r   	classnameo   s    
r)   c                 C   s>   t | p:t | p:t | p:t | p:t | p:t |  S )z>Check if an object is of a type that probably means it's data.)r   ismoduleisclass	isroutineZisframeZistracebackZiscoder   r   r   r   isdatav   s    r.   c                 G   s.   |r*|d  | |d } |dd }q | S )z/Do a series of global replacements on a string.r   r   r   N)r!   r   )textZpairsr   r   r   replace|   s    r0   c                 C   sX   t | |krTtd|d d }td|d | }| d| d | t | | d  S | S )zCOmit part of a string if needed to make it fit in a maximum length.r      r   N...)r    max)r/   maxlenZpreZpostr   r   r   cram   s
    $r5   z at 0x[0-9a-f]{6,16}(>+)$c                 C   s   t d| S )z>Remove the hexadecimal id from a Python object representation.z\1)_re_stripidr   r/   r   r   r   stripid   s    r8   c                 C   s<   t | rdS t | r8t| dd}t |p4|dk S dS )zo
    Returns True if fn is a bound method, regardless of whether
    fn was implemented in Python or in C.
    T__self__NF)r   ismethod	isbuiltingetattrr*   )fnselfr   r   r   _is_bound_method   s    

r?   c                 C   s^   i }t | t jD ]\}}d||< q| jD ]}|t| q*| D ]}t| |||< qF|S Nr   )r   
getmembersr,   	__bases__update
allmethodskeysr<   )clmethodskeyvaluebaser   r   r   rD      s    

rD   c                 C   s8   g }g }| D ]"}||r$| | q| | q||fS )zSplit sequence s via predicate, and return pair ([true], [false]).

    The return value is a 2-tuple of lists,
        ([x for x in s if predicate(x)],
         [x for x in s if not predicate(x)])
    r   )s	predicateZyesZnoxr   r   r   _split_list   s    rO   c                 C   s\   | dkrdS |  dr$| dr$dS |  dr<t|dr<dS |dk	rL| |kS |  d S dS )	z3Decide whether to show documentation on a variable.>   __spec__r%   r&   __qualname__
__loader____version____credits____date____builtins__
__cached____file____doc____path__
__author____package__	__slots__r   __r   __fieldsTN)
startswithendswithhasattr)r(   allobjr   r   r   visiblename   s     rf   c                 C   sX   g }t | D ]D\}}}}t |r@d}t|tr@|jdkr@d}|||||f q|S )zCWrap inspect.classify_class_attrs, with fixup for data descriptors.data descriptorNreadonly property)r   classify_class_attrsisdatadescriptor
isinstancepropertyfsetr   )r   resultsr(   kindclsrI   r   r   r   ri      s    
ri   c                    s\   t |dg zfddtD  W n tk
r>   i  Y nX  fdd}| j|d dS )zGSort the attrs list in-place by _fields and then alphabetically by namer`   c                    s   i | ]\}}||t   qS r   )r    ).0ir(   )fieldsr   r   
<dictcomp>   s      z#sort_attributes.<locals>.<dictcomp>c                    s     | d d| d fS Nr   )get)attr)field_orderr   r   <lambda>       z!sort_attributes.<locals>.<lambda>rH   N)r<   	enumerate	TypeErrorsort)attrsr   Zkeyfuncr   )rx   rs   r   sort_attributes   s    
r   c                 C   s:   t j| r6dD ]$}t jt j| d| r dS qdS )z3Guess whether a path refers to a package directory.)z.pyz.pyc__init__TF)r	   r   r   isfiler!   )r   extr   r   r   	ispackage   s
    r   c                 C   s   |   }|d d dks | s0|   }|sq0q| }|d d dkrT|dd  }|d d dkr|dd  }|dd  dkr|d d }| s|   }|sqq|dd	  }nd }|S )
Nr   #   zr"""r1   """\r   )readliner   r   )fileliner   r   r   r   source_synopsis   s&        r   c           
   	   C   s   t | j}|| d\}}|dks.||k r| ttjjrJtjj	}n | ttjj
rftjj}nd}|dkrzt| }W n tk
r   Y dS X | t|}W 5 Q R X n^|d| }tjjd| |d}ztj|}	W n   Y dS X tjd= |	jr|	j d nd}||f|| < |S )z.Get the one-line summary out of a module file.)NNNZ__temp__loaderr   )r	   statst_mtimerv   rb   tuple	importlib	machineryBYTECODE_SUFFIXESSourcelessFileLoaderEXTENSION_SUFFIXESExtensionFileLoadertokenizeopenOSErrorr   utilspec_from_file_location
_bootstrap_loadr   modulesrY   
splitlines)
filenamecachemtimeZ
lastupdater   Z
loader_clsr   r   specmoduler   r   r   synopsis   s6    



r   c                   @   s    e Zd ZdZdd Zdd ZdS )ErrorDuringImportzEErrors that occurred while trying to import something to document it.c                 C   s   || _ |\| _| _| _d S N)r   excrI   tb)r>   r   exc_infor   r   r   r   (  s    zErrorDuringImport.__init__c                 C   s   | j j}d| j|| jf S )Nzproblem in %s - %s: %s)r   r%   r   rI   )r>   r   r   r   r   __str__,  s    zErrorDuringImport.__str__N)r%   r&   rQ   rY   r   r   r   r   r   r   r   &  s   r   c           	   	   C   s   t jj}t| d}||t|k}W 5 Q R X tj| }tj	|\}}|r`t j
|| }nt j
|| }t jj|| |d}zt j|W S    t| t Y nX dS )z<Import a Python source file or compiled file given its path.rbr   N)r   r   MAGIC_NUMBERr   readr    r	   r   basenamesplitext_bootstrap_externalr   SourceFileLoaderr   r   r   r   r   r   )	r   magicr   Zis_bytecoder   r(   r   r   r   r   r   r   
importfile0  s    r   c              	      s   z^|rT t jkrT t jkrT fddt jD } g| D ]}t j| ||< t j|= q8t }W nz   t   \}}}}	 t jkrtt j  j|	n>|tkrt|j|	n(t	|t
r|j krY dS t t  Y nX  ddd D ].}
zt||
}W q tk
r   Y  dS X q|S )a  Import a module; handle errors; return None if the module isn't found.

    If the module *is* found but an exception occurs, it's wrapped in an
    ErrorDuringImport exception and reraised.  Unlike __import__, if a
    package path is specified, the module at the end of the path is returned,
    not the package at the beginning.  If the optional 'forceload' argument
    is 1, we reload the module from disk (unless it's a dynamic extension).c                    s   g | ]}|  d  r|qS )r   )ra   )rq   mr   r   r   
<listcomp>V  s      zsafeimport.<locals>.<listcomp>Nr   r   )r   r   builtin_module_names
__import__r   r   rX   SyntaxErrorr   
issubclassImportErrorr(   r   r<   AttributeError)r   	forceloadr   ZsubsrH   r   r   rI   r   infopartr   r   r   
safeimportB  s.    


  r   c                   @   sf   e Zd Zejddejdd  ZdddZ	dddZ
e
 Z Z Z Z ZZed	fd
dZdS )Doc
PYTHONDOCSz%https://docs.python.org/%d.%d/libraryNr   c                 G   s   ||f| }zFt |r$| j| W S t |r:| j| W S t |rP| j| W S W n tk
rf   Y nX t |r|| j	| S | j
| S )z%Generate documentation for an object.)r   r*   	docmoduler+   docclassr,   
docroutiner   rj   docdatadocother)r>   r   r(   argsr   r   r   documentx  s    
 
 
 
 
zDoc.documentc                 G   s*   d|odt | t|jf }t|dS )z+Raise an exception for unimplemented types.z.don't know how to document object%s of type %s N)reprtyper%   r}   )r>   r   r(   r   messager   r   r   fail  s
     zDoc.failZstdlibc                 C   s   zt |}W n tk
r&   d}Y nX tjd| j}tj|}t	|t
tr|jdks||r|tj|ds|tj|ds|jdkr|drd|d	|j f }qtj||j d
 }nd}|S )z*Return the location of module docs or None
(built-in)r   )
errno
exceptionsZgcZimpmarshalposixsignalr   _threadZ	zipimportzdist-packageszsite-packages)z	xml.etreeztest.pydoc_mod)zhttp://zhttps://z%s/%s/.htmlN)r   
getabsfiler}   r	   environrv   r   r   r   rk   r   r%   ra   r!   r   lower)r>   r   Zbasedirr   doclocr   r   r   	getdocloc  s,    

zDoc.getdocloc)N)N)r%   r&   rQ   r	   r   rv   r   version_infor   r   r   r   r   r   r   docpropertyr   	sysconfigZget_pathr   r   r   r   r   r   r  s   

r   c                   @   sH   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d ZeZ	dd Z
eZdS )HTMLReprzBClass for safely making an HTML representation of a Python object.c                 C   s,   t |  d | _| _d| _d | _| _d S N   
   d   r   r   maxlistmaxtuplemaxdict	maxstringmaxotherr>   r   r   r   r     s    
zHTMLRepr.__init__c                 C   s   t |ddddddS )N&z&amp;<z&lt;>z&gt;)r0   r>   r/   r   r   r   escape  s    zHTMLRepr.escapec                 C   s   t | |S r   )r   r   r>   r   r   r   r   r     s    zHTMLRepr.reprc                 C   sZ   t t|dr@ddt|j  }t | |r@t| |||S | ttt	|| j
S Nr%   repr_r_   )rc   r   r!   r%   r   r<   r   r5   r8   r   r   r>   rN   levelZ
methodnamer   r   r   repr1  s
    
zHTMLRepr.repr1c                 C   s^   t || j}t|}d|krJdt|ddkrJd|d  | | |d  S tdd| |S )Nr   \\r   rr   z-((\\[\\abfnrtv\'"]|\\[0-9]..|\\x..|\\u....)+)z<font color="#c040c0">\1</font>)r5   r   r   r0   r   r   r   r>   rN   r   ZtestZtestreprr   r   r   repr_string  s    zHTMLRepr.repr_stringc                 C   s@   z|  ttt|| jW S    |  d|jj  Y S X d S Nz<%s instance>)r   r5   r8   r   r   	__class__r%   r>   rN   r   r   r   r   repr_instance  s    zHTMLRepr.repr_instanceN)r%   r&   rQ   rY   r   r   r   r   r   repr_strr  Zrepr_unicoder   r   r   r   r     s   r   c                   @   s   e Zd ZdZe ZejZejZdd Zd1ddZ	d2d
dZ
dd Zdd Zd3ddZdd Zdd Zdd Zdd Zdd Zdd Zdi i i fdd Zd4d!d"Zd5d#d$Zddi i fd%d&Zd'd( Zddi i i dfd)d*Zd6d+d,ZeZd7d-d.Zd8d/d0ZdS )9HTMLDocz'Formatter class for HTML documentation.c                 C   s   d||f S )Format an HTML page.z<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html><head><title>Python: %s</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head><body bgcolor="#f0f0f8">
%s
</body></html>r   )r>   titlecontentsr   r   r   page  s    zHTMLDoc.pager   c                 C   s   d|||||pdf S )zFormat a page heading.a'  
<table width="100%%" cellspacing=0 cellpadding=2 border=0 summary="heading">
<tr bgcolor="%s">
<td valign=bottom>&nbsp;<br>
<font color="%s" face="helvetica, arial">&nbsp;<br>%s</font></td
><td align=right valign=bottom
><font color="%s" face="helvetica, arial">%s</font></td></tr></table>
    &nbsp;r   )r>   r  fgcolbgcolZextrasr   r   r   heading  s    zHTMLDoc.heading   Nr  c	           
      C   s^   |dkrdd|  d }d|||f }	|r@|	d||||f  }	n|	d|||f  }	|	d|  S )	z Format a section with a heading.Nz<tt>r  z</tt>z<p>
<table width="100%%" cellspacing=0 cellpadding=2 border=0 summary="section">
<tr bgcolor="%s">
<td colspan=3 valign=bottom>&nbsp;<br>
<font color="%s" face="helvetica, arial">%s</font></td></tr>
    zR
<tr bgcolor="%s"><td rowspan=2>%s</td>
<td colspan=2>%s</td></tr>
<tr><td>%s</td>z(
<tr><td bgcolor="%s">%s</td><td>%s</td>z'
<td width="100%%">%s</td></tr></table>r   )
r>   r  r	  r
  r  widthZpreludeZ
marginaliaZgapr   r   r   r   section  s    
zHTMLDoc.sectionc                 G   s   d| }| j |f| S )z$Format a section with a big heading.z<big><strong>%s</strong></big>)r  )r>   r  r   r   r   r   
bigsection  s    zHTMLDoc.bigsectionc              
   C   s&   |  | }t|dddddddd	S )z!Format literal preformatted text.

z
 
r   r  r   <br>
)r   
expandtabsr0   r   r   r   r   	preformat  s       zHTMLDoc.preformatr   c                 C   s   d}t || d | }t|D ]X}|dd|   }t|| || | D ]$}|t |k rJ||||  d }qJ|d }q d| S )z0Format a list of items into a multi-column list.r   r   z<td width="%d%%" valign=top>r   r  z</td>z7<table width="100%%" summary="list"><tr>%s</tr></table>)r    range)r>   listformatZcolsr   rowscolrr   r   r   r   multicolumn  s    
zHTMLDoc.multicolumnc                 C   s   d| S )Nz<font color="#909090">%s</font>r   r   r   r   r   grey"  rz   zHTMLDoc.greyc                 G   s*   |D ] }||krd|| |f   S q|S )z:Make a link for an identifier, given name-to-URL mappings.<a href="%s">%s</a>r   )r>   r(   Zdictsdictr   r   r   namelink$  s    zHTMLDoc.namelinkc                 C   sN   |j tj|j }}t||rDt|||krDd|j |t||f S t||S )zMake a link for a class.z<a href="%s.html#%s">%s</a>)r%   r   r   rv   r&   rc   r<   r)   )r>   r   r'   r(   r   r   r   r   	classlink+  s      zHTMLDoc.classlinkc                 C   s   d|j |j f S )zMake a link for a module.<a href="%s.html">%s</a>)r%   r   r   r   r   
modulelink3  s    zHTMLDoc.modulelinkc                 C   sR   |\}}}}|r|  |S |r,d||f }nd| }|rBd| }n|}d||f S )z;Make a link for a module or package to display in an index.z
%s.%s.htmlz%s.htmlz"<strong>%s</strong>&nbsp;(package)r  )r  )r>   Z
modpkginfor(   r   r   shadowedurlr/   r   r   r   
modpkglink7  s    

zHTMLDoc.modpkglinkc                 C   s   d||f S )zMake a link to source file.z<a href="file:%s">%s</a>r   )r>   r"  r   r   r   r   filelinkF  s    zHTMLDoc.filelinkc                 C   s  |p| j }g }d}td}|||}	|	s0qh|	 \}
}|||||
  |	 \}}}}}}|r||dd}|d||f  n|rdt| }|d|||f  n|rdt| }|d|||f  n|r"|||d  d	kr|d
| 	||  n|d|  n@|||d  d	krP|| 	|||| n|| 	|| |}q||||d  d
|S )zMark up some plain text, given a context of symbols to look for.
        Each context dictionary maps object names to anchor names.r   zD\b((http|ftp)://\S+[\w/]|RFC[- ]?(\d+)|PEP[- ]?(\d+)|(self\.)?(\w+))"z&quot;r  z'http://www.rfc-editor.org/rfc/rfc%d.txtz(http://www.python.org/dev/peps/pep-%04d/r   (zself.zself.<strong>%s</strong>Nr   )r   r   compilesearchspanr   groupsr0   intr  r!   )r>   r/   r   funcsclassesrG   rn   herepatternmatchstartendrd   ZschemeZrfcZpepZselfdotr(   r"  r   r   r   markupJ  s:    

 zHTMLDoc.markupc           
      C   s   d}|D ]}t |t dkr|\}}|d }|| || }|r||fkrg }|D ]}	|| |	| qR|d d| d }|d }qt |t g kr|d| |||  }qd	| S )
zAProduce HTML for a class tree as given by inspect.getclasstree().r   r   z"<dt><font face="helvetica, arial">r&  , )z
</font></dt>z<dd>
%s</dd>
z<dl>
%s</dl>
)r   r  r   r!   
formattree)
r>   treer'   parentr   entrycbasesparentsrJ   r   r   r   r6  u  s&    
  
zHTMLDoc.formattreec           #   
      s  |j }z
|j}W n tk
r(   d}Y nX |d}g }tt|d D ],}|dd|d|d  || f  qHd||dd  }	d|	 }
z&t	|}t
j|} ||}W n tk
r   d}Y nX g }t|dr6t|j}|dd	 d
kr"|dd dkr"|d	d  }|d |  t|drX| t|j |rp|
dd|  }
 |}|dk	rdt  }nd} |
ddd| | }t|tj}g i  }}t|tjD ]Z\}}|dk	st|p||krt|||r|||f d|  ||< ||< q|D ]\}}|jD ]n}|j |j }}tj !|}||kr@|r@t||r@t"|||kr@||kr@|d |  ||< ||< q@q2g i  }}t|tj#D ]p\}}|dk	st$|st||krt|||r|||f d| ||< t%|r|| ||< qg }t|t&D ]&\}}t|||rN|||f qN 't(| j)||}|od| }|d|  }t|drg }t*+|j,D ]\}}} |||| df q|-   .| j/}!| 0ddd|! }n.|r< .| fdd}!| 0d dd|! }|rd!d" |D }" 1t2|"d|g}!|D ]"\}}|! 3||||| qj| 0d#dd$d%|! }|rg }!|D ]"\}}|! 3||||| q| 0d&dd'd%|! }|r:g }!|D ]\}}|! 3|| q| 0d(dd)d*|! }t|d+rn 't|j4 j)}!| 0d,dd|! }t|d-r 't|j5 j)}!| 0d.dd|! }|S )/z/Produce HTML documentation for a module object.Nr   r   z5<a href="%s.html"><font color="#ffffff">%s</font></a>r   )<big><big><strong>%s</strong></big></big>r   rS      $Revision: $z
version %srU   z (%s)r4  z-<br><a href="%(docloc)s">Module Reference</a>r   #ffffff#7799eez<a href=".">index</a><br>r   z.html#z#-z<tt>%s</tt>z
<p>%s</p>
rZ   r   zPackage Contentsz#aa55ccc                    s     | d S r@   )r   tr   r   r   ry     rz   z#HTMLDoc.docmodule.<locals>.<lambda>ZModulesc                 S   s   g | ]\}}|qS r   r   rq   rH   rI   r   r   r   r     s     z%HTMLDoc.docmodule.<locals>.<listcomp>ZClasses#ee77aar   Z	Functionsz#eeaa77ZDataz#55aa55r  r[   ZAuthorrT   ZCredits)6r%   __all__r   r   r  r    r   r!   r   r   urllibparseZquoter$  r}   rc   strrS   r   r   rU   r   localsr  rA   r*   r+   	getmodulerf   rB   r&   r   r   rv   r<   r,   r;   
isfunctionr.   r3  r   r  pkgutiliter_modulesrZ   r~   r  r#  r  r6  getclasstreer   r[   rT   )#r>   r   r(   modignoredrd   partsZlinksrr   Z
linkednameheadr   r"  r$  r   versionr   r   r   r-  ZcdictrH   rI   rJ   r'   r   r,  Zfdictdatar"   modpkgsimporterispkgr  	classlistr   r   r   r     s*   





$

  




 

     
                  zHTMLDoc.docmodulec              	      s  j }|p|}j}g }	|	jG fddd}
|
 tt}t|dkr  d |D ]}d|j	  qdd  fdd}fd	d
} fdd}fddt
D }i |D ]n\}}}}d| d |  |< }zt|}W n tk
r4   Y nX z||< W q tk
rX   Y qX q|rx|rr| n|d d t|fdd\}}tjk	rtjkr|}q\n"krd}ndj	 }|d7 }t| |d| |dd }|d| |dd }|d| |dd }|d| |dd }|d| |d d }|d!| |d"d }|g kspt|}q\d#|	}	||krd$||f }nd%|||f }|rg }|D ]}||j	 q|d&d'|  }d#}zt}W n ttfk
r   d(}Y nX |rFt|}|rF|d)krF|| d* }t}|rb||p^d# }|j }|od+| }|d,d-|	d.|S )/z.Produce HTML documentation for a class object.c                       s    e Zd Zdd Z fddZdS )z(HTMLDoc.docclass.<locals>.HorizontalRulec                 S   s
   d| _ d S ru   Zneedoner   r   r   r   r     s    z1HTMLDoc.docclass.<locals>.HorizontalRule.__init__c                    s   | j r d d| _ d S )Nz<hr>
r   r[  r   pushr   r   maybe  s    z.HTMLDoc.docclass.<locals>.HorizontalRule.maybeNr%   r&   rQ   r   r^  r   r\  r   r   HorizontalRule  s   r`  r   z&<dl><dt>Method resolution order:</dt>
z<dd>%s</dd>
</dl>
c                    s   t ||\}}|r  |  |D ]d\}}}}zt|}W n& tk
rf   || Y nX ||  d q&|S )Nr   rO   r^  r<   	Exceptionr   r   msgr   rM   okr(   ro   homeclsrI   r-  r,  hrmdictrQ  r   r]  r>   r   r   spill  s"       
zHTMLDoc.docclass.<locals>.spillc                    sJ   t ||\}}|rF   |  |D ]\}}}}|| q&|S r   rO   r^  r   rd  ri  rQ  r]  r>   r   r   spilldescriptors1  s    z*HTMLDoc.docclass.<locals>.spilldescriptorsc           
         s   t ||\}}|r  |  |D ]\}}}}t||}t|sXt|rft|dd }	nd }	|	d krd|  n0t|j	 }	d|	 }	d||	f  d q&|S )NrY   z<dl><dt>%s</dl>
z<dd><tt>%s</tt>z<dl><dt>%s%s</dl>
r   )
rO   r^  r   r<   callabler   rj   r3  r   r  )
re  r   rM   rf  r(   ro   rg  rI   rJ   r"   rh  r   r   	spilldata:  s(      
z#HTMLDoc.docclass.<locals>.spilldatac                    s,   g | ]$\}}}}t | d r||||fqS )re   rf   rq   r(   ro   rp   rI   r-   r   r   r   O  s   
z$HTMLDoc.docclass.<locals>.<listcomp>r   -r   c                    s   | d  kS Nr   r   rC  	thisclassr   r   ry   h  rz   z"HTMLDoc.docclass.<locals>.<lambda>defined hereinherited from %sz:<br>
z
Methods %sc                 S   s   | d dkS Nr   methodr   rC  r   r   r   ry   x  rz   zClass methods %sc                 S   s   | d dkS Nr   zclass methodr   rC  r   r   r   ry   z  rz   zStatic methods %sc                 S   s   | d dkS Nr   zstatic methodr   rC  r   r   r   ry   |  rz   zReadonly properties %sc                 S   s   | d dkS Nr   rh   r   rC  r   r   r   ry   ~  rz   zData descriptors %sc                 S   s   | d dkS Nr   rg   r   rC  r   r   r   ry     rz   zData and other attributes %sc                 S   s   | d dkS Nr   rV  r   rC  r   r   r   ry     rz   r   z*<a name="%s">class <strong>%s</strong></a>z/<strong>%s</strong> = <a name="%s">class %s</a>(%s)r4  N()r  z<tt>%s<br>&nbsp;</tt>z#000000z#ffc8d8r1   )r%   rB   r   r   r   getmror    r^  r  r&   ri   r<   rc  r}   popleftrO   builtinsr   r   AssertionErrorr!   	signature
ValueErrorrJ  r   r   r3  r  r  )r>   r   r(   rQ  r,  r-  rR  realnamer;  r  r`  mrorJ   rk  rn  rp  r   rH   ro   rg  rI   anchor	inheritedtagr  r<  declr  argspecr"   r   )	r-  r,  ri  rj  rQ  r   r]  r>   rw  r   r     s    

	











   
zHTMLDoc.docclassc                 C   s   |  d| | S z)Format an argument default value as text.=)r  r   r   r   r   r   formatvalue  s    zHTMLDoc.formatvaluec              	   C   s  |j }|p|}|r|j pdd | }	d}
d}t|r|jj}|rZ||k	rd| || }
n0|jdk	rzd| |jj| }
nd| || }
t|st|rd}nd}||krd	|	|f }nD|rt||g |krd
|j d | |f }d}n|}d|	||f }d}t	|rlzt
|}W n ttfk
r>   d}Y nX |rlt|}|dkrld| }|dd }|svd}|| | | |
o| d|
  }|rd| S | t|| j|||}|od| }d||f S dS )z;Produce HTML documentation for a function or method object.r   rt  r    from N method of %s instance unbound %s methodasync z$<a name="%s"><strong>%s</strong></a>z<a href="#%s">%s</a>r   z)<a name="%s"><strong>%s</strong></a> = %s<lambda>z$<strong>%s</strong> <em>lambda</em> r   (...)z'<font face="helvetica, arial">%s</font>z<dl><dt>%s</dt></dl>
z<dd><tt>%s</tt></dd>z<dl><dt>%s</dt>%s</dl>
)r%   r?   r9   r   r  r   iscoroutinefunctionisasyncgenfunctiongetattr_staticr,   r  r  r}   rJ  r   r  r3  r   r  )r>   r   r(   rQ  r,  r-  rG   rF   r  r  noteskipdocsimclassasyncqualifierr  Zreallinkr  r  r  r"   r   r   r   r     s|    
 
   

    zHTMLDoc.docroutinec                 C   sN   g }|j }|r|d|  | t|| j}|r<|d|  |d d|S )z1Produce html documentation for a data descriptor.z!<dl><dt><strong>%s</strong></dt>
z<dd><tt>%s</tt></dd>
ra  r   )r   r3  r   r  r!   r>   r   r(   rQ  rF   rn   r]  r"   r   r   r   r     s    zHTMLDoc.docdatac                 G   s   |rd| pd}||  | S )z-Produce HTML documentation for a data object.z<strong>%s</strong> = r   r   )r>   r   r(   rQ  rR  Zlhsr   r   r   r     s    zHTMLDoc.docotherc                 C   s   g }|dkri }t |gD ]<\}}}tdd |D r:q||d|||kf d||< q|  | || j}| |dd|S )z2Generate an HTML index for a directory of modules.Nc                 s   s*   | ]"}d t |  kodkn  V  qdS )i   i  N)ordrq   Zchr   r   r   	<genexpr>  s     z HTMLDoc.index.<locals>.<genexpr>r   r   rA  rF  )rN  rO  anyr   r~   r  r#  r  )r>   r   r!  rW  rX  r(   rY  r  r   r   r   index   s     
zHTMLDoc.index)r   )r  r   Nr  )r   )N)NN)NNN)NN)N)r%   r&   rQ   rY   r   _repr_instancer   r   r  r  r  r  r  r  r  r  r  r   r#  r$  r3  r6  r   r   r  r   r   r   r   r  r   r   r   r   r    sH   

      

+

y &   
A

r  c                   @   s4   e Zd ZdZdd Zdd Zdd ZeZdd	 Zd
S )TextReprzAClass for safely making a text representation of a Python object.c                 C   s,   t |  d | _| _d| _d | _| _d S r   r   r   r   r   r   r     s    
zTextRepr.__init__c                 C   sT   t t|dr@ddt|j  }t | |r@t| |||S ttt|| j	S r   )
rc   r   r!   r%   r   r<   r5   r8   r   r   r   r   r   r   r     s
    
zTextRepr.repr1c                 C   sH   t || j}t|}d|krDdt|ddkrDd|d  | |d  S |S )Nr   r   r   r   r   )r5   r   r   r0   r   r   r   r   r      s
    zTextRepr.repr_stringc                 C   s4   zt tt|| jW S    d|jj  Y S X d S r   )r5   r8   r   r   r   r%   r   r   r   r   r  +  s    zTextRepr.repr_instanceN)	r%   r&   rQ   rY   r   r   r   r  r  r   r   r   r   r    s   	r  c                   @   s~   e Zd ZdZe ZejZdd ZdddZdd Z	dddZ
dddZdddZdd ZdddZdddZeZdddZd	S ) TextDocz'Formatter class for text documentation.c                 C   s   d dd |D S )z(Format a string in bold by overstriking.r   c                 s   s   | ]}|d  | V  qdS )Nr   r  r   r   r   r  ;  s     zTextDoc.bold.<locals>.<genexpr>)r!   r   r   r   r   bold9  s    zTextDoc.bold    c                    s>   |sdS  fdd| dD }|r4|d  |d< d|S )z6Indent text by prepending a given prefix to each line.r   c                    s   g | ]} | qS r   r   rq   r   prefixr   r   r   @  s     z"TextDoc.indent.<locals>.<listcomp>r   r   )r   r   r!   )r>   r/   r  r#   r   r  r   indent=  s      zTextDoc.indentc                 C   s$   |  | }| |d | d S )z&Format a section with a given heading.r   r  )r  r   r  )r>   r  r  Zclean_contentsr   r   r   r  D  s    zTextDoc.sectionNr   c           
   	      s   d}|D ]}t |t dkrr|\}}|| t|  }|rh||fkrh fdd|D }	|dd|	  }|d }qt |t g kr|| | ||d  }q|S )	zBRender in text a class tree as returned by inspect.getclasstree().r   r   c                 3   s   | ]}t | V  qd S r   r)   )rq   r:  r'   r   r   r  S  s     z%TextDoc.formattree.<locals>.<genexpr>r  r4  r   r  )r   r)   r!   r6  )
r>   r7  r'   r8  r  r   r9  r:  r;  r<  r   r  r   r6  K  s"    
   zTextDoc.formattreec              	   C   s$  |j }tt|\}}| d||o(d|  }t|dd}| |}|dk	r`|| d|d  }|rt|| d| }g }	t|tjD ]<\}
}|dk	st	|p||krt
|
||r|	|
|f qg }t|tjD ]F\}
}|dk	st|st	||krt
|
||r||
|f qg }t|tD ]&\}
}t
|
||r.||
|f q.g }t }t|drt|jD ]6\}}}|| |r||d	  n
|| qx|  || d
d| }g }t|tjD ]0\}
}|j |d r|
|kr||
 q|r6|  || dd| }|	rdd |	D }| t|d|g}|	D ]\}
}|| ||
| qd|| dd| }|rg }|D ]\}
}|| ||
| q|| dd| }|r&g }|D ]"\}
}|| j||
|dd q|| dd| }t|drt|j}|dd dkrp|dd dkrp|dd  }|| d| }t|dr|| dt|j  }t|dr|| dt|j! }t|d r|| d!t|j" }zt#|}W n t$k
r   d"}Y nX || d#| }|S )$z5Produce text documentation for a given module object.NAME - rG  NzMODULE REFERENCEa.  

The following documentation is automatically generated from the Python
source files.  It may be incomplete, incorrect or include features that
are considered implementation detail and may vary between Python
implementations.  When in doubt, consult the module reference at the
location listed above.
ZDESCRIPTIONrZ   
 (package)zPACKAGE CONTENTSr   r   Z
SUBMODULESc                 S   s   g | ]\}}|qS r   r   rE  r   r   r   r     s     z%TextDoc.docmodule.<locals>.<listcomp>r   CLASSES	FUNCTIONSF   )r4   ZDATArS   r>  r?  r   r@  ZVERSIONrU   ZDATEr[   ZAUTHORrT   ZCREDITSr   ZFILE)%r%   r$   r   r  r<   r   r   rA   r+   rL  rf   r   r,   r;   r.   setrc   rN  rO  rZ   addr~   r!   r*   ra   r6  rP  r   r   rJ  rS   r   rU   r[   rT   r   r}   )r>   r   r(   rQ  Zsynopdescr   rd   r   r-  rH   rI   r,  rV  rW  Zmodpkgs_namesrX  r'   rY  Z
submodulesrZ  r  rU  r   r   r   r   r   [  s    
	

  
 
$
zTextDoc.docmodulec              	      sZ  j }|p|}j}jfdd}||kr:d| }n|d | }|rlt||}	|dd|	  }g }
|
jzt}W n t	t
fk
r   d}Y nX |rt|}|r|dkrʈ|| d	  t}|r|d	  tt}t|d
kr*d |D ]}d||  q
d tdd tD tjd}t|}d}|rd |d| D ]}d|  qn||krdt||  d  d G fddd}|   fdd} fdd} fdd}fddtD }|r&|r*| n|d d
 t|fd d!\}}tjk	rntjkrn|}qn kr~d"}nd#tj }t| |d$| |d%d! }|d&| |d'd! }|d(| |d)d! }|d*| |d+d! }|d,| |d-d! }|d.| |d/d! }|g kst|}qd	|
}
|
s>|d	 S |d	 |
 d0 d	 S )1z4Produce text documentation for a given class object.c                 S   s
   t | |S r   r  )r:  r   r   r   r   makename  s    z"TextDoc.docclass.<locals>.makenameclass z	 = class r  r4  Nr  r   r   zMethod resolution order:r  r   c                 s   s.   | ]&}|j d s|jdkrt|j V  qdS )r_   r  N)r%   ra   r&   rJ  )rq   rp   r   r   r   r    s     
z#TextDoc.docclass.<locals>.<genexpr>r{   r   zBuilt-in subclasses:z    ... and z other subclassesc                       s    e Zd Zdd Z fddZdS )z(TextDoc.docclass.<locals>.HorizontalRulec                 S   s
   d| _ d S ru   r[  r   r   r   r   r     s    z1TextDoc.docclass.<locals>.HorizontalRule.__init__c                    s   | j r d d| _ d S )NzF----------------------------------------------------------------------r   r[  r   r\  r   r   r^    s    z.TextDoc.docclass.<locals>.HorizontalRule.maybeNr_  r   r\  r   r   r`    s   r`  c              
      s   t ||\}}|r~   |  |D ]V\}}}}zt|}W n& tk
rf   || Y q&X || q&|S r   rb  rd  ri  rQ  r   r]  r>   r   r   rk    s      zTextDoc.docclass.<locals>.spillc                    sJ   t ||\}}|rF   |  |D ]\}}}}|| q&|S r   rl  rd  rm  r   r   rn    s    z*TextDoc.docclass.<locals>.spilldescriptorsc           
   	      s   t ||\}}|r   |  |D ]v\}}}}t|sDt|rNt|}nd }zt|}	W n tk
r~   |j| }	Y nX j	|	|d|dd  q&|S )Nr  )r4   r"   r   )
rO   r^  ro  r   rj   r   r<   r   __dict__r   )
re  r   rM   rf  r(   ro   rg  rI   r"   re   r  r   r   rp    s     
z#TextDoc.docclass.<locals>.spilldatac                    s,   g | ]$\}}}}t | d r||||fqS rq  rr  rs  r-   r   r   r   1  s   
z$TextDoc.docclass.<locals>.<listcomp>r   c                    s   | d  kS ru  r   rC  rv  r   r   ry   :  rz   z"TextDoc.docclass.<locals>.<lambda>rx  ry  zMethods %s:
c                 S   s   | d dkS rz  r   rC  r   r   r   ry   I  rz   zClass methods %s:
c                 S   s   | d dkS r|  r   rC  r   r   r   ry   K  rz   zStatic methods %s:
c                 S   s   | d dkS r}  r   rC  r   r   r   ry   M  rz   zReadonly properties %s:
c                 S   s   | d dkS r~  r   rC  r   r   r   ry   O  rz   zData descriptors %s:
c                 S   s   | d dkS r  r   rC  r   r   r   ry   Q  rz   zData and other attributes %s:
c                 S   s   | d dkS r  r   rC  r   r   r   ry   S  rz   z |  )r%   rB   r&   r  mapr!   r   r   r  r  r}   rJ  r   r   r  r    sortedr   __subclasses__r   ri   r  rO   r  r   r)   r   r  r  r   )r>   r   r(   rQ  rR  r  r;  r  r  r<  r  r  r  r"   r  rJ   Z
subclassesZno_of_subclassesZMAX_SUBCLASSES_TO_DISPLAYZsubclassnamer`  rk  rn  rp  r   r  r  r   )ri  rQ  r   r]  r>   rw  r   r     s    



	










zTextDoc.docclassc                 C   s   d|  | S r  r  r   r   r   r   r  ]  s    zTextDoc.formatvaluec              	   C   s  |j }|p|}d}d}t|rn|jj}|rB||k	rndt|| }n,|jdk	r`dt|jj| }ndt|| }t|st|rd}	nd}	||kr| |}
n,|rt	||g |krd}| |d	 | }
d}t
|r<zt|}W n ttfk
r   d}Y nX |r<t|}|d
kr<| |d }
|dd }|sFd}|	|
 | | }|rd|d S t|ppd}|d |o| | d  S dS )z;Produce text documentation for a function or method object.r   r   r  Nr  r  r  r    = r  z lambda r   r  r   )r%   r?   r9   r   r)   r   r  r  r  r  r,   r  r  r}   rJ  r   r  r   )r>   r   r(   rQ  rF   r  r  r  r  r  r  r  r  r  r"   r   r   r   r   a  sV    
 


zTextDoc.docroutinec                 C   sT   g }|j }|r$|| | |d t|p.d}|rJ|| | |d d|S )z1Produce text documentation for a data descriptor.r   r   )r   r  r   r  r!   r  r   r   r   r     s    zTextDoc.docdatac           
      C   s   |  |}|rF|r|d pd| }|t| }	|	dk rF|d|	 d }|rX| |d pZd| }|dk	r~|d| t| 7 }|S )z-Produce text documentation for a data object.r  r   r   Nr2   r   )r   r    r  r  rJ  )
r>   r   r(   rQ  r8  r4   r"   r   r   Zchopr   r   r   r     s    
 zTextDoc.docother)r  )Nr   )NN)NN)NNN)NNN)NNNNN)r%   r&   rQ   rY   r  r  r   r  r  r  r6  r   r   r  r   r   r   r   r   r   r   r   r  1  s   


e
 
7
r  c                   @   s   e Zd ZdZdd ZdS )_PlainTextDocz2Subclass of TextDoc which overrides string stylingc                 C   s   |S r   r   r   r   r   r   r    s    z_PlainTextDoc.boldN)r%   r&   rQ   rY   r  r   r   r   r   r    s   r  c                 C   s   t  at|  dS )zCThe first time this is called, determine what kind of pager to use.N)getpagerpagerr7   r   r   r   r    s    r  c               	      s`  t tjdstS t tjds tS tj r4tj s8tS tjdpNtjd  rtj	dkrj fddS tjddkr fd	dS  fd
dS tjddkrtS tj	dkrdd S t tdrt
ddkrdd S t tdrt
ddkrdd S ddl} |  \}}t| z8t tdrDt
d| dkrDdd W S tW S W 5 t| X dS )z2Decide what method to use for paging through text.isattyZMANPAGERZPAGERZwin32c                    s   t t|  S r   tempfilepagerplainr7   Z	use_pagerr   r   ry     rz   zgetpager.<locals>.<lambda>ZTERM)ZdumbZemacsc                    s   t t|  S r   )	pipepagerr  r7   r  r   r   ry     rz   c                    s
   t |  S r   r  r7   r  r   r   ry     rz   c                 S   s   t t| dS )Nzmore <r  r7   r   r   r   ry     rz   systemz(pager) 2>/dev/nullr   c                 S   s
   t | dS )Nr  r  r7   r   r   r   ry     rz   z(less) 2>/dev/nullc                 S   s
   t | dS )NZlessr  r7   r   r   r   ry     rz   Nz	more "%s"c                 S   s
   t | dS )NZmorer  r7   r   r   r   ry     rz   )rc   r   stdin
plainpagerstdoutr  r	   r   rv   platformr  tempfileZmkstempcloseunlinkttypager)r  fdr   r   r  r   r    s:    


 r  c                 C   s   t dd| S )z%Remove boldface formatting from text.z.r   )r   r   r7   r   r   r   r    s    r  c              	   C   s   ddl }|j|d|jd}zDtj|jdd*}z||  W n tk
rP   Y nX W 5 Q R X W n tk
rr   Y nX z|	  W qW qt tk
r   Y qtX qtdS )z3Page through text by feeding it to another program.r   NT)shellr  backslashreplaceerrors)

subprocessPopenPIPEioTextIOWrapperr  writeKeyboardInterruptr   wait)r/   cmdr  procpiper   r   r   r    s    r  c              	   C   s`   ddl }| }t|ddd}||  W 5 Q R X zt|d | d  W 5 t| X dS )z<Page through text by invoking a program on a temporary file.r   Nwr  r  z "r%  )r  Zmktempr   r  r	   r  r  )r/   r  r  r   r   r   r   r   r     s    r  c                 C   s$   t tjdd pd}| |d|S )Nencodingutf-8r  )r<   r   r  encodedecode)r/   r  r   r   r   _escape_stdout  s    r  c           
      C   s  t t| d}z2ddl}tj }||}|| dd }W n( t	t
tjfk
rl   d}dd }Y nX z0zttjdd}W n tk
r   d}Y nX |dkrd	}|d  }}tjd|d| d  ||d rtjd
 tj  | }	|	dkrtjd qn,|	dkrJtjd||  d  |d }q|	dkrn|| | }|dk rnd}tjdd||||   d  || }qW 5 |r|||j| X dS )z%Page through text on a text terminal.r   r   Nc                   S   s   t jdS r@   )r   r  r   r   r   r   r   ry     rz   zttypager.<locals>.<lambda>c                   S   s   t j d d d d S )Nr   r   )r   r  r   r   r   r   r   ry     rz   ZLINESr      z
-- more --)qQz          )r   )bB)r  r  r   ttyr   r  filenoZ	tcgetattrZ	setcbreakr   r   r  UnsupportedOperationZ	tcsetattrZ	TCSAFLUSHr+  r	   r   rv   r  r  r  r!   flush)
r/   r#   r  r  oldZgetcharhr   Zincr:  r   r   r   r    sL    








 &r  c                 C   s   t jtt|  dS )z>Simply print unformatted text.  This is the ultimate fallback.N)r   r  r  r  r  r7   r   r   r   r  <  s    r  c                 C   s   t | r>| jtjkr d| j S t| dr4d| j S d| j S t | rRd| j S t | rtd| jj	| jj| jf S t 
| rd| jj	| jj| jf S t | rd| j S t | rd	| j S t | rd
| j S t| jS )z/Produce a short description of the given thing.zbuilt-in module rZ   zpackage zmodule zbuilt-in function zgetset descriptor %s.%s.%szmember descriptor %s.%s.%sr  z	function zmethod )r   r*   r%   r   r   rc   r;   Zisgetsetdescriptor__objclass__r&   Zismemberdescriptorr+   rM  r:   r   )thingr   r   r   describe@  s6    







 
 





r  c              	   C   s   dd |  dD }d\}}|t|k r\td|d|d  |}|r\||d  }}qq\q|rf|}nt}||d D ],}zt||}W qv tk
r   Y  dS X qv|S )z@Locate an object by name or dotted path, importing as necessary.c                 S   s   g | ]}|r|qS r   r   )rq   r   r   r   r   r   ]  s      zlocate.<locals>.<listcomp>r   ru   Nr   )r   r    r   r!   r  r<   r   )r   r   rS  r   nZ
nextmoduler   r   r   r   r   locate[  s      r  c                 C   sV   t | tr0t| |}|dkr(td|  || fS t| dd}| t |trL|ndfS dS )zDGiven an object or a path to an object, get the object and its name.Nz~No Python documentation found for %r.
Use help() to get the interactive help utility.
Use help(str) for help on the str class.r%   )rk   rJ  r  r   r<   )r  r   r   r(   r   r   r   resolvet  s    

r   Python Library Documentation: %sc                 C   s   |dkrt }t| |\}}t|}t|}|rTd|krT|d|d|d  7 }n|rn||k	rn|d|j 7 }t|st|st	|st
|st|}|d7 }|| d ||| S )zBRender text documentation, given an object or a path to an object.Nr   z in z in module z objectr  )r/   r  r  r   rL  rfindr%   r*   r+   r,   rj   r   r   )r  r  r   Zrendererr   r(   r  r   r   r   r   
render_doc  s&    

r  c              
   C   sf   z2|dkrt t| || n|t| ||t W n. ttfk
r` } zt| W 5 d}~X Y nX dS )zCDisplay text documentation, given an object or a path to an object.N)r  r  r  	plaintextr   r   print)r  r  r   outputrI   r   r   r   r"     s    r"   c              
   C   s   z`t | |\}}tt|t||}t|d ddd}|| W 5 Q R X td|d  W n. tt	fk
r } zt| W 5 d}~X Y nX dS )z<Write HTML documentation to a file in the current directory.r   r  r  )r  ZwroteN)
r  htmlr  r  r   r   r  r  r   r   )r  r   r   r(   r  r   rI   r   r   r   writedoc  s    r  r   c                 C   s2   |dkri }t | g|D ]\}}}t| qdS )zAWrite out HTML documentation for all modules in a directory tree.N)rN  walk_packagesr  )r   pkgpathZdonerX  r'   rY  r   r   r   	writedocs  s
     
r
  c                J   @   s"  e Zd Zddddddddddd	d
dddddddddddddddddddddddd#Zd d! d"D Zded'd(d)d*d+d,d-Zd.d/d0d1d2d3d4d5d6d7d8d9d9d:d:d;Ze D ]:\ZZ	e	D ],Z
ee
eZeekred< e Zeee
< qqd=d>d?d@dAdBdCdDdEdFdGdHdIdJdKdLdLdMdNdOdPdQddRdSdSdTdUdVdWdXdYdZd[d\d]d^d_d_d`dadbdcdddedfdgdhdidjdkdldmdndodpdqdrdsdtdudvdwdxdydzd{ddd|d}d~ddIZdddZedd Zedd Zdd Ze ZefddZdd Zdd Zdd Zdd ZdddZdd Zdd Zdd ZdddZdddZdd ZdddZ dS )Helperr   BOOLEANwith)assertr   )asyncr   )awaitr   )break	while for)classzCLASSES SPECIALMETHODS)continuer  )Zfunctionr   )delBASICMETHODSif)elser  try)forzbreak continue whileimport)globalznonlocal NAMESPACES)r  
TRUTHVALUE)r  MODULES)inSEQUENCEMETHODS
COMPARISON)lambdar  )nonlocalzglobal NAMESPACES)passr   )raise
EXCEPTIONS)returnr  )r  r&  )whilezbreak continue if TRUTHVALUE)r  z CONTEXTMANAGERS EXCEPTIONS yield)yieldr   )#FalseNoneTrueandasr  r  r  r  r  r  defr  elifr  exceptfinallyr  fromr  r  r  r  isr"  r#  notorr$  r%  r'  r  r(  r  r)  c                 C   s   g | ]}d D ]}|| qqS )'r%  r   )rq   pr  r   r   r   r     s       zHelper.<listcomp>)r  fr   ur8  '''r%  r   )+rt  ***r   z//%<<>>r   |^~r   r   <=>===!=<>)r   r   rF  rG  rH  rI  rJ  )rt  rE  )z+=z-=z*=z/=z%=z&=z|=z^=z<<=z>>=z**=z//=)rA  rB  r   rC  rD  rE  )jJ)STRINGS	OPERATORSr!  UNARYAUGMENTEDASSIGNMENTBITWISECOMPLEXzOPERATORS FORMATTINGPOWERzTUPLES LISTS FUNCTIONSz ATTRIBUTES FLOAT MODULES OBJECTSELLIPSISzSLICINGS DICTIONARYLITERALSz	def classrM  PRIVATENAMESzPRIVATENAMES SPECIALMETHODSZ
BACKQUOTESzTUPLES FUNCTIONS CALLSzLISTS SUBSCRIPTS SLICINGS)r@  r?  ,r   r2   :@r   r_   r^   `r&  r5  []r   )typeszRSTRINGS UNICODE NUMBERS SEQUENCES MAPPINGS FUNCTIONS CLASSES MODULES FILES inspect)stringsz4str UNICODE SEQUENCES STRINGMETHODS FORMATTING TYPES)zstring-methodszSTRINGS FORMATTING)ZformatstringsrN  )r]  z:encodings unicode SEQUENCES STRINGMETHODS FORMATTING TYPES)ZnumberszINTEGER FLOAT COMPLEX TYPES)Zintegersz	int range)Zfloatingz
float math)Z	imaginaryzcomplex cmath)Ztypesseqz$STRINGMETHODS FORMATTING range LISTSDICTIONARIES)Ztypesfunctionsz	def TYPES)Ztypesmethodszclass def CLASSES TYPES)zbltin-code-objectszcompile FUNCTIONS TYPES)zbltin-type-objectsztypes TYPESTYPES)zbltin-null-objectr   )zbltin-ellipsis-objectSLICINGS)Zspecialattrsr   )r\  z!class SPECIALMETHODS PRIVATENAMES)Ztypesmodulesr  )zoperator-summaryzlambda or and not in is BOOLEAN COMPARISON BITWISE SHIFTING BINARY FORMATTING POWER UNARY ATTRIBUTES SUBSCRIPTS SLICINGS CALLS TUPLES LISTS DICTIONARIESEXPRESSIONS)Zobjectsr_  )ZspecialnameszbBASICMETHODS ATTRIBUTEMETHODS CALLABLEMETHODS SEQUENCEMETHODS MAPPINGMETHODS NUMBERMETHODS CLASSES)Zcustomizationzhash repr str SPECIALMETHODS)zattribute-accesszATTRIBUTES SPECIALMETHODS)zcallable-typeszCALLS SPECIALMETHODS)sequence-typesz(SEQUENCES SEQUENCEMETHODS SPECIALMETHODS)rb  zMAPPINGS SPECIALMETHODS)znumeric-typesz*NUMBERS AUGMENTEDASSIGNMENT SPECIALMETHODS)Z	execmodelz%NAMESPACES DYNAMICFEATURES EXCEPTIONS)Znamingz3global nonlocal ASSIGNMENT DELETION DYNAMICFEATURES)zdynamic-featuresr   
NAMESPACES)r   ztry except finally raise)Zconversionsr   )Zidentifierszkeywords SPECIALIDENTIFIERS)z
id-classesr   )zatom-identifiersr   )zatom-literalsz=STRINGS NUMBERS TUPLELITERALS LISTLITERALS DICTIONARYLITERALS	SEQUENCES)Z	exprlistszTUPLES LITERALS)ztypesseq-mutableLISTLITERALS)ZlistszLISTS LITERALS)ZtypesmappingDICTIONARYLITERALS)r  zDICTIONARIES LITERALS)zattribute-referencesz(getattr hasattr setattr ATTRIBUTEMETHODS)Zsubscriptionsr   )Zslicingsr   )Zcallsra  )Zpowerra  )Zunaryra  )Zbinaryra  )Zshiftingra  )Zbitwisera  )ZcomparisonszEXPRESSIONS BASICMETHODS)ZbooleanszEXPRESSIONS TRUTHVALUEr  )Z
assignmentrP  )Z	augassignNUMBERMETHODSr  r'  )Zcompoundzfor while break continue)truthz if while and or not BASICMETHODS)ZdebuggerZpdb)zcontext-managersr  )Ir_  rM  ZSTRINGMETHODSZ
FORMATTINGUNICODEZNUMBERSZINTEGERZFLOATrR  rd  ZMAPPINGSr  ZMETHODSZCODEOBJECTSZTYPEOBJECTSZFRAMEOBJECTSZ
TRACEBACKSZNONErT  ZSPECIALATTRIBUTESr  r  ZPACKAGESra  rN  Z
PRECEDENCEZOBJECTSZSPECIALMETHODSr  ZATTRIBUTEMETHODSZCALLABLEMETHODSr   ZMAPPINGMETHODSrg  Z	EXECUTIONrc  ZDYNAMICFEATURESZSCOPINGZFRAMESr&  ZCONVERSIONSZIDENTIFIERSZSPECIALIDENTIFIERSrU  ZLITERALSZTUPLESZTUPLELITERALSZLISTSre  r^  rf  Z
ATTRIBUTESZ
SUBSCRIPTSr`  ZCALLSrS  rO  ZBINARYZSHIFTINGrQ  r!  r  Z	ASSERTIONZ
ASSIGNMENTrP  ZDELETIONZ	RETURNINGZ	IMPORTINGZCONDITIONALZLOOPINGr  Z	DEBUGGINGZCONTEXTMANAGERSNc                 C   s   || _ || _d S r   )_input_output)r>   inputr  r   r   r   r   e  s    zHelper.__init__c                 C   s   | j p
tjS r   )rj  r   r  r   r   r   r   rl  i  s    zHelper.inputc                 C   s   | j p
tjS r   )rk  r   r  r   r   r   r   r  m  s    zHelper.outputc                 C   s2   t  d d dkr|   dS d| jj| jjf S )Nr   r1   ?r   z<%s.%s instance>)r   stackr   r&   rQ   r   r   r   r   __repr__q  s    zHelper.__repr__c                 C   s6   || j k	r| | n|   |   | jd d S )Na  
You are now leaving help and returning to the Python interpreter.
If you want to ask for help on a particular object directly from the
interpreter, you can type "help(object)".  Executing "help('string')"
has the same effect as typing a particular string at the help> prompt.
)_GoInteractiver   introinteractr  r  r>   Zrequestr   r   r   __call__y  s
    
zHelper.__call__c              	   C   s   | j d z| d}|s W qW n ttfk
r>   Y qY nX | }t|dkr|d |d   krpdkrn n |d |dd kr|dd }| dkrq|d	kr|   q| 	| qd S )
Nr   zhelp> r   r   r   r7  r   )r  quitr   )
r  r  getliner  EOFErrorr   r    r   rq  r   rs  r   r   r   rr    s"    
 
, 
zHelper.interactc                 C   s8   | j tjkrt |S | j| | j  | j  S dS )z.Read one line, using input() when appropriate.N)rl  r   r  r  r  r  r   )r>   Zpromptr   r   r   rv    s
    
zHelper.getlinec                 C   s<  t |t dkr| }|dkr,|   n|dkr>|   n|dkrP|   n|dkrb|   n|d d dkr| | d  n|| jkr| | nj|d	krt	t
|d
 nR|| jkr| | n<|| jkr| | n&|rt	|d
| jd nt	td
| jd n$t|tr|   nt	|d
| jd | jd d S )Nr   keywordssymbolstopicsr      zmodules r   )r,  r*  r+  zHelp on %s:)r  r   )r   r   listkeywordslistsymbols
listtopicslistmodulesr   ry  
showsymbolr"   evalrx  	showtopicrz  rk  rJ  rk   r  r  r  rs  r   r   r   r     s6     
 
 
 

 
 
   zHelper.helpc                 C   s$   | j ddtjd d   d S )Na  
Welcome to Python {0}'s help utility!

If this is your first time using Python, you should definitely check out
the tutorial on the Internet at https://docs.python.org/{0}/tutorial/.

Enter the name of any module, keyword, or topic to get help on writing
Python programs and using Python modules.  To quit this help utility and
return to the interpreter, just type "quit".

To get a list of available modules, keywords, symbols, or topics, type
"modules", "keywords", "symbols", or "topics".  Each module also comes
with a one-line summary of what it does; to list the modules whose name
or summary contain a given string such as "spam", type "modules spam".
z%d.%dr   )r  r  r  r   r   r   r   r   r   rq    s    
zHelper.intror   P   c           	   
   C   s   t t|}|| }t|| d | }t|D ]v}t|D ]\}|| | }|t|k r<| j||  ||d k r<| jdd|d t||     q<| jd q0d S )Nr   r   r   )r  r  r    r  r  r  )	r>   itemscolumnsr  Zcolwr  rowr  rr   r   r   r   r    s    &zHelper.listc                 C   s    | j d | | j  d S )NzN
Here is a list of the Python keywords.  Enter any keyword to get more help.

)r  r  r  rx  rE   r   r   r   r   r|    s    zHelper.listkeywordsc                 C   s    | j d | | j  d S )Nzx
Here is a list of the punctuation symbols which Python assigns special meaning
to. Enter any symbol to get more help.

)r  r  r  ry  rE   r   r   r   r   r}    s    zHelper.listsymbolsc                 C   s    | j d | | j  d S )NzN
Here is a list of available topics.  Enter any topic name to get more help.

)r  r  r  rz  rE   r   r   r   r   r~    s    zHelper.listtopicsc                 C   s0  zdd l }W n" tk
r.   | jd Y d S X | j|| j|}|sb| jdt|  d S t|tdkr~| 	||S |\}}z|jj| }W n* t
k
r   | jdt|  Y d S X | d }|r|pdd | }|r$dd l}dd|  d }	||	d	}
|d
d|
 7 }t| d S )Nr   t
Sorry, topic and keyword documentation is not available because the
module "pydoc_data.topics" could not be found.
zno documentation found for %s
r   r   r   Related help topics: r4  H   z
%s
)pydoc_data.topicsr   r  r  rz  rv   rx  r   r   r  KeyErrorr   textwrapr!   r   Zwrapr  )r>   topic
more_xrefs
pydoc_datatargetlabelxrefsr"   r  r/   Zwrapped_textr   r   r   r    s4    zHelper.showtopicc                 C   s   zddl }W n tk
r"   Y dS X | j|| j|}|sFtdt|tr\| ||S |\}}|jj| }|r|pzdd | }||fS )a*  Return unbuffered tuple of (topic, xrefs).

        If an error occurs here, the exception is caught and displayed by
        the url handler.

        This function duplicates the showtopic method but returns its
        result directly so it can be formatted for display in an html page.
        r   N)r  r   zcould not find topicr   r   )	r  r   rz  rv   rx  r  rk   rJ  	_gettopic)r>   r  r  r  r  r  r  r"   r   r   r   r    s    	
zHelper._gettopicc                 C   s*   | j | }|d\}}}| || d S )Nr   )ry  	partitionr  )r>   symbolr  r  r_   r  r   r   r   r  (  s    
zHelper.showsymbolc                    sv   |r | j d| t| nR| j d i }|fdd  fdd}t j |d | |  | j d d S )	Nzy
Here is a list of modules whose name or summary contains '{}'.
If there are any, enter a module name to get more help.

zI
Please wait a moment while I gather a list of all available modules...

c                 S   s>   |r$|dd  dkr$|d d d }| ddk r:d||< d S )N	.__init__r  r   r   r   )find)r   r'   r  r   r   r   r   callback;  s    z$Helper.listmodules.<locals>.callbackc                    s    d | d  d S r   r   r  r  r   r   onerror@  s    z#Helper.listmodules.<locals>.onerrorr  z
Enter any module name to get more help.  Or, type "modules spam" to search
for modules whose name or summary contain the string "spam".
)r  r  r  aproposModuleScannerrunr  rE   )r>   rH   r   r  r   r  r   r  -  s    

zHelper.listmodules)r8  r<  r%  r   )NN)r   r  )r   )r   )r   )!r%   r&   rQ   rx  Z_strprefixesZ_symbols_inversery  r  r  Zsymbols_r  rv   rz  r   rl   rl  r  ro  r   rp  rt  rr  rv  r   rq  r  r|  r}  r~  r  r  r  r  r   r   r   r   r    sB  'W


	

 
r  c                   @   s   e Zd ZdZdddZdS )r  z7An interruptible scanner that searches module synopses.Nc              	   C   s
  |r|  }d| _i }tjD ]p}|dkrd||< |d krF|d |d qt|jpRd}|dd }|d | }|  |dkr|d || qtj	|dD ]\\}	}}
| jr q|d kr|d |d qzt
|	|}W n tk
r   Y qY nX |j}t|d	rnz||}W n( tk
r:   |r2|| Y qY nX tt|pNd}t|d
rh||}nd }n`ztj|}W n( tk
r   |r|| Y qY nX |jr|j d nd}t|dd }|d | }|  |dkr|||| q|r|  d S )NF__main__r   r   r   r   r  r  
get_sourceget_filenamerX   )r   ru  r   r   r   rY   r   r  rN  r  	_get_specr   r   rc   r  rc  r   r  StringIOr  r   r   r   r   r   r<   )r>   r  rH   Z	completerr  seenr'   r(   r  rX  rY  r   r   sourcer   r   r   r   r   r  N  s`     



zModuleScanner.run)NNN)r%   r&   rQ   rY   r  r   r   r   r   r  K  s   r  c              	   C   sD   dd }dd }t  " t d t j|| |d W 5 Q R X dS )zAPrint all the one-line module summaries that contain a substring.c                 S   s6   |dd  dkr |d d d }t ||o.d|  d S Nr  r  r  z- )r  r   r'   r  r   r   r   r    s    zapropos.<locals>.callbackc                 S   s   d S r   r   r  r   r   r   r    s    zapropos.<locals>.onerrorignorer  N)warningscatch_warningsfilterwarningsr  r  )rH   r  r  r   r   r   r    s
    

r  c                    s   ddl ddlddlddlG dd djj G fdddjjG  fdddj}|| ||}|  |j	s|j
std	 q~|S )
aA  Start an HTTP server thread on a specific port.

    Start an HTML/text server thread, so HTML or text documents can be
    browsed dynamically and interactively with a Web browser.  Example use:

        >>> import time
        >>> import pydoc

        Define a URL handler.  To determine what the client is asking
        for, check the URL and content_type.

        Then get or generate some text or HTML code and return it.

        >>> def my_url_handler(url, content_type):
        ...     text = 'the URL sent was: (%s, %s)' % (url, content_type)
        ...     return text

        Start server thread on port 0.
        If you use port 0, the server will pick a random port number.
        You can then use serverthread.port to get the port number.

        >>> port = 0
        >>> serverthread = pydoc._start_server(my_url_handler, port)

        Check that the server is really started.  If it is, open browser
        and get first page.  Use serverthread.url as the starting page.

        >>> if serverthread.serving:
        ...    import webbrowser

        The next two lines are commented out so a browser doesn't open if
        doctest is run on this module.

        #...    webbrowser.open(serverthread.url)
        #True

        Let the server do its thing. We just need to monitor its status.
        Use time.sleep so the loop doesn't hog the CPU.

        >>> starttime = time.monotonic()
        >>> timeout = 1                    #seconds

        This is a short timeout for testing purposes.

        >>> while serverthread.serving:
        ...     time.sleep(.01)
        ...     if serverthread.serving and time.monotonic() - starttime > timeout:
        ...          serverthread.stop()
        ...          break

        Print any errors that may have occurred.

        >>> print(serverthread.error)
        None
   r   Nc                   @   s   e Zd Zdd Zdd ZdS )z!_start_server.<locals>.DocHandlerc                 S   sX   | j drd}nd}| d | dd|  |   | j| | j |d dS )	zProcess a request from an HTML browser.

            The URL received is in self.path.
            Get an HTML page from self.urlhandler and send it.
            z.csstext/css	text/html   zContent-Typez%s; charset=UTF-8r  N)	r   rb   Zsend_responseZsend_headerZend_headersZwfiler  
urlhandlerr  )r>   content_typer   r   r   do_GET  s    

 z(_start_server.<locals>.DocHandler.do_GETc                 W   s   d S r   r   )r>   r   r   r   r   log_message  s    z-_start_server.<locals>.DocHandler.log_messageN)r%   r&   rQ   r  r  r   r   r   r   
DocHandler  s   r  c                       s(   e Zd Zdd Z fddZdd ZdS )z _start_server.<locals>.DocServerc                 S   s6   || _ | j |f| _|| _| j| | j| j d| _d S NF)hostZaddressr  rJ   r   handlerru  )r>   r  portr  r   r   r   r     s
    z)_start_server.<locals>.DocServer.__init__c                    s>   | j s2 | j gg g d\}}}|r |   q |   d S r@   )ru  selectZsocketr  Zhandle_requestZserver_close)r>   ZrdZwrZexr  r   r   serve_until_quit  s
    
z1_start_server.<locals>.DocServer.serve_until_quitc                 S   s    | j |  | jr| |  d S r   )rJ   server_activater  r   r   r   r   r    s    z0_start_server.<locals>.DocServer.server_activateN)r%   r&   rQ   r   r  r  r   r  r   r   	DocServer  s   r  c                       s:   e Zd ZfddZ fddZdd Zdd Zd	S )
z#_start_server.<locals>.ServerThreadc                    s2   || _ || _t|| _ j|  d| _d | _d S r  )r  r  r+  r  Threadr   servingerror)r>   r  r  r  )	threadingr   r   r    	  s    
z,_start_server.<locals>.ServerThread.__init__c              
      sx   zJj j_ _jj _t| j _| j	| j
| j}|| _|  W n( tk
rr } z
|| _W 5 d}~X Y nX dS )zStart the server.N)server
HTTPServerrJ   r  r   ZMessageZMessageClassstaticmethodr  r  r  ready	docserverr  rc  r  )r>   Zdocsvre)r  r  emailhttpr   r   r  	  s    

z'_start_server.<locals>.ServerThread.runc                 S   s,   d| _ |j| _|j| _d| j| jf | _d S )NTzhttp://%s:%d/)r  r  Zserver_portr  r"  )r>   r  r   r   r   r  	  s    z)_start_server.<locals>.ServerThread.readyc                 S   s&   d| j _|   d| _ d| _d| _dS )z&Stop the server and this thread nicelyTNF)r  ru  r!   r  r"  r   r   r   r   stop	  s
    z(_start_server.<locals>.ServerThread.stopN)r%   r&   rQ   r   r  r  r  r   )r  r  r  r  r  r   r   ServerThread  s   r  g{Gz?)Zhttp.serverZemail.messager  r  r  ZBaseHTTPRequestHandlerr  r  r1  r  r  timesleep)r  hostnamer  r  threadr   )r  r  r  r  r  r  r   _start_server  s    8'r  r  c              
      s(  G fdddt }|   fdd fdd fdd fd	d
 fdd fdd fdd fdd fdd}| dr| dd } |dkrtjtjt}tj|| }t|}d|	 W  5 Q R  S Q R X n|dkr|| S t
d|| f dS )a  The pydoc url handler for use with the pydoc server.

    If the content_type is 'text/css', the _pydoc.css style
    sheet is read and returned if it exits.

    If the content_type is 'text/html', then the result of
    get_html_page(url) is returned.
    c                       s   e Zd Z fddZdS )z_url_handler.<locals>._HTMLDocc                    s   d}d| }d||  |f S )r  zpydoc_data/_pydoc.cssz1<link rel="stylesheet" type="text/css" href="%s">a  <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html><head><title>Pydoc: %s</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
%s</head><body bgcolor="#f0f0f8">%s<div style="clear:both;padding-top:.5em;">%s</div>
</body></html>r   )r>   r  r  css_pathZcss_linkhtml_navbarr   r   r  9	  s    z#_url_handler.<locals>._HTMLDoc.pageN)r%   r&   rQ   r  r   r  r   r   _HTMLDoc7	  s   r  c                     s>     dt t d t f } d|   tjddf S )Nz%s [%s, %s]r   aZ  
            <div style='float:left'>
                Python %s<br>%s
            </div>
            <div style='float:right'>
                <div style='text-align:center'>
                  <a href="index.html">Module Index</a>
                  : <a href="topics.html">Topics</a>
                  : <a href="keywords.html">Keywords</a>
                </div>
                <div>
                    <form action="get" style='display:inline;'>
                      <input type=text name=key size=15>
                      <input type=submit value="Get">
                    </form>&nbsp;
                    <form action="search" style='display:inline;'>
                      <input type=text name=key size=15>
                      <input type=submit value="Search">
                    </form>
                </div>
            </div>
            T)Zterse)r   r  Zpython_versionZpython_buildZpython_compiler)rU  r  r   r   r  I	  s    
z!_url_handler.<locals>.html_navbarc                     s   dd }   ddd}dd tjD } || }|d d	dd
| g}i }tjD ]}| || qT|d dd|fS )zModule Index page.c                 S   s   d| | f S Nr  r   r(   r   r   r   	bltinlinkg	  s    z3_url_handler.<locals>.html_index.<locals>.bltinlinkz7<big><big><strong>Index of Modules</strong></big></big>rA  rB  c                 S   s   g | ]}|d kr|qS )r  r   )rq   r(   r   r   r   r   m	  s    z4_url_handler.<locals>.html_index.<locals>.<listcomp>z<p>zBuilt-in ModulesrF  z|<p align=right><font color="#909090" face="helvetica,arial"><strong>pydoc</strong> by Ka-Ping Yee&lt;ping@lfw.org&gt;</font>zIndex of Modulesr   )	r  r   r   r  r  r   r   r  r!   )r  r  namesr  r  r   r  r   r   
html_indexd	  s*        
z _url_handler.<locals>.html_indexc           	   	      s   g   fdd}t  * t d dd }t j|| |d W 5 Q R X dd }g }d	d
d} D ]\}}||||  qf|d|  d
dd| }d|fS )zSearch results page.c                    s:   |dd  dkr |d d d }  ||o0d| f d S r  rK   r  Zsearch_resultr   r   r  	  s    z3_url_handler.<locals>.html_search.<locals>.callbackr  c                 S   s   d S r   r   r  r   r   r   r  	  s    z2_url_handler.<locals>.html_search.<locals>.onerrorr  c                 S   s   d| | f S r  r   r  r   r   r   r  	  s    z4_url_handler.<locals>.html_search.<locals>.bltinlinkz5<big><big><strong>Search Results</strong></big></big>rA  rB  zkey = %srF  <br>zSearch Results)	r  r  r  r  r  r  r   r  r!   )	rH   r  r  r  rn   r  r(   r  r  r  r  r   html_search}	  s,    

    z!_url_handler.<locals>.html_searchc                     sL   dd }   ddd}ttj } || }| ddd| }d|fS )zIndex of topic texts available.c                 S   s   d| | f S Nz<a href="topic?key=%s">%s</a>r   r  r   r   r   r  	  s    z4_url_handler.<locals>.html_topics.<locals>.bltinlink,<big><big><strong>INDEX</strong></big></big>rA  rB  ZTopicsrF  )r  r  r  rz  rE   r  r  )r  r  r  r  r  r   r   html_topics	  s        z!_url_handler.<locals>.html_topicsc                     sL     ddd} ttj }dd } ||}|  ddd| }d|fS )zIndex of keywords.r  rA  rB  c                 S   s   d| | f S r  r   r  r   r   r   r  	  s    z6_url_handler.<locals>.html_keywords.<locals>.bltinlinkZKeywordsrF  )r  r  r  rx  rE   r  r  )r  r  r  r  r  r   r   html_keywords	  s        z#_url_handler.<locals>.html_keywordsc                    s   t  }t||}|| \}}| |jkr0d}nd} d| dd}d | } | dd|}|rt|	 }dd	 } 
||} d
dd|}d|| f d|||ffS )zTopic or keyword help page.ZKEYWORDZTOPICr=  rA  rB  z<pre>%s</pre>rF  c                 S   s   d| | f S r  r   r  r   r   r   r  	  s    z7_url_handler.<locals>.html_topicpage.<locals>.bltinlinkr  z%s %sr   )r  r  r  r  rx  r  r3  r  r  r   r  r  r!   )r  ZbufZhtmlhelpr  r  r  r  r  r  r   r   html_topicpage	  s2    

   
z$_url_handler.<locals>.html_topicpagec                    s@   t | dd}|d kr$| dkr$tdt|} || }||fS )Nr   )r   r+  zcould not find object)r  r  r  r   )r"  re   r  contentr  r   r   html_getobj	  s    z!_url_handler.<locals>.html_getobjc                    sP     ddd}d fddtt||D }| | dd| }d|  |fS )	Nz,<big><big><strong>Error</strong></big></big>rA  rB  r  c                 3   s   | ]}  |V  qd S r   )r   r  r  r   r   r  	  s     z3_url_handler.<locals>.html_error.<locals>.<genexpr>z#bb0000z
Error - %s)r  r!   r   r   r  )r"  r   r  r  r  r   r   
html_error	  s     z _url_handler.<locals>.html_errorc              
      sr  | }|  dr| dd } z| dkr2 \}}n| dkrF \}}n| dkrZ \}}nd| kr$| d\}}} |dkr| \}}n|d	krz| \}}W n  tk
r   | \}}Y nX n\|d
kr| dkr \}}n4z| \}}W n" tk
r   | \}}Y nX ntdn| \}}W n2 tk
rd } z||\}}W 5 d}~X Y nX  ||S )zGenerate an HTML page for url.r   N)r   r  rz  rx  r  z
search?keyz	topic?keyzget?keyzbad pydoc url)rb   r  r  rc  r  )r"  Zcomplete_urlr  r  opr_   r   )r  r  r  r  r  r  r  r  r   r   get_html_page	  s>    



 z#_url_handler.<locals>.get_html_pager   r   Nr  r   r  z"unknown content type %r for url %s)r  ra   r	   r   dirnamerealpathrX   r!   r   	readlinesr}   )r"  r  r  r  Z	path_herer  fpr   )	r  r  r  r  r  r  r  r  r  r   _url_handler.	  s*    	
(


"
r  T	localhost)open_browserr  c             	   C   s   ddl }tt|| }|jr(t|j dS |jrd}|rB||j z~zZtd|j t| |jrt	d}|
 }|dkr|qqZ|dkr||j qZt| qZW n ttfk
r   t  Y nX W 5 |jr|  td X dS )	zStart the enhanced pydoc Web server and open a Web browser.

    Use port '0' to start the server on an arbitrary port.
    Set open_browser to False to suppress opening a browser.
    r   Nz"Server commands: [b]rowser, [q]uitzServer stoppedzServer ready atzserver> r  r  )
webbrowserr  r  r  r  r  r   r"  r  rl  r   r  rw  )r  r  r  r  ZserverthreadZserver_help_msgr  r   r   r   browse
  s2    
r  c                 C   s   t | to| tjdkS ru   )rk   rJ  r  r	   sep)rN   r   r   r   ispath@
  s    r  c                 C   sv   d| kst j| kst  | kr"dS t jt}t j|}|  }|| krbt j||sb|| |	dt   |S )zEnsures current directory is on returned path, and argv0 directory is not

    Exception: argv0 dir is left alone if it's also pydoc's directory.

    Returns a new path entry list, or None if no adjustment is needed.
    r   Nr   )
r	   curdirgetcwdr   r  rX   copysamefileremoveinsert)Z
given_pathZargv0Z
stdlib_dirZ
script_dirrevised_pathr   r   r   _get_revised_pathC
  s    
r  c                  C   s,   t tjtjd } | dk	r(| tjdd< dS )zEnsures current directory is on sys.path, and __main__ directory is not.

    Exception: __main__ dir is left alone if it's also pydoc's directory.
    r   N)r  r   r   argv)r  r   r   r   _adjust_cli_sys_path_
  s    r  c                  C   s  ddl } G dd dt}t  zv|  tjdd d\}}d}d}d}d}d}|D ]\\}	}
|	d	krld
}d
}|	dkrt|
  W dS |	dkrd
}|
}|	dkrd
}|	dkrTd
}|
}qT|rt|||d W dS |s||D ]}t|rtj	
|std|   qz`t|r&tj	|r&t|}|rXt|rNtj	|rNt| nt| n
t| W q tk
r } zt| W 5 d}~X Y qX qW nN | j|fk
r   tj	tj	tjd d }tdj|tjd Y nX dS )z@Command-line interface (looks at sys.argv to decide what to do).r   Nc                   @   s   e Zd ZdS )zcli.<locals>.BadUsageN)r%   r&   rQ   r   r   r   r   BadUsagel
  s    r  r   zbk:n:p:wFr  z-bTz-kz-pz-wz-n)r  r  zfile %r does not exista  pydoc - the Python documentation tool

{cmd} <name> ...
    Show text documentation on something.  <name> may be the name of a
    Python keyword, topic, function, module, or package, or a dotted
    reference to a class or function within a module or module in a
    package.  If <name> contains a '{sep}', it is used as the path to a
    Python source file to document. If name is 'keywords', 'topics',
    or 'modules', a listing of these things is displayed.

{cmd} -k <keyword>
    Search for a keyword in the synopsis lines of all available modules.

{cmd} -n <hostname>
    Start an HTTP server with the given hostname (default: localhost).

{cmd} -p <port>
    Start an HTTP server on the given port on the local machine.  Port
    number 0 can be used to get an arbitrary unused port.

{cmd} -b
    Start an HTTP server on an arbitrary unused port and open a Web browser
    to interactively browse documentation.  This option can be used in
    combination with -n and/or -p.

{cmd} -w <name> ...
    Write out the HTML documentation for a module to a file in the current
    directory.  If <name> contains a '{sep}', it is treated as a filename; if
    it names a directory, documentation is written for all the contents.
)r  r  )getoptrc  r  r   r  r  r  r  r	   r   existsr  r   r   r   r
  r  r   r   r  r   r   r  r  )r  r  Zoptsr   ZwritingZstart_serverr  r  r  ZoptvalargrI   r  r   r   r   clii
  sd     

  r  r  )NN)r   )r   )r   r   N)r   r   N)r   )r   N)r  )r   )ZrY   rG  r[   rU   rT   r  Zimportlib._bootstrapr   importlib._bootstrap_externalimportlib.machineryimportlib.utilr   r  r	   rN  r  r   r   r   r  r   Zurllib.parserH  r  collectionsr   reprlibr   	tracebackr   r   r   r$   r)   r.   r0   r5   r'  
IGNORECASEr6   r8   r?   rD   rO   rf   ri   r   r   r   r   rc  r   r   r   r   r   r  r  r  r  r  r  r  r  r  r  r  r  r  r  r/   r  r  r  r  r"   r  r
  r  r   r  r  r  r  r  r  r  r  r  r%   r   r   r   r   <module>   s   *	

'
0;*    >    $,

  
  


   = 
 n%
U
