|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +# Copyright (C) 2008 Andi Albrecht, [email protected] |
| 4 | +# |
| 5 | +# This module is part of python-sqlparse and is released under |
| 6 | +# the BSD License: http://www.opensource.org/licenses/bsd-license.php. |
| 7 | + |
| 8 | +import optparse |
| 9 | +import os |
| 10 | +import sys |
| 11 | + |
| 12 | +import sqlparse |
| 13 | + |
| 14 | + |
| 15 | +_CASE_CHOICES = ['upper', 'lower', 'capitalize'] |
| 16 | + |
| 17 | + |
| 18 | +parser = optparse.OptionParser(usage='%prog [OPTIONS] FILE, ...', |
| 19 | + version='%%prog %s' % sqlparse.__version__) |
| 20 | +parser.set_description(('Format FILE according to OPTIONS. Use "-" as FILE ' |
| 21 | + 'to read from stdin.')) |
| 22 | +parser.add_option('-v', '--verbose', dest='verbose', action='store_true') |
| 23 | +parser.add_option('-o', '--outfile', dest='outfile', metavar='FILE', |
| 24 | + help='write output to FILE (defaults to stdout)') |
| 25 | +group = parser.add_option_group('Formatting Options') |
| 26 | +group.add_option('-k', '--keywords', metavar='CHOICE', |
| 27 | + dest='keyword_case', choices=_CASE_CHOICES, |
| 28 | + help=('change case of keywords, CHOICE is one of %s' |
| 29 | + % ', '.join('"%s"' % x for x in _CASE_CHOICES))) |
| 30 | +group.add_option('-i', '--identifiers', metavar='CHOICE', |
| 31 | + dest='identifier_case', choices=_CASE_CHOICES, |
| 32 | + help=('change case of identifiers, CHOICE is one of %s' |
| 33 | + % ', '.join('"%s"' % x for x in _CASE_CHOICES))) |
| 34 | +group.add_option('-l', '--language', metavar='LANG', |
| 35 | + dest='output_format', choices=['python', 'php'], |
| 36 | + help=('output a snippet in programming language LANG, ' |
| 37 | + 'choices are "python", "php"')) |
| 38 | +group.add_option('--strip-comments', dest='strip_comments', |
| 39 | + action='store_true', default=False, |
| 40 | + help='remove comments') |
| 41 | +group.add_option('-r', '--reindent', dest='reindent', |
| 42 | + action='store_true', default=False, |
| 43 | + help='reindent statements') |
| 44 | +group.add_option('--indent_width', dest='indent_width', default=2, |
| 45 | + help='indentation width (defaults to 2 spaces)') |
| 46 | + |
| 47 | +_FORMATTING_GROUP = group |
| 48 | + |
| 49 | + |
| 50 | +def _error(msg, exit_=None): |
| 51 | + """Print msg and optionally exit with return code exit_.""" |
| 52 | + print >>sys.stderr, '[ERROR] %s' % msg |
| 53 | + if exit_ is not None: |
| 54 | + sys.exit(exit_) |
| 55 | + |
| 56 | + |
| 57 | +def _build_formatter_opts(options): |
| 58 | + """Convert command line options to dictionary.""" |
| 59 | + d = {} |
| 60 | + for option in _FORMATTING_GROUP.option_list: |
| 61 | + d[option.dest] = getattr(options, option.dest) |
| 62 | + return d |
| 63 | + |
| 64 | + |
| 65 | +def main(): |
| 66 | + options, args = parser.parse_args() |
| 67 | + if options.verbose: |
| 68 | + print >>sys.stderr, 'Verbose mode' |
| 69 | + |
| 70 | + if len(args) != 1: |
| 71 | + _error('No input data.') |
| 72 | + parser.print_usage() |
| 73 | + sys.exit(1) |
| 74 | + |
| 75 | + if '-' in args: # read from stdin |
| 76 | + data = sys.stdin.read() |
| 77 | + else: |
| 78 | + try: |
| 79 | + data = '\n'.join(open(args[0]).readlines()) |
| 80 | + except OSError, err: |
| 81 | + _error('Failed to read %s: %s' % (args[0], err), exit_=1) |
| 82 | + |
| 83 | + if options.outfile: |
| 84 | + try: |
| 85 | + stream = open(options.outfile, 'w') |
| 86 | + except OSError, err: |
| 87 | + _error('Failed to open %s: %s' % (options.outfile, err), exit_=1) |
| 88 | + else: |
| 89 | + stream = sys.stdout |
| 90 | + |
| 91 | + formatter_opts = _build_formatter_opts(options) |
| 92 | + try: |
| 93 | + formatter_opts = sqlparse.formatter.validate_options(formatter_opts) |
| 94 | + except sqlparse.SQLParseError, err: |
| 95 | + _error('Invalid options: %s' % err, exit_=1) |
| 96 | + |
| 97 | + stream.write(sqlparse.format(data, **formatter_opts).encode('utf-8', |
| 98 | + 'replace')) |
| 99 | + stream.flush() |
| 100 | + |
| 101 | + |
| 102 | +if __name__ == '__main__': |
| 103 | + main() |
0 commit comments