[rtems-tools commit] rtemstoolkit/configuration: Add get_sections() to get the sections.

Chris Johns chrisj at rtems.org
Tue Jun 11 23:52:08 UTC 2019


Module:    rtems-tools
Branch:    master
Commit:    89dcdbc9fface5e52ad75043c3cb6745233ba987
Changeset: http://git.rtems.org/rtems-tools/commit/?id=89dcdbc9fface5e52ad75043c3cb6745233ba987

Author:    Chris Johns <chrisj at rtems.org>
Date:      Thu Jun  6 20:49:22 2019 +1000

rtemstoolkit/configuration: Add get_sections() to get the sections.

- Fix module access when catching exceptions.

---

 rtemstoolkit/configuration.py | 28 ++++++++++++++++++++--------
 1 file changed, 20 insertions(+), 8 deletions(-)

diff --git a/rtemstoolkit/configuration.py b/rtemstoolkit/configuration.py
index 3b03296..bc3ec93 100644
--- a/rtemstoolkit/configuration.py
+++ b/rtemstoolkit/configuration.py
@@ -40,16 +40,21 @@ import re
 from rtemstoolkit import error
 from rtemstoolkit import path
 
+try:
+    import configparser
+    has_strict = True
+except:
+    # python2
+    import ConfigParser as configparser
+    has_strict = False
+
 class configuration:
 
     def __init__(self, raw = True):
         self.raw = True
-        try:
-            import configparser
+        if has_strict:
             self.config = configparser.ConfigParser(strict = False)
-        except:
-            # python2
-            import ConfigParser as configparser
+        else:
             self.config = configparser.ConfigParser()
         self.ini = None
         self.macro_filter = re.compile('\$\{.+\}')
@@ -74,9 +79,14 @@ class configuration:
                                                      raw = self.raw))]
         return os.linesep.join(s)
 
+    def get_sections(self):
+        return self.config.sections()
+
     def get_item(self, section, label, err = True):
         try:
-            rec = self.config.get(section, label, raw = self.raw).replace(os.linesep, ' ')
+            rec = self.config.get(section,
+                                  label,
+                                  raw = self.raw).replace(os.linesep, ' ')
         except:
             if err:
                 raise error.general('config: no "%s" found in "%s"' % (label, section))
@@ -88,10 +98,12 @@ class configuration:
         #
         for m in self.macro_filter.findall(rec):
             if ':' not in m:
-                raise error.general('config: interpolation is ${section:value}: %s' % (m))
+                err = 'config: interpolation is ${section:value}: %s' % (m)
+                raise error.general(err)
             section_value = m[2:-1].split(':')
             if len(section_value) != 2:
-                raise error.general('config: interpolation is ${section:value}: %s' % (m))
+                err = 'config: interpolation is ${section:value}: %s' % (m)
+                raise error.general(err)
             try:
                 ref = self.config.get(section_value[0],
                                       section_value[1],



More information about the vc mailing list