annotate python/ide/project.py @ 62:fd7d5069734e

Rename application to python
author windel
date Sun, 07 Oct 2012 16:56:50 +0200
parents applications/ide/project.py@600f48b74799
children
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
15
de004f808e56 Fixup in multidocument area stuff
windel
parents: 8
diff changeset
24 class ProjectFile:
de004f808e56 Fixup in multidocument area stuff
windel
parents: 8
diff changeset
25 def __init__(self, filename):
de004f808e56 Fixup in multidocument area stuff
windel
parents: 8
diff changeset
26 self.filename = filename
de004f808e56 Fixup in multidocument area stuff
windel
parents: 8
diff changeset
27
7
2db4d2b362e6 Added xml project
windel
parents:
diff changeset
28 class Project:
15
de004f808e56 Fixup in multidocument area stuff
windel
parents: 8
diff changeset
29 def __init__(self, filename, isnew=False):
7
2db4d2b362e6 Added xml project
windel
parents:
diff changeset
30 self.name = ""
2db4d2b362e6 Added xml project
windel
parents:
diff changeset
31 self.files = []
2db4d2b362e6 Added xml project
windel
parents:
diff changeset
32 self.settings = {}
15
de004f808e56 Fixup in multidocument area stuff
windel
parents: 8
diff changeset
33 self.filename = filename
de004f808e56 Fixup in multidocument area stuff
windel
parents: 8
diff changeset
34
de004f808e56 Fixup in multidocument area stuff
windel
parents: 8
diff changeset
35 if not isnew:
de004f808e56 Fixup in multidocument area stuff
windel
parents: 8
diff changeset
36 """ Load the project from the XML file """
de004f808e56 Fixup in multidocument area stuff
windel
parents: 8
diff changeset
37 parser = make_parser()
de004f808e56 Fixup in multidocument area stuff
windel
parents: 8
diff changeset
38 handler = ProjectContentHandler(self)
de004f808e56 Fixup in multidocument area stuff
windel
parents: 8
diff changeset
39 parser.setContentHandler(handler)
de004f808e56 Fixup in multidocument area stuff
windel
parents: 8
diff changeset
40 parser.parse(filename)
7
2db4d2b362e6 Added xml project
windel
parents:
diff changeset
41
2db4d2b362e6 Added xml project
windel
parents:
diff changeset
42 def save(self, filename):
2db4d2b362e6 Added xml project
windel
parents:
diff changeset
43 """ Save the project in XML format """
2db4d2b362e6 Added xml project
windel
parents:
diff changeset
44 # Create document:
2db4d2b362e6 Added xml project
windel
parents:
diff changeset
45 doc = md.Document()
2db4d2b362e6 Added xml project
windel
parents:
diff changeset
46 # Add project:
2db4d2b362e6 Added xml project
windel
parents:
diff changeset
47 project = doc.createElement("Project")
2db4d2b362e6 Added xml project
windel
parents:
diff changeset
48 project.setAttribute("name", self.name)
2db4d2b362e6 Added xml project
windel
parents:
diff changeset
49 doc.appendChild(project)
2db4d2b362e6 Added xml project
windel
parents:
diff changeset
50
2db4d2b362e6 Added xml project
windel
parents:
diff changeset
51 # Add project files:
2db4d2b362e6 Added xml project
windel
parents:
diff changeset
52 filesNode = doc.createElement("Files")
2db4d2b362e6 Added xml project
windel
parents:
diff changeset
53 project.appendChild(filesNode)
2db4d2b362e6 Added xml project
windel
parents:
diff changeset
54 for f in self.files:
2db4d2b362e6 Added xml project
windel
parents:
diff changeset
55 fe = doc.createElement("File")
2db4d2b362e6 Added xml project
windel
parents:
diff changeset
56 fe.setAttribute("Filename", f)
2db4d2b362e6 Added xml project
windel
parents:
diff changeset
57 filesNode.appendChild(fe)
2db4d2b362e6 Added xml project
windel
parents:
diff changeset
58
2db4d2b362e6 Added xml project
windel
parents:
diff changeset
59 # Write the XML file:
2db4d2b362e6 Added xml project
windel
parents:
diff changeset
60 xml = doc.toprettyxml()
2db4d2b362e6 Added xml project
windel
parents:
diff changeset
61 with open(filename, 'w') as f:
2db4d2b362e6 Added xml project
windel
parents:
diff changeset
62 f.write(xml)
2db4d2b362e6 Added xml project
windel
parents:
diff changeset
63
15
de004f808e56 Fixup in multidocument area stuff
windel
parents: 8
diff changeset
64 def loadProjectFile(self, filename):
de004f808e56 Fixup in multidocument area stuff
windel
parents: 8
diff changeset
65 basedir = os.path.dirname(self.filename)
de004f808e56 Fixup in multidocument area stuff
windel
parents: 8
diff changeset
66 filename = os.path.join(basedir, filename)
de004f808e56 Fixup in multidocument area stuff
windel
parents: 8
diff changeset
67 with open(filename, 'r') as f:
de004f808e56 Fixup in multidocument area stuff
windel
parents: 8
diff changeset
68 src = f.read()
de004f808e56 Fixup in multidocument area stuff
windel
parents: 8
diff changeset
69 return src
7
2db4d2b362e6 Added xml project
windel
parents:
diff changeset
70