
    lKj4                        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
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_commands
usage_nameF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          d/Users/ahmed/devFolder/Ultron/claude-voice/.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-eEUcY\YbYbYdciikEU-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.rstr3   r'   flagintnonnegative_intstr	unchangedr"   r#   r   r$   r%   getr&   )r3   type_mappingoption_specr,   r-   	validators         r0   speczDirectiveOptions.specD   s     	4 *//++%%
 \\"23E**,,S#6Kzz==&00	(,,UZZ9M9MN	'0K$ 4     )__name__
__module____qualname____doc__r   r6   __annotations__r   r   listr8   r   r   r   r'   r   r   r   r   classmethoddictr1   staticmethodr   r>   r!   r?   r0   r   r      s    3M3s!%Hd3i$%)-d3i$&-!Jd
! 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)rI   rJ   rK   r   	full_pathiparent_paths          r0   _should_include_commandrU   \   s    2 67I ++y<L/Ls<()A((<!a%#89K.. * " ?*i?.Js<()A((<!a%#89Ko- *
  ? r?   r   rT   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.
    )itemsrU   )r   rK   r   rT   filteredr$   apps          r0   _filter_commandsrZ      sH    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   rN   >   "r   =^~r   z.. contents::z 	)
splitlinesrQ   r)   set
startswithappend)r[   r\   lines	processedrS   line	next_lines          r0   _process_rst_contentrj      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?   rf   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   )
StringListrN   c              3   &   K   | ]	  }|d k(    ywr   Nr!   .0cs     r0   	<genexpr>z(_create_section_nodes.<locals>.<genexpr>   s      =9ac9    r   z	cyclopts-zcli-cyclopts-idstextr    c              3   &   K   | ]	  }|d k(    ywrp   r!   rq   s     r0   rt   z(_create_section_nodes.<locals>.<genexpr>   s     1WDVq!s(DVru   z::z       N
c              3   &   K   | ]	  }|d k(    ywrp   r!   rq   s     r0   rt   z(_create_section_nodes.<locals>.<genexpr>$  s     $Ay!Q#Xyru   )docutils.statemachinern   rQ   r)   allr   sectionlowerr%   titlere   nested_parserd   literal_blockrO   	rawsourceText	paragraphchildrenextend)rf   rk   rn   resultrS   rh   ri   r   
title_textcontent_linesnext_line_strippedliteral_contentr   paras                 r0   _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.FrN   r   r   rl   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   r1   r   _generate_documentation_create_nodes	Exception_error_node)selfmodule_pathoptsrst_contentes        r0   runzCycloptsDirective.runL  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                  |j                        S )z'Generate RST documentation for the app.r   )generate_rst_docsT)	recursiver   r   r   r   rK   r   no_root_titler   r   r   )cyclopts.docs.rstr   r   r   r   r   r   r   r   r   r   r   r   )r   r   r   r   rY   s        r0   r   z)CycloptsDirective._generate_documentationW  s}    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\   )rj   r   rk   )r   r   r   rf   s       r0   r   zCycloptsDirective._create_nodesm  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.rz   rx   )loggererrorr   r   )r   r   s     r0   r   zCycloptsDirective._error_nodet  s+    WBW =>??r?   N)r@   rA   rB   rC   has_contentrequired_argumentsoptional_argumentsfinal_argument_whitespacer   r>   r<   rE   r   r8   r   r   r   r!   r?   r0   r   r   C  s    EK %"'')K	UT,' 	U
3 
>N 
SV 
,8 84D 8lI[ 8@3 @4+= @r?   r   rY   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   )rY   s    r0   setupr   z  s&    j"34"# r?   )N)F) rC   typingr   r   r"   r   r   cyclopts.utilsr   sphinx.applicationr   docutilsr   sphinx.utilr	   sphinx.util.docutilsr
   	getLoggerr@   r   definer   r8   rE   r'   rU   rG   rZ   rj   r   r   r   r!   r?   r0   <module>r      sn   @ %    %)  %  0			8	$ dC C CL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4@ 4@nx DcN r?   