summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChris Johns <chrisj@rtems.org>2019-06-10 17:08:34 +1000
committerChris Johns <chrisj@rtems.org>2019-06-10 17:08:34 +1000
commitad65d93774265cb0a9e65c5aa7378b5a10253340 (patch)
tree6ed4c83f8c4901967ed338a83bbc22cf8cf624a1
parentbf863a3a98465b0a2aa5f68542b91e135f1aabf5 (diff)
rtemstoolkit/configuration: Fix interpolation support.
- It was disabled always. Now optional by the constructor.
-rw-r--r--rtemstoolkit/configuration.py67
1 files 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 []