def OnSaveDocument(self, filename): file = open(filename, "wb") pickle.dump(self.strokes, file) file.close() self.SetModifiedFlag(0) win32ui.AddToRecentFileList(filename) return 1
def OnSaveDocument(self, fileName): win32ui.SetStatusText("Saving file...", 1) # rename to bak if required. dir, basename = os.path.split(fileName) if self.bakFileType == BAK_DOT_BAK: bakFileName = dir + '\\' + os.path.splitext(basename)[0] + '.bak' elif self.bakFileType == BAK_DOT_BAK_TEMP_DIR: bakFileName = win32api.GetTempPath() + '\\' + os.path.splitext( basename)[0] + '.bak' elif self.bakFileType == BAK_DOT_BAK_BAK_DIR: tempPath = os.path.join(win32api.GetTempPath(), 'bak') try: os.mkdir(tempPath, 0) except os.error: pass bakFileName = os.path.join(tempPath, basename) try: os.unlink(bakFileName) # raise NameError if no bakups wanted. except (os.error, NameError): pass try: # Do a copy as it might be on different volumes, # and the file may be a hard-link, causing the link # to follow the backup. shutil.copy2(fileName, bakFileName) except (os.error, NameError, IOError): pass try: self.SaveFile(fileName) except IOError as details: win32ui.MessageBox("Error - could not save file\r\n\r\n%s" % details) return 0 except (UnicodeEncodeError, LookupError) as details: rc = win32ui.MessageBox( "Encoding failed: \r\n%s" % details + '\r\nPlease add desired source encoding as first line of file, eg \r\n' + '# -*- coding: mbcs -*-\r\n\r\n' + 'If you continue, the file will be saved as binary and will\r\n' + 'not be valid in the declared encoding.\r\n\r\n' + 'Save the file as binary with an invalid encoding?', "File save failed", win32con.MB_YESNO | win32con.MB_DEFBUTTON2) if rc == win32con.IDYES: try: self.SaveFile(fileName, encoding="latin-1") except IOError as details: win32ui.MessageBox( "Error - could not save file\r\n\r\n%s" % details) return 0 else: return 0 self.SetModifiedFlag(0) # No longer dirty self.bDeclinedReload = 0 # They probably want to know if it changes again! win32ui.AddToRecentFileList(fileName) self.SetPathName(fileName) win32ui.SetStatusText("Ready") self._DocumentStateChanged() return 1
class EditorDocumentBase(ParentEditorDocument): def __init__(self, template): self.bAutoReload = GetEditorOption("Auto Reload", 1) self.bDeclinedReload = 0 # Has the user declined to reload. self.fileStat = None self.bReportedFileNotFound = 0 # what sort of bak file should I create. # default to write to %temp%/bak/filename.ext self.bakFileType=GetEditorOption("Backup Type", BAK_DOT_BAK_BAK_DIR) self.watcherThread = FileWatchingThread(self) self.watcherThread.CreateThread() # Should I try and use VSS integration? self.scModuleName=GetEditorOption("Source Control Module", "") self.scModule = None # Loaded when first used. ParentEditorDocument.__init__(self, template, template.CreateWin32uiDocument()) def OnCloseDocument(self ): self.watcherThread.SignalStop() return self._obj_.OnCloseDocument() # def OnOpenDocument(self, name): # rc = ParentEditorDocument.OnOpenDocument(self, name) # self.GetFirstView()._SetLoadedText(self.text) # self._DocumentStateChanged() # return rc def OnSaveDocument( self, fileName ): win32ui.SetStatusText("Saving file...",1) # rename to bak if required. dir, basename = os.path.split(fileName) if self.bakFileType==BAK_DOT_BAK: bakFileName=dir+'\\'+os.path.splitext(basename)[0]+'.bak' elif self.bakFileType==BAK_DOT_BAK_TEMP_DIR: bakFileName=win32api.GetTempPath()+'\\'+os.path.splitext(basename)[0]+'.bak' elif self.bakFileType==BAK_DOT_BAK_BAK_DIR: tempPath=os.path.join(win32api.GetTempPath(),'bak') try: os.mkdir(tempPath,0) except os.error: pass bakFileName=os.path.join(tempPath,basename) try: os.unlink(bakFileName) # raise NameError if no bakups wanted. except (os.error, NameError): pass try: # Do a copy as it might be on different volumes, # and the file may be a hard-link, causing the link # to follow the backup. shutil.copy2(fileName, bakFileName) except (os.error, NameError, IOError): pass try: self.SaveFile(fileName) except IOError, details: win32ui.MessageBox("Error - could not save file\r\n\r\n%s"%details) return 0 self.SetModifiedFlag(0) # No longer dirty self.bDeclinedReload = 0 # They probably want to know if it changes again! win32ui.AddToRecentFileList(fileName) self.SetPathName(fileName) win32ui.SetStatusText("Ready") self._DocumentStateChanged() return 1
'If you continue, the file will be saved as binary and will\r\n' + 'not be valid in the declared encoding.\r\n\r\n' + 'Save the file as binary with an invalid encoding?', "File save failed", win32con.MB_YESNO | win32con.MB_DEFBUTTON2) if rc == win32con.IDYES: try: self.SaveFile(fileName, encoding="latin-1") except IOError, details: win32ui.MessageBox( "Error - could not save file\r\n\r\n%s" % details) return 0 else: return 0 self.SetModifiedFlag(0) # No longer dirty self.bDeclinedReload = 0 # They probably want to know if it changes again! win32ui.AddToRecentFileList(fileName) self.SetPathName(fileName) win32ui.SetStatusText("Ready") self._DocumentStateChanged() return 1 def FinalizeViewCreation(self, view): ParentEditorDocument.FinalizeViewCreation(self, view) if view == self.GetFirstView(): self._DocumentStateChanged() if view.bFolding and GetEditorOption("Fold On Open", 0): view.FoldTopLevelEvent() def HookViewNotifications(self, view): ParentEditorDocument.HookViewNotifications(self, view)
def OnOpenDocument(self, filename): file = open(filename, "rb") self.strokes = pickle.load(file) file.close() win32ui.AddToRecentFileList(filename) return 1