a
    …Ï?e«E  ã                   @   sâ  d 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mZ G dd„ dƒZe	e	ddgd�d	�Z
ejf i e
¤Žejd
dddd�ejddddejdddd�dd�ejddddejdddd�dd�ejddddd�ejdd ded!d"�ejd#d$ded%d"�ejd&d'„ ƒƒƒƒƒƒƒƒZe d(¡e d)¡ejd*d(„ ƒƒƒZe d+¡e d)¡ejd,d+„ ƒƒƒZe d-¡ejd.d/d0ejdddd�d1d2�ejd3d4ed5d6�ejd#d7ddd8d9�ejd:ddd;d<�ejd=ddd>d<�ejd?d@dAe dAdBg¡dCd2�e dD¡ejdEdF„ ƒƒƒƒƒƒƒƒƒZe dG¡ejd?d@dAe dAdBg¡dCd2�ejdHdIdddJd<�ejdKdLddMd�e dG¡e dN¡ejdOdP„ ƒƒƒƒƒƒƒZe dQ¡ejdRdSddTd�ejd.d/d0ejdddd�d1d2�ejdUdVddWd�e dD¡ejdXdY„ ƒƒƒƒƒƒZe ¡ ejd.d/d0ejdddd�d1d2�ejdZd[d\e g d]¢¡d^d2�e dD¡ejd_d`„ ƒƒƒƒƒZe ¡ ejd.d/d0ejdddd�d1d2�ejd#d7dddad9�e db¡ejdcdA„ ƒƒƒƒƒZe ¡ ejd.d/d0ejdddd�d1d2�e dD¡e dd¡ejdedf„ ƒƒƒƒƒZe dg¡ejd#d7dejdddd�dhdi�ejd.d/d0ejdddd�djd2�e dD¡e dd¡ejdkdl„ ƒƒƒƒƒƒZe dm¡ejd.dndoejdddp�dqd2�e dD¡ejdrds„ ƒƒƒƒZe ¡ ejd.d/d0ejdddp�d1d2�e d)¡ejdtdu„ ƒƒƒƒZe ¡ dvdw„ ƒZdxdy„ Z dze!k�rÞe ƒ  dS ){z
Main CLI to moo
é    N)ÚpprintÚpformatc                   @   sj   e Zd ZdZddd„Zddd„Zdd	d
„Zdd„ Zddd„Zdd„ Z	dd„ Z
ddd„Zdd„ Zdd„ ZdS )ÚContextzQ
    Application context collects parameters and methods common to commands.
    Ú © c                 C   sR   || _ tj |d¡| _tj |d¡| _tƒ | _|rBtj ||¡| _|| _	|| _
dS )aH  
        A CLI context

        - dpath :: data path to sub-structure to use
        - mpath :: file paths to search for models
        - tpath :: file paths to search for templates
        - tla :: Jsonnet top level arguments
        - transform :: transformations to apply
        - graph :: data to graft on to model
        TN)ÚdpathÚmooÚutilZexisting_pathsÚmpathÚtpathÚdictÚtlasZtla_packÚ
transformsÚgrafts)Úselfr   r
   r   ÚtlaÚ	transformÚgraftr   r   úO/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/moo/__main__.pyÚ__init__   s    zContext.__init__Nc                 C   s>   t j ||¡\}}t jj|  |¡|  |¡|p0| jfi | j¤ŽS )z¤
        Simple load with path search and dpath reduction.

        Filename may be prefixed as dpath:filename to use that dpath instead of the one passed.
        )	r   r	   ÚunprefixÚioÚloadÚresolveÚsearch_pathr   r   )r   Úfilenamer   r   r   r   Ú	just_load*   s    þþzContext.just_loadc                 C   s6   t j ||¡\}}|  ||¡}|  |¡}|  |¡}|S )zÖ
        Load a data structure from a file.

        Search mpath for file and return substructure at dpath.

        Filename may be prefixed as dpath:filename to use that dpath instead of the one passed.
        )r   r	   r   r   r   r   )r   r   r   Úmodelr   r   r   r   5   s
    

zContext.loadc                 C   s:   | j D ].}tj |¡\}}|  |¡}tj |||¡}q|S )zApply any grafts to the model)r   r   r	   Zparse_ptr_specr   r   )r   r   ÚgÚptrÚfnameÚdatar   r   r   r   C   s
    

