📄 dialog_map.py
字号:
# This module is part of the spambayes project, which is Copyright 2003# The Python Software Foundation and is covered by the Python Software# Foundation license.from processors import *from opt_processors import *import wizard_processors as wizfrom dialogs import ShowDialog, MakePropertyPage, ShowWizardtry: enumerateexcept NameError: # enumerate new in 2.3 def enumerate(seq): return [(i, seq[i]) for i in xrange(len(seq))]# "dialog specific" processors:class StatsProcessor(ControlProcessor): def __init__(self, window, control_ids): self.button_id = control_ids[1] self.reset_date_id = control_ids[2] ControlProcessor.__init__(self, window, control_ids) self.stats = self.window.manager.stats def Init(self): text = "\n".join(self.stats.GetStats()) win32gui.SendMessage(self.GetControl(), win32con.WM_SETTEXT, 0, text) date_label = self.GetControl(self.reset_date_id) if self.stats.from_date: from time import localtime, strftime reset_date = localtime(self.stats.from_date) date_string = strftime("%a, %d %b %Y %I:%M:%S %p", reset_date) else: date_string = _("Never") win32gui.SendMessage(date_label, win32con.WM_SETTEXT, 0, date_string) def OnCommand(self, wparam, lparam): id = win32api.LOWORD(wparam) if id == self.button_id: self.ResetStatistics() def GetPopupHelpText(self, idFrom): if idFrom == self.control_id: return _("Displays statistics on mail processed by SpamBayes") elif idFrom == self.button_id: return _("Resets all SpamBayes statistics to zero") elif idFrom == self.reset_date_id: return _("The date and time when the SpamBayes statistics were last reset") def ResetStatistics(self): question = _("This will reset all your saved statistics to zero.\r\n\r\n" \ "Are you sure you wish to reset the statistics?") flags = win32con.MB_ICONQUESTION | win32con.MB_YESNO | win32con.MB_DEFBUTTON2 if win32gui.MessageBox(self.window.hwnd, question, "SpamBayes", flags) == win32con.IDYES: self.stats.Reset() self.stats.ResetTotal(True) self.Init() # update the statistics displayclass VersionStringProcessor(ControlProcessor): def Init(self): from spambayes.Version import get_current_version import sys v = get_current_version() vstring = v.get_long_version("SpamBayes Outlook Addin") if not hasattr(sys, "frozen"): vstring += _(" from source") win32gui.SendMessage(self.GetControl(), win32con.WM_SETTEXT, 0, vstring) def GetPopupHelpText(self, cid): return _("The version of SpamBayes running")class TrainingStatusProcessor(ControlProcessor): def Init(self): bayes = self.window.manager.classifier_data.bayes nspam = bayes.nspam nham = bayes.nham if nspam > 10 and nham > 10: db_status = _("Database has %d good and %d spam.") % (nham, nspam) db_ratio = nham/float(nspam) big = small = None if db_ratio > 5.0: db_status = _("%s\nWarning: you have much more ham than spam - " \ "SpamBayes works best with approximately even " \ "numbers of ham and spam.") % (db_status, ) elif db_ratio < (1/5.0): db_status = _("%s\nWarning: you have much more spam than ham - " \ "SpamBayes works best with approximately even " \ "numbers of ham and spam.") % (db_status, ) elif nspam > 0 or nham > 0: db_status = _("Database only has %d good and %d spam - you should " \ "consider performing additional training.") % (nham, nspam) else: db_status = _("Database has no training information. SpamBayes " \ "will classify all messages as 'unsure', " \ "ready for you to train.") win32gui.SendMessage(self.GetControl(), win32con.WM_SETTEXT, 0, db_status)class WizardTrainingStatusProcessor(ControlProcessor): def Init(self): bayes = self.window.manager.classifier_data.bayes nspam = bayes.nspam nham = bayes.nham if nspam > 10 and nham > 10: msg = _("SpamBayes has been successfully trained and configured. " \ "You should find the system is immediately effective at " \ "filtering spam.") else: msg = _("SpamBayes has been successfully trained and configured. " \ "However, as the number of messages trained is quite small, " \ "SpamBayes may take some time to become truly effective.") win32gui.SendMessage(self.GetControl(), win32con.WM_SETTEXT, 0, msg)class IntProcessor(OptionControlProcessor): def UpdateControl_FromValue(self): win32gui.SendMessage(self.GetControl(), win32con.WM_SETTEXT, 0, str(self.option.get())) def UpdateValue_FromControl(self): buf_size = 100 buf = win32gui.PyMakeBuffer(buf_size) nchars = win32gui.SendMessage(self.GetControl(), win32con.WM_GETTEXT, buf_size, buf) str_val = buf[:nchars] val = int(str_val) if val < 0 or val > 10: raise ValueError, "Value must be between 0 and 10" self.SetOptionValue(val) def OnCommand(self, wparam, lparam): code = win32api.HIWORD(wparam) if code==win32con.EN_CHANGE: try: self.UpdateValue_FromControl() except ValueError: # They are typing - value may be currently invalid passclass FilterEnableProcessor(BoolButtonProcessor): def OnOptionChanged(self, option): self.Init() def Init(self): BoolButtonProcessor.Init(self) reason = self.window.manager.GetDisabledReason() win32gui.EnableWindow(self.GetControl(), reason is None) def UpdateValue_FromControl(self): check = win32gui.SendMessage(self.GetControl(), win32con.BM_GETCHECK) if check: reason = self.window.manager.GetDisabledReason() if reason is not None: win32gui.SendMessage(self.GetControl(), win32con.BM_SETCHECK, 0) raise ValueError, reason check = not not check # force bool! self.SetOptionValue(check)class FilterStatusProcessor(ControlProcessor): def OnOptionChanged(self, option): self.Init() def Init(self): manager = self.window.manager reason = manager.GetDisabledReason() if reason is not None: win32gui.SendMessage(self.GetControl(), win32con.WM_SETTEXT, 0, reason) return if not manager.config.filter.enabled: status = _("Filtering is disabled. Select 'Enable SpamBayes' to enable.") win32gui.SendMessage(self.GetControl(), win32con.WM_SETTEXT, 0, status) return # ok, enabled and working - put together the status text. config = manager.config.filter certain_spam_name = manager.FormatFolderNames( [config.spam_folder_id], False) if config.unsure_folder_id: unsure_name = manager.FormatFolderNames( [config.unsure_folder_id], False) unsure_text = _("Unsure managed in '%s'") % (unsure_name,) else: unsure_text = _("Unsure messages untouched") if config.ham_folder_id: ham_name = manager.FormatFolderNames( [config.ham_folder_id], False) ham_text = _("Good managed in '%s'") % (ham_name,) else: ham_text = _("Good messages untouched") watch_names = manager.FormatFolderNames( config.watch_folder_ids, config.watch_include_sub) filter_status = _("Watching '%s'.\r\n%s.\r\nSpam managed in '%s'.\r\n%s.") \ % (watch_names, ham_text, certain_spam_name, unsure_text) win32gui.SendMessage(self.GetControl(), win32con.WM_SETTEXT, 0, filter_status)class TabProcessor(ControlProcessor): def __init__(self, window, control_ids, page_ids): ControlProcessor.__init__(self, window, control_ids) self.page_ids = page_ids.split() def Init(self): self.pages = {} self.currentPage = None self.currentPageIndex = -1 self.currentPageHwnd = None for index, page_id in enumerate(self.page_ids): template = self.window.manager.dialog_parser.dialogs[page_id] self.addPage(index, page_id, template[0][0]) self.switchToPage(0) def Done(self): if self.currentPageHwnd is not None: if not self.currentPage.SaveAllControls(): win32gui.SendMessage(self.GetControl(), commctrl.TCM_SETCURSEL, self.currentPageIndex,0) return False return True def OnNotify(self, nmhdr, wparam, lparam): # this does not appear to be in commctrl module selChangedCode = 5177342 code = nmhdr[2] if code==selChangedCode: index = win32gui.SendMessage(self.GetControl(), commctrl.TCM_GETCURSEL, 0,0) if index!=self.currentPageIndex: self.switchToPage(index) def switchToPage(self, index): if self.currentPageHwnd is not None: if not self.currentPage.SaveAllControls(): win32gui.SendMessage(self.GetControl(), commctrl.TCM_SETCURSEL, self.currentPageIndex,0) return 1 win32gui.DestroyWindow(self.currentPageHwnd) self.currentPage = MakePropertyPage(self.GetControl(), self.window.manager, self.window.config, self.pages[index]) self.currentPageHwnd = self.currentPage.CreateWindow() self.currentPageIndex = index return 0 def addPage(self, item, idName, label): format = "iiiiiii" lbuf = win32gui.PyMakeBuffer(len(label)+1) address,l = win32gui.PyGetBufferAddressAndLen(lbuf) win32gui.PySetString(address, label) buf = struct.pack(format, commctrl.TCIF_TEXT, # mask 0, # state 0, # state mask address, 0, #unused 0, #image item ) item = win32gui.SendMessage(self.GetControl(), commctrl.TCM_INSERTITEM, item, buf) self.pages[item] = idNamedef ShowAbout(window): """Opens the SpamBayes documentation in a browser""" window.manager.ShowHtml("about.html")def ShowTrainingDoc(window): """Opens documentation on SpamBayes training in a browser""" window.manager.ShowHtml("docs/welcome.html")def ShowDataFolder(window): """Uses Windows Explorer to show where SpamBayes data and configuration files are stored """ import os import sys filesystem_encoding = sys.getfilesystemencoding() os.startfile(window.manager.data_directory.encode(filesystem_encoding))def ShowLog(window): """Opens the log file for the current SpamBayes session """ import sys, os, win32api, win32con if hasattr(sys, "frozen"): # current log always "spambayes1.log" log_name = os.path.join(win32api.GetTempPath(), "spambayes1.log") if not os.path.exists(log_name): window.manager.ReportError(_("The log file for this session can not be located")) else: cmd = 'notepad.exe "%s"' % log_name win32api.WinExec(cmd, win32con.SW_SHOW) else: question = _("As you are running from source-code, viewing the\n" \ "log means executing a Python program. If you already\n" \ "have a viewer running, the output may appear in either.\n\n"\ "Do you want to execute this viewer?") if not window.manager.AskQuestion(question): return # source-code users - fire up win32traceutil.py import win32traceutil # will already be imported py_name = win32traceutil.__file__ if py_name[-1] in 'co': # pyc/pyo py_name = py_name[:-1] # execute the .py file - hope that this will manage to invoke # python.exe for it. If this breaks for you, feel free to send me # a patch :)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -