
    Ki4                        d Z ddlmZmZ ddlZddlmZ ddlmZ er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  ej"                  e      Z ej(                  d
       G d d             Zdedee   dee   dz  dee   dz  def
dZ	 d#dedee   dz  dee   dz  dee   dz  def
dZd$dededee   fdZdee   deded   fdZ G d de      Zd d!deeef   fd"Zy)%z:Sphinx extension for automatic Cyclopts CLI documentation.    )TYPE_CHECKINGAnyN)__version__)
import_app)Sphinx)nodes)logging)SphinxDirectiveT)kw_onlyc                       e Zd ZU dZdZeed<   dZeed<   dZe	e
   dz  ed<   dZe	e
   dz  ed<   d	Zeed
<   d	Zeed<   d	Zeed<   d	Zeed<   d	Zeed<   ededd fd       Zedee
ef   fd       Zy)DirectiveOptionsz)Configuration for the Cyclopts directive.   heading_level   max_heading_levelNcommandsexclude_commandsFno_recursiveinclude_hiddenflatten_commandscode_block_titleskip_preambleoptionsreturnc                    i }t        j                  |       D ]  }|j                  j                  dd      }|j                  t
        u r||v s6d||j                  <   F||v sK||   }|j                  dv r^|rL|j                  d      D cg c]#  }|j                         s|j                         % c}||j                  <   g ||j                  <   |||j                  <     | di |S c c}w )z1Create options from directive options dictionary._-Tr   r   , )attrsfieldsnamereplacetypeboolsplitstrip)clsr   kwargsfieldoption_namevaluecmds          _/home/jay/workspace/scripts/.codegraph-venv/lib/python3.12/site-packages/cyclopts/ext/sphinx.py	from_dictzDirectiveOptions.from_dict%   s     \\#& 	/E**,,S#6KzzT! '))-F5::&',::!AAEJ[[QTEU-ecY\YbYbYdciik-euzz* .0uzz*).F5::&+	/0 }V} .fs   C-!C-c                  z   ddl m}  t        | j                  t        | j
                  t        | j                  i}i }t        j                  t              D ]d  }|j                  j                  dd      }|j                  dv r| j                  }n&|j                  |j                  | j                        }|||<   f |S )z9Generate Sphinx option_spec from DirectiveOptions fields.r   )
directivesr   r   r   )docutils.parsers.rstr2   r&   flagintnonnegative_intstr	unchangedr!   r"   r   r#   r$   getr%   )r2   type_mappingoption_specr+   r,   	validators         r/   speczDirectiveOptions.specC   s     	4 *//++%%
 \\"23 	1E**,,S#6Kzz==&00	(,,UZZ9M9MN	'0K$	1     )__name__
__module____qualname____doc__r   r5   __annotations__r   r   listr7   r   r   r&   r   r   r   r   classmethoddictr0   staticmethodr   r=   r    r>   r/   r   r      s    3M3s!%Hd3i$%)-d3i$&- L$ ND "d""d"M4 );  : $sCx.  r>   r   command_namecommand_pathcommands_filterr   r   c                 B   dj                  || gz         }|r?| |v s||v ryt        t        |            D ]  }dj                  |d|dz          }||v s y |G| |v s||v ryt        t        |            D ]  }dj                  |d|dz          }||v s y |s| |v ryyy)a  Check if a command should be included in documentation.

    Parameters
    ----------
    command_name : str
        The name of the command.
    command_path : list[str]
        The full path to the command (including parent commands).
    commands_filter : list[str] | None
        If specified, only include commands in this list.
    exclude_commands : list[str] | None
        If specified, exclude commands in this list.

    Returns
    -------
    bool
        True if the command should be included.
    .FN   T)joinrangelen)rH   rI   rJ   r   	full_pathiparent_paths          r/   _should_include_commandrT   [   s    2 67I ++y<L/Ls<() 	A((<!a%#89K..	 " ?*i?.Js<() 	A((<!a%#89Ko-	
  ? r>   r   rS   c                 j    |g }i }| j                         D ]  \  }}t        ||||      s|||<    |S )a  Filter commands based on inclusion/exclusion lists.

    Parameters
    ----------
    commands : dict
        Dictionary mapping command names to App instances.
    commands_filter : Optional[List[str]]
        If specified, only include commands in this list.
    exclude_commands : Optional[List[str]]
        If specified, exclude commands in this list.
    parent_path : List[str]
        Path to the parent command for nested commands.

    Returns
    -------
    dict
        Filtered commands dictionary.
    )itemsrT   )r   rJ   r   rS   filteredr#   apps          r/   _filter_commandsrY      sM    0 H^^% !	c"4oGWX HTN! Or>   content