zContext.graftc                 C   s   | j rtj ||| j  ¡}|S )zTransform a model)r   r   r	   r   )r   r   r   r   r   r   r   K   s    zContext.transformc                 C   s*   |  d¡rtj || j¡S tj || j¡S )zE
        Return search path for given file name or extension
        Zj2)Úendswithr   r	   r   r   r
   )r   r    r   r   r   r   Q   s    
zContext.search_pathc                 C   s2   d}t j ||  |¡¡}|du r.td|› �ƒ‚|S )z6
        Resolve a filename to absolute path.
        Núcan not resolve )r   r	   r   r   ÚRuntimeError)r   r   Úretr   r   r   r   Y   s
    zContext.resolvec                 C   sH   |   |¡}| jddd�}t|d�}|r.||d< |  |¡}tj |||¡S )zo
        Render a template in templ_file.

        If model is given provide its data to the template.
        zmoo.jsonnetÚtempl)r   )r   r   )r   r   r   r   r   Z	templatesÚrender)r   Z
templ_filer   r&   ÚhelperÚparamsr   r   r   r   r'   d   s    


zContext.renderc                 C   s8   |   |¡}| d¡r| j}n| j}tj||fi | j¤ŽS )z>
        Return list of files the given file imports.
        z.j2)r   r"   r   r
   r   Úimportsr   )r   r   Zfpathr   r   r   r*   s   s
    

zContext.importsc                 C   s„   t j t j |¡¡}t j |¡s*t  |¡ t j |¡d }t|tƒrL| 	¡ }t
|dƒ�}| |¡ W d  ƒ n1 sv0    Y  dS )z_Save data to named file.  If intermediate path is missing, it will
        be created.
        éÿÿÿÿÚwbN)ÚosÚpathÚdirnameÚrealpathÚexistsÚmakedirsÚsplitextÚ
isinstanceÚstrÚencodeÚopenÚwrite)r   r   r!   ZabsdirZdotextÚfpr   r   r   Úsave~   s    

zContext.save)r   r   r   r   r   r   )N)N)r   )N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r   r   r   r'   r*   r:   r   r   r   r   r      s     ÿ




r   z-hz--help)Zhelp_option_names)Zcontext_settingsz-Dz--dpathr   z6Specify a selection path into the model data structure)ÚdefaultÚhelpz-Mz--mpathZMOO_LOAD_PATHTF)r1   Údir_okayÚ	file_okayzGAdd directory to data file search paths (can use MOO_LOAD_PATH env var))ZenvvarÚmultipleÚtyper@   z-Tz--tpathZMOO_TEMPLATE_PATHzKAdd directory to template file search paths (can use_TEMPLATE_PATH env var)z-Az--tlazXSpecify a 'top-level argument' to a functional model as a var=string or var=file.jsonnet)rC   r@   z-gz--graftz_Graft a data structure given in a model file into the model in the form: /json/ptr:file.jsonnet)rC   rD   r@   z-tz--transformzSpecify a model transformc                 C   s   t ||||||ƒ| _dS )z$
    moo command line interface
    N)r   Úobj)Úctxr   r
   r   r   r   r   r   r   r   ÚcliŽ   s    rG   r   r   c                 C   sB   z| j  |¡}W n" ty2   t d|› �¡ Y n0 t |¡ dS )z5
    Resolve a filename as moo would internally.
    r#   N)rE   r   Ú
ValueErrorÚclickÚecho)rF   r   r   r   r   r   ¥   s
    r.   c                 C   s0   d|vrd| }| j  |¡}t d |¡¡ dS )zJ
    Print search path for representative file or file extension name
    Ú.ztest.Ú
