o
    „^So  ã                   @   s2  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zddlmZ W n eyE   ddlmZ Y nw ej ¡ rRddlmZ nddlmZ zddlmZ W n eym   ddlZY nw ejj ddddd	d
ddd¡	ZdZej ej  e!¡d¡Z"da#ejj$ %¡  o ejj$ &¡  o ejj$ '¡  Z(dZ)dde) dddddfZ*edej+fdej,fdej-fdej.fdej/fdej0fdej1fdej2ffƒZ3G d d!„ d!e4ƒZ5d"d#„ Z6G d$d%„ d%e7ƒZ8eƒ d:d'd(„ƒZ9d)d*„ Z:d+d,„ Z;dded-fd.d/„Z<G d0d1„ d1e7ƒZ=d2d3„ Z>d4d5„ Z?d6d7„ Z@d8d9„ ZAdS );a  
Information available about Tor from `its manual
<https://www.torproject.org/docs/tor-manual.html.en>`_. This provides three
methods of getting this information...

* :func:`~stem.manual.Manual.from_cache` provides manual content bundled with
  Stem. This is the fastest and most reliable method but only as up-to-date as
  Stem's release.

* :func:`~stem.manual.Manual.from_man` reads Tor's local man page for
  information about it.

* :func:`~stem.manual.Manual.from_remote` fetches the latest manual information
  remotely. This is the slowest and least reliable method but provides the most
  recent information about Tor.

Manual information includes arguments, signals, and probably most usefully the
torrc configuration options. For example, say we want a little script that told
us what our torrc options do...

.. literalinclude::  /_static/example/manual_config_options.py
   :language: python

|

.. image:: /_static/manual_output.png

|

**Module Overview:**

::

  query - performs a query on our cached sqlite manual information
  is_important - Indicates if a configuration option is of particularly common importance.
  download_man_page - Downloads tor's latest man page.

  Manual - Information about Tor available from its manual.
   | |- from_cache - Provides manual information cached with Stem.
   | |- from_man - Retrieves manual information from its man page.
   | +- from_remote - Retrieves manual information remotely from tor's latest manual.
   |
   +- save - writes the manual contents to a given location

.. versionadded:: 1.5.0
é    N)ÚOrderedDict)Ú	lru_cacheÚGENERALÚCLIENTÚRELAYÚ	DIRECTORYÚ	AUTHORITYÚHIDDEN_SERVICEÚDENIAL_OF_SERVICEÚTESTINGÚUNKNOWNz9https://gitweb.torproject.org/tor.git/plain/doc/tor.1.txtzcached_manual.sqliteé   z$CREATE TABLE schema(version INTEGER)z'INSERT INTO schema(version) VALUES (%i)zdCREATE TABLE metadata(name TEXT, synopsis TEXT, description TEXT, man_commit TEXT, stem_commit TEXT)zACREATE TABLE commandline(name TEXT PRIMARY KEY, description TEXT)z=CREATE TABLE signals(name TEXT PRIMARY KEY, description TEXT)z;CREATE TABLE files(name TEXT PRIMARY KEY, description TEXT)z€CREATE TABLE torrc(key TEXT PRIMARY KEY, name TEXT, category TEXT, usage TEXT, summary TEXT, description TEXT, position INTEGER)zGENERAL OPTIONSzCLIENT OPTIONSzSERVER OPTIONSzDIRECTORY SERVER OPTIONSz"DIRECTORY AUTHORITY SERVER OPTIONSzHIDDEN SERVICE OPTIONSz$DENIAL OF SERVICE MITIGATION OPTIONSzTESTING NETWORK OPTIONSc                       s    e Zd ZdZ‡ fdd„Z‡  ZS )ÚSchemaMismatchz¿
  Database schema doesn't match what Stem supports.

  .. versionadded:: 1.6.0

  :var int database_schema: schema of the database
  :var tuple supported_schemas: schemas library supports
  c                    s    t t| ƒ |¡ || _|| _d S ©N)Úsuperr   Ú__init__Údatabase_schemaÚlibrary_schema)ÚselfÚmessager   r   ©Ú	__class__© ú-/usr/lib/python3/dist-packages/stem/manual.pyr   y   s   
