o
    NiZ                     @  s  d dl m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lmZ d dlmZmZ d dlmZ d d	lmZ d d
lmZmZ d dlmZ d dlmZ d dlmZmZ dd Zd$ddZ d%ddZ!			d&ddZ"d'ddZ#dd Z$G d d! d!eZ%d"d# Z&dS )(    )annotationsN)ArgumentParser)nodes)get_default_settings)Parser)flag	unchanged)
StringList)mock)SphinxDirectivenew_document)nested_parse_with_titles)__version__)parse_parserparser_navigatec           
      C  s*  | d u r
d}t |i }| D ]}t|tjsq|D ]x}t|tjs"qt|jdks*qd}|tj}|d urH|| }t|jdkrH|jd 	 }|d urY|dvrYd| }t ||tj
}|d ur|| }t|jdkr|jd 	 }|tj}|d urdd || D }	||| |	f||< qq|S )Nz+Nested content should be iterable, not nullr   @after>   @skipr   @before@replacezUnknown classifier: c                 S  s   g | ]
}t |tjr|qS  
isinstancer   definition_list).0_r   r   A/home/ubuntu/.local/lib/python3.10/site-packages/sphinxarg/ext.py
<listcomp>9   
    z*map_nested_definitions.<locals>.<listcomp>)	Exceptionr   r   r   definition_list_itemlenchildrenfirst_child_matching_class
classifierastextterm
definition)
nested_contentmsgdefinitionsitemsubitemr#   idxcir%   
subcontentr   r   r   map_nested_definitions   sF   
r/   c                 C  s   t | dkrg S |rddlm} |d| d S |du r!tt}g }| D ]%}t|tr@td|}t 	|d | ||j
7 }q%t|tjrJ||7 }q%|S )z\
    Given a list of reStructuredText or MarkDown sections, return a docutils node list
    r   parse_markdown_blockz


N )r    sphinxarg.markdownr1   joinr   r   r   strr   parser!   r   r&   )lmarkdown_helpsettingsr1   all_childrenelementdocumentr   r   r   render_listA   s"   

