prefs.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. # The contents of this file are subject to the BitTorrent Open Source License
  2. # Version 1.1 (the License). You may not copy or use this file, in either
  3. # source code or executable form, except in compliance with the License. You
  4. # may obtain a copy of the License at http://www.bittorrent.com/license/.
  5. #
  6. # Software distributed under the License is distributed on an AS IS basis,
  7. # WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  8. # for the specific language governing rights and limitations under the
  9. # License.
  10. class Preferences(object):
  11. def __init__(self, parent=None, persist_callback=None):
  12. self._parent = None
  13. self._options = {}
  14. self._persist_callback = persist_callback
  15. if parent:
  16. self._parent = parent
  17. def initWithDict(self, dict):
  18. self._options = dict
  19. return self
  20. def getDict(self):
  21. return dict(self._options)
  22. def getDifference(self):
  23. if self._parent:
  24. return dict([(x, y) for x, y in self._options.items() if y != self._parent.get(x, None)])
  25. else:
  26. return dict(self._options)
  27. # To catch encoding errors, if something was ever stored as
  28. # unicode, it must ALWAYS be stored as unicode.
  29. def __getitem__(self, option):
  30. if self._options.has_key(option):
  31. return self._options[option]
  32. elif self._parent:
  33. return self._parent[option]
  34. return None
  35. def __setitem__(self, option, value):
  36. if self._options.has_key(option):
  37. # The next two asserts are a short-term hack. A more general
  38. # solution is to associate allowed type(s) for each option, and
  39. # then have the Preferences object enforce those types. --Dave.
  40. assert not isinstance(self._options[option], unicode) or \
  41. isinstance(value, unicode), "'%s' is not unicode" % option
  42. assert not isinstance(self._options[option], str) or \
  43. isinstance(value, str), "'%s' is not str" % option
  44. self._options.__setitem__(option, value)
  45. if self._persist_callback:
  46. self._persist_callback()
  47. def __len__(self):
  48. l = len(self._options)
  49. if self._parent:
  50. return l + len(self._parent)
  51. else:
  52. return l
  53. def __delitem__(self, option):
  54. del(self._options[option])
  55. if self._persist_callback:
  56. self._persist_callback()
  57. def __contains__(self, option):
  58. if option in self._options:
  59. return True
  60. if self._parent and option in self._parent:
  61. return True
  62. return False
  63. def setdefault(self, option, default):
  64. if option not in self:
  65. self[option] = default
  66. return self[option]
  67. def clear(self):
  68. self._options.clear()
  69. if self._persist_callback:
  70. self._persist_callback()
  71. def has_key(self, option):
  72. if self._options.has_key(option):
  73. return True
  74. elif self._parent:
  75. return self._parent.has_key(option)
  76. return False
  77. def keys(self):
  78. l = self._options.keys()
  79. if self._parent:
  80. l += [key for key in self._parent.keys() if key not in l]
  81. return l
  82. def values(self):
  83. l = self._options.values()
  84. if self._parent:
  85. l += [value for value in self._parent.values() if value not in l]
  86. return l
  87. def items(self):
  88. l = self._options.items()
  89. if self._parent:
  90. l += [item for item in self._parent.items() if item not in l]
  91. return l
  92. def __iter__(self): return self.iterkeys()
  93. def __str__(self): return 'Preferences({%s})' % str(self.items())
  94. def iteritems(self): return self.items().__iter__()
  95. def iterkeys(self): return self.keys().__iter__()
  96. def itervalues(self): return self.values().__iter__()
  97. def update(self, dict):
  98. v = self._options.update(dict)
  99. if self._persist_callback:
  100. self._persist_callback()
  101. return v
  102. def get(self, key, failobj=None):
  103. if not self.has_key(key):
  104. return failobj
  105. return self[key]