
    !<i;                     `   d Z ddlZddlmZmZ ddl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 ddlm	Z	 ddlmZ ddl
mZ  ej        e          Zd	Zn# e$ r d
ZeseZdZY nw xY wdeddfd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"dS )(z:Sphinx extension for automatic Cyclopts CLI documentation.    N)TYPE_CHECKINGAnynodes)Sphinx)SphinxDirectiveApp)loggingTFmodule_pathreturnr
   c                    ddl m} d| v r|                     dd          \  }}n| d}}	 t          j        |          }n(# t
          $ r}t          d| d|           |d}~ww xY w|r[t          ||          st          d| d	| d
          t          ||          }t          ||          st          d
| d          |S dD ]'}t          ||d          }t          ||          r|c S (t          |          D ];}|                    d          s$t          ||          }t          ||          r|c S <t          d| d| d          )z)Import a Cyclopts App from a module path.r   r	   :   NzCannot import module 'z': zModule 'z' has no attribute ''z ' is not a Cyclopts App instance)appclimain_zNo Cyclopts App found in 'z'. Specify explicitly: 'z
:app_name')cycloptsr
   rsplit	importlibimport_moduleImportErrorhasattrAttributeErrorgetattr
isinstance	TypeErrordir
startswith)	r   r
   module_nameapp_namemoduleer   nameobjs	            /Users/kimhansen/Desktop/03 Workspace/ceo-agents/chl-effectiveness/mcp-servers/whoop/.venv/lib/python3.11/site-packages/cyclopts/sphinx_ext.py_import_appr)      s   
k + 2 23 : :XX +TXO(55 O O OF;FF1FFGGQNO  vx(( 	Z !XK!X!XX!X!X!XYYYfh''#s## 	LJJJJKKK
 '  fdD))c3 	JJJ	 F  s## 	&$''C#s## 



rkrr[frrr
s
sss   ? 
A$	AA$)kw_onlyc                       e Zd ZU dZ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
<   ededd fd            Zedee	ef         fd            ZdS )DirectiveOptionsz)Configuration for the Cyclopts directive.   heading_levelNcommandsexclude_commandsFno_recursiveinclude_hiddenflatten_commandsoptionsr   c                 \   i }t          j        |           D ]}|j                            dd          }|j        t
          u r||v r
d||j        <   :||v rP||         }|j        dv r5|r(d |                    d          D             ||j        <   yg ||j        <   |||j        <    | di |S )z1Create options from directive options dictionary.r   -Tr/   r0   c                 ^    g | ]*}|                                 |                                 +S  )strip).0cmds     r(   
<listcomp>z.DirectiveOptions.from_dict.<locals>.<listcomp>e   s2    -e-e-ecY\YbYbYdYd-eciikk-e-e-e    ,r9   )attrsfieldsr&   replacetypeboolsplit)clsr4   kwargsfieldoption_namevalues         r(   	from_dictzDirectiveOptions.from_dictQ   s     \#&& 	/ 	/E*,,S#66KzT!! ')))-F5:&'',:!AAA 0-e-eU[[QTEUEU-e-e-euz** .0uz**).F5:& s}}V}}r>   c                  N   t           si S ddlm}  t          | j        t
          | j        t          | j        i}i }t          j
        t                    D ]S}|j                            dd          }|j        dv r| j        }n |                    |j        | j                  }|||<   T|S )z9Generate Sphinx option_spec from DirectiveOptions fields.r   )
directivesr   r6   r7   )SPHINX_AVAILABLEdocutils.parsers.rstrM   rD   flagintnonnegative_intstr	unchangedr@   rA   r,   r&   rB   getrC   )rM   type_mappingoption_specrH   rI   	validators         r(   speczDirectiveOptions.speco   s       	I333333 */+%
 \"233 	1 	1E*,,S#66Kz===&0		(,,UZ9MNN	'0K$$r>   )__name__
__module____qualname____doc__r.   rQ   __annotations__r/   listrS   r0   r1   rD   r2   r3   classmethoddictrK   staticmethodr   rY   r9   r>   r(   r,   r,   D   s         33M3!%Hd3i$%%%)-d3i$&--- L$ ND   "d""" );    [: $sCx.    \  r>   r,   command_namecommand_pathcommands_filterr0   c                    d                     || gz             }|rP| |v s||v rdS t          t          |                    D ])}d                     |d|dz                      }||v r dS *|Z| |v s||v rdS t          t          |                    D ])}d                     |d|dz                      }||v r dS *|s| |v rdS dS dS )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.
    .FNr   T)joinrangelen)rc   rd   re   r0   	full_pathiparent_paths          r(   _should_include_commandrn      s4   2 677I  +++y<L/L/L5s<(()) 	 	A((<!a%#899K...uu / " ?**i?.J.J4s<(()) 	 	A((<!a%#899Ko--tt .  	 ? ?4u 4r>   r/   rm   c                 t    |g }i }|                                  D ]\  }}t          ||||          r|||<   |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.
    )itemsrn   )r/   re   r0   rm   filteredr&   r   s          r(   _filter_commandsrr      sW    0 H^^%% ! !	c"4oGWXX 	! HTNOr>   content
skip_titlec                 `   |                                  }g }d}|t          |          k     r||         }|rj|dk    rd|                                rP|dz   t          |          k     r:||dz                                            }|rt          |          h dk    r|dz  }|                                                    d          r|dz  }|t          |          k     rl||                                         rR||         d         dv rB|dz  }|t          |          k     r*||                                         r||         d         dv B|t          |          k     r||                                         s|dz  }g|                    |           |dz  }|t          |          k     |S )z3Process RST content to remove problematic elements.r   r   >   "r6   =^~r-   z.. contents::z 	)
splitlinesrj   r:   setr!   append)rs   rt   lines	processedrl   line	next_lines          r(   _process_rst_contentr      s     EI	A
c%jj..Qx  	!q&&TZZ\\&a!ec%jj6H6Ha!e**,,I S^^/H/H/HHHQ ::<<""?33 	FAc%jj..U1X^^%5%5.%(1+:N:NQ c%jj..U1X^^%5%5.%(1+:N:N3u::~~eAhnn&6&6~Q	Q) c%jj.., r>   r}   state
nodes.Nodec                 	   t           sg S ddlm} ddlm} g }d}|t          |           k     r| |         }|dz   t          |           k     r| |dz                                            }|rht          d |D                       rN |j                    }|                                }	|		                                
                    dd          
                    dd	          g|d