r>   r*   
str | Nonereturnboolc                 C  s,   | du rdS t | dddd} | dkS )z*Return whether item should not be printed.NT"r3   'z==SUPPRESS==)r6   replace)r*   r   r   r   _is_suppressedZ   s   rE   Fr3   c                 C  s  t |}g }d| v rg| d D ]V}|d dd }|r%| d| }tj|gd}	|	t|d |d 7 }	g }
|d rE|
|d  g }|d |v r||d  \}}}|dkr^|g}
n|dkrh|
| n|d	krs|
d
| n|dkrxqt|d
krt |	 D ]\}}|||< qt
|
|D ]}|	|7 }	q|}t|d
krt|	 }t |	 D ]\}}|||< qg }|d D ]}g }d|v r|dddd |d D  d d|v r||d  t|d st|d dd}|d| d |}
d|d }||v r/|| \}}}|dkr|g}
n|dkr$|
| n|d	kr/|
d
| d|d }tdtdtj|dtjdgt
|
||R  }|| q|	tjdg|R  7 }	||	 q|S )z
    Process all 'action groups', which are also include 'Options' and 'Required
    arguments'. A list of nodes is returned.
    action_groupstitle -idsdescriptionr   r   r   r   r   optionschoicesPossible choices: , c                 s  s    | ]}t |V  qd S N)r6   )r   cr   r   r   	<genexpr>   s    z&print_action_groups.<locals>.<genexpr>r2   helpdefault`z\`zDefault: ``z``namer3   text)r/   rD   lowerr   sectionrG   appendinsertr    itemsr>   dictr5   rE   r6   option_list_itemoption_groupoption_stringrL   option_list)datar'   r9   r:   	id_prefixr)   
nodes_listaction_grouptitle_as_idr[   descr.   r#   skvr<   local_definitionsr^   entryargdefault_strr%   nr   r   r   print_action_groupsb   s   







rr   c                 C  s  t |}g }d| v rtjdgd}|tdd7 }| d D ]}tj|d gd}|t|d |d 7 }d|v rB|d rB|d g}	n|d rL|d g}	ndg}	g }
|d |v r|||d  \}}}
|dkrh|g}	n|d	krr|	| n
|d
kr||	d| t|	|D ]}||7 }q|tj|d d7 }t|||
 ||dD ]}||7 }qt	|||
 ||dD ]}||7 }qd|v r|d rt|d g|D ]}||7 }q||7 }q|| |S )a  
    Each subcommand is a dictionary with the following keys:

    ['usage', 'action_groups', 'bare_usage', 'name', 'help']

    In essence, this is all tossed in a new section with the title 'name'.
    Apparently there can also be a 'description' entry.
    r!   zSub-commandsrJ   rW   rL   rT   Undocumentedr   r   r   r   
bare_usagerX   r:   epilog)
r/   r   r[   rG   r\   r]   r>   literal_blockrr   print_subcommands)rd   r'   r9   r:   r)   r^   subcommandschildsecri   r.   r#   rj   r<   xr   r   r   rx      sN   








rx   c                 C  s   t  }| D ]V}|jddddD ]K}t|tjrZ|d }t|D ]6\}}||vr-|| qd}| d| |v rE|d7 }| d| |v s8| d| ||< |||  q||d< qqdS )ah  
    If action groups are repeated, then links in the table of contents will
    just go to the first of the repeats. This may not be desirable, particularly
    in the case of subcommands where the option groups have different members.
    This function updates the title IDs by adding _repeatX, where X is a number
    so that the links are then unique.
    TF)descendsiblingsascendrK      _repeatN)setfindallr   r   r[   	enumerateadd)r^   rj   r*   rq   rK   r,   idir   r   r   ensure_unique_ids  s$   r   c                   @  sl   e Zd ZdZeeeeeeeeeeeeeeedZdd Zdd Zdd Z	d	d
 Z
dd Zdd Zdd ZdS )ArgParseDirectiveT)modulefuncrefprogpath	nodefaultnodefaultconstfilenamemanpagenosubcommands
passparsernoepilognodescriptionmarkdownmarkdownhelpc           
      C  s"  g }t jdt jddt j|d ddgd}|| d| jvrIt jdt jddt j|d	|d
d ddgd}t	| j
| j| || |drmd| jvrm|r`|t j|d d7 }nt j|d d}|| t jdt jdddgd}d|v r|t  7 }|t jdd7 }|| |7 }|d D ]}d|v r|t  7 }|t j|d d7 }|| |7 }qt|jdkr|| d| jvrt jdt jdddgd}d|v r|| |7 }t|dkr|| tdrddl}t jdt jddt j|j|dd dd!gd}	||	 |S )"a  
        Construct a typical man page consisting of the following elements:
            NAME (automatically generated, out of our control)
            SYNOPSIS
            DESCRIPTION
            OPTIONS
            FILES
            SEE ALSO
            BUGS
        r3   SynopsisrX   rt   zsynopsis-sectionrJ   r   DescriptionrL   rT   undocumentedzdescription-sectionrv   r   Optionszoptions-sectionargszPositional arguments:rF   rM   rG   r   r   zSub-Commandszsubcommands-sectionr!   INCLUDE_DEBUG_SECTIONr   NzArgparse + Sphinx Debuggingz  )indentzdebug-section)r   r[   rG   rw   r\   rM   	paragraphget
capitalizer   statecontentsubtitle_format_positional_arguments_format_optional_argumentsr    r!   _format_subcommandsosgetenvjsondumps)
selfparser_infor^   synopsis_sectiondescription_sectionoptions_sectionrg   subcommands_sectionr   debug_sectionr   r   r   %_construct_manpage_specific_structure0  sx   










