def SaveFile(self, SaveAsName=None): doc = self.currDoc if SaveAsName: oldname = doc.filename doc.filename = SaveAsName EpyGlob.EventMgr.PostFileSavingEvent(doc) if os.path.exists( doc.filename) and (not os.access(doc.filename, os.W_OK)): utils.ShowMessage(u'写入文件: "%s" 时发生错误, 请检查文件权限问题。' % (doc.filename), u'保存错误') if SaveAsName: doc.filename = oldname return False try: #if os.path.exists(doc.filename) : # try: # shutil.copyfile(doc.filename, doc.filename + ".bak") # except: # utils.ShowMessage((u"备份文件到: " + doc.filename + ".bak 发生错误"), u'保存错误') encoding = doc.GetEncoding() doc.RemoveTrailingWhitespace() ctext = drEncoding.DecodeText(doc.GetText(), encoding) cfile = file(doc.filename, 'wb') cfile.write(ctext) cfile.close() #Save Stat Info: doc.mtime = int(os.stat(doc.filename).st_mtime) except: utils.ShowMessage(("写入文件错误: " + doc.filename), u'保存错误') if SaveAsName: doc.filename = oldname return False if doc.untitlednumber > 0: doc.untitlednumber = -1 doc.SetSavePoint() doc.OnModified(None) doc.SetupPrefsDocument() EpyGlob.CurrDir = os.path.dirname(doc.filename) EpyGlob.EventMgr.PostFileSavedEvent(doc) return True
def EncodeText(text, encoding='<Default Encoding>', returnEncoding=False): etext = None if encoding != '<Default Encoding>': etext = EncodeTextWithCode(text, encoding) if etext is not None: if returnEncoding: return etext, encoding else: return etext defaultencoding = len(config.prefs.defaultencoding) > 0 enco = CheckForEncodingComment(text) if enco is not None: etext = EncodeTextWithCode(text, enco) if etext is not None: if returnEncoding: return etext, enco else: return etext if utf8Detect(text): etext = EncodeTextWithCode(text, 'utf-8') if etext is not None: if returnEncoding: return etext, 'utf-8' else: return etext if defaultencoding: etext = EncodeTextWithCode(text, config.prefs.defaultencoding) if etext is None: utils.ShowMessage('There was an error using the encoding "%s".' % (config.prefs.defaultencoding), 'Encoding Error') else: if returnEncoding: return etext, config.prefs.defaultencoding else: return etext if CheckAscii(text): if returnEncoding: return text, 'ascii' else: return text if etext is None: etext = EncodeTextWithCode(text, wx.GetDefaultPyEncoding()) if etext is None: # Patch by knuger, Jan 2007: added "Please try..." to error message raise Exception, \ 'Encoding Error! Please try another Default Encoding (See Options -> Preferences -> General)' else: if returnEncoding: return text, wx.GetDefaultPyEncoding() else: return text
def Init(): global app global prefs, prefsfile, AppDataDir, AppDir, BitmapDir global PLATFORM_IS_WIN, PLATFORM_IS_GTK, PLATFORM_IS_MAC global pythexec, pythexecw app = wx.GetApp() #System constants PLATFORM_IS_WIN = wx.Platform == '__WXMSW__' # (sys.platform == "win32") or #wx.PLATFORM_WINDOWS or wx.PLATFORM_WINDOWS PLATFORM_IS_GTK = wx.Platform == '__WXGTK__' PLATFORM_IS_MAC = wx.Platform == '__WXMAC__' if PLATFORM_IS_WIN: pythexec = sys.prefix.replace("\\", "/") + "/python.exe" pythexecw = sys.prefix.replace("\\", "/") + "/python.exe" else: pythexec = sys.executable #Preferences AppDir = utils.module_path() AppDataDir = os.path.join(AppDir, "data") if not os.path.exists(AppDataDir): os.mkdir(AppDataDir) BitmapDir = os.path.join(AppDir, "bitmaps") if not os.path.exists(BitmapDir): utils.ShowMessage( "Bitmap Directory (" + BitmapDir + ") does Not Exist.", "EasyPython Fatal Error") sys.exit(1) prefsfile = os.path.join(AppDataDir, "preferences.dat") prefs = drPreferences(PLATFORM_IS_WIN, AppDir) LoadPreferences() utils.Init() if prefs.defaultdirectory: EpyGlob.CurrDir = prefs.defaultdirectory else: #add limodou 2004/04/17 #if defaultdirectory is empty, then use the last recently file's dir if EpyGlob.recentfiles: EpyGlob.CurrDir = os.path.dirname(EpyGlob.recentfiles[0]) #end limodou else: EpyGlob.CurrDir = AppDir try: os.chdir(EpyGlob.CurrDir) except: EpyGlob.CurrDir = AppDir os.chdir(EpyGlob.CurrDir)
def OnOpenRecentFile(self, event): recentmenuindex = event.GetId() - self.ID_RECENT_FILES_BASE filename = EpyGlob.RecentFiles[recentmenuindex] if not os.path.isfile(filename): utils.ShowMessage(u"历史文件[%s]已经不存在!" % filename, "EasyPython Error") #TODO: clear recent files return self.docMgr.OpenOrSwitchToFile(filename)
def __init__(self, parent, id, drframe): wx.Panel.__init__(self, parent, id) try: f = file(config.AppDir + "/documentation/gpl.html", 'rb') text = f.read() f.close() except: utils.ShowMessage('Error Reading the GPL!', 'About Dialog Error') return self.htmlBox = wx.html.HtmlWindow(self, -1) self.htmlBox.SetPage(text) self.theSizer = wx.BoxSizer(wx.VERTICAL) self.theSizer.Add(self.htmlBox, 1, wx.EXPAND) self.SetAutoLayout(True) self.SetSizer(self.theSizer)
def OnRun(self, event): if self.docMgr.selection < 0: return #patch [ 1367222 ] Improved Run Command + HTML Browser if self.docMgr.currDoc.GetModify(): if not utils.Ask(u"文件已经被修改了,必须保存后才能运行.\n你需要保存文件吗?", "EasyPython"): return if not self.OnSaveFile(event): return if not utils.IsPythonFile(self.docMgr.currDoc.filename): return cwd = os.getcwd() cdir, filen = os.path.split(self.docMgr.currDoc.filename) try: os.chdir(cdir) except: utils.ShowMessage(u"不能转换当前工作目录到:%s." % cdir, u"EasyPython运行错误") return largs = "" if (len(EpyGlob.LastProgArgs) > 0): largs = ' ' + EpyGlob.LastProgArgs if config.PLATFORM_IS_WIN: self.RunCmd(("cmd /k " + config.pythexecw + ' "' + self.docMgr.currDoc.filename.replace("\\", "/") + '"' + largs), "Running " + filen, filen) else: self.RunCmd( (config.pythexec + " -u " + config.prefs.pythonargs + ' "' + self.docMgr.currDoc.filename + '"' + largs), "Running " + filen, filen ) #patch: [ 1366679 ] Goto Line Should Not Display At Top Of Window os.chdir(cwd)
def OnKeyDown(self, event): if self.pid == -1: return result = EpyGlob.mainFrame.RunShortcuts(event) if result > -1: pos = self.GetCurrentPos() if not self.pid == -1: if (pos >= self.editpoint) and (result == wx.stc.STC_CMD_NEWLINE): self.commandinprogress = True text = self.GetTextRange(self.editpoint, self.GetLength()) l = len(self.CommandArray) if l < self.MAX_PROMPT_COMMANDS: if text in self.CommandArray: self.CommandArray.pop(self.CommandArray.index(text)) self.CommandArray.insert(0, text) self.CommandArrayPos = -1 else: self.CommandArray.pop() self.CommandArray.insert(0, text) self.CommandArrayPos = -1 if not text: text = '\n' self.GotoPos(self.GetLength()) self.AddText(self.GetEndOfLineCharacter()) elif text[-1] != '\n': text += '\n' self.GotoPos(self.GetLength()) self.AddText(self.GetEndOfLineCharacter()) try: etext = drEncoding.EncodeText(text) except: print 'Error Encoding Text' return #ugly hack, but otherwise, drpython crashes etext, corrected = self.CheckAndCorrectAsciiText(etext) if corrected: utils.ShowMessage("Sorry, only ascii codes until 127 possible.\nOtherwiese the program crashes", "EasyPython Error") self.outputstream.write(etext) self.GotoPos(self.GetLength()) self._addoutput() self.editpoint = self.GetLength() self.ScrollToLine(self.LineFromPosition(self.editpoint)) self.commandinprogress = False elif result == wx.stc.STC_CMD_LINEUP: l = len(self.CommandArray) if self.CommandArray: if (self.CommandArrayPos + 1) < l: self.GotoPos(self.editpoint) self.SetTargetStart(self.editpoint) self.SetTargetEnd(self.GetLength()) self.CommandArrayPos = self.CommandArrayPos + 1 self.ReplaceTarget(self.CommandArray[self.CommandArrayPos]) elif result == wx.stc.STC_CMD_LINEDOWN: if (len(self.CommandArray) > 0): self.GotoPos(self.editpoint) self.SetTargetStart(self.editpoint) self.SetTargetEnd(self.GetLength()) if (self.CommandArrayPos - 1) > -1: self.CommandArrayPos = self.CommandArrayPos - 1 self.ReplaceTarget(self.CommandArray[self.CommandArrayPos]) else: if (self.CommandArrayPos - 1) > -2: self.CommandArrayPos = self.CommandArrayPos - 1 self.ReplaceTarget("") if ((pos > self.editpoint) and (result in reserved)) or \ (pos >= self.editpoint) and (result == wx.stc.STC_CMD_CHARRIGHT): event.Skip()
def OpenFile(self, filename, oldDoc=None): filename = os.path.abspath(filename).replace("\\", '/') if type(filename) != unicode: filename = filename.decode(wx.GetDefaultPyEncoding()) if not os.path.isfile(filename): utils.ShowMessage(u"文件[%s]不存在!" % filename, "EasyPython Error") return wx.BeginBusyCursor() encoding = 'utf-8' try: cfile = file(filename, 'rb') except: utils.ShowMessage("Error Opening: " + filename, "EasyPython Error") wx.EndBusyCursor() return ''' if editrecentfiles: self.DestroyRecentFileMenu() if EpyGlob.RecentFiles.count(filename) != 0: EpyGlob.RecentFiles.remove(filename) if len(EpyGlob.RecentFiles) == config.prefs.recentfileslimit: EpyGlob.RecentFiles.pop() EpyGlob.RecentFiles.insert(0, filename) self.WriteRecentFiles() ''' if oldDoc != None: newDoc = oldDoc else: newDoc = DrText(self.frame) newDoc.filename = filename newDoc.untitlednumber = -1 self.docs.append(newDoc) EpyGlob.EventMgr.PostFileLoadingEvent(newDoc) self.docbook.AddPage(newDoc, newDoc.GetFileNameTitle()) try: oof = cfile.read() #Encoding try: oof, e = drEncoding.EncodeText(oof, encoding, True) newDoc.SetText(oof) newDoc.SetEncoding(e) except: utils.ShowMessage( 'There was an error opening the document %s.' % (filename), 'Open Error') wx.EndBusyCursor() self.frame.OnCloseFile(None) return cfile.close() newDoc.EmptyUndoBuffer() newDoc.SetSavePoint() newDoc.SetupPrefsDocument() newDoc.SetupLineNumbersMargin() newDoc.indentationtype = newDoc.CheckIndentation() self.CheckIndentation(newDoc, oof) newDoc.mtime = int(os.stat(filename).st_mtime) newDoc.SetScrollWidth(1) self.CheckLineEnding(newDoc, oof) #Scrolling lines = oof.split(newDoc.GetEndOfLineCharacter()) spaces = "\t".expandtabs(config.prefs.doctabwidth[newDoc.filetype]) line = '' length = 0 x = 0 for l in lines: if len(l) > length: line = l length = len(l) x += 1 line = line.replace('\t', spaces) + '000' scrollwidth = newDoc.TextWidth(wx.stc.STC_STYLE_DEFAULT, line) newDoc.SetScrollWidth(scrollwidth) newDoc.SetXOffset(0) #/End Scrolling except: utils.ShowMessage( "Error Opening: " + filename + "Processing failed", "EasyPython Error") #self.frame.CreateRecentFileMenu() index = self.docs.index(newDoc) self.SelectDoc(index) EpyGlob.EventMgr.PostFileLoadedEvent(newDoc) wx.EndBusyCursor()