comparison python/libs/project.py @ 63:32078200cdd6

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