zSchemaMismatch.__init__)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   Ú__classcell__r   r   r   r   r   o   s    	r   c                 G   s8   t j ¡ s	tdƒ‚ddl}tdu r| t¡at | |¡S )a,  
  Performs the given query on our sqlite manual cache. This database should
  be treated as being read-only. File permissions generally enforce this, and
  in the future will be enforced by this function as well.

  ::

    >>> import stem.manual
    >>> print(stem.manual.query('SELECT description FROM torrc WHERE key=?', 'CONTROLSOCKET').fetchone()[0])
    Like ControlPort, but listens on a Unix domain socket, rather than a TCP socket.  0 disables ControlSocket. (Unix and Unix-like systems only.) (Default: 0)

  .. versionadded:: 1.6.0

  :param str query: query to run on the cache
  :param list param: query parameters

  :returns: :class:`sqlite3.Cursor` with the query results

  :raises:
    * **ImportError** if the sqlite3 module is unavailable
    * **sqlite3.OperationalError** if query fails
  z$Querying requires the sqlite3 moduler   N)	ÚstemÚprereqÚis_sqlite_availableÚImportErrorÚsqlite3ÚDATABASEÚconnectÚ
CACHE_PATHÚexecute)ÚqueryZparamr#   r   r   r   r(      s   

r(   c                   @   s<   e Zd ZdZejdddfdd„Zdd„ Zdd„ Zd	d
„ Z	dS )ÚConfigOptiona¬  
  Tor configuration attribute found in its torrc.

  :var str name: name of the configuration option
  :var stem.manual.Category category: category the config option was listed
    under, this is Category.UNKNOWN if we didn't recognize the category
  :var str usage: arguments accepted by the option
  :var str summary: brief description of what the option does
  :var str description: longer manual description with details
  Ú c                 C   s"   || _ || _|| _|| _|| _d S r   ©ÚnameÚcategoryÚusageÚsummaryÚdescription)r   r,   r-   r.   r/   r0   r   r   r   r   ¹   s
   
zConfigOption.__init__c              	   C   s   t jj| dddddddS )Nr,   r-   r.   r/   r0   T©Úcache©r   ÚutilZ
_hash_attr©r   r   r   r   Ú__hash__À   s   zConfigOption.__hash__c                 C   ó   t |tƒrt| ƒt|ƒkS dS ©NF)Ú
isinstancer)   Úhash©r   Úotherr   r   r   Ú__eq__Ã   ó   zConfigOption.__eq__c                 C   ó
   | |k S r   r   r;   r   r   r   Ú__ne__Æ   ó   
zConfigOption.__ne__N)
r   r   r   r   ÚCategoryr   r   r6   r=   r@   r   r   r   r   r)   ­   s    r)   Tc              
      s°   t jj ¡ ‰ tj tj t¡d¡}z'ˆ  	|¡ t
‡ ‡fdd„ˆ  ¡ D ƒƒ}‡fdd„ˆ jdg ddD ƒ|d< |W S  tyW } zt jj d||f ¡ i W  Y d	}~S d	}~ww )
aM  
  Provides a dictionary for our settings.cfg. This has a couple categories...

    * manual.important (list) - configuration options considered to be important
    * manual.summary.* (str) - summary descriptions of config options

  :param bool lowercase: uses lowercase keys if **True** to allow for case
    insensitive lookups
  zsettings.cfgc                    s0   g | ]}|  d ¡rˆr| ¡ n|ˆ  |¡f‘qS )zmanual.summary.)Ú
