forked from tjg-global/xlsxlib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxl.py
103 lines (87 loc) · 2.91 KB
/
xl.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#!python3
import os, sys
import sql
import sql2xlsxlib
import re
import traceback
import logging
logging.basicConfig(level=logging.INFO)
import sql
try:
raw_input
except NameError:
raw_input = input
DEFAULT_DATABASE = "SVR-DB-CAS-DEV/TDI"
DEFAULT_DATABASES = {
"TDI": "SVR09",
"STAGING": "SVR-DWSQLPRD",
"WALRUS": "SVR-DWSQLPRD",
}
def from_code(code):
return " ".join(code.split("_")).title()
def main(script_filepath, database=None, xls_filepath=None, *params):
if database is None:
database = raw_input("Database [%s]: " % DEFAULT_DATABASE) or DEFAULT_DATABASE
#
# Ignore .SQL (or any other extension)
#
head, tail = os.path.split(script_filepath)
basename, ext = os.path.splitext(os.path.basename(script_filepath))
path = head or os.getcwd()
if xls_filepath is None:
xls_filepath = os.path.join(basename + ".xlsx")
xls_filepath = os.path.abspath(xls_filepath)
query = open(script_filepath).read()
#
# If we haven't specified a database and the script has a USE
# statement, use the database in that USE statement (and then
# strip it out!).
#
driver, _, database_name, _, _ = sql.parse_dburi_ex(database)
if not database_name:
for database_name in re.findall("USE\s+(.*)", query):
database = (database.rstrip("/")) + "/" + database_name
break
query = re.sub(r"USE\s+.*", "", query)
query = re.sub(r"GO\s*", "", query)
vars = re.findall(r"%\((\w+)\)s", query)
values = {}
for index, var in enumerate(vars):
try:
values[var] = params[index]
except IndexError:
if var not in values:
values[var] = raw_input("%s: " % from_code(var))
if values:
for k, v in values.items():
logging.info("%s => %s" % (k, v))
query = query % values
logging.info("Writing to %s ...", xls_filepath)
if os.path.isfile(xls_filepath):
os.remove(xls_filepath)
db, driver = sql.database_ex(database)
if driver == "mssql":
with db.cursor() as q:
q.execute("SET ANSI_WARNINGS ON\nSET ANSI_NULLS ON\n")
try:
db.adoConn.CommandTimeout = 600
except AttributeError:
pass
for info in sql2xlsxlib.query2xlsx(
db=db, query=query, spreadsheet_filepath=xls_filepath, driver=driver
):
logging.info(info)
#
# Assume that if any parameters were passed, we're in
# "batch" mode and won't want to open the spreadsheet.
#
if not params:
os.startfile(xls_filepath)
if __name__ == "__main__":
#
# If no extra params are supplied, dump out a useful help string
#
if len(sys.argv) == 1:
print("%s sql_filepath dburi [xls_filepath [params...]]" % sys.argv[0])
else:
main(*sys.argv[1:])