# Path: format_sql/parser.py
# class InvalidSQL(Exception):
#     pass
# 
# Path: format_sql/shortcuts.py
# def format_sql(s, debug=False):
#     tokens = list(tokenize(s))
#     if debug:
#         print_non_data('Tokens: %s' % tokens)
#     parsed = list(parse(tokens))
#     if debug:
#         print_non_data('Statements: %s' % parsed)
#     styled = style(parsed)
#     if debug:
#         print_non_data('Output: %s' % styled)
#     return styled
# 
# Path: format_sql/util.py
# def print_data(msg):
#     print(msg)
# 
# def print_non_data(msg):
#     print(msg, file=sys.stderr)


import os
import re
import sys
from argparse import ArgumentParser
from glob import glob
from format_sql.parser import InvalidSQL
from format_sql.shortcuts import format_sql
from format_sql.util import print_data, print_non_data

def handle_py_file(filename, debug=False):
    with open(filename) as f:
        lines = f.read()

    for old_query, query, indent in get_statements(lines):
        if debug:
            print_non_data('Found query: %s' % query)

        try:
            fmt = format_sql(query, debug)
        except InvalidSQL as e:
            print_non_data(e)
            continue

        fs = []
        for line in fmt:
            s = '%s%s' % (indent, line)
            fs.append(s.rstrip())

        lines = lines.replace(old_query, '\n%s ' % '\n'.join(fs))
    return lines


def handle_sql_file(filename, debug=False):
    with open(filename) as f:
        lines = f.read()

    try:
        sql = format_sql(lines, debug)

# Path: format_sql/parser.py
# class InvalidSQL(Exception):
#     pass
# 
# Path: format_sql/shortcuts.py
# def format_sql(s, debug=False):
#     tokens = list(tokenize(s))
#     if debug:
#         print_non_data('Tokens: %s' % tokens)
#     parsed = list(parse(tokens))
#     if debug:
#         print_non_data('Statements: %s' % parsed)
#     styled = style(parsed)
#     if debug:
#         print_non_data('Output: %s' % styled)
#     return styled
# 
# Path: format_sql/util.py
# def print_data(msg):
#     print(msg)
# 
# def print_non_data(msg):
#     print(msg, file=sys.stderr)


# Path: format_sql/main.py
import os
import re
import sys
from argparse import ArgumentParser
from glob import glob
from format_sql.parser import InvalidSQL
from format_sql.shortcuts import format_sql
from format_sql.util import print_data, print_non_data


def handle_py_file(filename, debug=False):
    with open(filename) as f:
        lines = f.read()

    for old_query, query, indent in get_statements(lines):
        if debug:
            print_non_data('Found query: %s' % query)

        try:
            fmt = format_sql(query, debug)
        except InvalidSQL as e:
            print_non_data(e)
            continue

        fs = []
        for line in fmt:
            s = '%s%s' % (indent, line)
            fs.append(s.rstrip())

        lines = lines.replace(old_query, '\n%s ' % '\n'.join(fs))
    return lines


def handle_sql_file(filename, debug=False):
    with open(filename) as f:
        lines = f.read()

    try:
        sql = format_sql(lines, debug)

    except InvalidSQL as e: