-
Notifications
You must be signed in to change notification settings - Fork 0
/
getTemplate.cgi
executable file
·169 lines (134 loc) · 5.29 KB
/
getTemplate.cgi
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#!./python
# Program: getTemplate.cgi
# Purpose: Propagates global template files; is the web-based access point for
# template retrieval. Also serves as the translation layer for the raw
# '.shell' template files by converting both config parameters and any
# '.element' files in the /template/elements/ directory
#
# For the requested template, insert page elements as requested (in the shell)
# and translate any config param names in the shells with their respective
# values in the config object
import sys
import os
if "LIB_PY_MISC" not in os.environ:
sys.path.insert (0, '/usr/local/mgi/live/lib/python/')
else:
sys.path.insert (0, os.environ["LIB_PY_MISC"])
import Configuration
config = Configuration.get_Configuration ("webshare.config")
commonConfig = Configuration.get_Configuration (config['COMMON_CONFIG'])
config.merge(commonConfig)
# Attempt to import the module that will ask Python to ignore any
# deprecation errors. If it fails, ignore it and go forward.
try:
import ignoreDeprecation
except:
pass
import CGI
import string
import re
import db
class getTemplateCGI (CGI.CGI):
# CLASS VARIABLES
pageElements = {} # holds element file -> file content mapping
re_parm = re.compile ('\${\([^}]+.element\)}') # to find 'foo.element'
def resolve (self,
file, # string; parameter name
steps = 10 # integer; maximum recursive levels allowed
):
# Purpose: return the value of the element file named 'file',
# with any embedded parameter references ( ${PARM} )
# having been resolved if we can do it in under the
# given number of 'steps'
# Returns: string
# Assumes: nothing
# Effects: nothing
# Throws: raises a KeyError if 'file' is not one of the
# parameter names. raises 'error' if 'steps' reaches
# zero.
# Notes: Mainly, we use 'steps' to prevent an infinite loop if
# we have two parameters that reference each other, like
# A "My ${B} value"
# B "My ${A} value"
# or one references itself like
# A "My ${A} value"
if steps == 0:
raise error('Could not resolve parameter.')
s = getTemplateCGI.pageElements[file]
while getTemplateCGI.re_parm.search (s) != None:
m = getTemplateCGI.re_parm.search (s)
start, stop = m.regs[0]
parm = getTemplateCGI.re_parm.group(1)
s = s[:start] + self.resolve(parm, steps-1) + s[stop:]
return s
def getDbDate (self):
# Purpose: return value of latest db update
# Returns: string
# Assumes: nothing
# Effects: nothing
# Throws: db module may throw DB exceptions
# pull the MGI:ID and geneSymbol from the database
db.useOneConnection(1)
db.set_sqlUser(config["DB_USER"])
db.set_sqlPassword(config["DB_PASSWORD"])
db.set_sqlServer(config["DB_SERVER"])
db.set_sqlDatabase(config["DB_DATABASE_FE"])
dateSql = """
SELECT value
FROM database_info
WHERE name = 'built from mgd database date'
"""
# Excecute queries
results = db.sql(dateSql, 'auto')
db.useOneConnection(0)
dbDateInfo = results[0]['value']
yyyy = dbDateInfo[0:4]
mm = dbDateInfo[5:7]
dd = dbDateInfo[8:10]
return mm + '/' + dd + '/' + yyyy
def main (self):
requestedTemplate = ''
templateShell = ''
shellContents = ''
# gather form parameters
self.parms = self.get_parms()
# gather date of most recent DB load
config['dbDate'] = self.getDbDate()
# get the requested template parameter
if 'template' in self.parms:
requestedTemplate = self.parms['template']
templateFile = config["INSTALL_PATH"] + "template/" + requestedTemplate + ".shell"
# if the file exists, read it in
if os.path.exists(templateFile):
fileObject = open(templateFile, mode='r')
for line in fileObject:
shellContents = shellContents + line
fileObject.close()
#read in the possible page elements into a class var dictionary
elementDirListing = os.listdir(config["INSTALL_PATH"] + "template/elements/")
for element in elementDirListing:
if element[-8:] == ".element":
fileContent = ''
file = open(config["INSTALL_PATH"] + "template/elements/" + element, mode='r')
for line in file:
fileContent = fileContent + line
file.close()
getTemplateCGI.pageElements[element] = fileContent
# empty google analytics element if this isn't a public install
if (config["ADD_GA4_TAG"] != "Yes"):
getTemplateCGI.pageElements["GA4.element"] = ""
#for each page element, update the shell files
for file in list(getTemplateCGI.pageElements.keys()):
searchKey = "${" + file + "}"
replaceValue = self.resolve(file)
shellContents = shellContents.replace(searchKey, replaceValue)
#for each config parameter, update the shell files
for key in list(config.keys()):
searchKey = "${" + key + "}"
replaceValue = config[key]
shellContents = shellContents.replace(searchKey, replaceValue)
#return translated string representation of file
print(shellContents)
if __name__ == '__main__':
myCgi = getTemplateCGI()
myCgi.go()