startswithÚlowerÚ	get_value)Ú.0Úkey©ZconfigÚ	lowercaser   r   Ú
<listcomp>Û   s   0 z_config.<locals>.<listcomp>c                    s   g | ]
}ˆ r
|  ¡ n|‘qS r   )rD   )rF   r,   )rI   r   r   rJ   Ü   s    úmanual.importantT)ZmultiplezFBUG: stem failed to load its internal manual information from '%s': %sN)r   r4   ÚconfÚConfigÚosÚpathÚjoinÚdirnameÚ__file__ÚloadÚdictÚkeysrE   Ú	ExceptionÚlogÚwarn)rI   Zconfig_pathZconfig_dictÚexcr   rH   r   Ú_configÊ   s   
"€þrZ   c                 C   sv  g }dD ]±}t | |ƒ}t ||ƒ}||krµ| d| ¡ |dv r.| d| ¡ | d| ¡ n‚|dkr‚| ¡ D ]5\}}| |¡}|du rK| d| ¡ q6||krkd	D ]}t ||ƒt ||ƒkrj| d
||t ||ƒf ¡ qQq6t| ¡ ƒ | ¡ ¡D ]	}| d| ¡ qwn.t| ¡ ƒ | ¡ ¡}	t| ¡ ƒ | ¡ ¡}
|	D ]	}| d| ¡ qš|
D ]	}| d| ¡ q¦| d¡ qd |¡S )z7
  Provides a description of how two manuals differ.
  )r,   Úsynopsisr0   Úcommandline_optionsÚsignalsÚfilesÚconfig_optionsz * Manual's %s attribute changed
)r,   r[   r0   z  Previously...

%s
z  Updating to...

%sr_   Nz   adding new config option => %sr+   z  modified %s (%s) => %sz  removing config option => %sz  adding %s => %sz  removing %s => %sÚ
)ÚgetattrÚappendÚitemsÚgetÚsetrU   Ú
differencerP   )Zprevious_manualZ
new_manualÚlinesÚattrZprevious_attrZnew_attrZconfig_nameZconfig_attrZpreviousZadded_itemsZremoved_itemsZ
added_itemZremoved_itemr   r   r   Ú_manual_differencesã   s>   


€ÿ
€
ri   c                 C   s   |   ¡ tƒ d v S )zì
  Indicates if a configuration option of particularly common importance or not.

  :param str option: tor configuration option to check

  :returns: **bool** that's **True** if this is an important option and
    **False** otherwise
  rK   )rD   rZ   )Úoptionr   r   r   Úis_important  s   
rk   é   c              
   C   s  | s|st dƒ‚tjj d¡stdƒ‚t ¡ }tj	 
|d¡}tj	 
|d¡}zÕz$t|dƒ}tj||d}t ||¡ W d  ƒ n1 sDw   Y  W n   t ¡ d	d
… \}	}
d|||	f }t ||	|
|¡‚ztjj d| ¡ tj	 |¡s{tdƒ‚W n tjjjy• }	 z
td|	j|	jf ƒ‚d}	~	ww | rÂztj	 | ¡}tj	 |¡sªt |¡ t || ¡ W n tyÁ }	 zt|	ƒ‚d}	~	ww |rët|dƒ}t ||¡ | ¡  W d  ƒ n1 sÞw   Y  W t |¡ dS W t |¡ dS W t |¡ dS t |¡ w )a#  
  Downloads tor's latest man page from `gitweb.torproject.org
  <https://gitweb.torproject.org/tor.git/plain/doc/tor.1.txt>`_. This method is
  both slow and unreliable - please see the warnings on
  :func:`~stem.manual.Manual.from_remote`.

  :param str path: path to save tor's man page to
  :param file file_handle: file handler to save tor's man page to
  :param str url: url to download tor's asciidoc manual from
  :param int timeout: seconds to wait before timing out the request

  :raises: **IOError** if unable to retrieve the manual
  z?Either the path or file_handle we're saving to must be providedZa2xz2We require a2x from asciidoc to provide a man pagez	tor.1.txtztor.1Úwb)ÚtimeoutNr   é   z1Unable to download tor's manual from %s to %s: %sza2x -f manpage %szno man page was generatedúUnable to run '%s': %sÚrb)Ú
