2 |
2 |
3 # Copyright (c) 2004 - 2010 Detlev Offenbach <detlev@die-offenbachs.de> |
3 # Copyright (c) 2004 - 2010 Detlev Offenbach <detlev@die-offenbachs.de> |
4 # |
4 # |
5 |
5 |
6 """ |
6 """ |
7 Module implementing the writer class for writing an XML project session file. |
7 Module implementing the writer class for writing an XML session file. |
8 """ |
8 """ |
9 |
9 |
10 import time |
10 import time |
11 |
11 |
12 from E5Gui.E5Application import e5App |
12 from E5Gui.E5Application import e5App |
13 |
13 |
14 from .XMLWriterBase import XMLWriterBase |
14 from .XMLStreamWriterBase import XMLStreamWriterBase |
15 from .Config import sessionFileFormatVersion |
15 from .Config import sessionFileFormatVersion |
16 |
16 |
17 import Preferences |
17 import Preferences |
18 |
18 |
19 class SessionWriter(XMLWriterBase): |
19 class SessionWriter(XMLStreamWriterBase): |
20 """ |
20 """ |
21 Class implementing the writer class for writing an XML project session file. |
21 Class implementing the writer class for writing an XML session file. |
22 """ |
22 """ |
23 def __init__(self, file, projectName): |
23 def __init__(self, device, projectName): |
24 """ |
24 """ |
25 Constructor |
25 Constructor |
26 |
26 |
27 @param file open file (like) object for writing |
27 @param device reference to the I/O device to write to (QIODevice) |
28 @param projectName name of the project (string) or None for the |
28 @param projectName name of the project (string) or None for the |
29 global session |
29 global session |
30 """ |
30 """ |
31 XMLWriterBase.__init__(self, file) |
31 XMLStreamWriterBase.__init__(self, device) |
32 |
32 |
33 self.name = projectName |
33 self.name = projectName |
34 self.project = e5App().getObject("Project") |
34 self.project = e5App().getObject("Project") |
35 self.multiProject = e5App().getObject("MultiProject") |
35 self.multiProject = e5App().getObject("MultiProject") |
36 self.vm = e5App().getObject("ViewManager") |
36 self.vm = e5App().getObject("ViewManager") |
41 """ |
41 """ |
42 Public method to write the XML to the file. |
42 Public method to write the XML to the file. |
43 """ |
43 """ |
44 isGlobal = self.name is None |
44 isGlobal = self.name is None |
45 |
45 |
46 XMLWriterBase.writeXML(self) |
46 XMLStreamWriterBase.writeXML(self) |
47 |
47 |
48 self._write('<!DOCTYPE Session SYSTEM "Session-{0}.dtd">'.format( |
48 self.writeDTD('<!DOCTYPE Session SYSTEM "Session-{0}.dtd">'.format( |
49 sessionFileFormatVersion)) |
49 sessionFileFormatVersion)) |
50 |
50 |
51 # add some generation comments |
51 # add some generation comments |
52 if not isGlobal: |
52 if not isGlobal: |
53 self._write("<!-- eric5 session file for project {0} -->".format(self.name)) |
53 self.writeComment(" eric5 session file for project {0} ".format(self.name)) |
54 self._write("<!-- This file was generated automatically, do not edit. -->") |
54 self.writeComment(" This file was generated automatically, do not edit. ") |
55 if Preferences.getProject("XMLTimestamp") or isGlobal: |
55 if Preferences.getProject("XMLTimestamp") or isGlobal: |
56 self._write("<!-- Saved: {0} -->".format(time.strftime('%Y-%m-%d, %H:%M:%S'))) |
56 self.writeComment(" Saved: {0} ".format(time.strftime('%Y-%m-%d, %H:%M:%S'))) |
57 |
57 |
58 # add the main tag |
58 # add the main tag |
59 self._write('<Session version="{0}">'.format(sessionFileFormatVersion)) |
59 self.writeStartElement("Session") |
|
60 self.writeAttribute("version", sessionFileFormatVersion) |
60 |
61 |
61 # step 0: save open multi project and project for the global session |
62 # step 0: save open multi project and project for the global session |
62 if isGlobal: |
63 if isGlobal: |
63 if self.multiProject.isOpen(): |
64 if self.multiProject.isOpen(): |
64 self._write(' <MultiProject>{0}</MultiProject>'.format( |
65 self.writeTextElement("MultiProject", |
65 self.multiProject.getMultiProjectFile())) |
66 self.multiProject.getMultiProjectFile()) |
66 if self.project.isOpen(): |
67 if self.project.isOpen(): |
67 self._write(' <Project>{0}</Project>'.format( |
68 self.writeTextElement("Project", self.project.getProjectFile()) |
68 self.project.getProjectFile())) |
|
69 |
69 |
70 # step 1: save all open (project) filenames and the active window |
70 # step 1: save all open (project) filenames and the active window |
71 allOpenFiles = self.vm.getOpenFilenames() |
71 allOpenFiles = self.vm.getOpenFilenames() |
72 self._write(" <Filenames>") |
72 self.writeStartElement("Filenames") |
73 for of in allOpenFiles: |
73 for of in allOpenFiles: |
74 if isGlobal or of.startswith(self.project.ppath): |
74 if isGlobal or of.startswith(self.project.ppath): |
75 ed = self.vm.getOpenEditor(of) |
75 ed = self.vm.getOpenEditor(of) |
76 if ed is not None: |
76 if ed is not None: |
77 line, index = ed.getCursorPosition() |
77 line, index = ed.getCursorPosition() |
79 zoom = ed.getZoom() |
79 zoom = ed.getZoom() |
80 else: |
80 else: |
81 line, index = 0, 0 |
81 line, index = 0, 0 |
82 folds = '' |
82 folds = '' |
83 zoom = -9999 |
83 zoom = -9999 |
84 self._write( |
84 self.writeStartElement("Filename") |
85 ' <Filename cline="{0:d}" cindex="{1:d}" folds="{2}" zoom="{3:d}">' |
85 self.writeAttribute("cline", str(line)) |
86 '{4}</Filename>'.format(line, index, folds, zoom, of)) |
86 self.writeAttribute("cindex", str(index)) |
87 self._write(" </Filenames>") |
87 self.writeAttribute("folds", folds) |
|
88 self.writeAttribute("zoom", str(zoom)) |
|
89 self.writeCharacters(of) |
|
90 self.writeEndElement() |
|
91 self.writeEndElement() |
88 |
92 |
89 aw = self.vm.getActiveName() |
93 aw = self.vm.getActiveName() |
90 if aw and aw.startswith(self.project.ppath): |
94 if aw and aw.startswith(self.project.ppath): |
91 ed = self.vm.getOpenEditor(aw) |
95 ed = self.vm.getOpenEditor(aw) |
92 if ed is not None: |
96 if ed is not None: |
93 line, index = ed.getCursorPosition() |
97 line, index = ed.getCursorPosition() |
94 else: |
98 else: |
95 line, index = 0, 0 |
99 line, index = 0, 0 |
96 self._write(' <ActiveWindow cline="{0:d}" cindex="{1:d}">{2}</ActiveWindow>'\ |
100 self.writeStartElement("ActiveWindow") |
97 .format(line, index, aw)) |
101 self.writeAttribute("cline", str(line)) |
|
102 self.writeAttribute("cindex", str(index)) |
|
103 self.writeCharacters(aw) |
|
104 self.writeEndElement() |
98 |
105 |
99 # step 2a: save all breakpoints |
106 # step 2a: save all breakpoints |
100 allBreaks = Preferences.getProject("SessionAllBreakpoints") |
107 allBreaks = Preferences.getProject("SessionAllBreakpoints") |
101 projectFiles = self.project.getSources(True) |
108 projectFiles = self.project.getSources(True) |
102 bpModel = self.dbs.getBreakPointModel() |
109 bpModel = self.dbs.getBreakPointModel() |
103 self._write(" <Breakpoints>") |
110 self.writeStartElement("Breakpoints") |
104 for row in range(bpModel.rowCount()): |
111 for row in range(bpModel.rowCount()): |
105 index = bpModel.index(row, 0) |
112 index = bpModel.index(row, 0) |
106 fname, lineno, cond, temp, enabled, count = \ |
113 fname, lineno, cond, temp, enabled, count = \ |
107 bpModel.getBreakPointByIndex(index)[:6] |
114 bpModel.getBreakPointByIndex(index)[:6] |
108 if isGlobal or allBreaks or fname in projectFiles: |
115 if isGlobal or allBreaks or fname in projectFiles: |
109 self._write(" <Breakpoint>") |
116 self.writeStartElement("Breakpoint") |
110 self._write(" <BpFilename>{0}</BpFilename>".format(fname)) |
117 self.writeTextElement("BpFilename", fname) |
111 self._write(' <Linenumber value="{0:d}" />'.format(lineno)) |
118 self.writeEmptyElement("Linenumber") |
112 self._write(" <Condition>{0}</Condition>".format( |
119 self.writeAttribute("value", str(lineno)) |
113 self.escape(str(cond)))) |
120 self.writeTextElement("Condition", str(cond)) |
114 self._write(' <Temporary value="{0}" />'.format(temp)) |
121 self.writeEmptyElement("Temporary") |
115 self._write(' <Enabled value="{0}" />'.format(enabled)) |
122 self.writeAttribute("value", str(temp)) |
116 self._write(' <Count value="{0:d}" />'.format(count)) |
123 self.writeEmptyElement("Enabled") |
117 self._write(" </Breakpoint>") |
124 self.writeAttribute("value", str(enabled)) |
118 self._write(" </Breakpoints>") |
125 self.writeEmptyElement("Count") |
|
126 self.writeAttribute("value", str(count)) |
|
127 self.writeEndElement() |
|
128 self.writeEndElement() |
119 |
129 |
120 # step 2b: save all watch expressions |
130 # step 2b: save all watch expressions |
121 self._write(" <Watchexpressions>") |
131 self.writeStartElement("Watchexpressions") |
122 wpModel = self.dbs.getWatchPointModel() |
132 wpModel = self.dbs.getWatchPointModel() |
123 for row in range(wpModel.rowCount()): |
133 for row in range(wpModel.rowCount()): |
124 index = wpModel.index(row, 0) |
134 index = wpModel.index(row, 0) |
125 cond, temp, enabled, count, special = wpModel.getWatchPointByIndex(index)[:5] |
135 cond, temp, enabled, count, special = wpModel.getWatchPointByIndex(index)[:5] |
126 self._write(' <Watchexpression>') |
136 self.writeStartElement("Watchexpression") |
127 self._write(" <Condition>{0}</Condition>".format(self.escape(str(cond)))) |
137 self.writeTextElement("Condition", str(cond)) |
128 self._write(' <Temporary value="{0}" />'.format(temp)) |
138 self.writeEmptyElement("Temporary") |
129 self._write(' <Enabled value="{0}" />'.format(enabled)) |
139 self.writeAttribute("value", str(temp)) |
130 self._write(' <Count value="{0:d}" />'.format(count)) |
140 self.writeEmptyElement("Enabled") |
131 self._write(' <Special>{0}</Special>'.format(special)) |
141 self.writeAttribute("value", str(enabled)) |
132 self._write(' </Watchexpression>') |
142 self.writeEmptyElement("Count") |
133 self._write(' </Watchexpressions>') |
143 self.writeAttribute("value", str(count)) |
|
144 self.writeTextElement("Special", special) |
|
145 self.writeEndElement() |
|
146 self.writeEndElement() |
134 |
147 |
135 # step 3: save the debug info |
148 # step 3: save the debug info |
136 self._write(" <DebugInfo>") |
149 self.writeStartElement("DebugInfo") |
137 if isGlobal: |
150 if isGlobal: |
138 if len(self.dbg.argvHistory): |
151 if len(self.dbg.argvHistory): |
139 dbgCmdline = str(self.dbg.argvHistory[0]) |
152 dbgCmdline = str(self.dbg.argvHistory[0]) |
140 else: |
153 else: |
141 dbgCmdline = "" |
154 dbgCmdline = "" |
145 dbgWd = "" |
158 dbgWd = "" |
146 if len(self.dbg.envHistory): |
159 if len(self.dbg.envHistory): |
147 dbgEnv = self.dbg.envHistory[0] |
160 dbgEnv = self.dbg.envHistory[0] |
148 else: |
161 else: |
149 dbgEnv = "" |
162 dbgEnv = "" |
150 self._write(" <CommandLine>{0}</CommandLine>"\ |
163 self.writeTextElement("CommandLine", dbgCmdline) |
151 .format(self.escape(dbgCmdline))) |
164 self.writeTextElement("WorkingDirectory", dbgWd) |
152 self._write(" <WorkingDirectory>{0}</WorkingDirectory>".format(dbgWd)) |
165 self.writeTextElement("Environment", dbgEnv) |
153 self._write(" <Environment>{0}</Environment>".format(dbgEnv)) |
166 self.writeEmptyElement("ReportExceptions") |
154 self._write(' <ReportExceptions value="{0}" />'\ |
167 self.writeAttribute("value", str(self.dbg.exceptions)) |
155 .format(self.dbg.exceptions)) |
168 self.writeStartElement("Exceptions") |
156 self._write(" <Exceptions>") |
|
157 for exc in self.dbg.excList: |
169 for exc in self.dbg.excList: |
158 self._write(" <Exception>{0}</Exception>".format(exc)) |
170 self.writeTextElement("Exception", exc) |
159 self._write(" </Exceptions>") |
171 self.writeEndElement() |
160 self._write(" <IgnoredExceptions>") |
172 self.writeStartElement("IgnoredExceptions") |
161 for iexc in self.dbg.excIgnoreList: |
173 for iexc in self.dbg.excIgnoreList: |
162 self._write(" <IgnoredException>{0}</IgnoredException>".format(iexc)) |
174 self.writeTextElement("IgnoredException", iexc) |
163 self._write(" </IgnoredExceptions>") |
175 self.writeEndElement() |
164 self._write(' <AutoClearShell value="{0}" />'\ |
176 self.writeEmptyElement("AutoClearShell") |
165 .format(self.dbg.autoClearShell)) |
177 self.writeAttribute("value", str(self.dbg.autoClearShell)) |
166 self._write(' <TracePython value="{0}" />'.format(self.dbg.tracePython)) |
178 self.writeEmptyElement("TracePython") |
167 self._write(' <AutoContinue value="{0}" />'.format(self.dbg.autoContinue)) |
179 self.writeAttribute("value", str(self.dbg.tracePython)) |
168 self._write(" <CovexcPattern></CovexcPattern>") # kept for compatibility |
180 self.writeEmptyElement("AutoContinue") |
|
181 self.writeAttribute("value", str(self.dbg.autoContinue)) |
|
182 self.writeEmptyElement("CovexcPattern") # kept for compatibility |
169 else: |
183 else: |
170 self._write(" <CommandLine>{0}</CommandLine>".format( |
184 self.writeTextElement("CommandLine", self.project.dbgCmdline) |
171 self.escape(str(self.project.dbgCmdline)))) |
185 self.writeTextElement("WorkingDirectory", self.project.dbgWd) |
172 self._write(" <WorkingDirectory>{0}</WorkingDirectory>".format( |
186 self.writeTextElement("Environment", self.project.dbgEnv) |
173 self.project.dbgWd)) |
187 self.writeEmptyElement("ReportExceptions") |
174 self._write(" <Environment>{0}</Environment>".format( |
188 self.writeAttribute("value", str(self.project.dbgReportExceptions)) |
175 self.project.dbgEnv)) |
189 self.writeStartElement("Exceptions") |
176 self._write(' <ReportExceptions value="{0}" />'.format( |
|
177 self.project.dbgReportExceptions)) |
|
178 self._write(" <Exceptions>") |
|
179 for exc in self.project.dbgExcList: |
190 for exc in self.project.dbgExcList: |
180 self._write(" <Exception>{0}</Exception>".format(exc)) |
191 self.writeTextElement("Exception", exc) |
181 self._write(" </Exceptions>") |
192 self.writeEndElement() |
182 self._write(" <IgnoredExceptions>") |
193 self.writeStartElement("IgnoredExceptions") |
183 for iexc in self.project.dbgExcIgnoreList: |
194 for iexc in self.project.dbgExcIgnoreList: |
184 self._write(" <IgnoredException>{0}</IgnoredException>".format(iexc)) |
195 self.writeTextElement("IgnoredException", iexc) |
185 self._write(" </IgnoredExceptions>") |
196 self.writeEndElement() |
186 self._write(' <AutoClearShell value="{0}" />'.format( |
197 self.writeEmptyElement("AutoClearShell") |
187 self.project.dbgAutoClearShell)) |
198 self.writeAttribute("value", str(self.project.dbgAutoClearShell)) |
188 self._write(' <TracePython value="{0}" />'.format( |
199 self.writeEmptyElement("TracePython") |
189 self.project.dbgTracePython)) |
200 self.writeAttribute("value", str(self.project.dbgTracePython)) |
190 self._write(' <AutoContinue value="{0}" />'.format( |
201 self.writeEmptyElement("AutoContinue") |
191 self.project.dbgAutoContinue)) |
202 self.writeAttribute("value", str(self.project.dbgAutoContinue)) |
192 self._write(" <CovexcPattern></CovexcPattern>") # kept for compatibility |
203 self.writeEmptyElement("CovexcPattern") # kept for compatibility |
193 self._write(" </DebugInfo>") |
204 self.writeEndElement() |
194 |
205 |
195 # step 4: save bookmarks of all open (project) files |
206 # step 4: save bookmarks of all open (project) files |
196 self._write(" <Bookmarks>") |
207 self.writeStartElement("Bookmarks") |
197 for of in allOpenFiles: |
208 for of in allOpenFiles: |
198 if isGlobal or of.startswith(self.project.ppath): |
209 if isGlobal or of.startswith(self.project.ppath): |
199 editor = self.vm.getOpenEditor(of) |
210 editor = self.vm.getOpenEditor(of) |
200 for bookmark in editor.getBookmarks(): |
211 for bookmark in editor.getBookmarks(): |
201 self._write(" <Bookmark>") |
212 self.writeStartElement("Bookmark") |
202 self._write(" <BmFilename>{0}</BmFilename>".format(of)) |
213 self.writeTextElement("BmFilename", of) |
203 self._write(' <Linenumber value="{0:d}" />'.format(bookmark)) |
214 self.writeEmptyElement("Linenumber") |
204 self._write(" </Bookmark>") |
215 self.writeAttribute("value", str(bookmark)) |
205 self._write(" </Bookmarks>") |
216 self.writeEndElement() |
206 |
217 self.writeEndElement() |
207 self._write("</Session>", newline = False) |
218 |
|
219 # add the main end tag |
|
220 self.writeEndElement() |
|
221 self.writeEndDocument() |