From 27683041209d4cf7bd5276e82a1622d6b3b01c14 Mon Sep 17 00:00:00 2001 From: Javi Merino Date: Mon, 25 Jan 2016 15:38:39 +0000 Subject: [PATCH 1/2] libs/utils/conf: Make regexp string a raw string The regexp string for comments has characters like \S that are not valid in strings. This is a raw string, so mark it as such. --- libs/utils/conf.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libs/utils/conf.py b/libs/utils/conf.py index ab40c10f6..b28bdfe7d 100644 --- a/libs/utils/conf.py +++ b/libs/utils/conf.py @@ -63,7 +63,7 @@ class JsonConf(object): # Regular expression for comments JSON_COMMENTS_RE = re.compile( - '(^)?[^\S\n]*/(?:\*(.*?)\*/[^\S\n]*|/[^\n]*)($)?', + r'(^)?[^\S\n]*/(?:\*(.*?)\*/[^\S\n]*|/[^\n]*)($)?', re.DOTALL | re.MULTILINE ) -- GitLab From 0759fbf90dfcf666d32e48f63a06871956bddd0c Mon Sep 17 00:00:00 2001 From: Javi Merino Date: Mon, 25 Jan 2016 15:27:23 +0000 Subject: [PATCH 2/2] libs/utils/conf: Make the json parser cope with trailing commas The configuration files are written using JSON but the standard JSON doesn't allow trailing commas in dicts or arrays, which is very annoying when enabling and disabling entries. Some JSON parsers do silently ignore trailing commas, but that is not the case for the python implementation. Add a simple regexp to remove commas at the end of arrays and dictionaries. This is a fix for issue #4 --- libs/utils/conf.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/libs/utils/conf.py b/libs/utils/conf.py index b28bdfe7d..98b30d544 100644 --- a/libs/utils/conf.py +++ b/libs/utils/conf.py @@ -52,6 +52,12 @@ class JsonConf(object): content = content[:match.start()] + content[match.end():] match = JSON_COMMENTS_RE.search(content) + # Allow trailing commas in dicts an lists in JSON + # Note that this simple implementation will mangle things like: + # {"config": ", }"} + content = re.sub(r',[ \t\r\n]+}', '}', content) + content = re.sub(r',[ \t\r\n]+\]', ']', content) + # Return json file self.json = json.loads(content, parse_int=int) logging.debug('Loaded JSON configuration:\n%s', self.json) -- GitLab