ValueErrorr   r4   ÚsystemZis_availableÚIOErrorÚtempfileZmkdtemprN   rO   rP   ÚopenÚurllibZurlopenÚshutilÚcopyfileobjÚsysÚexc_infoZDownloadFailedÚcallÚexistsÚOSErrorZ	CallErrorZcommandÚstderrrQ   ÚmakedirsÚcopyfileÚflushÚrmtree)rO   Úfile_handleZurlrn   ÚdirpathZasciidoc_pathZmanual_pathZasciidoc_fileÚrequestrY   Z
stacktracer   Zpath_dirZmanual_filer   r   r   Údownload_man_page  s^   þ€ÿ€ÿ
€ÿ
þûür‡   c                   @   sŠ   e Zd ZdZdd„ Zeddd„ƒZedd„ ƒZed	d
„ ƒZeddd„ƒZ	eddd„ƒZ
dd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ ZdS ) ÚManualaµ  
  Parsed tor man page. Tor makes no guarantees about its man page format so
  this may not always be compatible. If not you can use the cached manual
  information stored with Stem.

  This does not include every bit of information from the tor manual. For
  instance, I've excluded the 'THE CONFIGURATION FILE FORMAT' section. If
  there's a part you'd find useful then `file an issue
  <https://trac.torproject.org/projects/tor/wiki/doc/stem/bugs>`_ and we can
  add it.

  :var str name: brief description of the tor command
  :var str synopsis: brief tor command usage
  :var str description: general description of what tor does

  :var collections.OrderedDict commandline_options: mapping of commandline arguments to their descripton
  :var collections.OrderedDict signals: mapping of signals tor accepts to their description
  :var collections.OrderedDict files: mapping of file paths to their description

  :var collections.OrderedDict config_options: :class:`~stem.manual.ConfigOption` tuples for tor configuration options

  :var str man_commit: latest tor commit editing the man page when this
    information was cached
  :var str stem_commit: stem commit to cache this manual information
  c                 C   sP   || _ || _|| _t|ƒ| _t|ƒ| _t|ƒ| _t|ƒ| _d | _d | _	d | _
d S r   )r,   r[   r0   r   r\   r]   r^   r_   Ú
man_commitÚstem_commitÚschema)r   r,   r[   r0   r\   r]   r^   r_   r   r   r   r   u  s   




zManual.__init__Nc                 C   s2   | du rt } | dur|  d¡rt | ¡S t | ¡S )ax  
    Provides manual information cached with Stem. Unlike
    :func:`~stem.manual.Manual.from_man` and
    :func:`~stem.manual.Manual.from_remote` this doesn't have any system
    requirements, and is faster too. Only drawback is that this manual
    content is only as up to date as the Stem release we're using.

    .. versionchanged:: 1.6.0
       Added support for sqlite cache. Support for
       :class:`~stem.util.conf.Config` caches will be dropped in Stem 2.x.

    :param str path: cached manual content to read, if not provided this uses
      the bundled manual information

    :returns: :class:`~stem.manual.Manual` with our bundled manual information

    :raises:
      * **ImportError** if cache is sqlite and the sqlite3 module is
        unavailable
      * **IOError** if a **path** was provided and we were unable to read
        it or the schema is out of date
    Nú.sqlite)r&   Úendswithrˆ   Ú_from_sqlite_cacheÚ_from_config_cache)rO   r   r   r   Ú
from_cache  s
   

zManual.from_cachec                 C   sv  t j ¡ s	tdƒ‚dd l}tj | ¡std|  ƒ‚| 	| ¡“}z'| 
d¡ ¡ d }|tkr9tdt| |f |tfƒ‚| 
d¡ ¡ \}}}}}W n |jy[ }	 ztd| |	f ƒ‚d }	~	ww t| 
d¡ ¡ ƒ}
t| 
d	¡ ¡ ƒ}t| 
d
¡ ¡ ƒ}tƒ }| 
d¡ ¡ D ]}|\}}}}}t|||||ƒ||< qt||||
|||ƒ}||_||_||_|W  d   ƒ S 1 s´w   Y  d S )Nz2Reading a sqlite cache requires the sqlite3 moduler   z%s doesn't existzSELECT version FROM schemazAStem's current manual schema version is %s, but %s was version %szISELECT name, synopsis, description, man_commit, stem_commit FROM metadataz,Failed to read database metadata from %s: %sz)SELECT name, description FROM commandlinez%SELECT name, description FROM signalsz#SELECT name, description FROM fileszOSELECT name, category, usage, summary, description FROM torrc ORDER BY position)r   r    r!   r"   r#   rN   rO   r}   rt   r%   r'   ZfetchoneÚSCHEMA_VERSIONr   ZOperationalErrorrT   Zfetchallr   r)   rˆ   r‰   rŠ   r‹   )rO   r#   Úconnr‹   r,   r[   r0   r‰   rŠ   rY   Zcommandliner]   r^   r_   Úentryrj   r-   r.   r/   Zoption_descriptionÚmanualr   r   r   rŽ   ¤  s8   
€ÿ$æzManual._from_sqlite_cachec              
   C   s  t jj ¡ }|j| dd tƒ }| ¡ D ]:}| d¡rN| d¡d }||vrNt	| 
