Created
July 4, 2012 08:04
-
-
Save husio/3046012 to your computer and use it in GitHub Desktop.
Pretty print of any SQL
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python | |
| import os | |
| import sys | |
| import urllib | |
| import urllib2 | |
| url = 'http://sqlformat.appspot.com/format/' | |
| def main(): | |
| if len(sys.argv) == 1: | |
| data = sys.stdin.read() | |
| else: | |
| if os.path.isfile(sys.argv[1]): | |
| with open(sys.argv[1]) as fd: | |
| data = fd.read() | |
| else: | |
| data = " ".join(sys.argv[1:]) | |
| payload = ( | |
| ('data', data), | |
| ('format', 'text'), | |
| ('keyword_case', 'upper'), | |
| ('reindent', True), | |
| ('n_indents', 2), | |
| ) | |
| response = urllib2.urlopen(url, urllib.urlencode(payload)) | |
| sql = response.read().strip() | |
| try: | |
| from pygments import highlight | |
| from pygments.lexers import SqlLexer | |
| from pygments.formatters import TerminalFormatter | |
| sql = highlight(sql, SqlLexer(), TerminalFormatter()) | |
| except ImportError: | |
| pass | |
| print sql | |
| if __name__ == '__main__': | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment