annotate ide/project.py @ 11:607898120eb1

Work on error in stdarg
author windel
date Sun, 13 Nov 2011 23:24:10 +0100
parents edd70006d3e4
children de004f808e56
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 = {}
8
edd70006d3e4 Started with MDI functions
windel
parents: 7
diff changeset
29 self.filename = ""
7
2db4d2b362e6 Added xml project
windel
parents:
diff changeset
30
2db4d2b362e6 Added xml project
windel
parents:
diff changeset
31 def save(self, filename):
2db4d2b362e6 Added xml project
windel
parents:
diff changeset
32 """ Save the project in XML format """
2db4d2b362e6 Added xml project
windel
parents:
diff changeset
33 # Create document:
2db4d2b362e6 Added xml project
windel
parents:
diff changeset
34 doc = md.Document()
2db4d2b362e6 Added xml project
windel
parents:
diff changeset
35 # Add project:
2db4d2b362e6 Added xml project
windel
parents:
diff changeset
36 project = doc.createElement("Project")
2db4d2b362e6 Added xml project
windel
parents:
diff changeset
37 project.setAttribute("name", self.name)
2db4d2b362e6 Added xml project
windel
parents:
diff changeset
38 doc.appendChild(project)
2db4d2b362e6 Added xml project
windel
parents:
diff changeset
39
2db4d2b362e6 Added xml project
windel
parents:
diff changeset
40 # Add project files:
2db4d2b362e6 Added xml project
windel
parents:
diff changeset
41 filesNode = doc.createElement("Files")
2db4d2b362e6 Added xml project
windel
parents:
diff changeset
42 project.appendChild(filesNode)
2db4d2b362e6 Added xml project
windel
parents:
diff changeset
43 for f in self.files:
2db4d2b362e6 Added xml project
windel
parents:
diff changeset
44 fe = doc.createElement("File")
2db4d2b362e6 Added xml project
windel
parents:
diff changeset
45 fe.setAttribute("Filename", f)
2db4d2b362e6 Added xml project
windel
parents:
diff changeset
46 filesNode.appendChild(fe)
2db4d2b362e6 Added xml project
windel
parents:
diff changeset
47
2db4d2b362e6 Added xml project
windel
parents:
diff changeset
48 # Write the XML file:
2db4d2b362e6 Added xml project
windel
parents:
diff changeset
49 xml = doc.toprettyxml()
2db4d2b362e6 Added xml project
windel
parents:
diff changeset
50 with open(filename, 'w') as f:
2db4d2b362e6 Added xml project
windel
parents:
diff changeset
51 f.write(xml)
2db4d2b362e6 Added xml project
windel
parents:
diff changeset
52
2db4d2b362e6 Added xml project
windel
parents:
diff changeset
53 def load(self, filename):
2db4d2b362e6 Added xml project
windel
parents:
diff changeset
54 """ Load the project from the XML file """
2db4d2b362e6 Added xml project
windel
parents:
diff changeset
55 if not os.path.exists(filename):
2db4d2b362e6 Added xml project
windel
parents:
diff changeset
56 return
8
edd70006d3e4 Started with MDI functions
windel
parents: 7
diff changeset
57 self.filename = filename
edd70006d3e4 Started with MDI functions
windel
parents: 7
diff changeset
58 print(self.filename)
7
2db4d2b362e6 Added xml project
windel
parents:
diff changeset
59 parser = make_parser()
2db4d2b362e6 Added xml project
windel
parents:
diff changeset
60 handler = ProjectContentHandler(self)
2db4d2b362e6 Added xml project
windel
parents:
diff changeset
61 parser.setContentHandler(handler)
2db4d2b362e6 Added xml project
windel
parents:
diff changeset
62 parser.parse(filename)
2db4d2b362e6 Added xml project
windel
parents:
diff changeset
63