d| d¡| 
d| d¡| 
d	| d¡| 
d
| d¡| 
d| d¡ƒ||< qt| 
dd¡| 
dd¡| 
dd¡| 
dtƒ ¡| 
dtƒ ¡| 
dtƒ ¡|ƒ}| 
dd ¡|_| 
dd ¡|_|S )NF)Z
commentingzconfig_options.Ú.r   úconfig_options.%s.namer*   úconfig_options.%s.categoryúconfig_options.%s.usageúconfig_options.%s.summaryúconfig_options.%s.descriptionr,   r[   r0   r\   r]   r^   r‰   rŠ   )r   r4   rL   rM   rS   r   rU   rC   Úsplitr)   rd   rˆ   r‰   rŠ   )rO   rL   r_   rG   r”   r   r   r   r   Ê  s6   
û€


ù
zManual._from_config_cacheÚtorc           	      C   s$  dt rdnd| f }ztjjj|ddid}W n ty+ } ztd||f ƒ‚d}~ww t|ƒtƒ }}t	 
¡ D ]\}}t||| |g ¡ƒ q8|D ]}| d	¡rc|t	vrc|d
vrct|tj| |g ¡ƒ qItt| dg ¡ƒt| dg ¡ƒt| dg ¡ƒt| dg ¡ƒt| dg ¡ƒt| dg ¡ƒ|ƒS )aí  
    Reads and parses a given man page.

    On OSX the man command doesn't have an '--encoding' argument so its results
    may not quite match other platforms. For instance, it normalizes long
    dashes into '--'.

    :param str man_path: path argument for 'man', for example you might want
      '/path/to/tor/doc/tor.1' to read from tor's git repository

    :returns: :class:`~stem.manual.Manual` for the system's man page

    :raises: **IOError** if unable to retrieve the manual
    zman %s -P cat %sz--encoding=asciir*   ZMANWIDTHZ10000000)Úenvrp   Nz OPTIONS)úCOMMAND-LINE OPTIONSzNON-PERSISTENT OPTIONSÚNAMEZSYNOPSISZDESCRIPTIONrž   ZSIGNALSZFILES)ÚHAS_ENCODING_ARGr   r4   rs   r|   r~   rt   Ú_get_categoriesr   ÚCATEGORY_SECTIONSrc   Ú_add_config_optionsrd   r   rB   r   rˆ   Ú_join_linesÚ_get_indented_descriptions)	Zman_pathZman_cmdZ
man_outputrY   Ú
categoriesr_   Zcategory_headerZcategory_enumr-   r   r   r   Úfrom_maní  s.   €ÿ€ùzManual.from_mané<   c                 C   sD   t  ¡ }t|| d t |j¡W  d  ƒ S 1 sw   Y  dS )a³  
    Reads and parses the latest tor man page `from gitweb.torproject.org
    <https://gitweb.torproject.org/tor.git/plain/doc/tor.1.txt>`_. Note that
    while convenient, this reliance on GitWeb means you should alway call with
    a fallback, such as...

    ::

      try:
        manual = stem.manual.from_remote()
      except IOError:
        manual = stem.manual.from_cache()

    In addition to our GitWeb dependency this requires 'a2x' which is part of
    `asciidoc <http://asciidoc.org/INSTALL.html>`_ and... isn't quick.
    Personally this takes ~7.41s, breaking down for me as follows...

      * 1.67s to download tor.1.txt
      * 5.57s to convert the asciidoc to a man page
      * 0.17s for stem to read and parse the manual

    :param int timeout: seconds to wait before timing out the request

    :returns: latest :class:`~stem.manual.Manual` available for tor

    :raises: **IOError** if unable to retrieve the manual
    )r„   rn   N)ru   ZNamedTemporaryFiler‡   rˆ   r§   r,   )rn   Útmpr   r   r   Úfrom_remote  s   

