diff options
author | Victor Uriarte <victor.m.uriarte@intel.com> | 2016-06-05 14:55:09 -0700 |
---|---|---|
committer | Victor Uriarte <victor.m.uriarte@intel.com> | 2016-06-12 12:44:02 -0700 |
commit | db0f9247b30a905778288f4343888b0510cee093 (patch) | |
tree | b819c2ff5e65733223b00d47f140a3c931854e80 /sqlparse | |
parent | b04b5d326175da6267817faca6239bf02a6e5931 (diff) | |
download | sqlparse-db0f9247b30a905778288f4343888b0510cee093.tar.gz |
Update setup.py
switch scripts to console-script
Diffstat (limited to 'sqlparse')
-rw-r--r-- | sqlparse/__main__.py | 115 |
1 files changed, 115 insertions, 0 deletions
diff --git a/sqlparse/__main__.py b/sqlparse/__main__.py new file mode 100644 index 0000000..3f61064 --- /dev/null +++ b/sqlparse/__main__.py @@ -0,0 +1,115 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# +# Copyright (C) 2016 Andi Albrecht, albrecht.andi@gmail.com +# +# This module is part of python-sqlparse and is released under +# the BSD License: http://www.opensource.org/licenses/bsd-license.php + +import optparse +import sys + +import sqlparse +from sqlparse.exceptions import SQLParseError + +_CASE_CHOICES = ['upper', 'lower', 'capitalize'] + +parser = optparse.OptionParser(usage='%prog [OPTIONS] FILE, ...', + version='%%prog %s' % sqlparse.__version__) +parser.set_description(('Format FILE according to OPTIONS. Use "-" as FILE ' + 'to read from stdin.')) +parser.add_option('-v', '--verbose', dest='verbose', action='store_true') +parser.add_option('-o', '--outfile', dest='outfile', metavar='FILE', + help='write output to FILE (defaults to stdout)') +group = parser.add_option_group('Formatting Options') +group.add_option('-k', '--keywords', metavar='CHOICE', + dest='keyword_case', choices=_CASE_CHOICES, + help=('change case of keywords, CHOICE is one of %s' + % ', '.join('"%s"' % x for x in _CASE_CHOICES))) +group.add_option('-i', '--identifiers', metavar='CHOICE', + dest='identifier_case', choices=_CASE_CHOICES, + help=('change case of identifiers, CHOICE is one of %s' + % ', '.join('"%s"' % x for x in _CASE_CHOICES))) +group.add_option('-l', '--language', metavar='LANG', + dest='output_format', choices=['python', 'php'], + help=('output a snippet in programming language LANG, ' + 'choices are "python", "php"')) +group.add_option('--strip-comments', dest='strip_comments', + action='store_true', default=False, + help='remove comments') +group.add_option('-r', '--reindent', dest='reindent', + action='store_true', default=False, + help='reindent statements') +group.add_option('--indent_width', dest='indent_width', default=2, + help='indentation width (defaults to 2 spaces)') +group.add_option('-a', '--reindent_aligned', + action='store_true', default=False, + help='reindent statements to aligned format') +group.add_option('-s', '--use_space_around_operators', + action='store_true', default=False, + help='place spaces around mathematical operators') +group.add_option('--wrap_after', dest='wrap_after', default=0, + help='Column after which lists should be wrapped') + +_FORMATTING_GROUP = group + + +def _error(msg, exit_=None): + """Print msg and optionally exit with return code exit_.""" + sys.stderr.write('[ERROR] %s\n' % msg) + if exit_ is not None: + sys.exit(exit_) + + +def _build_formatter_opts(options): + """Convert command line options to dictionary.""" + d = {} + for option in _FORMATTING_GROUP.option_list: + d[option.dest] = getattr(options, option.dest) + return d + + +def main(): + options, args = parser.parse_args() + if options.verbose: + sys.stderr.write('Verbose mode\n') + + if len(args) != 1: + _error('No input data.') + parser.print_usage() + sys.exit(1) + + if '-' in args: # read from stdin + data = sys.stdin.read() + else: + try: + data = ''.join(open(args[0]).readlines()) + except OSError: + err = sys.exc_info()[1] # Python 2.5 compatibility + _error('Failed to read %s: %s' % (args[0], err), exit_=1) + + if options.outfile: + try: + stream = open(options.outfile, 'w') + except OSError: + err = sys.exc_info()[1] # Python 2.5 compatibility + _error('Failed to open %s: %s' % (options.outfile, err), exit_=1) + else: + stream = sys.stdout + + formatter_opts = _build_formatter_opts(options) + try: + formatter_opts = sqlparse.formatter.validate_options(formatter_opts) + except SQLParseError: + err = sys.exc_info()[1] # Python 2.5 compatibility + _error('Invalid options: %s' % err, exit_=1) + + s = sqlparse.format(data, **formatter_opts) + if sys.version_info < (3,): + s = s.encode('utf-8', 'replace') + stream.write(s) + stream.flush() + + +if __name__ == '__main__': + main() |