
v^                 @   s   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 d d	  } | j d
  | j d d d d d d d d | j d d d d t j | j d  } | j d d d d d d d |  d d j d j d d   |  D   | j d  d! d d d d" d |  d d# j d j d$ d   |  D   | j d% d& d d' d d( d d) d* g d d+ | j d, d d- d d. d/ d0 d d1 | j d2 d3 d d4 d d. d/ d0 d d5 | j d6 d d7 d/ d8 d9 t d d: | j d; d d< d d. d/ d0 d d= | j d> d d? d d. d/ d0 d d@ | j dA dB d d. d/ d0 d dC | j dD dE d d. d/ d0 d dF | j dG d dH d/ dI d9 t d dJ | j dK d dL d/ d0 d9 t	 d dM | j dN d dO d/ dP d dQ | S)RNupperlower
capitalizeprogZ	sqlformatdescriptionzEFormat FILE according to OPTIONS. Use "-" as FILE to read from stdin.usagez%(prog)s  [OPTIONS] FILE, ...filenamez-oz	--outfiledestoutfilemetavarFILEhelpz)write output to FILE (defaults to stdout)z	--versionactionversionzFormatting Optionsz-kz
--keywordsZCHOICEZkeyword_casechoicesz-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-8lau8j11/sqlparse/sqlparse/cli.py	<genexpr>B   s    z create_parser.<locals>.<genexpr>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_truedefaultFzremove commentsz-rz
--reindentZreindentzreindent statementsz--indent_widthZindent_width   typez(indentation width (defaults to 2 spaces)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 formatz-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))
argparseArgumentParseradd_argumentsqlparse__version__Z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    r4   c       
   -   C   s  t    } | j |   }  |  j d k r t rN t |  j  t j  j   } qt	 t j j
 d |  j } z | j   } Wd  | j   Xn| y; t |  j d |  j   } d j | j    } Wd  QRXWn> t k
 r} z t d j |  j |   SWYd  d  } ~ Xn Xd } |  j r{y" t |  j d |  j  } d } Wqt k
 rw} z t d	 j |  j |   SWYd  d  } ~ XqXn	 t j } t |   } y t j j |  } Wn8 t k
 r} z t d
 j |   SWYd  d  } ~ Xn Xt j | |  }	 | 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#   r0   stdinreadr   bufferdetachr   r)   	readlinesIOErrorr4   r   r   stdoutvarsr'   	formatterZvalidate_optionsr   r2   flushclose)
argsr,   datawrapperfeZclose_streamstreamZformatter_optssr   r   r   main   sD    	! )	
/	&

rL   )__doc__r$   r0   ior   codecsr   r   r'   Zsqlparse.compatr   Zsqlparse.exceptionsr   r.   r4   rL   r   r   r   r   <module>   s   w