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 = ShellConfig()
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 getShellCommandOutput(self, shell_command):
115
84
self.logger.info("Running shell command '%s'"%shell_command)
117
86
proc = subprocess.Popen(shell_command,
119
88
stdout=subprocess.PIPE,
121
90
output = proc.communicate()[0].rstrip("\n")
123
output = self.getHTMLText(output)
92
output = getHTMLText(output)
219
188
return output.encode("utf-8")
221
def getHTMLText(self,text):
224
for char in text: #html:
226
htmlentities.append(char)
228
htmlentities.append('&%s;' % codepoint2name[ord(char)])
229
html = "".join(htmlentities)
231
html = html.replace("\n","<br>\n") # switch out new line for html breaks
236
def getCleanText(self,html):
239
text = text.replace("\n","") # remove new lines from html
240
text = text.replace("'","'") # workaround for shitty xml codes not compliant with html
241
text = text.replace("<br>","\n") # switch out html breaks for new line
242
text = re.sub('<(.|\n)+?>','',text) # remove any html tags
243
text = re.sub('&(%s);' % '|'.join(name2codepoint), lambda m: chr(name2codepoint[m.group(1)]), text)
248
def isNumeric(self,value):
255
190
def getHTML(options):
256
191
output = Output(options)
257
192
html = output.getOutput()
261
196
# to enable testing in isolation
262
197
if __name__ == "__main__":
264
199
parser = OptionParser()
265
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")
200
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")
266
201
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.")
267
202
parser.add_option("--template", dest="template", type="string", metavar="FILE", help=u"Override the template for the plugin, default or config based template ignored.")
268
203
parser.add_option("--verbose", dest="verbose", default=False, action="store_true", help=u"Outputs verbose info to the terminal")
269
204
parser.add_option("--version", dest="version", default=False, action="store_true", help=u"Displays the version of the script.")
270
parser.add_option("--logfile", dest="logfile", type="string", metavar="FILE", help=u"If a filepath is set, the script logs to the filepath.")
205
parser.add_option("--logfile", dest="logfile", type="string", metavar="FILE", help=u"If a filepath is set, the script logs to the filepath.")
272
207
(options, args) = parser.parse_args()
274
209
output = Output(options)
275
210
html = output.getOutput()