a
    …Ï?eÃ3  ã                   @   sè   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d„Zd0dd„Zd1dd	„Zd
d„ Z	d2dd„Z
d3dd„Zd4dd„Zd5dd„Zd6dd„Zdd„ Zdd„ Zd7dd„Zd8dd „Zd!d"„ Zd#d$„ Zd%d&„ Zd'd(„ Zd)d*„ Zd+d,„ Zd-d.„ ZdS )9é    NÚ.c                 C   sf   t ƒ }|  ¡ D ]R\}}|}t| |¡ƒ}|dd… D ]}||vrJt ƒ ||< || }q4|||d < q|S )z€
    Take dict like: {"a.dot.path":"value", ...}
    And produce a dict like:
    {"a": {"dot" : {"path":value}, ...}, ...}
    Néÿÿÿÿ)ÚdictÚitemsÚlistÚsplit)ZflatdictÚdelimÚretZflatkeyÚvalÚhereÚkeysÚone© r   úK/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/moo/util.pyÚ	unflatten   s    

r   c                    s$   d‡fdd„	‰‡ fdd„ˆ| ƒD ƒS )z
    Reverse of unflatten
    r   c                 3   sJ   t | tƒr:|  ¡ D ]$\}}ˆ |||f ƒD ]
}|V  q*qn|| f V  d S ©N)Ú
isinstancer   r   )ÚthingÚcurÚnÚsÚpath)Úpathsr   r   r   !   s
    
zflatten.<locals>.pathsc                    s$   i | ]}ˆ   |d d… ¡|d “qS )Nr   )Újoin©Ú.0Úp)r   r   r   Ú
<dictcomp>)   ó    zflatten.<locals>.<dictcomp>)r   r   )Zdeepdictr   r   )r   r   r   Úflatten   s    r   Tc                 C   s(   t | d ƒ}|r| | d ¡ | |¡S )z)
    Return the flattened path name.
    r   Úname)r   Úappendr   )ÚschemaZ	with_leafr   r   r   r   r   Úflatpath,   s    r#   c                 C   s8   d| v rt | ƒ| iS t| tƒr*t|  ¡ ƒ} dd„ | D ƒS )zl
    Given a list of schema or a "hier" of schema, produce a flattened map from FQ pathname to oschema.
    r"   c                 S   s   i | ]}t |ƒ|“qS r   )r#   )r   Úor   r   r   r   @   r   zpathify.<locals>.<dictcomp>)r#   r   r   r   Úvalues)Zoschemar   r   r   Úpathify6   s
    
r&   ú:c                 C   s   || v r|   |d¡S || fS )z
    Return (prefix,rest) 
    é   )r   )ÚstringÚdefaultr   r   r   r   ÚunprefixC   s    r+   c           	   
      s  dd„ }| du s*t | ttfƒr@t| ƒdkr@||ƒr8|gS tdƒ‚t | ttfƒrrtƒ }| D ]‰ |tˆ ||ƒ7 }qX|S | ‰ |ˆ ƒr„ˆ gS ztˆ ƒ}W n" ty¢   Y n` ty²   Y nP0 t |ttfƒrÌ|| gS |duròt|ƒ}|t| 	¡ ƒ|  gS td|› d�ƒ‚t ˆ t
ƒ�rêdˆ v �r4td	d
„ ˆ  d¡D ƒ||ƒS |�rzz|ˆ ƒ}W n& t�yl } zW Y d}~nd}~0 0 t|||ƒS |du �r”tdˆ › d�ƒ‚ˆ |v �r¨|ˆ  gS t|ƒ}ˆ |v �rÄ|ˆ  gS ‡ fdd
„| ¡ D ƒ}|�rê|d gS tdˆ › dt|ƒ› �ƒ‚dS )aª  
    Return list of schema objects from targets potentially referencing context.

    A target may be a number, string, an object or a list thereof.

    If targets is the empty list, the context is returned if it looks like a schema.

    If file_loader is given it is a function which must accept a filename with optional datapath: prefix and which must return the object representation of the file or raise exception.

    c                 S   sT   | d u rdS t | tƒsdS tg d¢ƒ}t| |  ¡ ¡ƒt|ƒkrDdS d| v rPdS dS )NF)r    r"   r   Tz$schema)r   r   ÚsetÚlenÚintersectionr   )r   Zmooishr   r   r   Ú	is_schemaX   s    
