comparison ide/project.py @ 7:2db4d2b362e6

Added xml project
author windel
date Sat, 15 Oct 2011 10:03:21 +0200
parents
children edd70006d3e4
comparison
equal deleted inserted replaced
6:1784af239df4 7:2db4d2b362e6
1 """
2 Project that can be stored to and loaded from XML.
3 """
4
5 from xml.sax import ContentHandler, make_parser
6 import xml.dom.minidom as md
7 import os.path
8
9 class ProjectContentHandler(ContentHandler):
10 def __init__(self, project):
11 self.project = project
12 self.inFiles = False
13 def startElement(self, name, attrs):
14 if name == 'Project':
15 self.project.name = attrs['name']
16 if name == 'Files':
17 self.inFiles = True
18 if name == 'File' and self.inFiles:
19 self.project.files.append(attrs['Filename'])
20 def endElement(self, name):
21 if name == 'Files':
22 self.inFiles = False
23
24 class Project:
25 def __init__(self):
26 self.name = ""
27 self.files = []
28 self.settings = {}
29
30 def save(self, filename):
31 """ Save the project in XML format """
32 # Create document:
33 doc = md.Document()
34 # Add project:
35 project = doc.createElement("Project")
36 project.setAttribute("name", self.name)
37 doc.appendChild(project)
38
39 # Add project files:
40 filesNode = doc.createElement("Files")
41 project.appendChild(filesNode)
42 for f in self.files:
43 fe = doc.createElement("File")
44 fe.setAttribute("Filename", f)
45 filesNode.appendChild(fe)
46
47 # Write the XML file:
48 xml = doc.toprettyxml()
49 with open(filename, 'w') as f:
50 f.write(xml)
51
52 def load(self, filename):
53 """ Load the project from the XML file """
54 if not os.path.exists(filename):
55 return
56 parser = make_parser()
57 handler = ProjectContentHandler(self)
58 parser.setContentHandler(handler)
59 parser.parse(filename)
60