From ea39715305b2b835757bc45b7bc1e64d82516a48 Mon Sep 17 00:00:00 2001 From: root Date: Wed, 20 Jan 2021 02:11:55 +0000 Subject: [PATCH] read_json_with_comments --- TTS/utils/io.py | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/TTS/utils/io.py b/TTS/utils/io.py index 2c5c8e49..46abf1c8 100644 --- a/TTS/utils/io.py +++ b/TTS/utils/io.py @@ -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