N)rE   r   rI   rJ   Újoin)rF   r   Úpr   r   r   r.   ´   s    Úvalidatez-oz--outputz/dev/stdoutzOutput file, default is stdout)r?   rD   r@   z-sz--schemaz-File containing a representation of a schema.)rD   r@   z--targetz"Specify target schema of the model)r?   rC   r@   z
--sequencezCIndicate the model is a sequence of models (ie, not an array model))r?   Zis_flagr@   z
--passfailz(Print PASS or FAIL instead of null/throwz-Vz--validatorÚ
jsonschemaÚfastjsonschemazSpecify which validatorr   c                 C   s¶   |du rdn
| j  |¡}tj ||| j j¡}	t|	ƒdkr<d}| j  |¡}
|sR|
g}
t|	ƒt|
ƒkr€tdt|
ƒ› dt|	ƒ› d�ƒ‚tj 	|
|	|| |¡}t
j|dd�}| j  ||¡ dS )	a?  
    Validate models against target schema.

    A full "context" schema must be provided by -s/--schema if it is required for target schema to resolve any dependencies.  The "context" schema is identified with a string of the form "filename with optional dataprefix".

        -s myschema.subschema:my-schema.jsonnet

    This resulst ins the "subschema" attribute of the "myschema" attribute of the top level object from "my-schema.jsonnet" to be used as the "context" schema.

    A "target" schema is what is used to validate a model and may be specified in a variety of "target forms" with the -t/--target option.  The supported target forms are:

    - an integer indicating an index into the full "context" schema is alloweed when the context is of a sequence form.

    - a simple string indicating either a key of the full "context" schema, allowed only if the context is an object, or indicating the "name" attribute of an moo oschema object held in the context (be it of sequence or object form).

    - a filename with optional "datapath:" prefix.

    When this last form is used the resulting data structure may be any target form listed above or may directly be an moo oschema object or a JSON Schema object.

    By default, this command operates in "scalar mode" meaning a single model and single target schema are processed.  It may instead operate in "sequence mode" which expects a matching sequence of models and target schema.

    Sequence mode is entered when any of the following are true:

    - the --sequence option is given indicating array of models is given

    - more than one -t/--target is given

    - a -t/--target value is a comma-separated list of target forms

    - a -t/--target is a filename with optional "datapath:" prefix and the loaded data produces a list or tuple form.

    The multiple targets are concantenated and the resulting sequence must match the supplied sequence of models.

    In the special cases that all target schema are either in JSON Schema form or are in moo oschema form but lack any type dependency, a context schema is not required.


    Né   Tz sequence size mismatch: #models:z, #targets:z
Did you forget --sequence?é   ©Úindent)rE   r   r   r	   Úresolve_schemaÚlenr   rH   ZovalidrO   ÚjsonÚdumpsr:   )rF   ÚoutputZschemaÚtargetÚsequenceZpassfailÚ	validatorr   ÚcontextÚtargetsZmodelsÚresÚtextr   r   r   Úcmd_validateÁ   s    8rb   Úregexz-Oz--onlyzAssure regex is bound by ^$z-Rz--rpathz5Specify a data path and treat regex as a file to loadÚstringc                 C   sr   |r| j  ||¡}|r>|d dkr*d| }|d dkr>|d }td|d�}tj |||¡}|rnt|ƒ t|ƒ dS )z+
    Validate a string against a regex
    r   ú^r+   ú$rd   )rD   ÚpatternN)rE   r   r   r   r	   rO   Úprint)rF   r]   ÚonlyZrpathrc   rd   Zvalidr`   r   r   r   Ú	cmd_regex  s    rj   Úcompilez-mz--multizWrite multiple output filesz--string/--no-stringz-S/ zTreat output as string not JSONc                 C   s„   | j  |¡}|r`tj|dd� | ¡ D ]6\}}tj ||¡}|sNtj|dd�}| j  	||¡ q&n |srtj|dd�}| j  	||¡ dS )z!
    Compile a model to JSON
    T)Úexist_okrS   rT   N)
rE   r   r-   r2   Úitemsr.   rM   rX   rY   r:   )rF   ZmultirZ   rd   r   r!   ÚoneZdatr   r   r   Ú	compileit*  s    ro   z-fz--formatÚrepr)rX   rp   ÚprettyÚplainÚtypeszOutput formatc                 C   sÀ   | j  |¡}|dkr(ttj|dd�ƒ n”|dkr>tt|ƒƒ n~|dkrPt|ƒ nl|dkr´t|tƒrz|D ]}tt	|ƒƒ qfq¼t|t
ƒr¦| ¡ D ]\}}t|t	|ƒƒ qŒq¼tt	|ƒƒ nt|ƒ dS )zH
    Like render but print model that would be sent to the template
    rX   rS   rT   rp   rq   rs   N)rE   r   rh   rX   rY   rp   r   r4   ÚlistrD   r   rm   )rF   rZ   Úformatr   r!   rn   ÚkeyÚvalr   r   r   ÚdumpF  s     