<   | |j        |	          z  }g }
|dz  }|t          |           k     r|dz   t          |           k     r| |dz                                            nd}|rt          d |D                       rn3|
                    | |                    |dz  }|t          |           k     |
r |                     ||
          d|           |                    |           |                                dk    rg|dz  }|t          |           k     r| |                                         s|dz  }g }|t          |           k     rq| |                             d          rV|                    | |         dd                    |dz  }|t          |           k     r| |                             d          V|rt |j                    }d                    |          |_        |                     |j        d                    |                               |                    |           |t          |           k     r| |                                         s|dz  };|                                rz|g}
|dz  }|t          |           k     r|dz   t          |           k     r| |dz                                            nd}|rt          d |D                       rn| |                                         dk    rn| |                                         sO|
                    | |                    |dz  }|t          |           k    s| |                                         sn5n |
                    | |                    |dz  }|t          |           k      |j                    }|                     ||
          d|           |j        r|                    |j                   n|dz  }|t          |           k     |S )z$Create section nodes from RST lines.r   r   )
StringListr   c              3   "   K   | ]
}|d k    V  dS r6   Nr9   r;   cs     r(   	<genexpr>z(_create_section_nodes.<locals>.<genexpr>  s&       = =ac = = = = = =r>    r6   z	cyclopts-zcli-cyclopts-idstextr-    c              3   "   K   | ]
}|d k    V  dS r   r9   r   s     r(   r   z(_create_section_nodes.<locals>.<genexpr>%  s&      1W1Wq!s(1W1W1W1W1W1Wr>   z::z       N
c              3   "   K   | ]
}|d k    V  dS r   r9   r   s     r(   r   z(_create_section_nodes.<locals>.<genexpr>V  s&      $A$A!Q#X$A$A$A$A$A$Ar>   )rN   docutilsr   docutils.statemachiner   rj   r:   allsectionlowerrB   titler|   nested_parser!   literal_blockrh   	rawsourceText	paragraphchildrenextend)r}   r   r   r   resultrl   r   r   r   
title_textcontent_linesnext_line_strippedliteral_contentr   paras                  r(   _create_section_nodesr     s     	000000F	A
c%jj..Qx q53u::a!e**,,I S = =9 = = === '%-//!ZZ\\
","2"2"4"4"<"<S#"F"F"N"N{\k"l"l!m;5;J7777 !#Q#e**nnABQUASASq1u););)=)=)=Y[&) c1W1WDV1W1W1W.W.W !((q222FA #e**nn ! N&&zz-'@'@!WMMMg&&& ::<<4FA 3u::~~eAhnn&6&6~Q !Oc%jj..U1X%8%8%@%@.&&uQx|444Q c%jj..U1X%8%8%@%@.  - 3 3 5 5*.))O*D*D'$$ZUZ		/0J0J%K%KLLLm,,, 3u::~~eAhnn&6&6~Q ::<< "	!FMFA c%jj..45ECJJ4F4FE!a%L..000B	 $A$Ay$A$A$A!A!A  8>>##t++ Qx~~'' 
!((q222FACJJeAhnn.>.> ' "((q222FA+ c%jj..0 #5?$$Dzz-88!TBBB} -dm,,,FA c%jj..B Mr>   c                       e Zd ZdZdZdZdZdZe	                                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dS )CycloptsDirectivez;Sphinx directive for documenting Cyclopts CLI applications.Fr   r   r   r   c                    t           sg S | j        d         }t                              | j                  }	 |                     ||          }|                     ||          S # t          t          t          f$ r"}| 
                    d|           cY d}~S d}~wt          $ r"}| 
                    d|           cY d}~S d}~ww xY w)z2Generate documentation nodes for the Cyclopts app.r   z)Error generating Cyclopts documentation: NzUnexpected error: )rN   	argumentsr,   rK   r4   _generate_documentation_create_nodesr   r   r   _error_node	Exception)selfr   optsrst_contentr%   s        r(   runzCycloptsDirective.run~  s     	InQ'))$,77	>66{DIIK%%k4888^Y7 	U 	U 	U##$SPQ$S$STTTTTTTT 	> 	> 	>##$<$<$<========	>s/   +A# #C:BCC$C;CCr   r   c           
          ddl m} t          |          } |||j         |j        |j        |j        |j        |j        d          S )z'Generate RST documentation for the app.r   )generate_rst_docsT)	recursiver2   r.   r3   re   r0   no_root_title)	cyclopts.docs.rstr   r)   r1   r2   r.   r3   r/   r0   )r   r   r   r   r   s        r(   r   z)CycloptsDirective._generate_documentation  si    777777+&& ! ++.,!2 M!2	
 	
 	
 		
