[PATCH v2 7/8] rtemstoolkit/configuration: Fix interpolation support.

chrisj at rtems.org chrisj at rtems.org
Mon Jun 10 07:23:14 UTC 2019


From: Chris Johns <chrisj at rtems.org>

- It was disabled always. Now optional by the constructor.
---
 rtemstoolkit/configuration.py | 67 +++++++++++++++++++++--------------
 1 file changed, 40 insertions(+), 27 deletions(-)

diff --git a/rtemstoolkit/configuration.py b/rtemstoolkit/configuration.py
index bc3ec93..a78a75f 100644
--- a/rtemstoolkit/configuration.py
+++ b/rtemstoolkit/configuration.py
@@ -51,13 +51,13 @@ except:
 class configuration:
 
     def __init__(self, raw = True):
-        self.raw = True
+        self.raw = raw
         if has_strict:
             self.config = configparser.ConfigParser(strict = False)
         else:
             self.config = configparser.ConfigParser()
         self.ini = None
-        self.macro_filter = re.compile('\$\{.+\}')
+        self.macro_filter = re.compile('\$\{[^\}]+\}')
 
     def __str__(self):
         if self.ini is None:
@@ -79,6 +79,38 @@ class configuration:
                                                      raw = self.raw))]
         return os.linesep.join(s)
 
+    def _interpolate(self, section, rec):
+        #
+        # On Python 2.7 there is no extended interpolation so add support here.
+        # On Python 3 this should happen automatically and the findall
+        # should find nothing.
+        #
+        if not self.raw:
+            not_found = []
+            while True:
+                macros = [m for m in self.macro_filter.findall(rec) if m not in not_found]
+                if len(macros) == 0:
+                    break
+                for m in macros:
+                    if m in not_found:
+                        continue
+                    if ':' in m:
+                        section_value = m[2:-1].split(':')
+                        if len(section_value) != 2:
+                            err = 'config: interpolation is ${section:item}: %s' % (m)
+                            raise error.general(err)
+                    else:
+                        section_value = [section, m[2:-1]]
+                    try:
+                        ref = self.config.get(section_value[0],
+                                              section_value[1],
+                                              raw = self.raw).replace(os.linesep, ' ')
+                        rec = rec.replace(m, ref)
+                    except:
+                        not_found += [m]
+                        pass
+        return rec
+
     def get_sections(self):
         return self.config.sections()
 
@@ -91,38 +123,19 @@ class configuration:
             if err:
                 raise error.general('config: no "%s" found in "%s"' % (label, section))
             return None
-        #
-        # On Python 2.7 there is no extended interpolation so add support here.
-        # On Python 3 this should happen automatically and so the findall
-        # should find nothing.
-        #
-        for m in self.macro_filter.findall(rec):
-            if ':' not in m:
-                err = 'config: interpolation is ${section:value}: %s' % (m)
-                raise error.general(err)
-            section_value = m[2:-1].split(':')
-            if len(section_value) != 2:
-                err = 'config: interpolation is ${section:value}: %s' % (m)
-                raise error.general(err)
-            try:
-                ref = self.config.get(section_value[0],
-                                      section_value[1],
-                                      raw = self.raw).replace(os.linesep, ' ')
-                rec = rec.replace(m, ref)
-            except:
-                pass
-        return rec
+        return self._interpolate(section, rec)
 
     def get_items(self, section, err = True, flatten = True):
         try:
             items = []
-            for name, key in self.config.items(section, raw = self.raw):
+            for name, value in self.config.items(section, raw = self.raw):
                 if flatten:
-                    items += [(name, key.replace(os.linesep, ' '))]
-                else:
-                    items += [(name, key)]
+                    value = value.replace(os.linesep, ' ')
+                value = self._interpolate(section, value)
+                items += [(name, value)]
             return items
         except:
+            raise
             if err:
                 raise error.general('config: section "%s" not found' % (section))
         return []
-- 
2.20.1 (Apple Git-117)




More information about the devel mailing list