eric6/MultiProject/MultiProject.py

branch
jsonfiles
changeset 8006
c4110b8b5931
parent 7923
91e843545d9a
child 8049
af58c1d47e5e
equal deleted inserted replaced
8003:59d1c0f80286 8006:c4110b8b5931
24 24
25 import UI.PixmapCache 25 import UI.PixmapCache
26 26
27 import Preferences 27 import Preferences
28 import Utilities 28 import Utilities
29
30 from .MultiProjectFile import MultiProjectFile
29 31
30 32
31 class MultiProject(QObject): 33 class MultiProject(QObject):
32 """ 34 """
33 Class implementing the project management functionality. 35 Class implementing the project management functionality.
73 self.ui = parent 75 self.ui = parent
74 self.projectObject = project 76 self.projectObject = project
75 77
76 self.__initData() 78 self.__initData()
77 79
80 self.__multiProjectFile = MultiProjectFile(self)
81
78 self.recent = [] 82 self.recent = []
79 self.__loadRecent() 83 self.__loadRecent()
80 84
81 if filename is not None: 85 if filename is not None:
82 self.openMultiProject(filename) 86 self.openMultiProject(filename)
215 """ 219 """
216 return [c for c in self.categories if c] 220 return [c for c in self.categories if c]
217 221
218 def __readMultiProject(self, fn): 222 def __readMultiProject(self, fn):
219 """ 223 """
220 Private method to read in a multi project (.e4m, .e5m) file. 224 Private method to read in a multi project (.emj, .e4m, .e5m) file.
221 225
222 @param fn filename of the multi project file to be read (string) 226 @param fn filename of the multi project file to be read (string)
223 @return flag indicating success 227 @return flag indicating success
224 """ 228 """
225 f = QFile(fn) 229 if os.path.splitext(fn)[1] == ".emj":
226 if f.open(QIODevice.ReadOnly): 230 # new JSON based format
227 with E5OverrideCursor(): 231 with E5OverrideCursor():
228 from E5XML.MultiProjectReader import MultiProjectReader 232 res = self.__multiProjectFile.readFile(fn)
229 reader = MultiProjectReader(f, self)
230 reader.readXML()
231 f.close()
232 if reader.hasError():
233 return False
234 else: 233 else:
235 E5MessageBox.critical( 234 # old XML based format
236 self.ui, 235 f = QFile(fn)
237 self.tr("Read multiproject file"), 236 if f.open(QIODevice.ReadOnly):
238 self.tr( 237 with E5OverrideCursor():
239 "<p>The multiproject file <b>{0}</b> could not be" 238 from E5XML.MultiProjectReader import MultiProjectReader
240 " read.</p>").format(fn)) 239 reader = MultiProjectReader(f, self)
241 return False 240 reader.readXML()
242 241 f.close()
243 self.pfile = os.path.abspath(fn) 242 res = not reader.hasError()
244 self.ppath = os.path.abspath(os.path.dirname(fn)) 243 else:
245 244 E5MessageBox.critical(
246 self.__extractCategories() 245 self.ui,
247 246 self.tr("Read Multi Project File"),
248 # insert filename into list of recently opened multi projects 247 self.tr(
249 self.__syncRecent() 248 "<p>The multi project file <b>{0}</b> could not be"
250 249 " read.</p>").format(fn))
251 self.name = os.path.splitext(os.path.basename(fn))[0] 250 res = False
252 251
253 # check, if the files of the multi project still exist 252 if res:
254 self.__checkFilesExist() 253 self.pfile = os.path.abspath(fn)
255 254 self.ppath = os.path.abspath(os.path.dirname(fn))
256 return True 255
256 self.__extractCategories()
257
258 # insert filename into list of recently opened multi projects
259 self.__syncRecent()
260
261 self.name = os.path.splitext(os.path.basename(fn))[0]
262
263 # check, if the files of the multi project still exist
264 self.__checkFilesExist()
265
266 return res
257 267
258 def __writeMultiProject(self, fn=None): 268 def __writeMultiProject(self, fn=None):
259 """ 269 """
260 Private method to save the multi project infos to a multi project file. 270 Private method to save the multi project infos to a multi project file.
261 271
267 @return flag indicating success 277 @return flag indicating success
268 """ 278 """
269 if fn is None: 279 if fn is None:
270 fn = self.pfile 280 fn = self.pfile
271 281
272 f = QFile(fn) 282 if os.path.splitext(fn)[1] == ".emj":
273 if f.open(QIODevice.WriteOnly): 283 # new JSON based format
274 from E5XML.MultiProjectWriter import MultiProjectWriter 284 res = self.__multiProjectFile.writeFile(fn)
275 MultiProjectWriter(
276 f,
277 self, os.path.splitext(os.path.basename(fn))[0]
278 ).writeXML()
279 res = True
280 else: 285 else:
281 E5MessageBox.critical( 286 # old XML based format
282 self.ui, 287 f = QFile(fn)
283 self.tr("Save multiproject file"), 288 if f.open(QIODevice.WriteOnly):
284 self.tr( 289 from E5XML.MultiProjectWriter import MultiProjectWriter
285 "<p>The multiproject file <b>{0}</b> could not be " 290 MultiProjectWriter(
286 "written.</p>").format(fn)) 291 f,
287 res = False 292 self, os.path.splitext(os.path.basename(fn))[0]
293 ).writeXML()
294 res = True
295 else:
296 E5MessageBox.critical(
297 self.ui,
298 self.tr("Save Multi Project File"),
299 self.tr(
300 "<p>The multi project file <b>{0}</b> could not be "
301 "written.</p>").format(fn))
302 res = False
288 303
289 if res: 304 if res:
290 self.pfile = os.path.abspath(fn) 305 self.pfile = os.path.abspath(fn)
291 self.ppath = os.path.abspath(os.path.dirname(fn)) 306 self.ppath = os.path.abspath(os.path.dirname(fn))
292 self.name = os.path.splitext(os.path.basename(fn))[0] 307 self.name = os.path.splitext(os.path.basename(fn))[0]
529 def openMultiProject(self, fn=None, openMaster=True): 544 def openMultiProject(self, fn=None, openMaster=True):
530 """ 545 """
531 Public slot to open a multi project. 546 Public slot to open a multi project.
532 547
533 @param fn optional filename of the multi project file to be 548 @param fn optional filename of the multi project file to be
534 read (string) 549 read
550 @type str
535 @param openMaster flag indicating, that the master project 551 @param openMaster flag indicating, that the master project
536 should be opened depending on the configuration (boolean) 552 should be opened depending on the configuration
553 @rtype bool
537 """ 554 """
538 if not self.checkDirty(): 555 if not self.checkDirty():
539 return 556 return
540 557
541 if fn is None: 558 if fn is None:
542 fn = E5FileDialog.getOpenFileName( 559 fn = E5FileDialog.getOpenFileName(
543 self.parent(), 560 self.parent(),
544 self.tr("Open multiproject"), 561 self.tr("Open Multi Project"),
545 Preferences.getMultiProject("Workspace") or 562 Preferences.getMultiProject("Workspace") or
546 Utilities.getHomeDir(), 563 Utilities.getHomeDir(),
547 self.tr("Multiproject Files (*.e5m *.e4m)")) 564 self.tr("Multi Project Files (*.emj);;"
565 "XML Multi Project Files (*.e5m *.e4m)"))
548 566
549 if fn == "": 567 if fn == "":
550 fn = None 568 fn = None
551 569
552 QApplication.processEvents() 570 QApplication.processEvents()
570 588
571 def saveMultiProject(self): 589 def saveMultiProject(self):
572 """ 590 """
573 Public slot to save the current multi project. 591 Public slot to save the current multi project.
574 592
575 @return flag indicating success (boolean) 593 @return flag indicating success
594 @rtype bool
576 """ 595 """
577 if self.isDirty(): 596 if self.isDirty():
578 if len(self.pfile) > 0: 597 if len(self.pfile) > 0:
579 if self.pfile.endswith(".e4m"): 598 if self.pfile.endswith((".e4m", ".e5m")):
580 self.pfile = self.pfile.replace(".e4m", ".e5m") 599 self.pfile = (self.pfile
600 .replace(".e4m", ".emj")
601 .replace(".e5m", ".emj"))
581 self.__syncRecent() 602 self.__syncRecent()
582 ok = self.__writeMultiProject() 603 ok = self.__writeMultiProject()
583 else: 604 else:
584 ok = self.saveMultiProjectAs() 605 ok = self.saveMultiProjectAs()
585 else: 606 else:
588 609
589 def saveMultiProjectAs(self): 610 def saveMultiProjectAs(self):
590 """ 611 """
591 Public slot to save the current multi project to a different file. 612 Public slot to save the current multi project to a different file.
592 613
593 @return flag indicating success (boolean) 614 @return flag indicating success
594 """ 615 @rtype bool
595 defaultFilter = self.tr("Multiproject Files (*.e5m)") 616 """
617 defaultFilter = self.tr("Multi Project Files (*.emj)")
596 if self.ppath: 618 if self.ppath:
597 defaultPath = self.ppath 619 defaultPath = self.ppath
598 else: 620 else:
599 defaultPath = ( 621 defaultPath = (
600 Preferences.getMultiProject("Workspace") or 622 Preferences.getMultiProject("Workspace") or
602 ) 624 )
603 fn, selectedFilter = E5FileDialog.getSaveFileNameAndFilter( 625 fn, selectedFilter = E5FileDialog.getSaveFileNameAndFilter(
604 self.parent(), 626 self.parent(),
605 self.tr("Save multiproject as"), 627 self.tr("Save multiproject as"),
606 defaultPath, 628 defaultPath,
607 self.tr("Multiproject Files (*.e5m)"), 629 self.tr("Multi Project Files (*.emj);;"
630 "XML Multi Project Files (*.e5m)"),
608 defaultFilter, 631 defaultFilter,
609 E5FileDialog.Options(E5FileDialog.DontConfirmOverwrite)) 632 E5FileDialog.Options(E5FileDialog.DontConfirmOverwrite))
610 633
611 if fn: 634 if fn:
612 ext = QFileInfo(fn).suffix() 635 ext = QFileInfo(fn).suffix()

eric ide

mercurial