
    9j                         d dl Z d dlZd dlZddlmZmZ dZdZdZdZ	dZ
d	 Zd
 Zd Zd Zd Zd Zd Zd Zd Zd Zedk(  r e        yy)    N   )DocxTemplateTemplateErrortemplate_path	json_pathoutput_filename	overwritequietc                     t        j                  dj                  t        t        t
              d      } | j                  t        t        d       | j                  t        t        d       | j                  t
        t        d       | j                  dt        d	   z   d
t        z   dd       | j                  dt        d	   z   d
t        z   dd       | S )Nz)python -m docxtpl [-h] [-o] [-q] {} {} {}z9Make docx file from existing template docx and json data.)usagedescriptionz#The path to the template docx file.)typehelpz(The path to the json file with the data.z(The filename to save the generated docx.-r   z--
store_truezIIf output file already exists, overwrites without asking for confirmation)actionr   z#Do not display unnecessary messages)
argparseArgumentParserformatTEMPLATE_ARGJSON_ARG
OUTPUT_ARGadd_argumentstrOVERWRITE_ARG	QUIET_ARGparsers    \/var/www/html/eeg-assistent/abrechnung/venv/lib/python3.12/site-packages/docxtpl/__main__.pymake_arg_parserr       s    $$9@@(J
 P	F 3%J   s!K   #M   mA}X	   ily2	   M    c                     	 t        | j                               }|S # t        $ r5}|j                  dk(  rt        t	        dj                  |             d }~ww xY w)Nr   z Correct usage is:
{parser.usage}r   )vars
parse_args
SystemExitcodeRuntimeErrorr   )r   parsed_argses      r   get_argsr*   -   sa    6,,./  66Q;3::&:I 	s    	A0AAc                 d   | t         k(  r2t        j                  j                  |      xr |j	                  d      S | t
        k(  r2t        j                  j                  |      xr |j	                  d      S | t        k(  r|j	                  d      xr t        ||      S | t        t        fv r|dv S y )Nz.docxz.json)TF)
r   ospathisfileendswithr   r   check_exists_ask_overwriter   r   )arg_name	arg_valuer	   s      r   is_argument_validr3   <   s    <ww~~i(HY-?-?-HH	X	ww~~i(HY-?-?-HH	Z	!!'* 
/Iy0
 	
 
mY/	/M)) 
0r!   c                     t         j                  j                  |       r+|s)	 d| z  }t        |      j	                         dk(  ryt
        y# t
        $ r t        d| z        w xY w)NzLFile %s already exists, would you like to overwrite the existing file? (y/n)yTz7File %s already exists, please choose a different name.)r,   r-   existsinputlowerOSErrorr'   )r2   r	   msgs      r   r0   r0   J   sw     
ww~~i 	#$  Sz!S(   	IIU 	s   !A A A$c                     | t            }	 | j                         D ]  \  }}t        |||      rt         y # t        $ r t	        dj                              w xY w)Nz4The specified {arg_name} "{arg_value}" is not valid.)r1   r2   )r   itemsr3   AssertionErrorr'   r   )r(   r	   r1   r2   s       r   validate_all_argsr>   `   sw    M*I	
#.#4#4#6 	%Hi$Xy)D$$	%  
BII!Y J 
 	

s   #7 7 &Ac                     t        |       5 }	 t        j                  |      }|cd d d        S # t        j                  $ r,}t	        dj                  ||              t        d      d }~ww xY w# 1 sw Y   y xY w)Nz^There was an error on line {e.lineno}, column {e.colno} while trying to parse file {json_path})r)   r   zFailed to get json data.)openjsonloadJSONDecodeErrorprintr   r'   )r   file	json_datar)   s       r   get_json_datarG   o   s{    	i 	;D	;		$I	; 	; ## 	;,,2FQ)F,L 9::	;		; 	;s&   A0.A-'A((A--A00A9c                 J    	 t        |       S # t        $ r t        d      w xY w)NzCould not create docx template.)r   r   r'   )r   s    r   make_docxtemplaterI   |   s-    >M** ><==>s   
 "c                 Z    	 | j                  |       | S # t        $ r t        d      w xY w)Nz0An error ocurred while trying to render the docx)renderr   r'   )docrF   s     r   render_docxrM      s6    O

9
 OMNNOs    *c                     	 |t            }| j                  |       |t           st        dj	                  |             y y # t
        $ r+}t        dj	                  |             t        d      d }~ww xY w)Nz:Document successfully generated and saved at {output_path})output_pathz/{e.strerror}. Could not save file {e.filename}.)r)   zFailed to save file.)r   saver   rD   r   r9   r'   )rL   r(   rO   r)   s       r   	save_filerQ      s~    3!*-9%LSS + T  &  3?FFFKL1223s   >A 	A6&A11A6c                      t               } t        |       }	 t        |       t        t        j
                  j                  |t                       }t        t        j
                  j                  |t                       }t        ||      }t        ||       	 |t           st        d       y y # t        $ r;}t        d|j                         z          Y d }~|t           st        d       y y d }~ww xY w# |t           st        d       w w xY w)NzError: zExiting program!)r    r*   r>   rG   r,   r-   abspathr   rI   r   rM   rQ   r'   rD   __str__r   )r   r(   rF   rL   r)   s        r   mainrU      s    F 6"K&+&!"''//+h2G"HI	L0I JK#y)#{#
 9%$% &	  i!))+%&9%$% &	 9%$% &s*   BB/ /	C38C.C6 .C33C6 6D__main__)r   rA   r,   templater   r   r   r   r   r   r   r    r*   r3   r0   r>   rG   rI   rM   rQ   rU   __name__ r!   r   <module>rZ      sq      	 1
	>*,

;>O3&( zF r!   