Project/Project.py

changeset 609
463fc2891cbf
parent 608
d8fea1e76975
child 613
5a6ee2af8ec0
equal deleted inserted replaced
608:d8fea1e76975 609:463fc2891cbf
35 from .DebuggerPropertiesDialog import DebuggerPropertiesDialog 35 from .DebuggerPropertiesDialog import DebuggerPropertiesDialog
36 from .FiletypeAssociationDialog import FiletypeAssociationDialog 36 from .FiletypeAssociationDialog import FiletypeAssociationDialog
37 from .LexerAssociationDialog import LexerAssociationDialog 37 from .LexerAssociationDialog import LexerAssociationDialog
38 from .UserPropertiesDialog import UserPropertiesDialog 38 from .UserPropertiesDialog import UserPropertiesDialog
39 39
40 from E5XML.XMLUtilities import make_parser 40 from E5XML.ProjectReader import ProjectReader
41 from E5XML.XMLErrorHandler import XMLErrorHandler, XMLFatalParseError
42 from E5XML.XMLEntityResolver import XMLEntityResolver
43
44 from E5XML.ProjectHandler import ProjectHandler
45 from E5XML.ProjectWriter import ProjectWriter 41 from E5XML.ProjectWriter import ProjectWriter
46 from E5XML.UserProjectReader import UserProjectReader 42 from E5XML.UserProjectReader import UserProjectReader
47 from E5XML.UserProjectWriter import UserProjectWriter 43 from E5XML.UserProjectWriter import UserProjectWriter
48 from E5XML.SessionReader import SessionReader 44 from E5XML.SessionReader import SessionReader
49 from E5XML.SessionWriter import SessionWriter 45 from E5XML.SessionWriter import SessionWriter
571 Private method to read in a project (.e4p) file. 567 Private method to read in a project (.e4p) file.
572 568
573 @param fn filename of the project file to be read (string) 569 @param fn filename of the project file to be read (string)
574 @return flag indicating success 570 @return flag indicating success
575 """ 571 """
576 try: 572 f = QFile(fn)
577 if fn.lower().endswith("e4pz"): 573 if f.open(QIODevice.ReadOnly):
578 try: 574 reader = ProjectReader(f, self)
579 import gzip 575 reader.readXML()
580 except ImportError: 576 res = not reader.hasError()
581 QApplication.restoreOverrideCursor()
582 E5MessageBox.critical(self.ui,
583 self.trUtf8("Read project file"),
584 self.trUtf8("""Compressed project files not supported."""
585 """ The compression library is missing."""))
586 return False
587 g = gzip.open(fn, "rb")
588 f = io.StringIO(g.read().decode("utf-8"))
589 g.close()
590 else:
591 f = open(fn, "r", encoding = "utf-8")
592 line = f.readline()
593 dtdLine = f.readline()
594 f.close() 577 f.close()
595 except EnvironmentError: 578 else:
596 QApplication.restoreOverrideCursor() 579 QApplication.restoreOverrideCursor()
597 E5MessageBox.critical(self.ui, 580 E5MessageBox.critical(self.ui,
598 self.trUtf8("Read project file"), 581 self.trUtf8("Read project file"),
599 self.trUtf8("<p>The project file <b>{0}</b> could not be read.</p>")\ 582 self.trUtf8("<p>The project file <b>{0}</b> could not be read.</p>")\
600 .format(fn)) 583 .format(fn))
601 return False 584 return False
602 585
603 self.pfile = os.path.abspath(fn) 586 self.pfile = os.path.abspath(fn)
604 self.ppath = os.path.abspath(os.path.dirname(fn)) 587 self.ppath = os.path.abspath(os.path.dirname(fn))
605 if Utilities.isWindowsPlatform(): 588 if Utilities.isWindowsPlatform():
606 self.ppathRe = re.compile(re.escape(self.ppath + os.sep), re.IGNORECASE) 589 self.ppathRe = re.compile(re.escape(self.ppath + os.sep), re.IGNORECASE)
607 else: 590 else:
608 self.ppathRe = re.compile(re.escape(self.ppath + os.sep)) 591 self.ppathRe = re.compile(re.escape(self.ppath + os.sep))
609 592
610 # insert filename into list of recently opened projects 593 # insert filename into list of recently opened projects
611 self.__syncRecent() 594 self.__syncRecent()
612 595
613 # now read the file
614 if not line.startswith('<?xml'):
615 QApplication.restoreOverrideCursor()
616 E5MessageBox.critical(self.ui,
617 self.trUtf8("Read project file"),
618 self.trUtf8("<p>The project file <b>{0}</b> has an unsupported"
619 " format.</p>").format(fn))
620 return False
621
622 res = self.__readXMLProject(fn, dtdLine.startswith("<!DOCTYPE"))
623 if res: 596 if res:
624 if len(self.pdata["TRANSLATIONPATTERN"]) == 1: 597 if len(self.pdata["TRANSLATIONPATTERN"]) == 1:
625 self.translationsRoot = \ 598 self.translationsRoot = \
626 self.pdata["TRANSLATIONPATTERN"][0].split("%language%")[0] 599 self.pdata["TRANSLATIONPATTERN"][0].split("%language%")[0]
627 elif len(self.pdata["MAINSCRIPT"]) == 1: 600 elif len(self.pdata["MAINSCRIPT"]) == 1:
667 self.pdata["HASH"] = [hash] 640 self.pdata["HASH"] = [hash]
668 self.setDirty(True) 641 self.setDirty(True)
669 642
670 return res 643 return res
671 644
672 def __readXMLProject(self, fn, validating):
673 """
674 Private method to read the project data from an XML file.
675
676 @param fn filename of the project file to be read (string)
677 @param validating flag indicating a validation of the XML file is
678 requested (boolean)
679 @return flag indicating success (boolean)
680 """
681 if fn.lower().endswith("e4pz"):
682 # work around for a bug in xmlproc
683 validating = False
684
685 parser = make_parser(validating)
686 handler = ProjectHandler(self)
687 er = XMLEntityResolver()
688 eh = XMLErrorHandler()
689
690 parser.setContentHandler(handler)
691 parser.setEntityResolver(er)
692 parser.setErrorHandler(eh)
693
694 try:
695 if fn.lower().endswith("e4pz"):
696 try:
697 import gzip
698 except ImportError:
699 QApplication.restoreOverrideCursor()
700 E5MessageBox.critical(self.ui,
701 self.trUtf8("Read project file"),
702 self.trUtf8("""Compressed project files not supported."""
703 """ The compression library is missing."""))
704 return False
705 g = gzip.open(fn, "rb")
706 f = io.StringIO(g.read().decode("utf-8"))
707 g.close()
708 else:
709 f = open(fn, "r", encoding = "utf-8")
710 try:
711 try:
712 parser.parse(f)
713 except UnicodeEncodeError:
714 f.seek(0)
715 buf = io.StringIO(f.read())
716 parser.parse(buf)
717 finally:
718 f.close()
719 except IOError:
720 QApplication.restoreOverrideCursor()
721 E5MessageBox.critical(self.ui,
722 self.trUtf8("Read project file"),
723 self.trUtf8("<p>The project file <b>{0}</b> could not be read.</p>")\
724 .format(fn))
725 return False
726 except XMLFatalParseError:
727 QApplication.restoreOverrideCursor()
728 E5MessageBox.critical(self.ui,
729 self.trUtf8("Read project file"),
730 self.trUtf8("<p>The project file <b>{0}</b> has invalid contents.</p>")\
731 .format(fn))
732 eh.showParseMessages()
733 return False
734
735 QApplication.restoreOverrideCursor()
736 eh.showParseMessages()
737 QApplication.setOverrideCursor(QCursor(Qt.WaitCursor))
738 QApplication.processEvents()
739 return True
740
741 def __writeProject(self, fn = None): 645 def __writeProject(self, fn = None):
742 """ 646 """
743 Private method to save the project infos to a project file. 647 Private method to save the project infos to a project file.
744 648
745 @param fn optional filename of the project file to be written (string). 649 @param fn optional filename of the project file to be written (string).

eric ide

mercurial