U
    5i                     @   sZ   d Z ddlZddlZddlmZ ddlZddlmZ dd Zdd Z	d	d
 Z
dddZdS )ae  Module that contains the command line app.

Why does this file exist, and why not put this in __main__?
  You might be tempted to import things from __main__ later, but that will
  cause problems: the code will get executed twice:
  - When you run `python -m sqlparse` python will execute
    ``__main__.py`` as a script. That means there won't be any
    ``sqlparse.__main__`` in ``sys.modules``.
  - When you import __main__ it will get executed again (as a module) because
    there's no ``sqlparse.__main__`` in ``sys.modules``.
  Also see (1) from http://click.pocoo.org/5/setuptools/#setuptools-integration
    N)TextIOWrapper)SQLParseErrorc                  C   s  dddg} t jdddd}|jdd	d
d |jdddddd |jdddddd |jddtjd |d}|jdddd| d d!d"d# | D d$ |jd%d&dd'| d(d!d)d# | D d$ |jd*d+d,d-d.d/gd0d$ |jd1d2ddd3d |jd4d5d6ddd7d |jd8d9d:td;d< |jd=d>ddd?d |jd@dAdddBd |jdCdDdddEdF |jdGdHdddIdF |jdJdKdLtdMd< |jdNdOdt	dPd< |jdQdRdt	dSd< |jdTdUdVdWdX |S )YNupperlower
capitalizeZ	sqlformatzEFormat FILE according to OPTIONS. Use "-" as FILE to read from stdin.z"%(prog)s [OPTIONS] FILE [FILE ...])progdescriptionusagefilename+z%file(s) to format (use "-" for stdin))nargshelpz-oz	--outfileoutfileFILEz)write output to FILE (defaults to stdout))destmetavarr   z
--in-placeinplace
store_trueFz0format files in-place (overwrite existing files))r   actiondefaultr   z	--versionversion)r   r   zFormatting Optionsz-kz
--keywordsZCHOICEZkeyword_casez,change case of keywords, CHOICE is one of {}z, c                 s   s   | ]}d | d V  qdS "N .0xr   r   0/tmp/pip-unpacked-wheel-uuk014bl/sqlparse/cli.py	<genexpr>G   s     z create_parser.<locals>.<genexpr>)r   r   choicesr   z-iz--identifiersZidentifier_casez/change case of identifiers, CHOICE is one of {}c                 s   s   | ]}d | d V  qdS r   r   r   r   r   r   r   O   s     z-lz
--languageLANGZoutput_formatpythonZphpzJoutput a snippet in programming language LANG, choices are "python", "php"z--strip-commentsstrip_commentszremove commentsz-rz
--reindentZreindentzreindent statementsz--indent_widthZindent_width   z(indentation width (defaults to 2 spaces))r   r   typer   z--indent_after_firstZindent_after_firstz2indent after first line of statement (e.g. SELECT)z--indent_columnsZindent_columnsz<indent all columns by indent_width instead of keyword lengthz-az--reindent_alignedz%reindent statements to aligned format)r   r   r   z-sz--use_space_around_operatorsz*place spaces around mathematical operatorsz--wrap_afterZ
wrap_afterr   z*Column after which lists should be wrappedz--comma_firstZcomma_firstz-Insert linebreak before comma (default False)z	--compactcompactz2Try to produce more compact output (default False)z
--encodingencodingzutf-8z*Specify the input encoding (default utf-8))r   r   r   )
argparseArgumentParseradd_argumentsqlparse__version__add_argument_groupformatjoinintbool)Z_CASE_CHOICESparsergroupr   r   r   create_parser   s   
 
      r3   c                 C   s   t jd|  d dS )z5Print msg and optionally exit with return code exit_.z[ERROR] 
   )sysstderrwrite)msgr   r   r   _error   s    r:   c           
   
   C   s  | dkr|j rtdS | dkrJttjj|jd}z| }W 5 |  X nhz,t	| |jd}d
| }W 5 Q R X W n: tk
r } ztd|  d|  W Y S d}~X Y nX d}|j rzt	| d	|jd}d
}W n< tk
r } ztd|  d|  W Y S d}~X Y nX nj|jrzzt	|jd	|jd}d
}W n> tk
rv } ztd|j d|  W Y S d}~X Y nX ntj}t|}ztj|}W n6 tk
r } ztd|  W Y S d}~X Y nX tj|f|}	||	 |  |r|  dS )zdProcess a single file with the given formatting options.

    Returns 0 on success, 1 on error.
    -z Cannot use --in-place with stdin)r&    zFailed to read z: NFwTzFailed to open zInvalid options: r   )r   r:   r   r6   stdinbufferr&   detachreadopenr.   	readlinesOSErrorr   stdoutvarsr*   	formatterZvalidate_optionsr   r-   r8   flushclose)
r
   argswrapperdatafeZclose_streamstreamZformatter_optssr   r   r   _process_file   sJ    *,.$
rQ   c                 C   sd   t  }|| } t| jdkr:| jr,tdS | js:tdS d}| jD ]}t|| }|dkrD|}qD|S )Nr5   z+Cannot use -o/--outfile with multiple filesz&Multiple files require --in-place flagr   )r3   
parse_argslenr
   r   r:   r   rQ   )rJ   r1   Z	exit_coder
   resultr   r   r   main   s    


rU   )N)__doc__r'   r6   ior   r*   Zsqlparse.exceptionsr   r3   r:   rQ   rU   r   r   r   r   <module>   s    	7