瀏覽代碼

Add StackedConfig.

Jelmer Vernooij 13 年之前
父節點
當前提交
718e8185f8
共有 2 個文件被更改,包括 62 次插入6 次删除
  1. 52 5
      dulwich/config.py
  2. 10 1
      dulwich/tests/test_config.py

+ 52 - 5
dulwich/config.py

@@ -20,10 +20,18 @@
 
 """
 
+import errno
+import os
+
 from dulwich.file import GitFile
 
 
-class ConfigFile(object):
+class Config(object):
+    """A Git configuration."""
+
+
+
+class ConfigFile(Config):
     """A Git configuration file, like .git/config or ~/.gitconfig."""
 
     def __init__(self):
@@ -42,15 +50,19 @@ class ConfigFile(object):
     @classmethod
     def from_path(cls, path):
         """Read configuration from a file on disk."""
-        f = GitFile(path, 'r')
+        f = GitFile(path, 'rb')
         try:
-            return cls.from_file(f)
+            ret = cls.from_file(f)
+            ret.path = path
+            return ret
         finally:
             f.close()
 
-    def write_to_path(self, path):
+    def write_to_path(self, path=None):
         """Write configuration to a file on disk."""
-        f = GitFile(path, 'w')
+        if path is None:
+            path = self.path
+        f = GitFile(path, 'wb')
         try:
             self.write_to_file(f)
         finally:
@@ -59,3 +71,38 @@ class ConfigFile(object):
     def write_to_file(self, f):
         """Write configuration to a file-like object."""
         # FIXME
+
+
+class StackedConfig(Config):
+    """Configuration which reads from multiple config files.."""
+
+    def __init__(self, backends):
+        self._backends = backends
+
+    def __repr__(self):
+        return "<%s for %r>" % (self.__class__.__name__, self._backends)
+
+    @classmethod
+    def default(cls, for_path=None):
+        """Retrieve the default configuration.
+
+        This will look in the repository configuration (if for_path is
+        specified), the users' home directory and the system
+        configuration.
+        """
+        paths = []
+        if for_path is not None:
+            paths.append(for_path)
+        paths.append(os.path.expanduser("~/.gitconfig"))
+        paths.append("/etc/gitconfig")
+        backends = []
+        for path in paths:
+            try:
+                cf = ConfigFile.from_path(path)
+            except IOError, e:
+                if e.errno != errno.ENOENT:
+                    raise
+                else:
+                    continue
+            backends.append(cf)
+        return cls(backends)

+ 10 - 1
dulwich/tests/test_config.py

@@ -19,7 +19,10 @@
 """Tests for reading and writing configuraiton files."""
 
 from cStringIO import StringIO
-from dulwich.config import ConfigFile
+from dulwich.config import (
+    ConfigFile,
+    StackedConfig,
+    )
 from dulwich.tests import TestCase
 
 
@@ -37,3 +40,9 @@ class ConfigFileTests(TestCase):
     def test_from_file_empty(self):
         cf = self.from_file("")
         self.assertEquals(ConfigFile(), cf)
+
+
+class StackedConfigTests(TestCase):
+
+    def test_default(self):
+        StackedConfig.default()