def getContentFromUrl(self, progress_dlg): co.log("fetch '%s'.." % self.url) if co.httpExists(self.url): if progress_dlg: progress_dlg.Update(10), try: cnt = [] up = 10 url_o = urllib.urlopen(self.url) while True: c = url_o.read(1024) if not c: break cnt.append(c) up += 1 if up < 60: if progress_dlg: progress_dlg.Update(up), cnt = "".join(cnt) if progress_dlg: progress_dlg.Update(60), except Exception: co.debugErr() return "" self.last_fetch_dt = datetime.datetime.now() else: cnt = u"### URL无法访问! ###".encode("utf-8") return cnt
def parseConfigs(self, json_str=None): try: cfg = json.loads(json_str) except Exception: co.log(json_str) co.debugErr() return if type(cfg) != dict: return if self.is_origin: pass elif cfg.get("title"): if not self.title or not self.is_online: self.title = cfg["title"] if cfg.get("url"): if not self.is_online and not self.is_origin: self.url = cfg["url"] self.is_online = True # self.getContent() if cfg.get("icon_idx") is not None: icon_idx = cfg.get("icon_idx") if type(icon_idx) not in (int, long) or \ icon_idx < 0 or icon_idx > len(co.ICONS): icon_idx = 0 self.icon_idx = icon_idx
def OnTreeEndDrag(self, event): co.log("drag end..") target_item = event.GetItem() target_hosts = self.getHostsFromTreeByEvent(event) source_item = self.__dragging_item source_hosts = self.current_dragging_hosts self.__dragging_item = None self.current_dragging_hosts = None # co.log(target_hosts.title) def getHostsIdx(hosts): idx = 0 for h in self.hostses: if h == hosts: break if h.is_online == hosts.is_online: idx += 1 return idx is_dragged = False if target_hosts and target_hosts != source_hosts and \ source_hosts.is_online == target_hosts.is_online: # 拖到目标 hosts 上了 parent = self.m_tree.GetItemParent(target_item) added_item_id = self.m_tree.InsertItemBefore(parent, getHostsIdx(target_hosts), source_hosts.title ) source_hosts.tree_item_id = added_item_id # self.updateHostsTitle(source_hosts) self.updateHostsIcon(source_hosts) if source_hosts == self.current_using_hosts: self.highLightHosts(source_hosts) self.hostses.remove(source_hosts) self.hostses.insert(self.hostses.index(target_hosts), source_hosts) is_dragged = True elif target_item == self.m_tree_local and not source_hosts.is_online: # 拖到本地树上了 pass elif target_item == self.m_tree_online and source_hosts.is_online: # 拖到在线树上了 pass if is_dragged: self.updateConfigs({ "hostses": [hosts.filename for hosts in self.hostses], }) self.saveConfigs() self.m_tree.Delete(source_item) self.m_tree.SelectItem(source_hosts.tree_item_id)
def useHosts(self, hosts): if hosts.is_forever: return if hosts.is_loading: wx.MessageBox(u"当前 hosts 内容正在下载中,请稍后再试...") return try: forever_hosts = self.forever_hostses[0] hosts.save(path=self.sys_hosts_path, extraContent="\n" * 2 + forever_hosts.content) self.notify(msg=u"hosts 已切换为「%s」。" % hosts.title, title=u"hosts 切换成功") except Exception: err = traceback.format_exc() co.log(err) if "Permission denied:" in err: msg = u"切换 hosts 失败!\n没有修改 '%s' 的权限!" % self.sys_hosts_path else: msg = u"切换 hosts 失败!\n\n%s" % err if self.current_showing_hosts: wx.MessageBox(msg, caption=u"出错啦!") return self.tryToFlushDNS() self.highLightHosts(hosts)
def useHosts(self, hosts): if hosts.is_loading: wx.MessageBox(u"当前 hosts 内容正在下载中,请稍后再试...") return try: for common_hosts in self.common_hostses: if common_hosts.is_common: break else: common_hosts = None hosts.save(path=self.sys_hosts_path, common=common_hosts) self.notify(msg=u"hosts 已切换为「%s」。" % hosts.title, title=u"hosts 切换成功") except Exception: err = traceback.format_exc() co.log(err) if "Permission denied:" in err: msg = u"切换 hosts 失败!\n没有修改 '%s' 的权限!" % self.sys_hosts_path else: msg = u"切换 hosts 失败!\n\n%s" % err if self.current_showing_hosts: wx.MessageBox(msg, caption=u"出错啦!") return self.tryToFlushDNS() self.highLightHosts(hosts)
def OnTreeEndDrag(self, event): co.log("drag end..") target_item = event.GetItem() target_hosts = self.getHostsFromTreeByEvent(event) source_item = self.__dragging_item source_hosts = self.current_dragging_hosts self.__dragging_item = None self.current_dragging_hosts = None def getHostsIdx(hosts): idx = 0 for h in self.hostses: if h == hosts: break if h.is_online == hosts.is_online: idx += 1 return idx is_dragged = False if target_hosts and target_hosts != source_hosts and \ source_hosts.is_online == target_hosts.is_online: # 拖到目标 hosts 上了 parent = self.m_tree.GetItemParent(target_item) added_item_id = self.m_tree.InsertItemBefore(parent, getHostsIdx(target_hosts), source_hosts.title ) source_hosts.tree_item_id = added_item_id # self.updateHostsTitle(source_hosts) self.updateHostsIcon(source_hosts) if source_hosts == self.current_using_hosts: self.highLightHosts(source_hosts) self.hostses.remove(source_hosts) self.hostses.insert(self.hostses.index(target_hosts), source_hosts) is_dragged = True elif target_item == self.m_tree_local and not source_hosts.is_online: # 拖到本地树上了 pass elif target_item == self.m_tree_online and source_hosts.is_online: # 拖到在线树上了 pass if is_dragged: self.updateConfigs({ "hostses": [hosts.filename for hosts in self.hostses], }) self.saveConfigs() self.m_tree.Delete(source_item) self.m_tree.SelectItem(source_hosts.tree_item_id)
def updateIcon(self): co.log("update icon") if self.current_using_hosts: if len(self.origin_hostses) > 0: self.updateHostsIcon(self.origin_hostses[0]) self.SetIcon(co.GetMondrianIcon(self.current_using_hosts.icon_idx)) self.taskbar_icon.updateIcon()
def __init__(self, version=None, latest_stable_version=None): wx.Dialog.__init__(self, None, -1, u"关于", style=wx.DEFAULT_DIALOG_STYLE | wx.THICK_FRAME | wx.TAB_TRAVERSAL) update_version = u"欢迎使用!" co.log([version, latest_stable_version]) if latest_stable_version and latest_stable_version != "0": cv = co.compareVersion(version, latest_stable_version) if cv < 0: update_version = u"更新的稳定版 v%s 已经发布!" % latest_stable_version else: update_version = u"您正在使用最新稳定版本。" hwin = AboutHtml(self) hwin.SetPage(u""" <font size="9" color="#44474D"><b>SwitchHosts!</b></font><br /> <font size="3" color="#44474D">%(version)s</font><br /><br /> <font size="3" color="#909090"><i>%(update_version)s</i></font><br /> <p> 本程序用于在多个 hosts 之间快速切换。 </p> <p> 主页:<a href="http://oldj.github.io/SwitchHosts/">http://oldj.github.io/SwitchHosts/</a><br /> <!--源码:<a href="https://github.com/oldj/SwitchHosts">https://github.com/oldj/SwitchHosts</a><br />--> 作者:<a href="http://oldj.net">oldj</a><br /> <br /> 以下网友对本软件也有贡献:<br /> <a href="http://weibo.com/charlestang">@charlestang</a>, <a href="http://weibo.com/allenm56">@allenm</a>, <a href="http://www.weibo.com/emersonli">@emersonli</a>, <a href="https://github.com/qiyuan4f">@qiyuan4f</a> <br /> <br /> <font color="#909090">本程序完全免费,如果您觉得它还不错,欢迎<a href="https://me.alipay.com/oldj">捐赠</a>支持作者,谢谢!</font> </p> """ % { "version": version, "update_version": update_version, }) hwin.FindWindowById(wx.ID_OK) irep = hwin.GetInternalRepresentation() hwin.SetSize((irep.GetWidth() + 25, irep.GetHeight() + 30)) self.SetClientSize(hwin.GetSize()) self.CenterOnParent(wx.BOTH) self.SetFocus()
def OnTreeBeginDrag(self, event): item = event.GetItem() hosts = self.getHostsFromTreeByEvent(event) if not hosts or hosts.is_origin or hosts.is_common: event.Veto() return co.log("drag start..") self.current_dragging_hosts = hosts self.__dragging_item = item event.Allow() self.m_tree.Bind(wx.EVT_MOTION, self._drag_OnMotion) self.m_tree.Bind(wx.EVT_LEFT_UP, self._drag_OnMouseLeftUp)
def __init__(self, version=None, latest_stable_version=None): wx.Dialog.__init__(self, None, -1, u"关于", style=wx.DEFAULT_DIALOG_STYLE|wx.THICK_FRAME|wx.TAB_TRAVERSAL ) update_version = u"欢迎使用!" co.log([version, latest_stable_version]) if latest_stable_version and latest_stable_version != "0": cv = co.compareVersion(version, latest_stable_version) if cv < 0: update_version = u"更新的稳定版 v%s 已经发布!" % latest_stable_version else: update_version = u"您正在使用最新稳定版本。" hwin = AboutHtml(self) hwin.SetPage(u""" <font size="9" color="#44474D"><b>SwitchHosts!</b></font><br /> <font size="3" color="#44474D">%(version)s</font><br /><br /> <font size="3" color="#909090"><i>%(update_version)s</i></font><br /> <p> 本程序用于在多个 hosts 之间快速切换。 </p> <p> 主页:<a href="http://oldj.github.io/SwitchHosts/">http://oldj.github.io/SwitchHosts/</a><br /> <!--源码:<a href="https://github.com/oldj/SwitchHosts">https://github.com/oldj/SwitchHosts</a><br />--> 作者:<a href="http://oldj.net">oldj</a><br /> <br /> 以下网友对本软件也有贡献:<br /> <a href="http://weibo.com/charlestang">@charlestang</a>, <a href="http://weibo.com/allenm56">@allenm</a>, <a href="http://www.weibo.com/emersonli">@emersonli</a>, <a href="https://github.com/qiyuan4f">@qiyuan4f</a> <br /> <br /> <font color="#909090">本程序完全免费,如果您觉得它还不错,欢迎<a href="https://me.alipay.com/oldj">捐赠</a>支持作者,谢谢!</font> </p> """ % { "version": version, "update_version": update_version, }) hwin.FindWindowById(wx.ID_OK) irep = hwin.GetInternalRepresentation() hwin.SetSize((irep.GetWidth() + 25, irep.GetHeight() + 30)) self.SetClientSize(hwin.GetSize()) self.CenterOnParent(wx.BOTH) self.SetFocus()
def useHosts(self, hosts): if hosts.is_loading: wx.MessageBox(u"当前 hosts 内容正在下载中,请稍后再试...") return msg = None is_success = False common_hosts = None try: for common_hosts in self.common_hostses: if common_hosts.is_common: break hosts.save(path=self.sys_hosts_path, common=common_hosts) is_success = True except Exception: err = traceback.format_exc() co.log(err) if "Permission denied:" in err: if sys_type in ("linux", "mac") and self.tryToSaveBySudoPassword( hosts, common_hosts): is_success = True else: msg = u"切换 hosts 失败!\n没有修改 '%s' 的权限!" % self.sys_hosts_path else: msg = u"切换 hosts 失败!\n\n%s" % err if msg and self.current_showing_hosts: wx.MessageBox(msg, caption=u"出错啦!") return if is_success: if len(self.origin_hostses) > 0: self.origin_hostses[0].icon_idx = hosts.icon_idx self.notify(msg=u"hosts 已切换为「%s」。" % hosts.title, title=u"hosts 切换成功") self.tryToFlushDNS() self.highLightHosts(hosts)
def getLatestStableVersion(self, alert=False): url = "https://github.com/oldj/SwitchHosts/blob/master/README.md" # progress_dlg = wx.ProgressDialog(u"检查更新", # u"正在检查最新版本...", 100, # style=wx.PD_AUTO_HIDE # ) # wx.CallAfter(progress_dlg.Update, 10) ver = None try: c = urllib.urlopen(url).read() # wx.CallAfter(progress_dlg.Update, 50) v = re.search(r"\bLatest Stable:\s?(?P<version>[\d\.]+)\b", c) if v: ver = v.group("version") self.latest_stable_version = ver co.log("last_stable_version: %s" % ver) except Exception: pass # wx.CallAfter(progress_dlg.Update, 100) # progress_dlg.Destroy() if not alert: return def _msg(): if not ver: wx.MessageBox(u"未能取得最新版本号!", caption=u"出错啦!") else: cmp = co.compareVersion(self.version, self.latest_stable_version) try: if cmp >= 0: wx.MessageBox(u"当前已是最新版本!") else: wx.MessageBox(u"更新的稳定版 %s 已经发布!" % self.latest_stable_version) except Exception: co.debugErr() pass wx.CallAfter(_msg)
def useHosts(self, hosts): if hosts.is_loading: wx.MessageBox(u"当前 hosts 内容正在下载中,请稍后再试...") return msg = None is_success = False common_hosts = None try: for common_hosts in self.common_hostses: if common_hosts.is_common: break hosts.save(path=self.sys_hosts_path, common=common_hosts) is_success = True except Exception: err = traceback.format_exc() co.log(err) if "Permission denied:" in err: if sys_type in ("linux", "mac") and self.tryToSaveBySudoPassword( hosts, common_hosts ): is_success = True else: msg = u"切换 hosts 失败!\n没有修改 '%s' 的权限!" % self.sys_hosts_path else: msg = u"切换 hosts 失败!\n\n%s" % err if msg and self.current_showing_hosts: wx.MessageBox(msg, caption=u"出错啦!") return if is_success: if len(self.origin_hostses) > 0: self.origin_hostses[0].icon_idx = hosts.icon_idx self.notify(msg=u"hosts 已切换为「%s」。" % hosts.title, title=u"hosts 切换成功") self.tryToFlushDNS() self.highLightHosts(hosts)
def getLatestStableVersion(self, alert=False): url = "https://github.com/oldj/SwitchHosts/blob/master/README.md" ver = None try: c = urllib.urlopen(url).read() # wx.CallAfter(progress_dlg.Update, 50) v = re.search(r"\bLatest Stable:\s?(?P<version>[\d\.]+)\b", c) if v: ver = v.group("version") self.latest_stable_version = ver co.log("last_stable_version: %s" % ver) except Exception: pass if not alert: return def _msg(): if not ver: wx.MessageBox(u"未能取得最新版本号!", caption=u"出错啦!") else: cmpv = co.compareVersion(self.version, self.latest_stable_version) try: if cmpv >= 0: wx.MessageBox(u"当前已是最新版本!") else: if ( wx.MessageBox( u"更新的稳定版 %s 已经发布,现在立刻查看吗?" % self.latest_stable_version, u"发现新版本!", wx.YES_NO | wx.ICON_INFORMATION, ) == wx.YES ): self.openHomepage() except Exception: co.debugErr() pass wx.CallAfter(_msg)
def saveHosts(self, hosts): try: if hosts.save(): co.log("saved.") return True except Exception: err = traceback.format_exc() if "Permission denied:" in err: msg = u"没有修改 '%s' 的权限!" % hosts.path else: msg = u"保存 hosts 失败!\n\n%s" % err wx.MessageBox(msg, caption=u"出错啦!") return False
def getLatestStableVersion(self, alert=False): url = "https://github.com/oldj/SwitchHosts/blob/master/README.md" ver = None try: c = urllib.urlopen(url).read() # wx.CallAfter(progress_dlg.Update, 50) v = re.search(r"\bLatest Stable:\s?(?P<version>[\d\.]+)\b", c) if v: ver = v.group("version") self.latest_stable_version = ver co.log("last_stable_version: %s" % ver) except Exception: pass if not alert: return def _msg(): if not ver: wx.MessageBox(u"未能取得最新版本号!", caption=u"出错啦!") else: cmpv = co.compareVersion(self.version, self.latest_stable_version) try: if cmpv >= 0: wx.MessageBox(u"当前已是最新版本!") else: if wx.MessageBox( u"更新的稳定版 %s 已经发布,现在立刻查看吗?" % self.latest_stable_version, u"发现新版本!", wx.YES_NO | wx.ICON_INFORMATION) == wx.YES: self.openHomepage() except Exception: co.debugErr() pass wx.CallAfter(_msg)
def useHosts(self, hosts): if hosts.is_loading: wx.MessageBox(u"当前 hosts 内容正在下载中,请稍后再试...") return try: for common_hosts in self.common_hostses: if common_hosts.is_common: break else: common_hosts = None hosts.save(path=self.sys_hosts_path, common=common_hosts) if len(self.origin_hostses) > 0: self.origin_hostses[0].icon_idx = hosts.icon_idx self.notify(msg=u"hosts 已切换为「%s」。" % hosts.title, title=u"hosts 切换成功") except Exception: err = traceback.format_exc() co.log(err) if "Permission denied:" in err: msg = u"切换 hosts 失败!\n没有修改 '%s' 的权限!" % self.sys_hosts_path else: msg = u"切换 hosts 失败!\n\n%s" % err if self.current_showing_hosts: wx.MessageBox(msg, caption=u"出错啦!") return self.tryToFlushDNS() self.highLightHosts(hosts)
def OnTreeMenu(self, event): co.log("tree menu...")
def updateIcon(self): co.log("update icon") if self.current_using_hosts: self.SetIcon(co.GetMondrianIcon(self.current_using_hosts.icon_idx)) self.taskbar_icon.updateIcon()
def _drag_OnMouseLeftUp(self, event): co.log("mouse left up..") self.m_tree.Unbind(wx.EVT_MOTION) self.m_tree.Unbind(wx.EVT_LEFT_UP) event.Skip()