
    Ki                     z    d 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
 erddlmZ ddlmZ e	 G d d	             Zy)
z9Lazy-loadable command specification for deferred imports.    N)chain)TYPE_CHECKINGAny)FactorydefinefieldApp)Groupc                      e Zd ZU dZeed<   dZeeedf   z  dz  ed<    ee	      Z
e	eef   ed<   dZedz  ed<   dZeed<   dZd	ed
<    edd      Zedz  ed<   edefd       Z eddd      Zded<   ddZedefd       Zy)CommandSpeca  Specification for a command that will be lazily loaded on first access.

    This allows registering commands via import path strings (e.g., "myapp.commands:create")
    without importing them until they're actually used, improving CLI startup time.

    Parameters
    ----------
    import_path : str
        Import path in the format "module.path:attribute_name".
        The attribute should be either a function or an App instance.
    name : str | tuple[str, ...] | None
        CLI command name. If None, will be derived from the attribute name via name_transform.
        For function imports: used as the name of the wrapper App.
        For App imports: must match the App's internal name, or ValueError is raised at resolution.
    app_kwargs : dict
        Keyword arguments to pass to App() if wrapping a function.
        Raises ValueError if used with App imports (Apps should be configured in their own definition).

    Examples
    --------
    >>> from cyclopts import App
    >>> app = App()
    >>> # Lazy load - doesn't import myapp.commands until "create" is executed
    >>> app.command("myapp.commands:create_user", name="create")
    >>> app()
    import_pathN.name
app_kwargshelpsort_keyz,Group | str | tuple[Group | str, ...] | Nonegroupshow)defaultalias_showreturnc                 4    | j                   y| j                   S )NT)r   selfs    a/home/jay/workspace/scripts/.codegraph-venv/lib/python3.12/site-packages/cyclopts/command_spec.pyr   zCommandSpec.show3   s    ::zz    F)initr   reprz
App | None	_resolvedc                    | j                   | j                   S | j                  j                  d      \  }}}|r|st        d| j                  d      	 t	        j
                  |      }	 t        ||      }ddl	m
} t        ||      r| j                  r&t        d| j                  d| j                  d      | j                  o|j                  d   | j                  k7  rSt        d| j                  d|j                  d   d| j                  d| j                   d|j                  d    d      ddl	m}	  |	||       || _         nt        | j                        }
ddl	m} |
j#                  d|j$                         |
j#                  d|j&                         d|
vr|j(                  |j(                  |
d<    ||
|        |dd| j                  i|
| _         | j                   j+                  |       | j,                  | j,                  | j                   _        | j.                  | j.                  | j                   _        | j0                  | j0                  | j                   _        | j2                  | j2                  | j                   _        | j                   j6                  |j8                  | j                   _        t;        | j                   j$                  | j                   j&                        D ]  }d| j                   |   _         | j                   S # t        $ r!}t        d|d| j                        |d}~ww xY w# t        $ r%}t        d|d|d	| j                  d
      |d}~ww xY w)a  Import and resolve the command on first access.

        Parameters
        ----------
        parent_app : App
            Parent app to inherit defaults from (help_flags, version_flags, groups).
            Required to match the behavior of direct command registration.

        Returns
        -------
        App
            The resolved App instance, either imported directly or wrapping a function.

        Raises
        ------
        ValueError
            If import_path is not in the correct format "module.path:attribute_name".
        ImportError
            If the module cannot be imported.
        AttributeError
            If the attribute doesn't exist in the module.
        N:zInvalid import path: z/. Expected format: 'module.path:attribute_name'zCannot import module z from zModule z has no attribute z (from import path )r   r	   z8Cannot apply configuration to imported App. Import path z0 resolves to an App, but kwargs were specified: z.. Configure the App in its definition instead.z(Imported App name mismatch. Import path z resolves to an App with name=z., but it was registered with CLI command name=z. Either use app.command('z	', name='z%') or change the App's name to match.)_apply_parent_defaults_to_app)_apply_parent_groups_to_kwargs
help_flagsversion_flagsversionr   F )r    r   
rpartition
ValueError	importlibimport_moduleImportErrorgetattrAttributeErrorcyclopts.corer
   
isinstancer   r   r$   dictr%   
setdefaultr&   r'   r(   r   r   r   r   r   r   _name_transformname_transformr   )r   
parent_appmodule_path_	attr_namemoduleetargetr
   r$   r   r%   flags                r   resolvezCommandSpec.resolve;   sa   . >>%>>! %)$4$4$?$?$D!Q	)'(8(8';;jk 
	h,,[9F	VY/F 	&fc" ##'#3#3"6 7226//1D ECD  yy$Q499)D ##'#3#3"66TU[U`U`abUcTf gCCG99- P//3/?/?.@	&++VW.IY Z9:  D)&*=#DN doo.JD!!,
0E0EF!!/:3K3KL
*z/A/A/M(2(:(:
9%*:zB >dii>:>DNNN""6* 99 "&))DNN==$&*mmDNN#::!#'::DNN ::!"&**DNN>>))1,6,E,EDNN) $..33T^^5Q5QR 	.D(-DNN4 %	. ~~U  	h 5k_F4K[K[J^_`fgg	h
  	 +(:9-GZ[_[k[kZnnop	s0   L# ,M #	M,MM	M> M99M>c                     | j                   duS )z9Check if this command has been imported and resolved yet.N)r    r   s    r   is_resolvedzCommandSpec.is_resolved   s     ~~T))r   )r7   r
   r   r
   )__name__
__module____qualname____doc__str__annotations__r   tupler   r3   r   r   r   r   r   r   r   boolpropertyr   r    r?   rA   r)   r   r   r   r      s    6 )-D#c3h
$
&-!(JS#X.D#*Hc<@E9@t6:E4$;:d  
 $5II|In` *T * *r   r   )rE   r,   	itertoolsr   typingr   r   attrsr   r   r   r1   r
   cyclopts.groupr   r   r)   r   r   <module>rO      s<    ?   % ( (!$ _* _* _*r   