34
34
self.options = options
35
35
self.logger = logging.getLogger(app_name+"."+module_name)
36
36
self.loadConfigData()
38
38
def loadConfigData(self):
41
41
self.config = NullConfig()
43
43
if self.options.config != None:
44
44
# load the config based on options passed in from the main app
45
45
configfilepath = self.options.config
47
47
# load plugin config from home directory of the user
48
48
configfilepath = os.path.join(os.path.expanduser('~'), ".config/"+app_name+"/"+module_name+".config")
50
50
if os.path.exists(configfilepath):
52
52
self.logger.info("Loading config settings from \"%s\""%configfilepath)
54
54
for line in fileinput.input(os.path.expanduser(configfilepath)):
55
55
line = line.strip()
56
56
if len(line) > 0 and line[0:1] != "#": # ignore commented lines or empty ones
58
58
name = line.split("=")[0].strip().upper() # config setting name on the left of =
59
59
value = line.split("=")[1].split("#")[0].strip() # config value on the right of = (minus any trailing comments)
62
62
if name == "HEADERTEMPLATE":
63
self.config.HEADERTEMPLATE = self.getTypedValue(value, "string")
63
self.config.HEADERTEMPLATE = getTypedValue(value, "string")
64
64
elif name == "TEMPLATE":
65
self.config.TEMPLATE = self.getTypedValue(value, "string")
65
self.config.TEMPLATE = getTypedValue(value, "string")
67
67
self.logger.error("Unknown option in config file: " + name)
69
69
self.logger.info("Config data file %s not found, using defaults and setting up config file for next time" % configfilepath)
71
71
userconfigpath = os.path.join(os.path.expanduser('~'), ".config/"+app_name+"/")
72
72
configsource = os.path.join(app_path, "config/"+module_name+".config")
74
74
if os.path.exists(userconfigpath) == False:
75
75
os.makedirs(userconfigpath)
77
77
shutil.copy(configsource, configfilepath)
79
79
except Exception, e:
80
80
self.logger.error(e.__str__()+"\n"+traceback.format_exc())
82
def getTypedValue(self, value, expectedtype):
85
if len(value.strip(" ")) == 0:
88
elif value.lower() == "true":
89
if expectedtype == "boolean":
92
self.logger.error("Expected type was '%s', but the value '%s' was given"%(expectedtype, value))
94
elif value.lower() == "false":
95
if expectedtype == "boolean":
98
self.logger.error("Expected type was '%s', but the value '%s' was given"%(expectedtype, value))
100
elif self.isNumeric(value) == True:
101
if expectedtype == "integer":
104
self.logger.error("Expected type was '%s', but the value '%s' was given"%(expectedtype, value))
109
except (TypeError, ValueError):
110
self.logger.error("Cannot convert '%s' to expected type of '%s'"%(value,expectedtype))
113
82
def getOutput(self):
115
84
if self.options.noheader == True:
154
123
template = inputfile.read()
156
125
inputfile.close()
158
127
output = headertemplate + "\n" + template
160
129
return output.encode("utf-8")
162
def getHTMLText(self,text):
165
for char in text: #html:
167
htmlentities.append(char)
169
htmlentities.append('&%s;' % codepoint2name[ord(char)])
170
html = "".join(htmlentities)
172
html = html.replace("\n","<br>\n") # switch out new line for html breaks
177
def getCleanText(self,html):
180
text = text.replace("\n","") # remove new lines from html
181
text = text.replace("'","'") # workaround for shitty xml codes not compliant with html
182
text = text.replace("<br>","\n") # switch out html breaks for new line
183
text = re.sub('<(.|\n)+?>','',text) # remove any html tags
184
text = re.sub('&(%s);' % '|'.join(name2codepoint), lambda m: chr(name2codepoint[m.group(1)]), text)
189
def isNumeric(self,value):
196
131
def getHTML(options):
197
132
output = Output(options)
198
133
html = output.getOutput()
202
137
# to enable testing in isolation
203
138
if __name__ == "__main__":
205
140
parser = OptionParser()
206
parser.add_option("--noheader", dest="noheader", default=False, action="store_true", help=u"Turn off header output. This will override any header template setting to be nothing")
141
parser.add_option("--noheader", dest="noheader", default=False, action="store_true", help=u"Turn off header output. This will override any header template setting to be nothing")
207
142
parser.add_option("--headertemplate", dest="headertemplate", type="string", metavar="FILE", help=u"Override the header template for the plugin, default or config based template ignored.")
208
143
parser.add_option("--template", dest="template", type="string", metavar="FILE", help=u"Override the template for the plugin, default or config based template ignored.")
209
144
parser.add_option("--verbose", dest="verbose", default=False, action="store_true", help=u"Outputs verbose info to the terminal")
210
145
parser.add_option("--version", dest="version", default=False, action="store_true", help=u"Displays the version of the script.")
211
parser.add_option("--logfile", dest="logfile", type="string", metavar="FILE", help=u"If a filepath is set, the script logs to the filepath.")
146
parser.add_option("--logfile", dest="logfile", type="string", metavar="FILE", help=u"If a filepath is set, the script logs to the filepath.")
213
148
(options, args) = parser.parse_args()
215
150
output = Output(options)
216
151
html = output.getOutput()
b'\\ No newline at end of file'