Skip to content

Commit

Permalink
Refactor config paths and remove deprecated JSON config format
Browse files Browse the repository at this point in the history
  • Loading branch information
ejohb committed Dec 30, 2022
1 parent 06df4ab commit 1ab3f87
Showing 1 changed file with 13 additions and 13 deletions.
26 changes: 13 additions & 13 deletions amniotic/config.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from os import getenv

import json
import logging
import tempfile
import yaml
Expand Down Expand Up @@ -46,8 +45,12 @@ def __post_init__(self):
self.logging = self.logging or logging.INFO

@classmethod
def from_file(cls):
def get_path_config(cls) -> Path:
"""
Get path to config file from environment variable, or default location
"""
path_config = getenv('AMNIOTIC_CONFIG_PATH')

if not path_config:
Expand All @@ -56,25 +59,22 @@ def from_file(cls):

path_config = Path(path_config).absolute()

return path_config

@classmethod
def from_file(cls):

path_config = cls.get_path_config()

if not path_config.exists():
msg = f'Config file not found at "{path_config}". Default values will be used.'
logging.warning(msg)
config = {}
else:
msg = f'Config file found at "{path_config}"'
logging.info(msg)

config_str = Path(path_config).read_text()

if path_config.suffix in {'.yml', '.yaml'}:
config = yaml.safe_load(config_str)
elif path_config.suffix in {'.json'}:
config = json.loads(config_str)
else:
msg = f'Unknown config format "{path_config.suffix}"'
raise ValueError(msg)

logging.warning(msg)
config = yaml.safe_load(config_str)

field_names = {field.name for field in fields(Config)}
for key in field_names:
Expand Down

0 comments on commit 1ab3f87

Please sign in to comment.