o
    ^i9                     @  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Zd dlZd dlm	Z	 d dl
mZmZ d dlmZmZmZmZmZmZ d dlmZmZmZ ddlmZmZmZ z
d d	lmZmZ W n eyn   e dZG d
d dZY nw dZ!e"e#ej$fZ%eG dd dZ&eG dd dZ'eG dd dZ(dS )    )annotationsN)Callable)	dataclassfield)AnyOptionalUnionget_args
get_originget_type_hints)	DocstringDocstringParamparse   )Colorformat_textget_underline)NoneType	UnionTypec                   @  s   e Zd ZdS )r   N)__name__
__module____qualname__ r   r   R/var/www/html/stock_analysis/be/venv/lib/python3.10/site-packages/targ/__init__.pyr      s    r   z0.6.0c                   @  s2   e Zd ZU eedZded< eedZded< dS )	Argumentsdefault_factory	list[str]argszdict[str, Any]kwargsN)	r   r   r   r   listr   __annotations__dictr   r   r   r   r   r   #   s   
 r   c                   @  s   e Zd ZU dZded< dZded< dZded< eedZ	d	ed
< d&ddZ
edd Zed'ddZd(ddZd)ddZd*ddZed'ddZed'ddZd d! Zd+d$d%ZdS ),Commanda  
    Represents a CLI command.

    :param command:
        The Pyton function or coroutine which gets called.
    :param group_name:
        Commands can belong to a group. In this situation, the group name must
        appear before the command name when called from the command line. For
        example `python my_file.py group_name command_name`.
    :param command_name:
        By default the command name is the same as the callable, but it can
        be overriden here.
    :param aliases:
        You can provide aliases, which can be abbreviations or common
        mispellings. For example, for a `command_name` of ``run``, we could
        have aliases like ``['start', 'rn']``.

    r   commandNOptional[str]
group_namecommand_namer   r   aliasesreturnNonec                 C  sJ   t | jjpd| _t| j| _t| j| _d| _| j	s#| jj
| _	d S d S )N F)r   r$   __doc__command_docstringr   r   inspect	signaturesolor'   r   selfr   r   r   __post_init__C   s   zCommand.__post_init__c                 C  s   | j r| j  d| j S | jS )N )r&   r'   r1   r   r   r   	full_nameK   s
   zCommand.full_namestrc                 C  s*   t | jjpd}d|jpd|jpdgS )Nr+   r4   )r   r$   r,   joinshort_descriptionlong_description)r2   	docstringr   r   r   descriptionS   s   zCommand.descriptionOptional[DocstringParam]c                 C  s$   | j jD ]}|j|kr|  S qd S N)r-   paramsarg_name)r2   r?   paramr   r   r   _get_docstring_param]   s
   
zCommand._get_docstring_paramr?   c                 C  s   |  |}|r|jpdS dS )Nr+   )rA   r;   )r2   r?   docstring_paramr   r   r   _get_arg_descriptionc   s   

zCommand._get_arg_descriptionc                 C  s*   | j j|}|r|j}|tjur|S d S r=   )r/   
parametersgetdefaultr.   _empty)r2   r?   	parameterrF   r   r   r   _get_arg_defaultj   s   
zCommand._get_arg_defaultc                 C  s   g }| j  D ]<\}}| j|d}| j|d}t|}|dur&d| dnd}|t|tj	dd|   |r>|| |d qd
|S )	zP
        :returns: A string containing a description for each argument.
        )r?   Nz	[default=]r+   colorr4   
)r   itemsrC   rI   jsondumpsappendr   r   cyanr7   )r2   outputr?   _arg_descriptionarg_defaultarg_default_jsondefault_strr   r   r   arguments_descriptionr   s    


