42
42
HEADERTEMPLATE = None
48
48
def __init__(self, options):
49
49
self.options = options
50
50
self.logger = logging.getLogger(app_name+"."+module_name)
51
51
self.loadConfigData()
53
53
def loadConfigData(self):
56
56
self.config = TomboyConfig()
58
58
if self.options.config != None:
59
59
# load the config based on options passed in from the main app
60
60
configfilepath = self.options.config
62
62
# load plugin config from home directory of the user
63
63
configfilepath = os.path.join(os.path.expanduser('~'), ".config/"+app_name+"/"+module_name+".config")
65
65
if os.path.exists(configfilepath):
67
67
self.logger.info("Loading config settings from \"%s\""%configfilepath)
69
69
for line in fileinput.input(os.path.expanduser(configfilepath)):
70
70
line = line.strip()
71
71
if len(line) > 0 and line[0:1] != "#": # ignore commented lines or empty ones
73
73
name = line.split("=")[0].strip().upper() # config setting name on the left of =
74
74
value = line.split("=")[1].split("#")[0].strip() # config value on the right of = (minus any trailing comments)
77
77
if name == "HEADERTEMPLATE":
78
self.config.HEADERTEMPLATE = self.getTypedValue(value, "string")
78
self.config.HEADERTEMPLATE = getTypedValue(value, "string")
79
79
elif name == "TEMPLATE":
80
self.config.TEMPLATE = self.getTypedValue(value, "string")
80
self.config.TEMPLATE = getTypedValue(value, "string")
81
81
elif name == "TAGSLIST":
82
self.config.TAGSLIST = self.getTypedValue(value, "string")
82
self.config.TAGSLIST = getTypedValue(value, "string")
84
84
self.logger.error("Unknown option in config file: " + name)
86
86
self.logger.info("Config data file %s not found, using defaults and setting up config file for next time" % configfilepath)
88
88
userconfigpath = os.path.join(os.path.expanduser('~'), ".config/"+app_name+"/")
89
89
configsource = os.path.join(app_path, "config/"+module_name+".config")
91
91
if os.path.exists(userconfigpath) == False:
92
92
os.makedirs(userconfigpath)
94
94
shutil.copy(configsource, configfilepath)
96
96
except Exception, e:
97
97
self.logger.error(e.__str__()+"\n"+traceback.format_exc())
99
def getTypedValue(self, value, expectedtype):
102
if len(value.strip(" ")) == 0:
105
elif value.lower() == "true":
106
if expectedtype == "boolean":
109
self.logger.error("Expected type was '%s', but the value '%s' was given"%(expectedtype, value))
111
elif value.lower() == "false":
112
if expectedtype == "boolean":
115
self.logger.error("Expected type was '%s', but the value '%s' was given"%(expectedtype, value))
117
elif self.isNumeric(value) == True:
118
if expectedtype == "integer":
121
self.logger.error("Expected type was '%s', but the value '%s' was given"%(expectedtype, value))
126
except (TypeError, ValueError):
127
self.logger.error("Cannot convert '%s' to expected type of '%s'"%(value,expectedtype))
130
99
def testDBus(self, bus, interface):
131
100
obj = bus.get_object('org.freedesktop.DBus', '/org/freedesktop/DBus')
132
101
dbus_iface = dbus.Interface(obj, 'org.freedesktop.DBus')
134
103
return interface in avail
136
105
def getTemplateOutput(self, template, title, content):
140
109
output = template
142
111
output = output.replace("[title]",title)
143
112
output = output.replace("[content]",content)
145
114
return output.encode("utf-8")
147
116
except Exception,e:
148
117
self.logger.error(e.__str__()+"\n"+traceback.format_exc())
151
120
def getOutput(self):
153
122
if self.options.noheader == True:
194
163
inputfile.close()
196
165
if DBUS_AVAIL == True:
200
169
bus = dbus.SessionBus()
202
171
if self.testDBus(bus, 'org.gnome.Tomboy'):
204
173
self.logger.info("Setting up dbus interface")
206
175
remote_object = bus.get_object("org.gnome.Tomboy","/org/gnome/Tomboy/RemoteControl")
207
176
iface = dbus.Interface(remote_object, "org.gnome.Tomboy.RemoteControl")
209
178
self.logger.info("Calling dbus interface for Tomboy Notes data")
211
180
# either get specific notes by tag or all notes
212
181
if self.config.TAGSLIST != None:
218
187
notes.append(notesfortag)
220
189
notes = iface.ListAllNotes()
222
191
output = headertemplate
224
193
for note in notes:
226
195
# get notes text and convert to html format
227
title = self.getHTMLText(iface.GetNoteTitle(note))
196
title = getHTMLText(iface.GetNoteTitle(note))
228
197
content = self.getHTMLFromXMLContent(iface.GetNoteContentsXml(note),title)
230
199
# output note data using the template
231
200
output = output + self.getTemplateOutput(template, title, content)
264
233
html = re.sub("\n{2,}","\n",html) # remove excess new lines
265
234
html = html.replace("\n","<br>\n") # switch out new line for html breaks
268
def getHTMLText(self,text):
271
for char in text: #html:
273
htmlentities.append(char)
275
htmlentities.append('&%s;' % codepoint2name[ord(char)])
276
html = "".join(htmlentities)
278
html = html.replace("\n","<br>\n") # switch out new line for html breaks
283
def getCleanText(self,html):
286
text = text.replace("\n","") # remove new lines from html
287
text = text.replace("'","'") # workaround for shitty xml codes not compliant with html
288
text = text.replace("<br>","\n") # switch out html breaks for new line
289
text = re.sub('<(.|\n)+?>','',text) # remove any html tags
290
text = re.sub('&(%s);' % '|'.join(name2codepoint), lambda m: chr(name2codepoint[m.group(1)]), text)
295
def isNumeric(self,value):
302
237
def getHTML(options):
303
238
output = Output(options)
308
243
# to enable testing in isolation
309
244
if __name__ == "__main__":
311
246
parser = OptionParser()
312
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")
247
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")
313
248
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.")
314
249
parser.add_option("--template", dest="template", type="string", metavar="FILE", help=u"Override the template for the plugin, default or config based template ignored.")
315
250
parser.add_option("--verbose", dest="verbose", default=False, action="store_true", help=u"Outputs verbose info to the terminal")
316
251
parser.add_option("--version", dest="version", default=False, action="store_true", help=u"Displays the version of the script.")
317
parser.add_option("--logfile", dest="logfile", type="string", metavar="FILE", help=u"If a filepath is set, the script logs to the filepath.")
252
parser.add_option("--logfile", dest="logfile", type="string", metavar="FILE", help=u"If a filepath is set, the script logs to the filepath.")
319
254
(options, args) = parser.parse_args()
321
256
output = Output(options)
322
257
html = output.getOutput()