-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathquery_builder.py
executable file
·74 lines (55 loc) · 1.64 KB
/
query_builder.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
#! /usr/bin/env python
import sys
import datetime
import time
import os
import re
from ConfigParser import ConfigParser
# ***********************************
# Runtime Variables
# ***********************************
PARENT_DIR = os.path.dirname(os.path.realpath(__file__))
QUERY_DIR = os.path.join(PARENT_DIR, "templates")
if len(sys.argv) <= 1:
INI = "site_variables.ini"
elif len(sys.argv) <= 2:
INI = sys.argv[1]
else:
print "Please enter only one argument, a settings file."
sys.exit(1)
# ***********************************
# INI Settings Import
# ***********************************
CONFIG_FILE = os.path.join(PARENT_DIR, INI)
config = ConfigParser()
config.read(CONFIG_FILE)
VERSION = config.get('settings', 'VERSION')
OUTPUT_DIR = config.get('settings', 'OUTPUT_DIR')
TIME_STAMP = datetime.datetime.now().strftime('%Y-%m-%d_%Hh%Mm%Ss')
OUTPUT_DIR = os.path.join(PARENT_DIR, OUTPUT_DIR + '_' + TIME_STAMP)
print "Writing Queries to: " + OUTPUT_DIR
os.makedirs(OUTPUT_DIR)
if VERSION == 'moodle1':
QUERY_DIR = os.path.join(QUERY_DIR, 'moodle1')
else:
QUERY_DIR = os.path.join(QUERY_DIR, 'moodle2')
tokens = dict(config.items('site_variables'))
def find_replace(f):
template = open(f, 'r')
output = []
for line in template:
for token in tokens.keys():
value = tokens[token]
line = line.replace(token.upper(), value)
output.append(line)
template.close()
return output
def write_query(f, query):
o = open(os.path.join(OUTPUT_DIR, f), 'w')
for l in query:
o.write(l)
#MAIN
for root,dirs,files in os.walk(QUERY_DIR):
for f in files:
q = find_replace(os.path.join(root, f))
write_query(f, q)