def test_disallow_mixed(raise_, test_input, expected): if raise_: with pytest.raises(TypeError): deep_merge( *test_input, merge_strategy=disallow_mixed(return_right) ) else: assert deep_merge( *test_input, merge_strategy=disallow_mixed(return_right) ) == expected
def _add(self, data, *, validating_data=None, validation_fields=None, client, metadata, check_unique=False, check_exists=False, validation_client=None): found_item = None if validating_data is not None: validate_against = deep_merge(data, validating_data) else: validate_against = data if validation_fields is not None: for field in validation_fields: found_item = self._find({field: validate_against[field]}, validation_client) if check_unique and found_item is not None: return None if check_exists and found_item is None: return None _, doc_ref = client.add(data) metadata.update({'total_documents': firestore.Increment(1)}) return doc_ref.id
def merge_configs( *files: str, merge_strategy: MergeStrategy = prefer_right, auto: bool = True, strict: bool = True, extra_parsers: Dict = {} ) -> Dict: ''' A function that takes a sequence of config filenames and produces a dictionary resulting from all the files being processed and then merged with a specified merge strategy. To use custom parsers, pass a flat dictionary of filetypes and parsers. If `auto` is set to False, use only the parsers explicitly passed through the `extra_parsers` parameter. If `strict` is set to True, all file names must have the same extension. This also applies to file type variations like `.yml` vs `.yaml` ''' if strict: file_iter = iter(files) first_file_name = next(file_iter) file_type = Path(first_file_name).suffix if file_type == '': raise FileTypeError( 'File "%s" has no file type extension' % first_file_name ) for file_name in file_iter: if Path(file_name).suffix != file_type: raise FileTypeError( 'File "%s" has improper filetype. ' 'Expected file type of "%s"' % (file_name, file_type) ) parsers = {} if auto: parsers = load_parsers() parsers.update(extra_parsers) file_data = [] for file_name in files: file_type = Path(file_name).suffix if file_type not in parsers: raise ParserNotAvailableError('No parser for "%s"' % file_type) parser = parsers[file_type] file_data.append(parser(file_name)) return deep_merge(*file_data, merge_strategy=merge_strategy)
def getPluginConfig(self, plugin: str) -> dict: pluginHostConfig = { k: (v['plugins'][plugin] if plugin in v['plugins'] else {}) for k, v in self.__config['hosts'].items() } try: pluginConfig = self.__config['plugins'][plugin] except KeyError: pluginConfig = {} for host, hostConfig in pluginHostConfig.items(): pluginHostConfig[host] = pydeepmerge.deep_merge( pluginConfig, hostConfig) return pluginHostConfig
def _loadConfig(self) -> dict: self._config = config().getPluginConfig(self.getName()) for host in list(self._config): try: pluginEnabled = self._config[host]['enabled'] except KeyError: pluginEnabled = False if not pluginEnabled: self._config.pop(host) if hasattr(self, '_configDefault'): for host, hostConfig in self._config.items(): self._config[host] = pydeepmerge.deep_merge( self._configDefault, hostConfig) return self._config
def get_configs_from_flags(absl_flags) -> Dict: ''' Extract the configurations from files specified on the CLI and CLI configs ''' default_configs = { 'acl': { 'defs_location': 'defs', 'pols_location': 'policies' }, 'prof': { 'pprof_file': 'latest.pprof', 'pprof_time': 5 }, 'acltest': { 'doc_output': 'latest', 'cleanup': True, 'svg': False, 'sanitize': False } } cli_configs = get_cli_configs(absl_flags) file_configs = merge_configs(*absl_flags.filename, strict=True) return deep_merge(default_configs, file_configs, cli_configs)
def cli( ctx: click.Context, no_update: bool, skip_config: bool, ): # pragma: no cover ''' Krait is a CLI to help start up new python application. To get information on specific subcommands, use `krait [COMMAND] --help`. ''' if ctx.invoked_subcommand == 'reset': return config_file = config_utils.get_config_file() if not config_file.exists(): click.secho('Welcome to Krait!', fg='green') click.echo('Krait is starting up for the first time ' 'and requires configuration') if skip_config: use_defaults = True else: use_defaults = yes_no_prompt( 'Would you like to use the default configurations?\n' 'Note: this can be changed later with `krait set-default`') configs = config_utils.get_config_defaults() if not use_defaults: new_configs = { 'lnt': default_value_prompt('linter', plugin_utils.get_plugin_keys(linters)), 'tc': default_value_prompt( 'type checker', plugin_utils.get_plugin_keys(type_checkers)), 'tf': default_value_prompt( 'testing framework', plugin_utils.get_plugin_keys(test_frameworks)), 'aut': default_value_prompt( 'automation system', plugin_utils.get_plugin_keys(automations)), 'prj': default_value_prompt( 'project framework', plugin_utils.get_plugin_keys(project_frameworks)), 'vcs_type': default_value_prompt('VCS Type', plugin_utils.get_plugin_keys(vcs_types)), 'always_suppress_prompt': yes_no_prompt('Always suppress interactive prompt?', default=False), 'require_author_name': yes_no_prompt('Require projects to have an author name?'), 'require_author_email': yes_no_prompt('Require projects to have an author email?'), 'auto_check_for_updates': yes_no_prompt('Check for new versions of krait?'), 'always_run_silent': yes_no_prompt('Suppress file creation outputs?'), } configs.update(new_configs) config_utils.write_configs(config_file, **configs) templates_folder = config_utils.get_config_folder() / 'templates' templates_folder.mkdir() click.secho('Copying template files to install directory...') config_utils.copy_all_files_to_target( pkg_resources.resource_filename(__name__, 'templates'), templates_folder, ) else: configs = config_utils.get_configs() defaults = config_utils.get_config_defaults() # Ensure new config options are loaded updated_configs = pdm.deep_merge(defaults, configs) if updated_configs != configs: config_utils.write_configs(config_file, **configs) configs = updated_configs configs['config_folder'] = config_file.parent ctx.obj = configs if not no_update and update_utils.should_check_update(ctx): update_ver = update_utils.check_for_update() if update_ver: click.secho( 'A new version of krait is available!\n' f'Install v{update_ver} by running `krait update`', fg='yellow')
def test_keep_default(test_input, expected): assert deep_merge( *test_input, merge_strategy=keep_default(return_right) ) == expected
def test_recurse(test_input, expected): assert deep_merge( *test_input, merge_strategy=recurse(return_right) ) == expected
def newstrategy(left, right): if isinstance(left, Mapping) and isinstance(right, Mapping): return deep_merge(left, right, merge_strategy=newstrategy) return strategy(left, right)