1
0
Fork 0
mirror of https://github.com/prise6/smart-iss-posts synced 2024-05-25 09:52:13 +02:00
smart-iss-posts/iss/tools/config.py

37 lines
971 B
Python
Raw Normal View History

2019-03-05 00:47:57 +01:00
# -*- coding: utf-8 -*-
import os
import sys
import yaml
from dotenv import find_dotenv, load_dotenv
2019-03-07 17:32:02 +01:00
import re
2019-03-05 00:47:57 +01:00
load_dotenv(find_dotenv())
2019-03-07 17:32:02 +01:00
2019-03-05 00:47:57 +01:00
class Config:
2019-03-07 17:32:02 +01:00
def __init__(self, project_dir = os.getenv("PROJECT_DIR"), mode = os.getenv("MODE")):
self.project_dir = project_dir
self.mode = mode
self.path_matcher = re.compile(r'\$\{([^}^{]+)\}')
yaml.add_implicit_resolver('!path', self.path_matcher, None, yaml.SafeLoader)
yaml.add_constructor('!path', self.path_constructor, yaml.SafeLoader)
with open(os.path.join(self.project_dir, 'config', 'config_%s.yaml' % (self.mode)), 'r') as ymlfile:
self.config = yaml.safe_load(ymlfile)
2019-03-05 00:47:57 +01:00
2019-03-07 17:32:02 +01:00
def get(self, key):
return self.config[key]
2019-03-05 00:47:57 +01:00
2019-03-07 17:32:02 +01:00
def path_constructor(self, loader, node):
''' Extract the matched value, expand env variable, and replace the match '''
value = node.value
match = self.path_matcher.match(value)
env_var = match.group()[2:-1]
2019-03-05 00:47:57 +01:00
2019-03-07 17:32:02 +01:00
return os.environ.get(env_var) + value[match.end():]
2019-03-05 00:47:57 +01:00