o
    i;                     @   s  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 z!ddlmZ ddlm	Z	 ddlmZ ddl
mZ eeZd	ZW n ey^   d
Zes\eZdZY nw deddfddZejd	dG dd dZdedee dee dB dee dB def
ddZ	d,dedee dB dee dB dee dB def
ddZd-dededee fdd Zd!ee d"eded# fd$d%Z G d&d' d'eZ!d(d)deeef fd*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           	   
   C   s,  ddl m} d| v r| dd\}}n| d}}zt|}W n ty6 } ztd| d| |d}~ww |r]t||sItd| d	| d
t||}t	||s[t
d
| d|S dD ]}t||d}t	||rp|  S q_t|D ]}|dst||}t	||r|  S qut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obj r&   Y/var/www/html/karishye-ai-python/venv/lib/python3.10/site-packages/cyclopts/sphinx_ext.py_import_app   s:   







r(   )kw_onlyc                   @   s   e Zd ZU dZdZeed< dZee	 dB ed< dZ
ee	 dB ed< dZeed< dZeed	< dZeed
< ededd fddZedee	ef fddZdS )DirectiveOptionsz)Configuration for the Cyclopts directive.   heading_levelNcommandsexclude_commandsFno_recursiveinclude_hiddenflatten_commandsoptionsr   c                 C   s   i }t | D ]@}|jdd}|jtu r||v rd||j< q||v rG|| }|jdv rB|r<dd |dD ||j< qg ||j< q|||j< q| d	i |S )
z1Create options from directive options dictionary.r   -Tr-   r.   c                 S   s   g | ]
}|  r|  qS r&   )strip).0cmdr&   r&   r'   
<listcomp>e   s    z.DirectiveOptions.from_dict.<locals>.<listcomp>,Nr&   )attrsfieldsr$   replacetypeboolsplit)clsr2   kwargsfieldoption_namevaluer&   r&   r'   	from_dictQ   s    



zDirectiveOptions.from_dictc                  C   sz   t si S ddlm}  t| jt| jt| ji}i }t	
tD ]}|jdd}|jdv r.| j}n||j| j}|||< q|S )z9Generate Sphinx option_spec from DirectiveOptions fields.r   )
directivesr   r3   r4   )SPHINX_AVAILABLEdocutils.parsers.rstrF   r>   flagintnonnegative_intstr	unchangedr:   r;   r*   r$   r<   getr=   )rF   type_mappingoption_specrB   rC   	validatorr&   r&   r'   speco   s   

zDirectiveOptions.spec)__name__
__module____qualname____doc__r,   rJ   __annotations__r-   listrL   r.   r/   r>   r0   r1   classmethoddictrE   staticmethodr   rR   r&   r&   r&   r'   r*   D   s   
 r*   command_namecommand_pathcommands_filterr.   c                 C   s   d || g }|r/| |v s||v rdS tt|D ]}d |d|d  }||v r. dS q|durb| |v s;||v r=dS tt|D ]}d |d|d  }||v rW dS qC|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)r\   r]   r^   r.   	full_pathiparent_pathr&   r&   r'   _should_include_command   s*   rf   r-   re   c                 C   s<   |du rg }i }|   D ]\}}t||||r|||< q|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.
    N)itemsrf   )r-   r^   r.   re   filteredr$   r   r&   r&   r'   _filter_commands   s   ri   content
