]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
ConfigParser:
authorFred Drake <fdrake@acm.org>
Tue, 18 May 2004 04:24:02 +0000 (04:24 +0000)
committerFred Drake <fdrake@acm.org>
Tue, 18 May 2004 04:24:02 +0000 (04:24 +0000)
- read() method returns a list of files parsed successfully
- add tests, documentation
(closes SF patch #677651)

Doc/lib/libcfgparser.tex
Lib/ConfigParser.py
Lib/test/cfgparser.1 [new file with mode: 0644]
Lib/test/test_cfgparser.py

index d5c9757ad0afd68cadd24be72e1f1c1dc0c3c466..bc15655c93ea51eba6b3533b672ad6520fb83dbe 100644 (file)
@@ -177,7 +177,8 @@ return \constant{True}; otherwise return \constant{False}.
 \end{methoddesc}
 
 \begin{methoddesc}{read}{filenames}
-Read and parse a list of filenames.  If \var{filenames} is a string or
+Attempt to read and parse a list of filenames, returning a list of filenames
+which were successfully parsed.  If \var{filenames} is a string or
 Unicode string, it is treated as a single filename.
 If a file named in \var{filenames} cannot be opened, that file will be
 ignored.  This is designed so that you can specify a list of potential
@@ -197,6 +198,7 @@ config = ConfigParser.ConfigParser()
 config.readfp(open('defaults.cfg'))
 config.read(['site.cfg', os.path.expanduser('~/.myapp.cfg')])
 \end{verbatim}
+\versionchanged[Returns list of successfully parsed filenames]{2.4}
 \end{methoddesc}
 
 \begin{methoddesc}{readfp}{fp\optional{, filename}}
index fccfcdfea644f58930c6615edd11b4c39eab2694..5f80269407c0dfc605875f7032159f98327457ac 100644 (file)
@@ -45,7 +45,7 @@ ConfigParser -- responsible for parsing a list of
     read(filenames)
         read and parse the list of named configuration files, given by
         name.  A single filename is also allowed.  Non-existing files
-        are ignored.
+        are ignored.  Return list of successfully read files.
 
     readfp(fp, filename=None)
         read and parse one configuration file, given as a file object.
@@ -252,9 +252,12 @@ class RawConfigParser:
         home directory, systemwide directory), and all existing
         configuration files in the list will be read.  A single
         filename may also be given.
+
+        Return list of successfully read files.
         """
         if isinstance(filenames, basestring):
             filenames = [filenames]
+        read_ok = []
         for filename in filenames:
             try:
                 fp = open(filename)
@@ -262,6 +265,8 @@ class RawConfigParser:
                 continue
             self._read(fp, filename)
             fp.close()
+            read_ok.append(filename)
+        return read_ok
 
     def readfp(self, fp, filename=None):
         """Like read() but the argument must be a file-like object.
diff --git a/Lib/test/cfgparser.1 b/Lib/test/cfgparser.1
new file mode 100644 (file)
index 0000000..3387f52
--- /dev/null
@@ -0,0 +1,2 @@
+[Foo Bar]
+foo=newbar
index b40cedf4ab17b50556f9f4ea73895d33be33e44e..c799c7df0a714cb4333452b0a41aa9304ad78ebb 100644 (file)
@@ -242,6 +242,27 @@ class TestCaseBase(unittest.TestCase):
         self.assertRaises(TypeError, cf.set, "sect", "option2", 1.0)
         self.assertRaises(TypeError, cf.set, "sect", "option2", object())
 
+    def test_read_returns_file_list(self):
+        file1 = test_support.findfile("cfgparser.1")
+        # check when we pass a mix of readable and non-readable files:
+        cf = self.newconfig()
+        parsed_files = cf.read([file1, "nonexistant-file"])
+        self.assertEqual(parsed_files, [file1])
+        self.assertEqual(cf.get("Foo Bar", "foo"), "newbar")
+        # check when we pass only a filename:
+        cf = self.newconfig()
+        parsed_files = cf.read(file1)
+        self.assertEqual(parsed_files, [file1])
+        self.assertEqual(cf.get("Foo Bar", "foo"), "newbar")
+        # check when we pass only missing files:
+        cf = self.newconfig()
+        parsed_files = cf.read(["nonexistant-file"])
+        self.assertEqual(parsed_files, [])
+        # check when we pass no files:
+        cf = self.newconfig()
+        parsed_files = cf.read([])
+        self.assertEqual(parsed_files, [])
+
     # shared by subclasses
     def get_interpolation_config(self):
         return self.fromstring(