Skip to content

Commit

Permalink
read_json_with_comments
Browse files Browse the repository at this point in the history
  • Loading branch information
erogol committed Jan 20, 2021
1 parent 563bc92 commit ea39715
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions TTS/utils/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,16 @@ def __init__(self, *args, **kwargs):
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
Expand All @@ -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

Expand Down

0 comments on commit ea39715

Please sign in to comment.