skip_titlec                    | j                         }g }d}|t        |      k  r)||   }|rT|dk(  rO|j                         r?|dz   t        |      k  r.||dz      j                         }|rt        |      h dk  r|dz  }j|j                         j	                  d      r|dz  }|t        |      k  rN||   j                         r;||   d   dv r1|dz  }|t        |      k  r||   j                         r||   d   dv r1|t        |      k  r||   j                         s|dz  }|j                  |       |dz  }|t        |      k  r)|S )z3Process RST content to remove problematic elements.r   rM   >   "r   =^~r   z.. contents::z 	)
splitlinesrP   r(   set
startswithappend)rZ   r[   lines	processedrR   line	next_lines          r/   _process_rst_contentri      sI    EI	A
c%j.Qx !q&TZZ\a!ec%j6Ha!e**,IS^/HHQ ::<""?3FAc%j.U1X^^%5%(1+:NQ c%j.U1X^^%5%(1+:N3u:~eAhnn&6Q	Q) c%j., r>   re   state
nodes.Nodec                    ddl m} g }d}|t        |       k  r| |   }|dz   t        |       k  rF| |dz      j                         }|r-t	        d |D              rt        j                         }|j                         }|j                         j                  dd      j                  dd      g|d	<   |t        j                  |
      z  }g }	|dz  }|t        |       k  rf|dz   t        |       k  r| |dz      j                         nd}