$þzManual.from_remotec                 C   s   |  d¡r
|  |¡S |  |¡S )aœ  
    Persists the manual content to a given location.

    .. versionchanged:: 1.6.0
       Added support for sqlite cache. Support for
       :class:`~stem.util.conf.Config` caches will be dropped in Stem 2.x.

    :param str path: path to save our manual content to

    :raises:
      * **ImportError** if saving as sqlite and the sqlite3 module is
        unavailable
      * **IOError** if unsuccessful
    rŒ   )r   Ú_save_as_sqliteÚ_save_as_config)r   rO   r   r   r   Úsave:  s   


zManual.savec           	      C   sj  t j ¡ s	tdƒ‚dd l}|d }tj |¡rt |¡ | 	|¡y}t
D ]}| |¡ q$| d| j| j| j| j| jf¡ | j ¡ D ]\}}| d||f¡ qA| j ¡ D ]\}}| d||f¡ qS| j ¡ D ]\}}| d||f¡ qet| j ¡ ƒD ]\}}| d|j ¡ |j|j|j|j|j|f¡ qyW d   ƒ n1 sw   Y  tj |¡r­t |¡ t ||¡ d S )	Nz1Saving a sqlite cache requires the sqlite3 moduler   z.newz]INSERT INTO metadata(name, synopsis, description, man_commit, stem_commit) VALUES (?,?,?,?,?)z7INSERT INTO commandline(name, description) VALUES (?,?)z3INSERT INTO signals(name, description) VALUES (?,?)z1INSERT INTO files(name, description) VALUES (?,?)zdINSERT INTO torrc(key, name, category, usage, summary, description, position) VALUES (?,?,?,?,?,?,?))r   r    r!   r"   r#   rN   rO   r}   Úremover%   ÚSCHEMAr'   r,   r[   r0   r‰   rŠ   r\   rc   r]   r^   Ú	enumerater_   ÚvaluesÚupperr-   r.   r/   Úrename)	r   rO   r#   Ztmp_pathr’   ÚcmdÚkÚvÚir   r   r   r«   Q  s.   

 ,ÿñ
zManual._save_as_sqlitec                 C   s^  t jj ¡ }| d| j¡ | d| j¡ | d| j¡ | jr%| d| j¡ | j	r/| d| j	¡ | j
 ¡ D ]\}}|jdd||f dd	 q4| j ¡ D ]\}}|jd
