eric6/QScintilla/Editor.py

changeset 7836
2f0d208b8137
parent 7790
32b8db5a1fb4
child 7850
e64b178499da
child 7864
431e6816c60c
equal deleted inserted replaced
7835:0835ed67714b 7836:2f0d208b8137
168 @param vm reference to the view manager object 168 @param vm reference to the view manager object
169 (ViewManager.ViewManager) 169 (ViewManager.ViewManager)
170 @param filetype type of the source file (string) 170 @param filetype type of the source file (string)
171 @param editor reference to an Editor object, if this is a cloned view 171 @param editor reference to an Editor object, if this is a cloned view
172 @param tv reference to the task viewer object 172 @param tv reference to the task viewer object
173 @exception IOError raised to indicate an issue accessing the file 173 @exception OSError raised to indicate an issue accessing the file
174 """ 174 """
175 super(Editor, self).__init__() 175 super(Editor, self).__init__()
176 self.setAttribute(Qt.WA_KeyCompression) 176 self.setAttribute(Qt.WA_KeyCompression)
177 self.setUtf8(True) 177 self.setUtf8(True)
178 178
371 """ Do you really want to load it?</p>""") 371 """ Do you really want to load it?</p>""")
372 .format(self.fileName, 372 .format(self.fileName,
373 QFileInfo(self.fileName).size() // 1024), 373 QFileInfo(self.fileName).size() // 1024),
374 icon=E5MessageBox.Warning) 374 icon=E5MessageBox.Warning)
375 if not res: 375 if not res:
376 raise IOError() 376 raise OSError()
377 self.readFile(self.fileName, True) 377 self.readFile(self.fileName, True)
378 self.__bindLexer(self.fileName) 378 self.__bindLexer(self.fileName)
379 self.__bindCompleter(self.fileName) 379 self.__bindCompleter(self.fileName)
380 self.checkSyntax() 380 self.checkSyntax()
381 self.isResourcesFile = self.fileName.endswith(".qrc") 381 self.isResourcesFile = self.fileName.endswith(".qrc")
3091 if encoding: 3091 if encoding:
3092 txt, self.encoding = Utilities.readEncodedFileWithEncoding( 3092 txt, self.encoding = Utilities.readEncodedFileWithEncoding(
3093 fn, encoding) 3093 fn, encoding)
3094 else: 3094 else:
3095 txt, self.encoding = Utilities.readEncodedFile(fn) 3095 txt, self.encoding = Utilities.readEncodedFile(fn)
3096 except (UnicodeDecodeError, IOError) as why: 3096 except (UnicodeDecodeError, OSError) as why:
3097 E5MessageBox.critical( 3097 E5MessageBox.critical(
3098 self.vm, 3098 self.vm,
3099 self.tr('Open File'), 3099 self.tr('Open File'),
3100 self.tr('<p>The file <b>{0}</b> could not be opened.</p>' 3100 self.tr('<p>The file <b>{0}</b> could not be opened.</p>'
3101 '<p>Reason: {1}</p>') 3101 '<p>Reason: {1}</p>')
3192 fn = os.path.realpath(fn) 3192 fn = os.path.realpath(fn)
3193 bfn = '{0}~'.format(fn) 3193 bfn = '{0}~'.format(fn)
3194 try: 3194 try:
3195 permissions = os.stat(fn).st_mode 3195 permissions = os.stat(fn).st_mode
3196 perms_valid = True 3196 perms_valid = True
3197 except EnvironmentError: 3197 except OSError:
3198 # if there was an error, ignore it 3198 # if there was an error, ignore it
3199 perms_valid = False 3199 perms_valid = False
3200 try: 3200 try:
3201 os.remove(bfn) 3201 os.remove(bfn)
3202 except EnvironmentError: 3202 except OSError:
3203 # if there was an error, ignore it 3203 # if there was an error, ignore it
3204 pass 3204 pass
3205 try: 3205 try:
3206 os.rename(fn, bfn) 3206 os.rename(fn, bfn)
3207 except EnvironmentError: 3207 except OSError:
3208 # if there was an error, ignore it 3208 # if there was an error, ignore it
3209 pass 3209 pass
3210 3210
3211 # now write text to the file fn 3211 # now write text to the file fn
3212 try: 3212 try:
3215 self.encoding = Utilities.writeEncodedFile( 3215 self.encoding = Utilities.writeEncodedFile(
3216 fn, txt, self.encoding, forcedEncoding=editorConfigEncoding) 3216 fn, txt, self.encoding, forcedEncoding=editorConfigEncoding)
3217 if createBackup and perms_valid: 3217 if createBackup and perms_valid:
3218 os.chmod(fn, permissions) 3218 os.chmod(fn, permissions)
3219 return True 3219 return True
3220 except (IOError, Utilities.CodingError, UnicodeError) as why: 3220 except (OSError, Utilities.CodingError, UnicodeError) as why:
3221 E5MessageBox.critical( 3221 E5MessageBox.critical(
3222 self, 3222 self,
3223 self.tr('Save File'), 3223 self.tr('Save File'),
3224 self.tr('<p>The file <b>{0}</b> could not be saved.<br/>' 3224 self.tr('<p>The file <b>{0}</b> could not be saved.<br/>'
3225 'Reason: {1}</p>') 3225 'Reason: {1}</p>')
6744 return # user aborted 6744 return # user aborted
6745 6745
6746 try: 6746 try:
6747 with open(fname, "r", encoding="utf-8") as f: 6747 with open(fname, "r", encoding="utf-8") as f:
6748 lines = f.readlines() 6748 lines = f.readlines()
6749 except IOError: 6749 except OSError:
6750 E5MessageBox.critical( 6750 E5MessageBox.critical(
6751 self, 6751 self,
6752 self.tr("Error loading macro"), 6752 self.tr("Error loading macro"),
6753 self.tr( 6753 self.tr(
6754 "<p>The macro file <b>{0}</b> could not be read.</p>") 6754 "<p>The macro file <b>{0}</b> could not be read.</p>")
6805 6805
6806 try: 6806 try:
6807 with open(fname, "w", encoding="utf-8") as f: 6807 with open(fname, "w", encoding="utf-8") as f:
6808 f.write("{0}{1}".format(name, "\n")) 6808 f.write("{0}{1}".format(name, "\n"))
6809 f.write(self.macros[name].save()) 6809 f.write(self.macros[name].save())
6810 except IOError: 6810 except OSError:
6811 E5MessageBox.critical( 6811 E5MessageBox.critical(
6812 self, 6812 self,
6813 self.tr("Error saving macro"), 6813 self.tr("Error saving macro"),
6814 self.tr( 6814 self.tr(
6815 "<p>The macro file <b>{0}</b> could not be written.</p>") 6815 "<p>The macro file <b>{0}</b> could not be written.</p>")
7190 return 7190 return
7191 7191
7192 # reread the file 7192 # reread the file
7193 try: 7193 try:
7194 self.readFile(self.fileName) 7194 self.readFile(self.fileName)
7195 except IOError: 7195 except OSError:
7196 # do not prompt for this change again... 7196 # do not prompt for this change again...
7197 self.lastModified = QDateTime.currentDateTime() 7197 self.lastModified = QDateTime.currentDateTime()
7198 self.setModified(False) 7198 self.setModified(False)
7199 self.__convertTabs() 7199 self.__convertTabs()
7200 7200

eric ide

mercurial