瀏覽代碼

add write_to_{file,path} and from_{file,path} methods to ConfigFile.

Jelmer Vernooij 13 年之前
父節點
當前提交
c2be965809
共有 2 個文件被更改,包括 35 次插入4 次删除
  1. 27 4
      dulwich/config.py
  2. 8 0
      dulwich/tests/test_config.py

+ 27 - 4
dulwich/config.py

@@ -20,6 +20,8 @@
 
 """
 
+from dulwich.file import GitFile
+
 
 class ConfigFile(object):
     """A Git configuration file, like .git/config or ~/.gitconfig."""
@@ -31,8 +33,29 @@ class ConfigFile(object):
         return isinstance(other, self.__class__)
 
     @classmethod
-    def from_file(cls, path):
+    def from_file(cls, f):
+        """Read configuration from a file-like object."""
         ret = cls()
-        f = open(path, 'r')
-        # FIXME?
-        f.close()
+        # FIXME
+        return ret
+
+    @classmethod
+    def from_path(cls, path):
+        """Read configuration from a file on disk."""
+        f = GitFile(path, 'r')
+        try:
+            return cls.from_file(f)
+        finally:
+            f.close()
+
+    def write_to_path(self, path):
+        """Write configuration to a file on disk."""
+        f = GitFile(path, 'w')
+        try:
+            self.write_to_file(f)
+        finally:
+            f.close()
+
+    def write_to_file(self, f):
+        """Write configuration to a file-like object."""
+        # FIXME

+ 8 - 0
dulwich/tests/test_config.py

@@ -18,14 +18,22 @@
 
 """Tests for reading and writing configuraiton files."""
 
+from cStringIO import StringIO
 from dulwich.config import ConfigFile
 from dulwich.tests import TestCase
 
 
 class ConfigFileTests(TestCase):
 
+    def from_file(self, text):
+        return ConfigFile.from_file(StringIO(text))
+
     def test_empty(self):
         ConfigFile()
 
     def test_eq(self):
         self.assertEquals(ConfigFile(), ConfigFile())
+
+    def test_from_file_empty(self):
+        cf = self.from_file("")
+        self.assertEquals(ConfigFile(), cf)