def getSysHostsTitle(): global g_local_hosts_dir sys_hosts = co.getSysHostsPath() path = os.path.join(g_local_hosts_dir, DEFAULT_HOSTS_FN) ohosts_sys = Hosts(path=sys_hosts) sys_hosts_title = ohosts_sys.getTitle() is_title_valid = False fns = listLocalHosts() for fn in fns: fn2 = os.path.split(fn)[1] i, t, t2 = fn2.partition(".") if not i.isdigit(): continue ohosts = Hosts(path=fn) if ohosts.getTitle() == sys_hosts_title: is_title_valid = True break if not is_title_valid: open(path, "wb").write(open(sys_hosts, "rb").read()) # else: # if os.path.isfile(path): # os.remove(path) return sys_hosts_title if is_title_valid else None
def newHosts(self, event=None, default=""): u"""新建一个 hosts""" global g_local_hosts_dir repeat = False title = default dlg = wx.TextEntryDialog(None, u"新建 hosts", u"输入 hosts 名:", title, style=wx.OK | wx.CANCEL) if dlg.ShowModal() == wx.ID_OK: title = dlg.GetValue().strip() if title: oh = self.getOHostsFromTitle(title) if oh: repeat = True self.alert(u"命名失败!", u"名为 '%s' 的 hosts 已经存在了!" % title) else: # 保存新文件 path = self.mkNewHostsPath() c = u"# %s" % title oh = Hosts(path=path) oh.setTitle(title) oh.setContent(c) self.updateHostsList() dlg.Destroy() if repeat: self.newHosts(event, default=title)
def newHosts(self, event=None, default=""): u"""新建一个 hosts""" global g_local_hosts_dir repeat = False title = default self.newHosts_test() return dlg = wx.TextEntryDialog(None, u"新建 hosts", u"输入 hosts 名:", title, style=wx.OK | wx.CANCEL) if dlg.ShowModal() == wx.ID_OK: title = dlg.GetValue().strip() if title: oh = self.getOHostsFromTitle(title) if oh: repeat = True self.alert(u"命名失败!", u"名为 '%s' 的 hosts 已经存在了!" % title) else: # 保存新文件 path = self.mkNewHostsPath() c = u"# %s" % title oh = Hosts(path=path) oh.setTitle(title) oh.setContent(c) self.updateHostsList() dlg.Destroy() if repeat: self.newHosts(event, default=title)
def updateHostsList(self, selected_title=None): u"""更新 hosts 列表""" hosts_list = listLocalHosts() # hosts_list.insert(0, co.getSysHostsPath()) hosts_list = [list(os.path.split(fn)) + [fn] for fn in hosts_list] self.hosts_lists = hosts_list self.hosts_objects = [] self.m_list.DeleteAllItems() ch = self.taskbar_icon.current_hosts c_idx = -1 il = wx.ImageList(16, 16, True) icons_count = len(co.ICONS) for i in xrange(icons_count): il.Add(co.GetMondrianBitmap(i)) self.m_list.AssignImageList(il, wx.IMAGE_LIST_SMALL) for idx, (folder, fn, fn2) in enumerate(hosts_list): icon_idx = idx if idx < icons_count else icons_count - 1 ohosts = Hosts(idx, fn2, icon_idx) self.hosts_objects.append(ohosts) i, t, t2 = fn.partition(".") if i.isdigit(): i = int(i) if i > self.current_max_hosts_index: self.current_max_hosts_index = i c = "" index = self.m_list.InsertStringItem(sys.maxint, ohosts.getTitle()) # 如果指定了当前选中的 hosts if ohosts.getTitle() == selected_title: ch = self.taskbar_icon.current_hosts = fn2 self.SetIcon(co.GetMondrianIcon(ohosts.icon_idx)) self.taskbar_icon.SetIcon(co.GetMondrianIcon(ohosts.icon_idx), u"当前 hosts 方案:%s" % ohosts.getTitle()) self.m_list.SetItemFont(idx, self.font_bold) if (ch and ch == fn2) or \ (not selected_title and not ch and co.decode(fn) == DEFAULT_HOSTS_FN): c = SELECTED_FLAG if c: c_idx = index self.m_list.SetStringItem(index, 1, c) self.m_list.SetItemImage(index, ohosts.icon_idx, ohosts.icon_idx) if self.current_selected_hosts_index >= 0: c_idx = self.current_selected_hosts_index while c_idx >= len(self.hosts_objects) and c_idx > 0: c_idx -= 1 ohosts = self.hosts_objects[c_idx] self.m_list.Select(c_idx) self.current_selected_hosts_index = c_idx self.current_selected_hosts_fn = self.hosts_objects[c_idx].path self.m_textCtrl_content.Value = ohosts.getContent()
def updateHostsList(self, selected_title=None): u"""更新 hosts 列表""" hosts_list = listLocalHosts() # hosts_list.insert(0, co.getSysHostsPath()) hosts_list = [list(os.path.split(fn)) + [fn] for fn in hosts_list] self.hosts_lists = hosts_list self.hosts_objects = [] self.m_list.DeleteAllItems() ch = self.taskbar_icon.current_hosts c_idx = -1 il = wx.ImageList(16, 16, True) icons_count = len(co.ICONS) for i in xrange(icons_count): il.Add(co.GetMondrianBitmap(i)) self.m_list.AssignImageList(il, wx.IMAGE_LIST_SMALL) for idx, (folder, fn, fn2) in enumerate(hosts_list): icon_idx = idx if idx < icons_count else icons_count - 1 ohosts = Hosts(idx, fn2, icon_idx) self.hosts_objects.append(ohosts) i, t, t2 = fn.partition(".") if i.isdigit(): i = int(i) if i > self.current_max_hosts_index: self.current_max_hosts_index = i c = "" index = self.m_list.InsertStringItem(sys.maxint, ohosts.getTitle()) # 如果指定了当前选中的 hosts if ohosts.getTitle() == selected_title: ch = self.taskbar_icon.current_hosts = fn2 self.SetIcon(co.GetMondrianIcon(ohosts.icon_idx)) self.taskbar_icon.SetIcon( co.GetMondrianIcon(ohosts.icon_idx), u"当前 hosts 方案:%s" % ohosts.getTitle()) self.m_list.SetItemFont(idx, self.font_bold) if (ch and ch == fn2) or \ (not selected_title and not ch and co.decode(fn) == DEFAULT_HOSTS_FN): c = SELECTED_FLAG if c: c_idx = index self.m_list.SetStringItem(index, 1, c) self.m_list.SetItemImage(index, ohosts.icon_idx, ohosts.icon_idx) if self.current_selected_hosts_index >= 0: c_idx = self.current_selected_hosts_index while c_idx >= len(self.hosts_objects) and c_idx > 0: c_idx -= 1 ohosts = self.hosts_objects[c_idx] self.m_list.Select(c_idx) self.current_selected_hosts_index = c_idx self.current_selected_hosts_fn = self.hosts_objects[c_idx].path self.m_textCtrl_content.Value = ohosts.getContent()