3
W­E^  ã               @   sn   d Z ddlZddlZddlmZ ddlmZmZ ddlZddl	m
Z
 ddlm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)ÚopenÚ	getreader)ÚPY2)ÚSQLParseErrorc           
   C   s’  dddg} t jdddd�}|jdƒ |jd	d
dddd� |jddtjd� |jdƒ}|jdddd| djdjdd„ | D ƒƒƒd� |jdddd| djdjd d„ | D ƒƒƒd� |jd!d"d#d$d%d&gd'd� |jd(d)d*d+d,d-� |jd.d/d0d*d+d1d-� |jd2d3d4td5d6� |jd7d8d*d+d9d-� |jd:d;d*d+d<d-� |jd=d>d*d+d?d@� |jdAdBd*d+dCd@� |jdDdEdFtdGd6� |jdHdId+t	dJd6� |jdKdLdMdNdO� |S )PNÚupperÚlowerÚ
capitalizeZ	sqlformatzEFormat FILE according to OPTIONS. Use "-" as FILE to read from stdin.z%(prog)s  [OPTIONS] FILE, ...)ÚprogÚdescriptionÚusageÚfilenamez-oz	--outfileÚoutfileÚFILEz)write output to FILE (defaults to stdout))ÚdestÚmetavarÚhelpz	--versionÚversion)Úactionr   zFormatting Optionsz-kz
--keywordsZCHOICEZkeyword_casez-change case of keywords, CHOICE is one of {0}z, c             s   s   | ]}d j |ƒV  qdS )z"{0}"N)Úformat)Ú.0Úx© r   ú0/tmp/pip-build-78s3knkw/sqlparse/sqlparse/cli.pyú	<genexpr>B   s    z create_parser.<locals>.<genexpr>)r   r   Úchoicesr   z-iz--identifiersZidentifier_casez0change case of identifiers, CHOICE is one of {0}c             s   s   | ]}d j |ƒV  qdS )z"{0}"N)r   )r   r   r   r   r   r   J   s    z-lz
--languageÚLANGZoutput_formatÚpythonZphpzJoutput a snippet in programming language LANG, choices are "python", "php"z--strip-commentsÚstrip_commentsÚ
store_trueFzremove comments)r   r   Údefaultr   z-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
--encodingÚencodingzutf-8z*Specify the input encoding (default utf-8))r   r    r   )
ÚargparseÚArgumentParserÚadd_argumentÚsqlparseÚ__version__Úadd_argument_groupr   ÚjoinÚintÚbool)Z_CASE_CHOICESÚparserÚgroupr   r   r   Úcreate_parser#   s¼    



r/   c             C   s   t jjdj| ƒƒ dS )z5Print msg and optionally exit with return code exit_.z[ERROR] {0}
é   )ÚsysÚstderrÚwriter   )Úmsgr   r   r   Ú_errorš   s    r5   c       
   -   C   s”  t ƒ }|j| ƒ} | jdkr`tr4t| jƒtjƒjƒ }q¾t	tjj
| jd�}z|jƒ }W d |jƒ  X n^y.t| jd| jƒ�}dj|jƒ ƒ}W d Q R X W n. tk
r¼ } ztdj| j|ƒƒS d }~X nX d}| j�ryt| jd| jƒ}d}W n0 tk
�r } ztd	j| j|ƒƒS d }~X nX ntj}t| ƒ}ytjj|ƒ}W n, tk
�r` } ztd
j|ƒƒS d }~X nX tj|f|Ž}	|j|	ƒ |jƒ  |�r�|jƒ  dS )Nú-)r#   ÚrÚ zFailed to read {0}: {1}FÚwTzFailed to open {0}: {1}zInvalid options: {0}r   )r/   Ú
parse_argsr   r   r   r#   r1   ÚstdinÚreadr   ÚbufferÚdetachr   r*   Ú	readlinesÚIOErrorr5   r   r   ÚstdoutÚvarsr'   Ú	formatterZvalidate_optionsr   r3   ÚflushÚclose)
Úargsr-   ÚdataÚwrapperÚfÚeZclose_streamÚstreamZformatter_optsÚsr   r   r   Úmain    sD    

 
rM   )N)Ú__doc__r$   r1   Úior   Úcodecsr   r   r'   Zsqlparse.compatr   Zsqlparse.exceptionsr   r/   r5   rM   r   r   r   r   Ú<module>   s   w