Project/Project.py

changeset 599
ee87fe94bf96
parent 594
cded89ee3e30
child 606
cc26d1c27da5
equal deleted inserted replaced
598:76c36b6ebbdb 599:ee87fe94bf96
43 43
44 from E5XML.ProjectHandler import ProjectHandler 44 from E5XML.ProjectHandler import ProjectHandler
45 from E5XML.ProjectWriter import ProjectWriter 45 from E5XML.ProjectWriter import ProjectWriter
46 from E5XML.UserProjectHandler import UserProjectHandler 46 from E5XML.UserProjectHandler import UserProjectHandler
47 from E5XML.UserProjectWriter import UserProjectWriter 47 from E5XML.UserProjectWriter import UserProjectWriter
48 from E5XML.SessionHandler import SessionHandler 48 from E5XML.SessionReader import SessionReader
49 from E5XML.SessionWriter import SessionWriter 49 from E5XML.SessionWriter import SessionWriter
50 from E5XML.TasksReader import TasksReader 50 from E5XML.TasksReader import TasksReader
51 from E5XML.TasksWriter import TasksWriter 51 from E5XML.TasksWriter import TasksWriter
52 from E5XML.DebuggerPropertiesHandler import DebuggerPropertiesHandler 52 from E5XML.DebuggerPropertiesHandler import DebuggerPropertiesHandler
53 from E5XML.DebuggerPropertiesWriter import DebuggerPropertiesWriter 53 from E5XML.DebuggerPropertiesWriter import DebuggerPropertiesWriter
895 self.trUtf8("Read project session"), 895 self.trUtf8("Read project session"),
896 self.trUtf8("Please save the project first.")) 896 self.trUtf8("Please save the project first."))
897 return 897 return
898 898
899 fn, ext = os.path.splitext(os.path.basename(self.pfile)) 899 fn, ext = os.path.splitext(os.path.basename(self.pfile))
900 900 fn = os.path.join(self.getProjectManagementDir(),
901 try: 901 '{0}{1}.e4s'.format(fn, indicator))
902 if ext.lower() in [".e4pz"]: 902
903 fn = os.path.join(self.getProjectManagementDir(), 903 f = QFile(fn)
904 '{0}{1}.e4sz'.format(fn, indicator)) 904 if f.open(QIODevice.ReadOnly):
905 try: 905 reader = SessionReader(f, False)
906 import gzip 906 reader.readXML(quiet = quiet)
907 except ImportError:
908 if not quiet:
909 E5MessageBox.critical(self.ui,
910 self.trUtf8("Read project session"),
911 self.trUtf8("""Compressed project session files not"""
912 """ supported. The compression library is missing."""))
913 return
914 g = gzip.open(fn, "rb")
915 f = io.StringIO(g.read().decode("utf-8"))
916 g.close()
917 else:
918 fn = os.path.join(self.getProjectManagementDir(),
919 '{0}{1}.e4s'.format(fn, indicator))
920 f = open(fn, "r", encoding = "utf-8")
921 line = f.readline()
922 dtdLine = f.readline()
923 f.close() 907 f.close()
924 except EnvironmentError:
925 if not quiet:
926 E5MessageBox.critical(self.ui,
927 self.trUtf8("Read project session"),
928 self.trUtf8("<p>The project session <b>{0}</b>"
929 " could not be read.</p>")\
930 .format(fn))
931 return
932
933 # now read the file
934 if line.startswith('<?xml'):
935 self.__readXMLSession(fn, dtdLine.startswith("<!DOCTYPE"), quiet)
936 else: 908 else:
937 if not quiet:
938 E5MessageBox.critical(self.ui,
939 self.trUtf8("Read project session"),
940 self.trUtf8("<p>The project session <b>{0}</b> has an unsupported"
941 " format.</p>").format(fn))
942
943 def __readXMLSession(self, fn, validating, quiet):
944 """
945 Private method to read the session data from an XML file.
946
947 The data read is:
948 <ul>
949 <li>all open source filenames</li>
950 <li>the active window</li>
951 <li>all breakpoints</li>
952 <li>the commandline</li>
953 <li>the working directory</li>
954 <li>the exception reporting flag</li>
955 <li>the list of exception types to be highlighted</li>
956 <li>all bookmarks</li>
957 </ul>
958
959 @param fn filename of the project session file to be read (string)
960 @param validating flag indicating a validation of the XML file is
961 requested (boolean)
962 @param quiet flag indicating quiet operations.
963 If this flag is true, no errors are reported.
964 """
965 if fn.lower().endswith("e4sz"):
966 # work around for a bug in xmlproc
967 validating = False
968
969 parser = make_parser(validating)
970 handler = SessionHandler(self)
971 er = XMLEntityResolver()
972 eh = XMLErrorHandler()
973
974 parser.setContentHandler(handler)
975 parser.setEntityResolver(er)
976 parser.setErrorHandler(eh)
977
978 try:
979 if fn.lower().endswith("e4sz"):
980 try:
981 import gzip
982 except ImportError:
983 if not quiet:
984 E5MessageBox.critical(self.ui,
985 self.trUtf8("Read project session"),
986 self.trUtf8("<p>The project session <b>{0}</b> could not"
987 " be read.</p>").format(fn))
988 return
989 g = gzip.open(fn, "rb")
990 f = io.StringIO(g.read().decode("utf-8"))
991 g.close()
992 else:
993 f = open(fn, "r", encoding = "utf-8")
994 try:
995 try:
996 parser.parse(f)
997 except UnicodeEncodeError:
998 f.seek(0)
999 buf = io.StringIO(f.read())
1000 parser.parse(buf)
1001 finally:
1002 f.close()
1003 except IOError:
1004 if not quiet: 909 if not quiet:
1005 E5MessageBox.critical(self.ui, 910 E5MessageBox.critical(self.ui,
1006 self.trUtf8("Read project session"), 911 self.trUtf8("Read project session"),
1007 self.trUtf8("<p>The project session file <b>{0}</b> could not be" 912 self.trUtf8("<p>The project session file <b>{0}</b> could not be"
1008 " read.</p>").format(fn)) 913 " read.</p>").format(fn))
1009 return
1010 except XMLFatalParseError:
1011 pass
1012
1013 if not quiet:
1014 eh.showParseMessages()
1015 914
1016 def __writeSession(self, quiet = False, indicator = ""): 915 def __writeSession(self, quiet = False, indicator = ""):
1017 """ 916 """
1018 Private method to write the session data to an XML file (.e4s). 917 Private method to write the session data to an XML file (.e4s).
1019
1020 The data saved is:
1021 <ul>
1022 <li>all open source filenames belonging to the project</li>
1023 <li>the active window, if it belongs to the project</li>
1024 <li>all breakpoints</li>
1025 <li>the commandline</li>
1026 <li>the working directory</li>
1027 <li>the exception reporting flag</li>
1028 <li>the list of exception types to be highlighted</li>
1029 <li>all bookmarks of files belonging to the project</li>
1030 </ul>
1031 918
1032 @param quiet flag indicating quiet operations. 919 @param quiet flag indicating quiet operations.
1033 If this flag is true, no errors are reported. 920 If this flag is true, no errors are reported.
1034 @keyparam indicator indicator string (string) 921 @keyparam indicator indicator string (string)
1035 """ 922 """
1039 self.trUtf8("Save project session"), 926 self.trUtf8("Save project session"),
1040 self.trUtf8("Please save the project first.")) 927 self.trUtf8("Please save the project first."))
1041 return 928 return
1042 929
1043 fn, ext = os.path.splitext(os.path.basename(self.pfile)) 930 fn, ext = os.path.splitext(os.path.basename(self.pfile))
1044 931 fn = os.path.join(self.getProjectManagementDir(),
1045 try: 932 '{0}{1}.e4s'.format(fn, indicator))
1046 if ext.lower() == ".e4pz": 933
1047 fn = os.path.join(self.getProjectManagementDir(), 934 f = QFile(fn)
1048 '{0}{1}.e4sz'.format(fn, indicator)) 935 if f.open(QIODevice.WriteOnly):
1049 try:
1050 import gzip
1051 except ImportError:
1052 if not quiet:
1053 E5MessageBox.critical(self.ui,
1054 self.trUtf8("Save project session"),
1055 self.trUtf8("""Compressed project session files not"""
1056 """ supported. The compression library is missing."""))
1057 return
1058 f = io.StringIO()
1059 else:
1060 fn = os.path.join(self.getProjectManagementDir(),
1061 '{0}{1}.e4s'.format(fn, indicator))
1062 f = open(fn, "w", encoding = "utf-8")
1063
1064 SessionWriter(f, os.path.splitext(os.path.basename(fn))[0]).writeXML() 936 SessionWriter(f, os.path.splitext(os.path.basename(fn))[0]).writeXML()
1065
1066 if fn.lower().endswith("e4sz"):
1067 g = gzip.open(fn, "wb")
1068 g.write(f.getvalue().encode("utf-8"))
1069 g.close()
1070 f.close() 937 f.close()
1071 938 else:
1072 except IOError:
1073 if not quiet: 939 if not quiet:
1074 E5MessageBox.critical(self.ui, 940 E5MessageBox.critical(self.ui,
1075 self.trUtf8("Save project session"), 941 self.trUtf8("Save project session"),
1076 self.trUtf8("<p>The project session file <b>{0}</b> could not be" 942 self.trUtf8("<p>The project session file <b>{0}</b> could not be"
1077 " written.</p>").format(fn)) 943 " written.</p>").format(fn))

eric ide

mercurial