z7ArgParseDirective._construct_manpage_specific_structurec                 C  s   d|v sJ g }|d D ]T}g }|d r | tj|d d nd|vr-| tjdd d|v rA| tjdd|d  d | tdtdtdtj|d	 dtjdg|R   qtj	dg|R  S )
Nr   rT   rX   rN   rs   rO   rP   r3   metavar)
r\   r   r   r5   r`   ra   optionrb   rL   rc   )r   r   r^   ro   	arg_itemsr   r   r   r     s,   	z.ArgParseDirective._format_positional_argumentsc              
   C  s(  d|v sJ g }|d D ]~}g }g }|d D ]*}t j|dg}t|d s4|t jddt|d  d7 }|t jdg|R   q|d rQ|t j|d d nd|vr^|t jd	d d|v rr|t jd
d|d  d |t 	dt j
dg|R  t jdg|R   qt jdg|R  S )NrM   rW   rX   rU   r3   =rT   rN   rs   rO   rP   )r   rb   rE   option_argumentr6   r\   r   r   r5   r`   ra   rL   rc   )r   r   r^   optnames	opt_itemsrW   option_declarationr   r   r   r     s8   z,ArgParseDirective._format_optional_argumentsc                 C  s   d|v sJ g }|d D ]9}g }|d r | tj|d d n	| tjdd | tdtddtj|d dtjdg|R   qtjdg|R  S )Nr!   rT   rX   rs   r3   rt   )r\   r   r   r   r%   strongr&   r   )r   r   r^   subcmdsubcmd_itemsr   r   r   r     s   z%ArgParseDirective._format_subcommandsc                 C  s&   t  }| jt|dd| |S )Nr2   r   )r   r   r   nested_parser	   split)r   rY   r   r   r   r   _nested_parse_paragraph  s   z)ArgParseDirective._nested_parse_paragraphc              	   C  s   zt | jd W S  ty   Y nw zt tj| jd W S  ty'   Y nw zt t| jd W S  ttfy>   Y nw t	| jd )Nr   )
openrM   OSErrorr   r   abspathshutilwhich	TypeErrorFileNotFoundError)r   r   r   r   _open_filename  s    z ArgParseDirective._open_filenamec                 C  s  d| j v rd| j v r| j d }| j d }nNd| j v r0| j d d}d|dd }|d }n3d| j v r\d| j v r\i }|  }t| | j d d}t|| d }| j d }|| }nd	}| |d| j vrt| j	j
I zt|}W n! ty }	 zd
| d| dt d  }| ||	d }	~	ww t||sd| d| d}| |t||}W d    n1 sw   Y  t|tr|}
nd| j v rt }
||
 n| }
d| j vrd| j d< t| j d }d| j v r| j d |
_t|
d| j v d| j v d}t||}d| j v r| |S g }t }d| j v r0ddlm} ||d| jd  n| j | j| j!| |j"}|dd |D 7 }d}d| j v rPd }d!|v rtd"| j vrt|rj|t#|d! gd  n
|$| %|d!  |$tj&|d# d$ |t'|||| jj(j)|r| d%| n|d& d'| j vr|t*|||| jj(j)d( d)|v rd*| j vr|$| %|d)  t+| |S )+Nr   r   r   .r   r   execzK:module: and :func: should be specified, or :ref:, or :filename: and :func:zFailed to import "z" from "z".
r   zModule "z" has no attribute "z/"
Incorrect argparse :module: or :func: values?r   r   r3   r   r   r   )skip_default_valuesskip_default_const_valuesr   r   r0   r2   c                 S  s   g | ]
}t |tjs|qS r   r   )r   r*   r   r   r   r   1  r   z)ArgParseDirective.run.<locals>.<listcomp>Fr   TrL   r   usagerX   rI   )r:   re   r   ru   rv   r   ),rM   r   r5   r   compilereadr   errorr
   configautodoc_mock_imports	importlibimport_moduleImportErrorsysexc_infohasattrgetattrr   r   r6   r   r   r   r   r   r   r4   r1   extendr   r   r   content_offsetr!   r>   r\   r   rw   rr   r=   r:   rx   r   )r   module_name	attr_name_partsmodfcoder   r(   excparserr   resultr^   r'   r1   r9   r   r   r   run  s   


















	zArgParseDirective.runN)__name__
__module____qualname__has_contentr   r   option_specr   r   r   r   r   r   r   r   r   r   r   r     s2    Yr   c                 C  s"   |  d | dt tdddS )Nzsphinx.ext.autodocargparseT)versionparallel_read_safeparallel_write_safe)setup_extensionadd_directiver   r   )appr   r   r   setupY  s   
r   rQ   )r*   r?   r@   rA   )FNr3   )FN)'
__future__r   r   r   r   r   r   r   docutilsr   docutils.frontendr   docutils.parsers.rstr   docutils.parsers.rst.directivesr   r   docutils.statemachiner	   sphinx.ext.autodocr
   sphinx.util.docutilsr   r   sphinx.util.nodesr   	sphinxargr   sphinxarg.parserr   r   r/   r>   rE   rr   rx   r   r   r   r   r   r   r   <module>   s:    
+


d=  ?