mirror of
https://github.com/wassname/TTS.git
synced 2026-09-11 12:00:24 +08:00
read_json_with_comments
This commit is contained in:
+11
-8
@@ -20,6 +20,16 @@ class AttrDict(dict):
|
||||
self.__dict__ = self
|
||||
|
||||
|
||||
def read_json_with_comments(json_path):
|
||||
# fallback to json
|
||||
with open(json_path, "r") as f:
|
||||
input_str = f.read()
|
||||
# handle comments
|
||||
input_str = re.sub(r'\\\n', '', input_str)
|
||||
input_str = re.sub(r'//.*\n', '\n', input_str)
|
||||
data = json.loads(input_str)
|
||||
return data
|
||||
|
||||
def load_config(config_path: str) -> AttrDict:
|
||||
"""Load config files and discard comments
|
||||
|
||||
@@ -33,14 +43,7 @@ def load_config(config_path: str) -> AttrDict:
|
||||
with open(config_path, "r") as f:
|
||||
data = yaml.safe_load(f)
|
||||
else:
|
||||
# fallback to json
|
||||
with open(config_path, "r") as f:
|
||||
input_str = f.read()
|
||||
# handle comments
|
||||
input_str = re.sub(r'\\\n', '', input_str)
|
||||
input_str = re.sub(r'//.*\n', '\n', input_str)
|
||||
data = json.loads(input_str)
|
||||
|
||||
data = read_json_with_comments(config_path)
|
||||
config.update(data)
|
||||
return config
|
||||
|
||||
|
||||
Reference in New Issue
Block a user