
    \j                         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      Zd Ze	dk(  r ed      j                          yy)	    N   )SchemaBuilder__version__c                   h    e Zd ZddZd Zd Zd Zd Zd ZddZ	d	 Z
d
 Zd Zd Zd Zed        Zy)CLINc                     | j                  |       | j                          t        | j                  j                        | _        y )N)
schema_uri)_make_parser_prepare_argsr   argsr	   builder)selfprogs     9/root/env/lib/python3.12/site-packages/genson/__main__.py__init__zCLI.__init__	   s2    $$		0D0DE    c                     | j                   j                  s'| j                   j                  s| j                  d       | j	                          | j                          | j                          y )Nz*noting to do - no schemas or objects given)r   schemaobjectfailadd_schemasadd_objectsprint_outputr   s    r   runzCLI.run   sL    yy		(8(8IIBCr   c                     | j                   j                  D ]8  }| j                  | j                  j                  |       |j                          : y N)r   r   _call_with_json_from_fpr   
add_schemacloser   fps     r   r   zCLI.add_schemas   >    ))"" 	B(()@)@"EHHJ	r   c                     | j                   j                  D ]8  }| j                  | j                  j                  |       |j                          : y r   )r   r   r   r   
add_objectr    r!   s     r   r   zCLI.add_objects   r#   r   c                 v    t        | j                  j                  | j                  j                               y )N)indent)printr   to_jsonr   r'   r   s    r   r   zCLI.print_output   s&    dll""$))*:*:";<r   c                 :    | j                   j                  |       y r   )parsererror)r   messages     r   r   zCLI.fail"   s    '"r   c                    t        j                  d| j                               }t        j                  d|d      | _        | j                  j                  dddt         j                  d	
       | j                  j                  ddt         j                  dj                  t              d       | j                  j                  dddd       | j                  j                  ddt        dd       | j                  j                  ddt        dd       | j                  j                  ddd g |d!"       | j                  j                  d#d$d%d&t        j                  d'j                  t        j                  t        j                  (      )       | j                  j                  d*t         j                  |d+,       y )-Nr)encodingFzGenerate one, unified JSON Schema from one or more
            JSON objects and/or JSON Schemas. Compatible with JSON-Schema Draft
            4 and above.)add_helpr   descriptionz-hz--helphelpz Show this help message and exit.)actiondefaultr3   z	--versionversionz%(prog)s {}zShow version number and exit.)r4   r5   r6   r3   z-dz--delimiterDELIMaC  Set a delimiter. Use this option if the input files
            contain multiple JSON objects/schemas. You can pass any string. A
            few cases ('newline', 'tab', 'space') will get converted to a
            whitespace character. If this option is omitted, the parser will
            try to auto-detect boundaries.)metavarr3   -e
--encodingENCODINGzUse ENCODING instead of the default system encoding
            when reading files. ENCODING must be a valid codec name or
            alias.)typer8   r3   z-iz--indentSPACESz1Pretty-print the output, indenting SPACES spaces.z-sz--schemaappendz]File containing a JSON Schema (can be specified multiple
            times to merge schemas).)r4   r5   r<   r3   z-$z--schema-uri
SCHEMA_URIr	   zThe value of the '$schema' keyword (defaults to {default!r}
            or can be specified in a schema with the -s option). If {null!r} is
            passed, the "$schema" keyword will not be included in the
            result.)r5   null)r8   destr5   r3   r   zYFiles containing JSON objects (defaults to stdin if no
            arguments are passed).)nargsr<   r3   )argparseFileType_get_encodingArgumentParserr+   add_argumentSUPPRESSformatr   strintr   DEFAULT_URINULL_URI	REMAINDER)r   r   	file_types      r   r
   zCLI._make_parser%   s   %%cD4F4F4HI	-- 	  (683D3D3 	! 	5 	  	83D3D!((50 	! 	2 	  -. 	! 	/ 	  ,S* 	! 	
 	  *3H 	! 	J 	  *Xr	( 	! 	) 	  .,\!-- vm&?&?#0#9#9  ; 	! 	< 	  H..Y& 	! 	'r   c                     t        j                  d      }|j                  ddt               |j	                         \  }}|j
                  S )zi
        use separate arg parser to grab encoding argument before
        defining FileType args
        F)r1   r9   r:   )r<   )rC   rF   rG   rJ   parse_known_argsr0   )r   r+   r   _s       r   rE   zCLI._get_encodingV   sE    
 ((%8D,S9))+a}}r   c                 4   | j                   j                         | _        | j                          | j                  j                  sSt
        j                  j                         s4| j                  j                  j                  t
        j                         y y y r   )	r+   
parse_argsr   _prepare_delimiterr   sysstdinisattyr>   r   s    r   r   zCLI._prepare_args`   sd    KK**,	! yy		(8(8(:II##CII. );r   c                    | j                   j                  dk(  rd| j                   _        y| j                   j                  dk(  rd| j                   _        y| j                   j                  dk(  rd| j                   _        yy)zJ
        manage special conversions for difficult bash characters
        newline
tab	space N)r   	delimiterr   s    r   rU   zCLI._prepare_delimiterh   sc     99)+"&DIIYY  E)"&DIIYY  G+"%DII ,r   c                 :   | j                  |j                         j                               D ]   }	 t        j                  |      } |       " y # t        j
                  $ r5}| j                  dj                  |j                  |             Y d }~Nd }~ww xY w)Nzinvalid JSON in {}: {})	_get_json_stringsreadstripjsonloadsJSONDecodeErrorr   rI   name)r   methodr"   json_stringjson_objerrs         r   r   zCLI._call_with_json_from_fps   s    11"'')//2CD 	KI::k2 8	 '' I		299"''3GHHIs   AB%+BBc                 6   | j                   j                  | j                   j                  dk(  r| j                  |      }n%|j                  | j                   j                        }|D cg c]#  }|j	                         s|j	                         % c}S c c}w )N )r   r`   _detect_json_stringssplitrd   )r   raw_textjson_stringsstrings       r   rb   zCLI._get_json_strings{   so    99&$))*=*=*C44X>L#>>$))*=*=>L .:L6V\\^LLLs   +BBc                     t        j                  d|       }|dd D cg c]  }|dz   	 }}|j                  |d          |S c c}w )z
        Use regex with lookaround to spot the boundaries between JSON
        objects. Unfortunately, it has to match *something*, so at least
        one character must be removed and replaced.
        z	}\s*(?={)N})rerp   r>   )rq   stringsrs   rr   s       r   ro   zCLI._detect_json_strings   sS     ((<2 4;3B<@@@ 	GBK( As   Ar   )__name__
__module____qualname__r   r   r   r   r   r   r
   rE   r   rU   r   rb   staticmethodro    r   r   r   r      sU    F


=#/'b/	&M  r   r   c                  4    t               j                          y r   )r   r   r}   r   r   mainr      s    EIIKr   __main__genson)rC   rV   rw   re   rn   r   r   r   r   ry   r   r}   r   r   <module>r      sE     
 	  (K K\ zM r   