def convert(self, value, param, ctx): cfg_file = super().convert(value, param, ctx) try: return traf_cfg.read_and_validate(cfg_file, self.trafaret) except traf_cfg.ConfigError as e: msg = "\n".join(str(err) for err in e.errors) self.fail("\n" + msg)
def read_and_validate(filepath, vars=None): # pylint: disable=W0622 from .application_config import app_schema if vars is None: vars = os.environ # NOTE: vars=os.environ in signature freezes default to os.environ before it gets # Cannot user functools.partial because os.environ gets then frozen return trafaret_config.read_and_validate(filepath, trafaret=app_schema, vars=vars)
def get_config(): BASE_PATH = os.getcwd() config = read_and_validate(BASE_PATH + '/config/settings.yml', TRAFARET) config['base_path'] = BASE_PATH return config
def app_config(here, webserver_environ) -> Dict: config_file_path = here / "config.yaml" def _recreate_config_file(): with app_resources.stream("config/server-docker-dev.yaml") as f: cfg = yaml.safe_load(f) # test webserver works in host cfg["main"]['host'] = '127.0.0.1' cfg["rabbit"]["host"] = '127.0.0.1' cfg["rabbit"]["port"] = "5672" cfg["director"]["host"] = "127.0.0.1" with config_file_path.open('wt') as f: yaml.dump(cfg, f, default_flow_style=False) _recreate_config_file() # Emulates cli config_environ = {} config_environ.update(webserver_environ) config_environ.update( create_environ(skip_host_environ=True) ) # TODO: can be done monkeypathcing os.environ and calling create_environ as well # validates cfg_dict = trafaret_config.read_and_validate(config_file_path, app_schema, vars=config_environ) yield cfg_dict # clean up # to debug configuration uncomment next line config_file_path.unlink()
def setup_app(app: FastAPI): """ Настройка приложения """ # Шаблон для задания структуры базы данных TRAFARET = T.Dict({ T.Key('databases'): T.List( T.Dict({ 'name': T.String(), 'address': T.String(), 'collections': T.List( T.Dict({ 'name': T.String(), 'fields': T.List(T.String), T.Key('index_fields', optional=True): T.List(T.String), })) })) }) config = read_and_validate('config.yaml', TRAFARET) app.config = config app.add_middleware(CheckUserAuthMiddleware)
def parse_config(path): try: config = read_and_validate(path, CONFIG_TRAFARET) except ConfigError as e: for error_message in e.errors: log.error(str(error_message)) sys.exit(1) return config
def from_path(path): """Loads schema from YAML file. :param str path: YAML file path. :returns: Airflow DAGs schema. :rtype: dict """ return trafaret_config.read_and_validate(path, SCHEMA)
def read_config(filepath): """Read a config file from a given path""" try: config = read_and_validate(filepath, CONFIG_SCHEMA) return config except ConfigError as ex: ex.output() raise
def load_settings() -> dict: """ Read settings.yml and, validation its content. :return: settings dict """ settings_file = SETTINGS_FILE.resolve() settings = read_and_validate(str(settings_file), SETTINGS_STRUCTURE) settings = substitute_environ(settings, ENV_PREFIX) return settings
def config_from_file(filepath) -> dict: """ Loads and validates app configuration from file Some values in the configuration are defined as environment variables Raises trafaret_config.ConfigError """ config = trafaret_config.read_and_validate(filepath, schema, vars=os.environ) return config
def process_config_file(filename): try: cwd = os.getcwd() config = read_and_validate(os.path.join(cwd, filename), CONFIG_TRAFARET) except ConfigError as e: print(e) sys.exit(1) return config
def read_config(filepath): """Read a config file from a given path. Args: filepath (str) : filepath to read config path Return: config json """ try: config = read_and_validate(filepath, config_schema) return config except ConfigError as e: e.output() return None
def app_cfg(here, osparc_simcore_root_dir): cfg_path = here / "config.yaml" assert cfg_path.exists() variables = dict(os.environ) variables.update({ 'OSPARC_SIMCORE_REPO_ROOTDIR': str(osparc_simcore_root_dir), }) # validates and fills all defaults/optional entries that normal load would not do cfg_dict = trafaret_config.read_and_validate(cfg_path, app_schema, vars=variables) return cfg_dict
def setup_app(app: Application): if isfile('config.yaml'): TRAFARET = T.Dict({ T.Key('services'): T.List( T.Dict({ 'name': T.String(), 'jwt_ttl_minutes': T.Int(), 'redirect_link': T.String(), 'algorithm': T.String(), 'secret_key': T.String() })) }) config = read_and_validate('config.yaml', TRAFARET) app['services'] = {x['name']: x for x in config['services']}
def default_app_cfg(osparc_simcore_root_dir, fake_static_dir): # NOTE: ONLY used at the session scopes cfg_path = current_dir / "config.yaml" assert cfg_path.exists() variables = dict(os.environ) variables.update( {"OSPARC_SIMCORE_REPO_ROOTDIR": str(osparc_simcore_root_dir),} ) # validates and fills all defaults/optional entries that normal load would not do cfg_dict = trafaret_config.read_and_validate(cfg_path, app_schema, vars=variables) assert Path(cfg_dict["main"]["client_outdir"]) == fake_static_dir # WARNING: changes to this fixture during testing propagates to other tests. Use cfg = deepcopy(cfg_dict) # FIXME: free cfg_dict but deepcopy shall be r/w return cfg_dict
def _webserver_dev_config(webserver_environ: Dict, docker_stack: Dict) -> Dict: """ Swarm with integration stack already started Configuration for a webserver provided it runs in host NOTE: Prefer using 'app_config' below instead of this as a function-scoped fixture """ config_file_path = current_dir / "webserver_dev_config.yaml" # recreate config-file with app_resources.stream("config/server-docker-dev.yaml") as f: cfg = yaml.safe_load(f) # test webserver works in host cfg["main"]["host"] = "127.0.0.1" with config_file_path.open("wt") as f: yaml.dump(cfg, f, default_flow_style=False) # Emulates cli config_environ = {} config_environ.update(webserver_environ) config_environ.update( create_environ(skip_host_environ=True) ) # TODO: can be done monkeypathcing os.environ and calling create_environ as well # validates cfg_dict = trafaret_config.read_and_validate(config_file_path, app_schema, vars=config_environ) # WARNING: changes to this fixture during testing propagates to other tests. Use cfg = deepcopy(cfg_dict) # FIXME: freeze read/only json obj yield cfg_dict # clean up # to debug configuration uncomment next line config_file_path.unlink() return cfg_dict
async def init_db_connect(cls, loop): config = read_and_validate('configs/db.yaml', TRAFARET) cls._pool = await create_pool(loop=loop, **config['postgres'])
def read_and_validate(filepath, vars=None): # pylint: disable=W0622 if vars is None: vars = os.environ # NOTE: vars=os.environ in signature freezes default to os.environ before it gets # Cannot user functools.partial because os.environ gets then frozen return _tc.read_and_validate(filepath, trafaret=CONFIG_SCHEMA, vars=vars)
def __init__(self, config_path): self.data = read_and_validate(config_path, CONFIG_TRAFARET)
def get_config(): return read_and_validate(DEFAULT_CONFIG_PATH.as_posix(), CONFIG_TRAFARET)