zCommand.arguments_descriptionc                 C  s   | j rg }n| jp
d}t|tjdg}| jj D ]7\}}|jt	j
u r.|t|tjd q|jdu rB|td| dtjd q|td| dtjd qd|S )zb
        Example:

        some_command required_arg [--optional_arg=value] [--some_flag]
        r+   rK   Fz[--rJ   z=X]r4   )r0   r'   r   r   greenr/   rD   rN   rF   r.   rG   rQ   rR   r7   )r2   rS   r'   r?   rH   r   r   r   usage   s   


zCommand.usagec                 C  s   t d t | j t tt| j t | j t d t d t tddd t | j t d t d t tddd | jrEt | j nt d | jrgt d	 t td
dd d| j}t t	|t
jd t d d S )Nr+   Usage   -)	characterArgs   zNo args
Aliases   z, rK   )printr'   r   lenr;   r[   rY   r(   r7   r   r   rZ   )r2   alias_stringr   r   r   
print_help   s(   


zCommand.print_help	arg_classr   c                 C  s  |j dr|   dS t| j}|j  }t|jD ]\}}t	| jj| }|||< qi }|
 D ];\}}||}|tv rE||}n%t|ttfv rjt|}	dd |	D }
t|
dkrj|
d }|tv rj||}|||< q3t| jrt| jdi | dS | jdi | dS )z
        Call the command function with the given arguments.

        The arguments are all strings at this point, as they're come from the
        command line.

        helpNc                 S  s   g | ]}|t ur|qS r   )r   ).0ir   r   r   
<listcomp>   s    z%Command.call_with.<locals>.<listcomp>r   r   r   )r   rE   rg   r   r$   copy	enumerater   r.   getfullargspecrN   CONVERTABLE_TYPESr
   r   r   r	   re   iscoroutinefunctionasynciorun)r2   rh   r   r   indexvaluekeycleaned_kwargs
annotationinner_annotationsfilteredr   r   r   	call_with   s0   





zCommand.call_with)r)   r*   r)   r6   )r)   r<   )r?   r6   r)   r6   )r?   r6   )rh   r   )r   r   r   r,   r!   r&   r'   r   r    r(   r3   propertyr5   r;   rA   rC   rI   rY   r[   rg   r{   r   r   r   r   r#   )   s(   
 


	

r#   c                   @  s   e Zd ZU dZdZded< eeddZded< d.ddZ	d/ddZ
ddg fd0ddZd1ddZd2ddZ	d3d4dd Zd5d#d$Zd6d'd(Zed7d)d*Zd8d9d,d-ZdS ):CLIz
    The root class for building the CLI.

    Example usage:

    .. code-block:: python

        cli = CLI()
        cli.register(some_function)

    :param description:
        Customise the title of your CLI tool.

    zTarg CLIr6   r;   F)r   initzlist[Command]commandsr&   r'   r)   boolc                 C  s*   | j D ]}|j|kr|j|kr dS qdS )z
        This isn't used by Targ itself, but is useful for third party code
        which wants to inspect the CLI, to find if a command with the given
        name exists.
        TF)r   r&   r'   )r2   r&   r'   r$   r   r   r   command_exists  s   
zCLI.command_existsnamec                 C  s   d|v rdS dS )z{
        Any custom names provided by user should not contain spaces (i.e.
        command names and group names).
        r4   FTr   )r2   r   r   r   r   _validate_name  s   zCLI._validate_nameNr$   r   r%   r(   r   c                 C  sH   |r|  |std|r|  |std| jt||||d dS )a  
        Register a function or coroutine as a CLI command.

        :param command:
            The function or coroutine to register as a CLI command.
        :param group_name:
            If specified, the CLI command will belong to a group. When calling
            a command which belongs to a group, it must be prefixed with the
            ``group_name``. For example
            ``python my_file.py group_name command_name``.
        :param command_name:
            By default, the name of the CLI command will be the same as the
            function or coroutine which is being called. You can override this
            here.
        :param aliases:
            The command can also be accessed using these aliases.

        z)The group name should not contain spaces.z+The command name should not contain spaces.)r$   r&   r'   r(   N)r   
