/
xml_jobs_handler.py
71 lines (54 loc) · 1.84 KB
/
xml_jobs_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
import xml.sax, xml.sax.handler
import xml.etree.ElementTree as ET
from job import Job
import os.path
import util
jobsPath = "data/jobs.xml"
class JobsHandler(xml.sax.handler.ContentHandler):
def __init__(self):
self.jobs = []
self.attributesList = []
self.buffer = ""
def startElement(self, name, attributes):
self.attributesList.append(attributes)
return
def characters(self, data):
self.buffer += data
def endElement(self, name):
attributes = self.attributesList.pop()
if name == "job":
# build job
job = Job(self.recipeRef)
executionType = attributes[u"type"]
executionTime = util.parseTime(attributes[u"time"])
executionDay = ""
if (not executionType == "daily"):
executionDay = attributes[u"day"]
job.setExecution(executionType, executionTime, executionDay)
self.jobs.append(job)
if name == "recipeRef":
self.recipeRef = self.buffer
self.buffer = ""
def loadJobs():
if os.path.isfile(jobsPath):
parser = xml.sax.make_parser()
handler = JobsHandler()
parser.setContentHandler(handler)
parser.parse(jobsPath)
return handler.jobs
else:
return []
def saveJobs(jobs):
root = ET.Element("jobs")
tree = ET.ElementTree(root)
for job in jobs:
attributes = {}
attributes["type"] = job.executionType
attributes["time"] = util.formatTime(job.executionTime)
if (not job.executionType == "daily"):
attributes["day"] = str(job.executionDay)
jobElem = ET.SubElement(root, "job", attributes)
recipeRefElem = ET.SubElement(jobElem, "recipeRef")
recipeRefElem.text = job.recipeRef
tree.write(jobsPath)
return