Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
) -> Config:
if not file_path.exists():
dump_config(Config({"auth": {}}), file_path)
try:
contents = file_path.text()
except OSError as os_error:
raise tsrc.InvalidConfig(file_path, os_error)
try:
if roundtrip:
yaml = ruamel.yaml.YAML(typ="rt")
else:
yaml = ruamel.yaml.YAML(typ="safe", pure=True)
parsed = yaml.load(contents)
except ruamel.yaml.error.YAMLError as yaml_error:
raise tsrc.InvalidConfig(file_path, yaml_error)
if config_schema:
try:
config_schema.validate(parsed)
except schema.SchemaError as schema_error:
raise tsrc.InvalidConfig(file_path, schema_error)
return Config(parsed)
def parse_config(
file_path: Path,
config_schema: Optional[schema.Schema] = None,
roundtrip: bool = False,
) -> Config:
if not file_path.exists():
dump_config(Config({"auth": {}}), file_path)
try:
contents = file_path.text()
except OSError as os_error:
raise tsrc.InvalidConfig(file_path, os_error)
try:
if roundtrip:
yaml = ruamel.yaml.YAML(typ="rt")
else:
yaml = ruamel.yaml.YAML(typ="safe", pure=True)
parsed = yaml.load(contents)
except ruamel.yaml.error.YAMLError as yaml_error:
raise tsrc.InvalidConfig(file_path, yaml_error)
if config_schema:
try:
config_schema.validate(parsed)
except schema.SchemaError as schema_error:
raise tsrc.InvalidConfig(file_path, schema_error)
return Config(parsed)
contents = file_path.text()
except OSError as os_error:
raise tsrc.InvalidConfig(file_path, os_error)
try:
if roundtrip:
yaml = ruamel.yaml.YAML(typ="rt")
else:
yaml = ruamel.yaml.YAML(typ="safe", pure=True)
parsed = yaml.load(contents)
except ruamel.yaml.error.YAMLError as yaml_error:
raise tsrc.InvalidConfig(file_path, yaml_error)
if config_schema:
try:
config_schema.validate(parsed)
except schema.SchemaError as schema_error:
raise tsrc.InvalidConfig(file_path, schema_error)
return Config(parsed)