skip_titlec                 C   s(  |   }g }d}|t|k r|| }|r;|dkr;| r;|d t|k r;||d   }|r;t|h dkr;|d7 }q| dr|d7 }|t|k rr||  rr|| d dv rr|d7 }|t|k rr||  rr|| d dv sZ|t|k r||  s|d7 }q|| |d7 }|t|k s|S )z3Process RST content to remove problematic elements.r   r   >   "r3   =^~r+   z.. contents::z 	)
splitlinesrb   r5   setr   append)rj   rk   lines	processedrd   line	next_liner&   r&   r'   _process_rst_content   s,   $((
rw   rs   state
nodes.Nodec                 C   sb  t sg S ddlm} ddlm} g }d}|t| k r| | }|d t| k r| |d   }|rtdd |D r| }| }	|		 
dd
d	d
g|d< ||j|	d7 }g }
|d7 }|t| k r|d t| k ru| |d   nd}|rtdd |D rn|
| |  |d7 }|t| k se|
r|||
d| || q| dkr|d7 }|t| k r| |  s|d7 }g }|t| k r| | dr|| | dd  |d7 }|t| k r| | ds|r| }d||_||d| || |t| k r| |  s|d7 }q| r|g}
|d7 }|t| k r|d t| k r?| |d   nd}|rOtdd |D rOn>| |  dkrYn4| |  s{|
| |  |d7 }|t| ksy| |  sznn|
| |  |d7 }|t| k s.| }|||
d| |jr||j n|d7 }|t| k s|S )z$Create section nodes from RST lines.r   r   )
StringListr   c                 s       | ]}|d kV  qdS r3   Nr&   r6   cr&   r&   r'   	<genexpr>      z(_create_section_nodes.<locals>.<genexpr> r3   z	cyclopts-zcli-cyclopts-idstextr+    c                 s   r{   r|   r&   r}   r&   r&   r'   r   %  r   z::z       N
c                 s   r{   r|   r&   r}   r&   r&   r'   r   V  r   )rG   docutilsr   docutils.statemachinerz   rb   r5   allsectionlowerr<   titlerr   nested_parser   literal_blockr`   	rawsourceText	paragraphchildrenextend)rs   rx   r   rz   resultrd   ru   rv   r   
title_textcontent_linesnext_line_strippedliteral_contentr   parar&   r&   r'   _create_section_nodes  s   $


&ar   c                   @   s   e Zd ZdZdZdZdZdZe	 Z
ded fddZd	ed
edefddZded
eded fddZdeded fddZdS )CycloptsDirectivez;Sphinx directive for documenting Cyclopts CLI applications.Fr   r   r   ry   c              
   C   s   t sg S | jd }t| j}z| ||}| ||W S  ttt	fy9 } z| 
d| W  Y d}~S d}~w tyR } z| 
d| W  Y d}~S d}~ww )z2Generate documentation nodes for the Cyclopts app.r   z)Error generating Cyclopts documentation: NzUnexpected error: )rG   	argumentsr*   rE   r2   _generate_documentation_create_nodesr   r   r   _error_node	Exception)selfr   optsrst_contentr#   r&   r&   r'   run~  s   
zCycloptsDirective.runr   r   c              
   C   s:   ddl m} t|}|||j |j|j|j|j|jddS )z'Generate RST documentation for the app.r   )generate_rst_docsT)	recursiver0   r,   r1   r^   r.   no_root_title)	cyclopts.docs.rstr   r(   r/   r0   r,   r1   r-   r.   )r   r   r   r   r   r&   r&   r'   r     s   z)CycloptsDirective._generate_documentationr   c                 C   s    t sg S t|dd}t|| jS )z'Create docutils nodes from RST content.F)rk   )rG   rw   r   rx   )r   r   r   rs   r&   r&   r'   r     s   zCycloptsDirective._create_nodesmessagec                 C   s8   t sg S ddlm} trt| |d|j|dgS )z,Create an error node with the given message.r   r   r   r   )rG   r   r   loggererrorr   )r   r   r   r&   r&   r'   r     s   
zCycloptsDirective._error_nodeN)rS   rT   rU   rV   has_contentrequired_argumentsoptional_argumentsfinal_argument_whitespacer*   rR   rP   rX   r   rL   r   r   r   r&   r&   r&   r'   r   u  s    
r   r   r   c                 C   s   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)rG   add_directiver   )r   r&   r&   r'   setup  s   r   )N)F)#rV   r   typingr   r   r:   r   r   sphinx.applicationr   sphinx.util.docutilsr   r   r	   sphinx.utilr
   	getLoggerrS   r   rG   r   objectrL   r(   definer*   rX   r>   rf   rZ   ri   rw   r   r   r   r&   r&   r&   r'   <module>   sj    

&E


>



#oA