浏览代码

Merge support for ConfigDict behaving more like a dictionary.

Jelmer Vernooij 13 年之前
父节点
当前提交
67871d3da6
共有 3 个文件被更改,包括 26 次插入1 次删除
  1. 3 0
      NEWS
  2. 12 1
      dulwich/config.py
  3. 11 0
      dulwich/tests/test_config.py

+ 3 - 0
NEWS

@@ -5,6 +5,9 @@
   * dulwich.__init__ no longer imports client, protocol, repo and
     server modules. (Jelmer Vernooij)
 
+  * ConfigDict now behaves more like a dictionary.
+    (Adam 'Cezar' Jenkins, issue #58)
+
  TESTING
 
   * Make index entry tests a little bit less strict, to cope with

+ 12 - 1
dulwich/config.py

@@ -28,6 +28,8 @@ import errno
 import os
 import re
 
+from UserDict import DictMixin
+
 from dulwich.file import GitFile
 
 
@@ -73,7 +75,7 @@ class Config(object):
         raise NotImplementedError(self.set)
 
 
-class ConfigDict(Config):
+class ConfigDict(Config, DictMixin):
     """Git configuration stored in a dictionary."""
 
     def __init__(self, values=None):
@@ -90,6 +92,15 @@ class ConfigDict(Config):
             isinstance(other, self.__class__) and
             other._values == self._values)
 
+    def __getitem__(self, key):
+        return self._values[key]
+      
+    def __setitem__(self, key, value):
+        self._values[key] = value
+        
+    def keys(self):
+        return self._values.keys()
+
     @classmethod
     def _parse_setting(cls, name):
         parts = name.split(".")

+ 11 - 0
dulwich/tests/test_config.py

@@ -170,6 +170,17 @@ class ConfigDictTests(TestCase):
         cd.set(("core", ), "foo", "invalid")
         self.assertRaises(ValueError, cd.get_boolean, ("core", ), "foo")
 
+    def test_dict(self):
+        cd = ConfigDict()
+        cd.set(("core", ), "foo", "bla")
+        cd.set(("core2", ), "foo", "bloe")
+
+        self.assertEqual([("core2", ), ("core", )], cd.keys())
+        self.assertEqual(cd[("core", )], {'foo': 'bla'})
+
+        cd['a'] = 'b'
+        self.assertEqual(cd['a'], 'b')
+
 
 class StackedConfigTests(TestCase):