z!resolve_schema.<locals>.is_schemaNr   z7null targets but context does not appear to be a schemazinteger target (z ) but no or non-sequence contextú,c                 S   s   g | ]}|  ¡ ‘qS r   )Ústrip)r   Útr   r   r   Ú
<listcomp>‡   r   z"resolve_schema.<locals>.<listcomp>zstring target (z) requires contextc                    s   g | ]}|d  ˆ kr|‘qS )r    r   )r   Úv©Útargetr   r   r3   �   r   zfailed to resolve target: |z| in context of type )r   r   Útupler-   Ú
ValueErrorÚresolve_schemaÚintÚ	TypeErrorr&   r   Ústrr   r%   Útype)	ÚtargetsÚcontextZfile_loaderr/   r	   ÚindexZflatZgotÚerrr   r5   r   r9   L   sZ    "







r9   c              	   C   sT   t |tƒr| |¡}|D ]6}|dkr&qzt|ƒ}W n tyD   Y n0 | | } q| S )zÜSelect out a part of obj structure based on a path.

    The path is a list or a delim-separated string.

    Any element of the path that looks like an integer will be cast to
    one assuming it indexes an array.

    Ú )r   r<   r   r:   r8   )Úobjr   r   r   r   r   r   Úselect_path¦   s    	


rD   Fc                 C   sT   t | tƒr| g} tƒ }| D ]4}tj |¡r6| |¡ q|rtj 	d|› d�¡ q|S )zB
    Return list of path elements which exist as directories.
    zpath does not exist: Ú
)
r   r<   r   Úosr   Úisdirr!   ÚsysÚstderrÚwrite)r   Úwarnr	   r   r   r   r   Úexisting_paths¼   s    

rL   c                 C   sN   t | tƒr|  d¡} dd„ | D ƒ} |rJtj tjj¡}|| vrJ|  d|¡ | S )zÖReturn list of paths made absolute with cwd as first .

    Input "paths" may be a ":"-separated string or list of string.

    If add_cwd is True and if cwd is not already in paths, it will be
    prepended.

    r'   c                 S   s   g | ]}t j |¡‘qS r   )rF   r   Úrealpathr   r   r   r   r3   ×   r   zclean_paths.<locals>.<listcomp>r   )r   r<   r   rF   r   rM   ÚcurdirÚinsert)r   Zadd_cwdÚcwdr   r   r   Úclean_pathsÌ   s    	

rQ   c                   C   s   t j t j t¡d¡gS )Nzjsonnet-code©rF   r   r   ÚdirnameÚ__file__r   r   r   r   Úsearch_path_modelsá   s    rU   c                   C   s   t j t j t¡d¡gS )NZ	templatesrR   r   r   r   r   Úsearch_path_templatesã   s    rV   c           	      C   sÊ   t |p
t ƒ ƒ}t ƒ }|tj d¡g7 }tj tj | ¡¡}||vrL| |¡ |D ]$}tj |¡}||v rjqP| |¡ qPt ƒ }tj | ¡d }|dv rštƒ }|dv r¨tƒ }|D ]}||v rºq¬| |¡ q¬|S )aL  
    Produce a list of absolute directories from which to search for
    files like the given file name given by 'likename'.

    List is prepended with cwd and directory holding likename followed
    by any given by user in 'paths' followed by built-in directories
    provided by moo.  Thus, user may override built-in files.
    r   r   )ú.jsonnetú.schema)z.j2)	r   rF   r   rM   rS   r!   ÚsplitextrU   rV   )	Zlikenamer   ÚuserÚspÚparentZupZbisÚextZbir   r   r   Úsearch_pathæ   s,    	
r^   r   c                 C   s€   | st dƒ‚|  d¡r4tj | ¡r&| S t d| › �ƒ‚t| |ƒ}t|ƒD ]&}tj || ¡}tj |¡rF|  S qFt d| › �ƒ‚dS )z�Resolve filename against moo built-in directories and any
    user-provided list in "paths".

    Raise ValueError if fail.

    zno file name providedú/zabsolute path not found: zfile not found: N)r8   Ú
startswithrF   r   Úexistsr^   rQ   r   )Úfilenamer   ÚmaybeÚfpr   r   r   Úresolve  s    


re   c                    s˜   t | ƒttttfv r| S t| ttfƒr8‡ fdd„| D ƒS tƒ }|  	¡ D ]L\}}t|tƒr„d|v r„|d }|t
dƒd… }tˆ |dƒ}t|ˆ ƒ||< qF|S )z~
    Convert special form substructure:

    {"$ref":"#/definitions/<def>"}

    To the value of attribute <def> of defs.
    c                    s   g | ]}t |ˆ ƒ‘qS r   )Ú
deref_defs)r   Zele©Údefsr   r   r3   7  r   zderef_defs.<locals>.<listcomp>z$refz#/definitions/Nr_   )r=   r:   Úfloatr<   Úboolr   r   r7   r   r   r-   rD   rf   )Úctxrh   r	   Úkeyr
   Úrefr   rg   r   rf   ,  s    rf   c                 C   sd   t | tƒst| ƒdfS zt| ƒ}| dfW S  ty:   Y n0 |  ¡ dv rLdS |  ¡ dv r\dS | dfS )z’
    Return tuple (value, iscode)

    If iscode is true if value should be considered for tla_codes.

    The value is turned into a string.
    T)ÚtrueÚyesÚon)rn   T)ÚfalseÚnoÚoff)rq   TF)r   r<   ri   r8   Úlower)r
   Zjunkr   r   r   Úscalar_typifyB  s    

