B
    Ad                 @   s   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	d
 Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zedkre  dS )    N   )DocxTemplateTemplateErrortemplate_path	json_pathZoutput_filename	overwritequietc              C   s   t jdtttdd} | jttdd | jttdd | jttdd | jdtd	  d
t ddd | jdt	d	  d
t	 d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)parser r   B/var/www/html/venv/lib/python3.7/site-packages/docxtpl/__main__.pymake_arg_parser   s&    r   c          
   C   sX   yt |  }|S  tk
rR } z$|jdkr2tntdj| dW d d }~X Y nX d S )Nr   z Correct usage is:
{parser.usage})r   )vars
parse_args
SystemExitcodeRuntimeErrorr   )r   parsed_argser   r   r   get_args$   s    
r%   c             C   sp   | t krtj|o|dS | tkr<tj|o:|dS | tkrX|doVt||S | tt	gkrl|dkS d S )Nz.docxz.json)TF)
r   ospathisfileendswithr   r   check_exists_ask_overwriter   r   )arg_name	arg_valuer   r   r   r   is_argument_valid1   s    r-   c             C   s`   t j| rX|sXy$d|  }t| dkr.dS tW q\ tk
rT   td|  Y q\X ndS d S )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"   )r,   r   msgr   r   r   r*   >   s    r*   c             C   s^   | t  }y*x$|  D ]\}}t|||stqW W n& tk
rX   tdj||dY nX d S )Nz4The specified {arg_name} "{arg_value}" is not valid.)r+   r,   )r   itemsr-   AssertionErrorr"   r   )r#   r   r+   r,   r   r   r   validate_all_argsO   s    r6   c             C   sf   t | T}yt|}|S  tjk
rV } ztdj|| d tdW d d }~X Y nX W d Q R X d S )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$   r   r   r   get_json_data]   s    

r>   c             C   s*   yt | S  tk
r$   tdY nX d S )NzCould not create docx template.)r   r   r"   )r   r   r   r   make_docxtemplatej   s    r?   c             C   s0   y|  | | S  tk
r*   tdY nX d S )Nz0An error ocurred while trying to render the docx)renderr   r"   )docr=   r   r   r   render_docxq   s
    
rB   c          
   C   sn   y.|t  }| | |t s,tdj|d W n: tk
rh } ztdj|d tdW d d }~X Y nX d S )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   r;   r   r2   r"   )rA   r#   rC   r$   r   r   r   	save_filey   s    
rE   c           
   C   s   t  } t| }zyHt| ttj|t }ttj|t	 }t
||}t|| W n2 tk
r } ztd|   d S d }~X Y nX W d |t std X d S )NzError: zExiting program!)r   r%   r6   r>   r&   r'   abspathr   r?   r   rB   rE   r"   r;   __str__r   )r   r#   r=   rA   r$   r   r   r   main   s    
rH   __main__)r   r8   r&   templater   r   r   r   r   r   r   r   r%   r-   r*   r6   r>   r?   rB   rE   rH   __name__r   r   r   r   <module>   s(   