📄 configreader.py
字号:
#written by John Hoffmanfrom ConnChoice import *from wxPython.wx import *from types import *from sys import *import osfalse = 0true = not falsebasepath=os.path.abspath(os.path.dirname(sys.argv[0]))class configReader: def __init__(self, defaults = None): self.configfile = wxConfig("BitTorrent") self.configMenuBox = None self.advancedMenuBox = None self.configReset = true # run reset for the first time self.configFileDefaults = { #args only available here: 'win32_taskbar_icon': 1, # "whether to iconize do system try or not on win32"), 'gui_stretchwindow': 0, # "whether to stretch the download status window to fit the torrent name"), 'gui_displaystats': 1, # "whether to display statistics on peers and seeds"), 'gui_displaymiscstats': 1, # "whether to display miscellaneous other statistics"), 'gui_ratesettingsdefault': 'unlimited', # "the default setting for maximum upload rate and users"), 'gui_ratesettingsmode': 'full', # "what rate setting controls to display; options are 'none', 'basic', and 'full'"), 'gui_forcegreenonfirewall': 0, # "forces the status icon to be green even if the client seems to be firewalled") 'gui_checkingcolor': 'FF FF FF', 'gui_downloadcolor': '00 00 00', 'gui_seedingcolor': '00 FF 00', 'gui_default_savedir': '', 'last_saved': '', # hidden; not set in config } if (sys.platform == 'win32'): self.FONT = 9 else: self.FONT = 10 self.configFileDefaults['gui_font'] = self.FONT self.configFileDefaults['gui_checkingcolor'] = self.ColorToHex(wx.wxSystemSettings_GetColour(wxSYS_COLOUR_3DSHADOW)) self.configFileDefaults['gui_downloadcolor'] = self.ColorToHex(wx.wxSystemSettings_GetColour(wxSYS_COLOUR_ACTIVECAPTION)) self.gui_ratesettingslist = [] for x in connChoices: if not x.has_key('super-seed'): self.gui_ratesettingslist.append(x['name']) self.configFileDefaults['gui_ratesettingsdefault'] = self.gui_ratesettingslist[0] configfileargs = { # args listed in download.py: 'minport': None, 'maxport': None, 'ip': None, 'bind': None, 'min_peers': None, 'max_initiate': None, 'display_interval': None, 'alloc_type': None, 'alloc_rate': None, 'max_files_open': None, 'security': None, 'super_seeder': None,
'max_connections': None, } self.downloaddefaultargs = configfileargs.keys() for name in self.configFileDefaults: configfileargs[name] = self.configFileDefaults[name] self.configfileargs = configfileargs self.import_defaults(self.configFileDefaults.keys()) self.checkingcolor = self.HexToColor(self.configfileargs['gui_checkingcolor']) self.downloadcolor = self.HexToColor(self.configfileargs['gui_downloadcolor']) self.seedingcolor = self.HexToColor(self.configfileargs['gui_seedingcolor']) self.FONT = self.configfileargs['gui_font'] self.default_font = wxFont(self.FONT, wxDEFAULT, wxNORMAL, wxNORMAL, false) if defaults is not None: self.setDownloadDefaults(defaults) def setDownloadDefaults(self, defaults = None): self.defaults = defaults self.import_defaults(self.downloaddefaultargs) updated = false # make all config default changes here if self.configfileargs['gui_ratesettingsdefault'] not in self.gui_ratesettingslist: self.configfileargs['gui_ratesettingsdefault'] = ( self.configFileDefaults['gui_ratesettingsdefault']) updated = true if updated: self.writeConfigFile() def import_defaults(self,list): for name in list: try: default = self.defaults[name] except: default = self.configFileDefaults[name] if type(default) is IntType: if self.configfile.Exists(name): try: setting = self.configfile.ReadInt(name,0) except: setting = None if setting is None: setting = default self.configfile.WriteInt(name,default) self.configfileargs[name] = setting else: self.configfileargs[name] = default self.configfile.WriteInt(name,default) elif type(default) is FloatType: if self.configfile.Exists(name): try: setting = self.configfile.ReadFloat(name,0.0) except: setting = None if setting is None: setting = default self.configfile.WriteFloat(name,default) self.configfileargs[name] = setting else: self.configfileargs[name] = default self.configfile.WriteFloat(name,default) elif type(default) is StringType: if self.configfile.Exists(name): try: setting = self.configfile.Read(name,'') except: setting = None if (setting is None) or (setting == '') or (setting == 'None'): setting = default self.configfile.Write(name,default) self.configfileargs[name] = setting else: self.configfileargs[name] = default self.configfile.Write(name,default) #else skip it... self.configfile.Flush() def HexToColor(self, s): r,g,b = s.split(' ') return wxColour(red=int(r,16), green=int(g,16), blue=int(b,16)) def ColorToHex(self, c): return hex(c.Red()) + ' ' + hex(c.Green()) + ' ' + hex(c.Blue()) def resetConfigDefaults(self): for name in self.configFileDefaults: self.configfileargs[name] = self.configFileDefaults[name] for name in self.configfileargs: default = self.defaults.get(name) if default is not None: self.configfileargs[name] = default writeConfigFile() def writeConfigFile(self): for name in self.configfileargs: default = self.configfileargs[name] if type(default) is IntType: self.configfile.WriteInt(name,default) elif type(default) is FloatType: self.configfile.WriteFloat(name,default) else: # assume StringType: self.configfile.Write(name,str(default)) self.configfile.Flush() def WriteLastSaved(self, l): self.configfileargs['last_saved'] = l self.configfile.Write('last_saved',l) self.configfile.Flush() def setColorIcon(self, xxicon, xxiconptr, xxcolor): idata = wxMemoryDC() idata.SelectObject(xxicon) idata.SetBrush(wxBrush(xxcolor,wxSOLID)) idata.DrawRectangle(0,0,16,16) idata.SelectObject(wxNullBitmap) xxiconptr.Refresh() def getColorFromUser(self, parent, colInit): data = wxColourData() if colInit.Ok(): data.SetColour(colInit) data.SetCustomColour(0, self.checkingcolormenu) data.SetCustomColour(1, self.downloadcolormenu) data.SetCustomColour(2, self.seedingcolormenu) dlg = wxColourDialog(parent,data) if not dlg.ShowModal(): return colInit return dlg.GetColourData().GetColour() def configMenu(self, parent): self.parent = parent try: self.checkingcolormenu = self.checkingcolor self.downloadcolormenu = self.downloadcolor self.seedingcolormenu = self.seedingcolor if (self.configMenuBox is not None): try: self.configMenuBox.Close () except wxPyDeadObjectError, e: self.configMenuBox = None self.configMenuBox = wxFrame(None, -1, 'BitTorrent Preferences', size = (1,1)) if (sys.platform == 'win32'): self.icon = wxIcon(os.path.join(basepath,'icon_bt.ico'), wxBITMAP_TYPE_ICO) self.configMenuBox.SetIcon(self.icon) panel = wxPanel(self.configMenuBox, -1) self.panel = panel def StaticText(text, font = self.FONT, underline = false, color = None, panel = panel): x = wxStaticText(panel, -1, text, style = wxALIGN_LEFT) x.SetFont(wxFont(font, wxDEFAULT, wxNORMAL, wxNORMAL, underline)) if color is not None: x.SetForegroundColour(color) return x colsizer = wxFlexGridSizer(cols = 1, vgap = 8) gui_stretchwindow_checkbox = wxCheckBox(panel, -1, "Stretch window to fit torrent name *") gui_stretchwindow_checkbox.SetFont(self.default_font) gui_stretchwindow_checkbox.SetValue(self.configfileargs['gui_stretchwindow']) gui_displaystats_checkbox = wxCheckBox(panel, -1, "Display peer and seed statistics") gui_displaystats_checkbox.SetFont(self.default_font) gui_displaystats_checkbox.SetValue(self.configfileargs['gui_displaystats']) gui_displaymiscstats_checkbox = wxCheckBox(panel, -1, "Display miscellaneous other statistics") gui_displaymiscstats_checkbox.SetFont(self.default_font) gui_displaymiscstats_checkbox.SetValue(self.configfileargs['gui_displaymiscstats']) security_checkbox = wxCheckBox(panel, -1, "Enable extra client security *") security_checkbox.SetFont(self.default_font) security_checkbox.SetValue(self.configfileargs['security']) gui_forcegreenonfirewall_checkbox = wxCheckBox(panel, -1, "Force icon to display green when firewalled") gui_forcegreenonfirewall_checkbox.SetFont(self.default_font) gui_forcegreenonfirewall_checkbox.SetValue(self.configfileargs['gui_forcegreenonfirewall']) minport_data = wxSpinCtrl(panel, -1, '', (-1,-1), (self.FONT*7, -1)) minport_data.SetFont(self.default_font) minport_data.SetRange(1,65535) minport_data.SetValue(self.configfileargs['minport']) maxport_data = wxSpinCtrl(panel, -1, '', (-1,-1), (self.FONT*7, -1)) maxport_data.SetFont(self.default_font) maxport_data.SetRange(1,65535) maxport_data.SetValue(self.configfileargs['maxport']) gui_font_data = wxSpinCtrl(panel, -1, '', (-1,-1), (self.FONT*5, -1)) gui_font_data.SetFont(self.default_font) gui_font_data.SetRange(8,16) gui_font_data.SetValue(self.configfileargs['gui_font'])
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -