document.py 文件源码

python
阅读 21 收藏 0 点赞 0 评论 0

项目:CodeReader 作者: jasonrbr 项目源码 文件源码
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
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号