def _load_config(): """ Load newslynx configurations from file / env variables. """ # load config file config_file = os.getenv('NEWSLYNX_CONFIG_FILE', '~/.newslynx/config.yaml') if config_file.startswith('~'): config_file = os.path.expanduser(config_file) if not os.path.exists(config_file): raise ConfigError( "No Config could be found at '{}'.".format(config_file)) try: config = yaml_stream_to_obj(open(config_file)) except Exception as e: raise ConfigError("There was an error loading config '{}'.\n" "Here is the error message:\n{}.".format( config_file, e.message)) # update with environment variables for name, value in sorted(os.environ.items()): if name.startswith('NEWSLYNX_') and name != 'NEWSLYNX_CONFIG_FILE': name = name.replace('NEWSLYNX_', '').lower() config[name] = value # check for required config parametrics for k in _CONFIG_REQUIRES: if k not in config: raise ConfigError( 'Required setting "{}"" is missing from {} / ENV variables'. format(k, config_file)) return config
def _load_config(): """ Load newslynx configurations from file / env variables. """ # load config file config_file = os.getenv('NEWSLYNX_CONFIG_FILE', '~/.newslynx/config.yaml') if config_file.startswith('~'): config_file = os.path.expanduser(config_file) if not os.path.exists(config_file): raise ConfigError( "No Config could be found at '{}'." .format(config_file)) try: config = yaml_stream_to_obj(open(config_file)) except Exception as e: raise ConfigError( "There was an error loading config '{}'.\n" "Here is the error message:\n{}." .format(config_file, e.message)) # update with environment variables for name, value in sorted(os.environ.items()): if name.startswith('NEWSLYNX_') and name != 'NEWSLYNX_CONFIG_FILE': name = name.replace('NEWSLYNX_', '').lower() config[name] = value # check for required config parametrics for k in _CONFIG_REQUIRES: if k not in config: raise ConfigError( 'Required setting "{}"" is missing from {} / ENV variables' .format(k, config_file)) return config
def load(fp): """ Load a sous chef allowing for include statements. """ try: sc = yaml_stream_to_obj(open(fp)) # update with includes. for incl_fp in sc.get('includes', []): if not incl_fp.endswith('.yaml'): incl_fp += ".yaml" incl_fp = os.path.join(os.path.dirname(fp), incl_fp) incl = yaml_stream_to_obj(open(incl_fp)) sc = update_nested_dict(sc, incl, overwrite=True) # validate return validate(sc, fp) except: msg = "{}\n{}".format(format_exc(), "failed on file {}".format(fp)) raise SousChefSchemaError(msg)
import importlib import os.path from traceback import format_exc from jsonschema import Draft4Validator from newslynx.sc import SousChef as SC from newslynx.exc import SousChefSchemaError from newslynx.models import SousChef from newslynx.lib.serialize import yaml_stream_to_obj from newslynx.lib.serialize import obj_to_json, json_to_obj from newslynx.constants import SOUS_CHEF_RESERVED_FIELDS from newslynx.util import here, update_nested_dict # load souschef schema + validator. SOUS_CHEF_JSON_SCHEMA = yaml_stream_to_obj( open(here(__file__, 'sous_chef.yaml'))) # these are default options that all sous chefs have. SOUS_CHEF_DEFAULT_OPTIONS = yaml_stream_to_obj( open(here(__file__, 'sous_chef_default_options.yaml'))) # a json-schema validator for a sous chef. SOUS_CHEF_VALIDATOR = Draft4Validator(SOUS_CHEF_JSON_SCHEMA) # a regex for validation option + metric names re_opt_name = re.compile(r'^[a-z][a-z_]+[a-z]$') def load(fp): """ Load a sous chef allowing for include statements.