d||f dd	 qJ| j ¡ D ]\}}|jdd||f dd	 q`| j ¡ D ]1\}}| d| |j¡ | d| |j¡ | d| |j¡ | d| |j¡ | d| |j¡ qv| |¡ d S )Nr,   r[   r0   r‰   rŠ   r\   z%s => %sF)Z	overwriter]   r^   r—   r–   r˜   r™   rš   )r   r4   rL   rM   re   r,   r[   r0   r‰   rŠ   r\   rc   r]   r^   r_   r-   r.   r/   r­   )r   rO   rL   rµ   r¶   r   r   r   r¬   r  s*   zManual._save_as_configc                 C   s   t jj| ddddddddd		S )
Nr,   r[   r0   r\   r]   r^   r_   Tr1   r3   r5   r   r   r   r6     r>   zManual.__hash__c                 C   r7   r8   )r9   rˆ   r:   r;   r   r   r   r=   “  r>   zManual.__eq__c                 C   r?   r   r   r;   r   r   r   r@   –  rA   zManual.__ne__r   )rœ   )r¨   )r   r   r   r   r   Ústaticmethodr   rŽ   r   r§   rª   r­   r«   r¬   r6   r=   r@   r   r   r   r   rˆ   Z  s&    "
%
"*!!rˆ   c                 C   s  | rd| d v r| dd… } | r| d   d¡r| dd… } tƒ }dg }}| D ]R}tj ¡ r0tnt}| |dƒd¡ |d	ƒd
¡ |dƒd¡}|ri|  d¡si|ra|r]|d dkr]|dd… }|||< | ¡ g }}q'|  d¡rt|dd… }| 	|¡ q'|r€|||< |S )z
  The man page is headers followed by an indented section. First pass gets
  the mapping of category titles to their lines.
  zTOR(1)r   r   NéÿÿÿÿZTori   ú'i   ú-é·   Ú*ú r*   z       é   )
rC   r   r   r    Zis_python_3ÚchrZunichrÚreplaceÚstriprb   )Zcontentr¦   r-   rg   ÚlineZchar_forr   r   r   r¡   š  s*   
(
r¡   c                 C   s|   t ƒ d}}| D ]*}|dkrd}q|r | d¡s g |||< }q|r2| d¡r2||  |dd… ¡ qtdd„ | ¡ D ƒƒS )a÷  
  Parses the commandline argument and signal sections. These are options
  followed by an indented description. For example...

  ::

    -f FILE
        Specify a new configuration file to contain further Tor configuration
        options OR pass - to make Tor read its configuration from standard
        input. (Default: /usr/local/etc/tor/torrc, or $HOME/.torrc if that file
        is not found)

  There can be additional paragraphs not related to any particular argument but
  ignoring those.
  Nz    Noter¾   ú    é   c                 S   s"   g | ]\}}|r|d   |¡f‘qS )r¾   )rP   )rF   ÚargZ
desc_linesr   r   r   rJ   ä  s   " z._get_indented_descriptions.<locals>.<listcomp>)r   rC   rb   rT   rc   )rg   ÚoptionsZlast_argrÃ   r   r   r   r¥   É  s   €r¥   c                    sº   ‡ ‡‡fdd„‰ dd„ t |ƒD ƒ}|r$|t|ƒd… }|| d¡d… }dg }}|D ]&}|rA| d¡sA|r;ˆ ||ƒ |g }}q+| d¡rL|d	d… }| |¡ q+|r[ˆ ||ƒ dS dS )
ai  
  Parses a section of tor configuration options. These have usage information,
  followed by an indented description. For instance...

  ::

    ConnLimit NUM
        The minimum number of file descriptors that must be available to the
        Tor process before it will start. Tor will ask the OS for as many file
        descriptors as the OS will allow (you can find this by "ulimit -H -n").
        If this number is less than ConnLimit, then Tor will refuse to start.


        You probably don't need to adjust this. It has no effect on Windows
        since that platform lacks getrlimit(). (Default: 1000)
  c                    sˆ   d| v rd S d| v r|   d¡D ]}ˆ ||ƒ qd S d| v r#|   dd¡n| df\}}tƒ  d| ¡  d¡}t|ˆ||t|ƒ ¡ ƒˆ|< d S )NzPER INSTANCE OPTIONSz, r¾   r   r*   zmanual.summary.%s)r›   rZ   rd   rD   r)   r¤   rÂ   )Útitler0   Zsubtitler,   r.   r/   ©Ú
add_optionr-   r_   r   r   rÊ   ù  s   ÿ  z'_add_config_options.<locals>.add_optionc                 S   s$   g | ]\}}d |v sd|v r|‘qS )zThe following optionszPER SERVICE OPTIONSr   )rF   r·   rÃ   r   r   r   rJ     s   $ z'_add_config_options.<locals>.<listcomp>Nr*   r¾   rÄ   rÅ   )r°   ÚmaxÚindexrC   rb   )r_   r-   rg   Zend_indicesZ
last_titler0   rÃ   r   rÉ   r   r£   ç  s"   


ÿr£   c                 C   sJ   g }| D ]}|s|r|d dkr|  d¡ q|  |d ¡ qd |¡ ¡ S )zI
  Simple join, except we want empty lines to still provide a newline.
  r¹   r`   r*   )rb   rP   rÂ   )rg   ÚresultrÃ   r   r   r   r¤   %  s   
€r¤   )T)Br   rN   rx   rz   ru   r   Zstem.prereqZ	stem.utilZstem.util.confZstem.util.enumZstem.util.logZstem.util.systemÚcollectionsr   r"   Zstem.util.ordereddictr    Z_is_lru_cache_availableÚ	functoolsr   Zstem.util.lru_cacheZurllib.requestr†   rw   Zurllib2r4   ÚenumÚEnumrB   ZGITWEB_MANUAL_URLrO   rP   rQ   rR   r&   r$   rs   Zis_macZis_bsdZis_slackwarer    r‘   r¯   r   r   r   r   r   r	   r
   r   r¢   rt   r   r(   Úobjectr)   rZ   ri   rk   r‡   rˆ   r¡   r¥   r£   r¤   r   r   r   r   Ú<module>   s€   /ÿ
ÿ*øø.-=  B/>