rx   zSpecify target schemaÚoschemac                    sh   ddl m‰ | j |¡‰ |du r(ˆ g}ntj |ˆ | jj¡}‡ ‡fdd„|D ƒ}| j |d |¡¡ dS )z1
    Convert from moo oschema to JSON Schema
    r   )ÚconvertNc                    s    g | ]}t jˆ|ˆ ƒd d�‘qS )rS   rT   )rX   rY   )Ú.0r[   ©r^   rz   r   r   Ú
<listcomp>{  ó    zjsonschema.<locals>.<listcomp>rL   )	Zmoo.jsonschemarz   rE   r   r   r	   rV   r:   rM   )rF   rZ   r[   ry   r_   Zjtextr   r|   r   rP   g  s    r&   c                 C   s4   d}|r| j  |¡}| j  ||¡}| j  ||¡ dS )zr
    Render a template against a model.

    An empty model may be given (ie, with "" on shell command line).
    N)rE   r   r'   r:   )rF   rZ   r   r&   r!   ra   r   r   r   r'     s
    r'   zrender-depszName of target in the .d file)ÚrequiredrD   r@   z)Dependency output file, default is stdoutc           
      C   sx   | j  |¡}| j  |¡}tj | j  |¡¡tj | j  |¡¡g}||| 7 }d |¡}|› d|› d�}	| j  ||	¡ dS )a?  Produce a .d dependencies file for the corresponding call to "render"

    Produce a .d dependencies file in the style of `gcc -MD` to be
    used by a build system (Makefile or ninja), corresponding to a
    call to "moo render". The arguments to "moo render-deps" should be
    the same as the arguments to the corresponding call to "moo
    render", with the following exceptions:

    * The -t option to "moo render-deps" is typically taken from the -o option to "moo render"
    * The -o option to "moo render-deps" is the name of the .d file that should be output

    ú z: rL   N)rE   r*   r-   r.   r0   r   rM   r:   )
rF   r[   rZ   r   r&   Z
model_depsZ
templ_depsÚdepsZdeps_stringÚresultr   r   r   Úrender_deps’  s    ÿ
rƒ   zrender-manyz--outdirrK   )rA   rB   z Output directory, default is '.'c                 C   sˆ   | j  |¡}|D ]r}| j  |d | dd¡¡}| j  |d |¡}tj ||d ¡}tj |¡}tj 	|¡stt 
|¡ | j  ||¡ qdS )z‰Render many files for a project.

    The model found at the data path dpath should be a Jsonnet array
    of moo.render() objects.

    r   r   r   Útemplater   N)rE   r   r   Úgetr'   r-   r.   rM   r/   r1   r2   r:   )rF   Úoutdirr   r!   rn   ra   rZ   Zodirr   r   r   Úrender_many¶  s    
r‡   c                 C   s�   | j  |¡}| d¡rttj tj |¡¡d }t dd|¡ 	¡ }d|› �g}|dd„ |D ƒ7 }|dg7 }d	 
|¡}n
d	 
|¡}| j  ||¡ d
S )z6
    Emit a list of imports required by the model
    z.cmaker   z[^a-zA-Z0-9]Ú_zset(c                 S   s   g | ]}d | ‘qS )z    "%s"r   )r{   rn   r   r   r   r}   Þ  r~   zimports.<locals>.<listcomp>ú)rL   N)rE   r*   r"   r-   r.   r3   ÚbasenameÚreÚsubÚupperrM   r:   )rF   rZ   r   r�   rŠ   ZvarnameÚlinesra   r   r   r   r*   Ï  s    



r*   c                   C   s   t  tj¡ dS )zPrint the versionN)rI   rJ   r   Ú__version__r   r   r   r   Úversionæ  s    r�   c                   C   s   t d d� d S )N)rE   )rG   r   r   r   r   Úmainì  s    r‘   Ú__main__)"r>   r-   r‹   rX   rI   r   r   r   r   r   ZcmddefÚgroupÚoptionÚPathr5   Zpass_contextrG   ÚcommandÚargumentr   r.   ZChoicerb   rj   ro   rx   rP   r'   rƒ   r‡   r*   r�   r‘   r;   r   r   r   r   Ú<module>   s<  
ÿþþ
ÿÿÿ

þ
ÿÿ
ÿ
ÿ
þ9
þÿ
ÿ
ÿ
þ
ÿ
þ
þ
þÿ
þ
þ
þ
þ
þ

