cls_Hosts.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. # -*- coding: utf-8 -*-
  2. import os
  3. import json
  4. import chardet
  5. import urllib2
  6. import common_operations as co
  7. DEFAULT_HOSTS_FN = u"DEFAULT.hosts"
  8. class Hosts(object):
  9. CONFIG_FLAG = "#@SwitchHost!"
  10. def __init__(self, index=0, path=None, icon_idx=0):
  11. self.index = index
  12. self.path = path
  13. self.dc_path = co.decode(path)
  14. self.folder, self.fn = os.path.split(path)
  15. # self.fn = co.decode(self.fn)
  16. # if os.name == "nt":
  17. # self.fn = self.fn.decode("GB18030")
  18. self.title = None
  19. self.icon_idx = icon_idx
  20. self.content = ""
  21. self.is_selected = False
  22. self.url = None # 如果是在线hosts方案,则此项不为空
  23. self.last_fetch_dt = None # 如果是在线hosts方案,则此项为最后更新时间
  24. self.read()
  25. @property
  26. def is_read_only(self):
  27. return self.url is not None
  28. def read(self):
  29. if not self.url:
  30. c = open(self.path, "rb").read().strip() if os.path.isfile(self.path) else ""
  31. else:
  32. c = urllib2.urlopen(self.url).read().strip() if co.httpExists(self.url) else ""
  33. # c = co.decode(c)
  34. self.setContent(c, save=False)
  35. def getConfig(self, ln):
  36. u"""从一行内容中取得配置信息"""
  37. cfg = None
  38. v = ln.partition(self.CONFIG_FLAG)[2].strip()
  39. if v:
  40. try:
  41. cfg = json.loads(v)
  42. except Exception:
  43. pass
  44. if cfg:
  45. self.title = cfg.get("title", self.title)
  46. self.icon_idx = cfg.get("icon_idx", self.icon_idx)
  47. def save(self):
  48. if not self.path:
  49. return
  50. cfg = {
  51. "title": self.title,
  52. "icon_idx": self.icon_idx,
  53. }
  54. if self.url:
  55. cfg.update({
  56. "url": self.url,
  57. })
  58. cfg_ln = u"%s %s" % (self.CONFIG_FLAG, json.dumps(cfg).replace("\n", "").replace("\r", ""))
  59. c = self.content
  60. if not repr(c).startswith("u"):
  61. c = c.decode("utf-8")
  62. c = u"%s\n%s" % (cfg_ln, c)
  63. open(self.path, "wb").write(c.encode("utf-8"))
  64. def getTitle(self):
  65. return self.title or self.fn if self.fn != DEFAULT_HOSTS_FN else self.fn
  66. def setTitle(self, title):
  67. self.title = title
  68. self.save()
  69. def setIcon(self, icon_idx):
  70. self.icon_idx = icon_idx
  71. self.save()
  72. def setContent(self, c, save=True):
  73. self.content = c #co.encode(c)
  74. # 查看第一行是否为配置内容
  75. # 第一行以 #SwitchHost 开头表示为配置信息
  76. a = [i.strip() for i in c.split("\n")]
  77. if a[0].startswith(self.CONFIG_FLAG):
  78. self.getConfig(a[0])
  79. self.content = "\n".join(a[1:])
  80. if save:
  81. self.save()
  82. def getContent(self):
  83. c = self.content
  84. if not repr(c).startswith("u"):
  85. try:
  86. cd = chardet.detect(c)
  87. c = c.decode(cd.get("encoding", "utf-8"))
  88. except Exception:
  89. c = c.decode("utf-8")
  90. return c