forked from baumartig/paperboy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
xml_settings_handler.py
99 lines (79 loc) · 3.24 KB
/
xml_settings_handler.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
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
import xml.sax, xml.sax.handler
from settings import Settings
import xml.etree.ElementTree as ET
import os.path
settingsPath = "data/settings.xml"
class SettingsHandler(xml.sax.handler.ContentHandler):
def __init__(self):
self.settings = Settings()
self.buffer = ""
self.attrBuffer = {}
self.inMailSettings = False
self.inSmtpServer = False
def startElement(self, name, attributes):
if name == "mail-settings": self.inMailSettings = True
if name == "smtp-server": self.inSmtpServer = True
for key in attributes.keys():
self.attrBuffer.update({key: attributes[key]})
return
def characters(self, data):
self.buffer += data
def endElement(self, name):
puffer = self.buffer.strip()
if name == "calibre-folder":
self.settings.setCalibreFolder(puffer)
if name == "export-format":
self.settings.setFormat(puffer)
if self.inMailSettings:
if name == "from":
self.settings.setMailFrom(puffer)
elif name == "to":
self.settings.setMailTo(puffer)
if self.inSmtpServer:
if name == "address":
self.settings.setSmtpServer(puffer,
self.attrBuffer["port"],
self.attrBuffer["security"])
elif name == "login":
self.settings.setSmtpLogin(puffer)
elif name == "password":
self.settings.setSmtpPassword(puffer)
# reset the stuff
if name == "mail-settings": self.inMailSettings = False
elif name == "smtp-server": self.inSmtpServer = False
self.buffer = ""
self.attrBuffer = {}
def loadSettings():
if os.path.isfile(settingsPath):
parser = xml.sax.make_parser()
handler = SettingsHandler()
parser.setContentHandler(handler)
parser.parse(settingsPath)
return handler.settings
else:
return Settings()
def saveSettings(settings):
root = ET.Element("configuration")
tree = ET.ElementTree(root)
appendTextElement(root, "calibre-folder", settings.calibreFolder)
appendTextElement(root, "export-format", settings.format)
mailElem = ET.SubElement(root, "mail-settings")
appendTextElement(mailElem, "from", settings.mailFrom)
appendTextElement(mailElem, "to", settings.mailTo)
if settings.smtpServer:
smtpElem = ET.SubElement(mailElem, "smtp-server")
addressElem = appendTextElement(smtpElem, "address", settings.mailFrom)
if "port" in settings.smtpServer:
addressElem.set("port", str(settings.smtpServer["port"]))
if "security" in settings.smtpServer:
addressElem.set("security", settings.smtpServer["security"])
if "login" in settings.smtpServer:
appendTextElement(smtpElem, "login", settings.smtpServer["login"])
if "password" in settings.smtpServer:
appendTextElement(smtpElem, "password", settings.smtpServer["password"])
tree.write(settingsPath)
return
def appendTextElement(parent, tagName, text):
newElem = ET.SubElement(parent, tagName)
newElem.text = text
return newElem