ValueErrorr   rQ   r#   )r2   r$   r&   r'   r(   r   r   r   register  s   zCLI.registerc                 C  sb   d| j tt| j dddddg}| jD ]}|t|jtjd ||j  |d qd	|S )Nr+   z=Enter the name of a command followed by --help to learn more.Commandsz--------rK   rM   )
r;   r   re   r   rQ   r   r5   r   rZ   r7   )r2   linesr$   r   r   r   get_help_textC  s   

zCLI.get_help_textc                 C  s   t jdd S )z1
        Remove any redundant arguments.
        r   N)sysargvr1   r   r   r   _get_cleaned_argsY  s   zCLI._get_cleaned_argsOptional[Command]c                 C  s<   | j D ]}|j|ks||jv r|r|j|krq|  S qd S r=   )r   r'   r(   r&   )r2   r'   r&   r$   r   r   r   _get_command_  s   
zCLI._get_commandru   r   c                 C  s   |dv rdS |dv rdS |S )N)TruetruetT)FalsefalsefFr   )r2   ru   r   r   r   _clean_cli_argumentl  s
   zCLI._clean_cli_argumentr   r   c                 C  s   t  }|D ]:}|dr4|dd  dd}t|dkr(|d }| |d }n|d }d}||j|< q| |}|j| q|S )Nz--   =r   r   T)r   
startswithsplitre   r   r   r   rQ   )r2   r   	argumentsarg_str
componentsr   ru   r   r   r   _get_arg_classs  s   

zCLI._get_arg_classc                 C  s   t | jdkS )Nr   )re   r   r1   r   r   r   _can_run_in_solo_mode  s   zCLI._can_run_in_solo_moder0   c           
   
   C  s  |   }d}z|d}W n ty   d}Y nw || d}|r6| js+td dS d}| jd }d|_n t|dkrDt| 	  dS |d }| j
|d}|rV|d	d }|sut|d
kru|d }|d	 }| j
||d}|ru|d
d }|rz| |}|| W dS  ty }	 z*ttdtjd t|	 |rtt  ntd |  td	 W Y d}	~	dS d}	~	ww td|  t| 	  dS )a  
        Run the CLI.

        :param solo:
            By default, a command name must be given when running the CLI, for
            example ``python my_file.py command_name``. In some situations, you
            may only have a single command registered with the CLI, so passing
            in the command name is redundant. If ``solo=True``, you can omit
            the command name i.e. ``python my_file.py``, and Targ will
            automatically call the single registered command.

        Nz--traceFTzQError - solo mode is only allowed if a single command is registered with the CLI.r+   r   )r'   r   r   )r'   r&   zThe command failed.rK   z#For a full stack trace, use --tracezUnrecognised command - )r   rt   r   popr   rd   r   r0   re   r   r   r   r{   	Exceptionr   r   red	traceback
format_excrg   r   exit)
r2   r0   cleaned_argsr$   rt   tracer'   r&   rh   	exceptionr   r   r   rs     sd   


zCLI.run)r&   r6   r'   r6   r)   r   )r   r6   r)   r   )r$   r   r&   r%   r'   r%   r(   r   r|   )r)   r   r=   )r'   r6   r&   r%   r)   r   )ru   r6   r)   r   )r   r   r)   r   )r)   r   )F)r0   r   )r   r   r   r,   r;   r!   r   r    r   r   r   r   r   r   r   r   r   r}   r   rs   r   r   r   r   r~      s&   
 


(


r~   ))
__future__r   rr   decimalr.   rO   r   r   collections.abcr   dataclassesr   r   typingr   r   r   r	   r
   r   docstring_parserr   r   r   formatr   r   r   typesr   r   ImportErrortype__VERSION__intfloatDecimalrp   r   r#   r~   r   r   r   r   <module>   s6      G