Skip to content

Commit d9d3c22

Browse files
committed
issue-34: add 'comment' support to psconfig json files
1 parent cea07b9 commit d9d3c22

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

psconfig/perfsonar-psconfig/psconfig/client/psconfig/base_connect.py

+29
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,33 @@
99
from urllib.parse import urlparse
1010
import logging
1111

12+
13+
def json_decomment(json_obj, prefix='#', null=False):
14+
"""
15+
Remove any JSON object emember whose name begins with 'prefix'
16+
(default '#') and return the result. If 'null' is True, replace
17+
the prefixed items with a null value instead of deleting them.
18+
"""
19+
if type(json_obj) is dict:
20+
result = {}
21+
for item in json_obj.keys():
22+
if item.startswith(prefix):
23+
if null:
24+
result[item] = None
25+
else:
26+
next
27+
else:
28+
result[item] = json_decomment(json_obj[item], prefix=prefix, null=null)
29+
return result
30+
elif type(json_obj) is list:
31+
result = []
32+
for item in json_obj:
33+
result.append(json_decomment(item, prefix=prefix, null=null))
34+
return result
35+
else:
36+
return json_obj
37+
38+
1239
class BaseConnect(object):
1340

1441
def __init__(self, **kwargs):
@@ -133,6 +160,8 @@ def _raw_config_to_psconfig(self, raw_config):
133160

134161
try:
135162
json_obj = json.loads(raw_config)
163+
# remove comments
164+
json_obj = json_decomment(json_obj)
136165
except Exception as e:
137166
json_error = e
138167

0 commit comments

Comments
 (0)