Support C-style comments for configuration JSON parsing
1. Add and set ignore_comment to true to all nlohmann::json::parse().
2. Add remove_c_comments() in `validate_configs.py` to remove C-style
comments before loading.
3. Attempt to reformat comments in the `autojson.py` taking liberal
short-cuts which are documented in the script.
Supported comment examples:
- Single-line style comments
```
{
// Single-line style comment (new line)
"Key": "Value" // Single-line comment (end of content)
}
```
- Multi-line style comments
```
{
/* Multi-line style comment */
/*
* Multi-line style comments
*/
}
```
Tested on harma system with manual applied patch below, which contains
a c-style comment in harma-pttv.json file.
Link: https://gerrit.openbmc.org/c/openbmc/entity-manager/+/67469/25
- scripts/autojson.py
Run autojson.py on harma-pttv.json, the output as same as original file.
- scripts/validate_configs.py
Run validate_configs.py passed.
- EntityManager service
EntityManager service loads and probes harma-pttv.json successfully.
```
root@harma:~# busctl introspect xyz.openbmc_project.EntityManager \
> /xyz/openbmc_project/inventory/system/board/Harma_PTTV \
> xyz.openbmc_project.Inventory.Item.Board
NAME TYPE SIGNATURE RESULT/VALUE FLAGS
.Name property s "Harma PTTV" emits-change
.Probe property s "xyz.openbmc_project.FruDevice({\'BOA... emits-change
.Type property s "Board" emits-change
```
Signed-off-by: Potin Lai <potin.lai@quantatw.com>
Signed-off-by: Patrick Williams <patrick@stwcx.xyz>
Change-Id: Ib235f2aa6a724615dc4c8184577f57abda8e17a6
diff --git a/scripts/validate_configs.py b/scripts/validate_configs.py
index 5ba8945..0b47420 100755
--- a/scripts/validate_configs.py
+++ b/scripts/validate_configs.py
@@ -6,6 +6,7 @@
import argparse
import json
import os
+import re
import sys
import jsonschema.validators
@@ -13,6 +14,21 @@
DEFAULT_SCHEMA_FILENAME = "global.json"
+def remove_c_comments(string):
+ # first group captures quoted strings (double or single)
+ # second group captures comments (//single-line or /* multi-line */)
+ pattern = r"(\".*?(?<!\\)\"|\'.*?(?<!\\)\')|(/\*.*?\*/|//[^\r\n]*$)"
+ regex = re.compile(pattern, re.MULTILINE | re.DOTALL)
+
+ def _replacer(match):
+ if match.group(2) is not None:
+ return ""
+ else:
+ return match.group(1)
+
+ return regex.sub(_replacer, string)
+
+
def main():
parser = argparse.ArgumentParser(
description="Entity manager configuration validator",
@@ -97,7 +113,7 @@
for config_file in config_files:
try:
with open(config_file) as fd:
- configs.append(json.load(fd))
+ configs.append(json.loads(remove_c_comments(fd.read())))
except FileNotFoundError:
sys.stderr.write(
"Could not parse config file '{}'\n".format(config_file)