|
rt	        d |
D              rn(|	j                  | |          |dz  }|t        |       k  rf|	r|j                   ||	      d|       |j                  |       l|j                         dk(  r&|dz  }|t        |       k  r| |   j                         s|dz  }g }|t        |       k  rS| |   j                  d      r?|j                  | |   dd        |dz  }|t        |       k  r| |   j                  d      r?|rnt        j                         }dj                  |      |_        |j                  t        j                   dj                  |                   |j                  |       |t        |       k  r| |   j                         s|dz  }|j                         r6|g}	|dz  }|t        |       k  r|dz   t        |       k  r| |dz      j                         nd}|rt	        d |D              rn| |   j                         dk(  rnv| |   j                         s;|	j                  | |          |dz  }|t        |       k\  s| |   j                         sn(|	j                  | |          |dz  }|t        |       k  rt        j"                         }|j                   ||	      d|       |j$                  r!|j'                  |j$                         n|dz  }|t        |       k  r|S )z$Create section nodes from RST lines.r   )
StringListrM   c              3   &   K   | ]	  }|d k(    ywr   Nr    .0cs     r/   	<genexpr>z(_create_section_nodes.<locals>.<genexpr>   s      =ac =    r   z	cyclopts-zcli-cyclopts-idstextr    c              3   &   K   | ]	  }|d k(    ywro   r    rp   s     r/   rs   z(_create_section_nodes.<locals>.<genexpr>   s     1Wq!s(1Wrt   z::z       N
c              3   &   K   | ]	  }|d k(    ywro   r    rp   s     r/   rs   z(_create_section_nodes.<locals>.<genexpr>#  s     $A!Q#X$Art   )docutils.statemachinerm   rP   r(   allr   sectionlowerr$   titlerd   nested_parserc   literal_blockrN   	rawsourceText	paragraphchildrenextend)re   rj   rm   resultrR   rg   rh   r   
title_textcontent_linesnext_line_strippedliteral_contentr   paras                 r/   _create_section_nodesr      s   0F	A
c%j.Qx q53u:a!e**,IS =9 ==--/!ZZ\
","2"2"4"<"<S#"F"N"N{\k"l!m5;;J77 !#Q#e*nABQUASq1u););)=Y[&)c1WDV1W.W!((q2FA #e*n !&&z-'@!WMg& ::<4FA 3u:~eAhnn&6Q !Oc%j.U1X%8%8%@&&uQx|4Q c%j.U1X%8%8%@  % 3 3 5*.))O*D'$$UZZ		/0J%KLm, 3u:~eAhnn&6Q ::<!FMFA c%j.45ECJ4FE!a%L..0B	$Ay$A!A 8>>#t+ Qx~~'!((q2FACJeAhnn.> "((q2FA+ c%j.0 ??$Dz-8!TB}}dmm,FA c%j.B Mr>   c                       e Zd ZdZdZdZdZdZej                         Z
ded   fdZded	edefd
Zded	eded   fdZdeded   fdZy)CycloptsDirectivez;Sphinx directive for documenting Cyclopts CLI applications.FrM   r   r   rk   c                     | j                   d   }t        j                  | j                        }	 | j	                  ||      }| j                  ||      S # t        $ r}| j                  d|       cY d}~S d}~ww xY w)z2Generate documentation nodes for the Cyclopts app.r   z)Error generating Cyclopts documentation: N)	argumentsr   r0   r   _generate_documentation_create_nodes	Exception_error_node)selfmodule_pathoptsrst_contentes        r/   runzCycloptsDirective.runK  s{    nnQ'))$,,7	U66{DIK%%k488 	U##&OPQs$STT	Us   #A 	A;A60A;6A;r   r   c                     ddl m} t        |      } |||j                   |j                  |j
                  |j                  |j                  |j                  |j                  d|j                  |j                        S )z'Generate RST documentation for the app.r   )generate_rst_docsT)
	recursiver   r   r   r   rJ   r   no_root_titler   r   )cyclopts.docs.rstr   r   r   r   r   r   r   r   r   r   r   )r   r   r   r   rX   s        r/   r   z)CycloptsDirective._generate_documentationV  sv    7% !+++..,,"44!22 MM!22!22,,
 	
r>   r   c                 H    t        |d      }t        || j                        S )z'Create docutils nodes from RST content.F)r[   )ri   r   rj   )r   r   r   re   s       r/   r   zCycloptsDirective._create_nodesk  s!    $[UC %UDJJ77r>   messagec                     t         j                  |       t        j                  dt        j                  |            gS )z,Create an error node with the given message.ry   rw   )loggererrorr   r   )r   r   s     r/   r   zCycloptsDirective._error_noder  s+    WBW =>??r>   N)r?   r@   rA   rB   has_contentrequired_argumentsoptional_argumentsfinal_argument_whitespacer   r=   r;   rD   r   r7   r   r   r   r    r>   r/   r   r   B  s    EK %"'')K	UT,' 	U
3 
>N 
SV 
*8 84D 8lI[ 8@3 @4+= @r>   r   rX   r   c                 B    | j                  dt               t        dddS )z(Setup function for the Sphinx extension.cycloptsT)versionparallel_read_safeparallel_write_safe)add_directiver   r   )rX   s    r/   setupr   x  s&    j"34"# r>   )N)F) rB   typingr   r   r!   r   r   cyclopts.utilsr   sphinx.applicationr   docutilsr   sphinx.utilr	   sphinx.util.docutilsr
   	getLoggerr?   r   definer   r7   rD   r&   rT   rF   rY   ri   r   r   r   r    r>   r/   <module>r      sn   @ %    %)  %  0			8	$ dB B BJ77s)7 #Y%7 3i$&	7
 
7| %)	  #Y%  3i$&  cT!	 
 
 F# 4 DI >hc h3 h4;M hV3@ 3@lx DcN r>   