annotate ide/project.py @ 7:2db4d2b362e6

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