ru   c                 C   s  t ƒ }t ƒ }| D ]ê}z| dd¡\}}W n. tyV } ztdƒ|‚W Y d}~n
d}~0 0 |d dv rn|||< q| d¡}t|ƒdkrØ|d }	|	d	v r°t||ƒ}
t|
ƒ ¡ ||< q|	tjv rØtj	 
||¡}t |¡||< qt|ƒ\}}|rò|||< q|||< qt ||d
�S )z½Pack list of things like var=value or var=file.jsonnet into
    keyword arguments suitable for passing to jsonnet.  A value is
    guessed to be either Jsonnet code or a simple string.
    ú=r(   z+Did you forget to specify the TLA variable?Nr   z{["]}r   r   )rW   z.jsonrX   )Útla_varsÚ	tla_codes)r   r   r8   r-   re   ÚopenÚreadÚmooZknown_extensionsÚioÚloadÚjsonÚdumpsru   )ZtlasZjpathrw   rx   r   rl   r
   rA   Úchunksr]   ÚfnameÚiscoder   r   r   Útla_packW  s4     




rƒ   c                 C   sL   ddl m} |s| S t||ƒs$|g}|D ]}t|tƒr>t|ƒ}|| ƒ} q(| S )aô  Transform a model

    The transform may be a single transform or a sequence of
    transforms.  As a sequence it represents a pipeline.  First
    transform is applied to model and its result is passed into
    the second, etc.

    A transform may be a string or a callable.  If a callable, it is
    applied as-is.

    If the transform is a string it is interpreted as:

        [<prefix>:][<modmeth>[|<modmeth>|...]]

    The <prefix> is a JSON Pointer into the model and if not given
    refers to the entire model.  For details about JSON Pointer see
    https://tools.ietf.org/html/rfc6901

    The <modmeth> is a dotted-path of modules with a final leaf that
    is a function.  The modules must be available and the function
    must take a single argument, which is the model (or pointed
    portion if <prefix>) is used and the function must return a stat
    strucutre.

    If multiple <modmeth> are provided, separated by a pipe ("|"), the
    output of one provides the input to the next.

    An example which applies three consecutive transforms to the first
    element of a "types" array.

        /types/0:moo.oschema.typify|moo.oschema.graph|moo.schema.namespacify

    If a pipeline gets much longer, best to provide it in a Python module.

    r   )ÚSequence)Úcollections.abcr„   r   r<   Útransform_parse)ÚmodelZ
transformsr„   r2   r   r   r   Ú	transform}  s    $


rˆ   c                 C   s   |s|S t  | ||¡S )zzAdd branch to model at pointer.

    Note, pointer must have already support in model.  Ie, there is no
    'mkdir'.

    )ÚjsonpointerZset_pointer)r‡   ÚpointerÚbranchr   r   r   Úgraft­  s    rŒ   c                 C   s(   |   dd¡}t|ƒdkr|S d|d fS )z+
    Parse pointer spec as used by CLI
    r'   r(   é   rB   r   )r   r-   )ÚtextÚpartsr   r   r   Úparse_ptr_spec¹  s    r�   c                    sz   t | ƒ\‰} tƒ ‰ |  d¡D ]J}| dd¡}t|ƒdkr@tdƒ‚t |d ¡}t||d ƒ}ˆ  	|¡ q‡ ‡fdd„}|S )	z@
    Parse transform spec.  See @ref tranform() for details
    ú|r   r(   r�   z)transform should a '.' path to a functionr   c                    s.   t  | ˆ¡}ˆ D ]}||ƒ}qt| ˆ|ƒ} | S r   )r‰   Zresolve_pointerrŒ   )r‡   r‹   Úmeth©ZmethsÚptrr   r   ÚtransÒ  s
    
ztransform_parse.<locals>.trans)
r�   r   r   Úrsplitr-   ÚRuntimeErrorÚ	importlibÚimport_moduleÚgetattrr!   )ZtspecZmodmethr�   Úmodr’   r•   r   r“   r   r†   Ã  s    r†   )r   )r   )Tr   )Nr'   )NN)r   )F)T)N)r   )rF   rH   r{   r˜   r~   r‰   r   r   r#   r&   r+   r9   rD   rL   rQ   rU   rV   r^   re   rf   ru   rƒ   rˆ   rŒ   r�   r†   r   r   r   r   Ú<module>   s2   




	
Z



(
&0
