def load_config_file(self, config_paths):
"""Load a yaml config file from path."""
config_path = ""
for possible_path in config_paths:
if not os.path.isfile(possible_path):
_LOGGER.debug("Config file %s not found.", possible_path)
else:
config_path = possible_path
break
if not config_path:
_LOGGER.info("No configuration files found.")
config_path = self.create_default_config(DEFAULT_CONFIG_PATH)
env_var_pattern = re.compile(r'^\$([A-Z_]*)$')
yaml.add_implicit_resolver("!envvar", env_var_pattern)
def envvar_constructor(loader, node):
"""Yaml parser for env vars."""
value = loader.construct_scalar(node)
[env_var] = env_var_pattern.match(value).groups()
return os.environ[env_var]
def include_constructor(loader, node):
"""Add a yaml file to be loaded inside another."""
main_yaml_path = os.path.split(stream.name)[0]
included_yaml = os.path.join(main_yaml_path,
loader.construct_scalar(node))
with open(included_yaml, 'r') as included:
return yaml.load(included)
yaml.add_constructor('!envvar', envvar_constructor)
yaml.add_constructor('!include', include_constructor)
try:
with open(config_path, 'r') as stream:
_LOGGER.info("Loaded config from %s.", config_path)
return yaml.load(stream)
except yaml.YAMLError as error:
self.opsdroid.critical(error, 1)
except FileNotFoundError as error:
self.opsdroid.critical(str(error), 1)
评论列表
文章目录