r>   r   c                 `    t           sg S t          |d          }t          || j                  S )z'Create docutils nodes from RST content.F)rt   )rN   r   r   r   )r   r   r   r}   s       r(   r   zCycloptsDirective._create_nodes  s5     	I$[UCCC %UDJ777r>   messagec                     t           sg S ddlm} t          rt                              |            |j        d |j        |                    gS )z,Create an error node with the given message.r   r   r   r   )rN   r   r   loggererrorr   )r   r   r   s      r(   r   zCycloptsDirective._error_node  sa     	I"""""" 	"LL!!!BW = = =>>??r>   N)rZ   r[   r\   r]   has_contentrequired_argumentsoptional_argumentsfinal_argument_whitespacer,   rY   rW   r_   r   rS   r   r   r   r9   r>   r(   r   r   u  s        EEK %"''))K>T,' > > > > 
3 
>N 
SV 
 
 
 
$8 84D 8lI[ 8 8 8 8	@3 	@4+= 	@ 	@ 	@ 	@ 	@ 	@r>   r   r   r   c                 R    t           r|                     dt                     ddddS )z(Setup function for the Sphinx extension.r   z1.0.0T)versionparallel_read_safeparallel_write_safe)rN   add_directiver   )r   s    r(   setupr     s9     9*&7888"#  r>   )N)F)#r]   r   typingr   r   r@   r   r   sphinx.applicationr   sphinx.util.docutilsr   r   r
   sphinx.utilr   	getLoggerrZ   r   rN   r   objectrS   r)   definer,   r_   rD   rn   ra   rr   r   r   r   r   r9   r>   r(   <module>r      s/   @ @     % % % % % % % %  ))))))444444))))))######444444Wx((F     	#tS #tU #t #t #t #tL dB B B B B B B BJ77s)7 #Y%7 3i$&	7
 
7 7 7 7| %)	    #Y%  3i$&  cT!	 
 
       F # 4 DI    >lc l3 l4;M l l l l^>@ >@ >@ >@ >@ >@ >@ >@Bx DcN      s   *A A)(A)