def get_replacements(config_path): replacements = [] lookup = functools.reduce(lambda x, y: x[y], config_path.split("."), config) for pattern, repl in lookup.get(dict).items(): try: replacements.append((re.compile(pattern), repl or '')) except re.error as exc: raise confuse.ConfigError(u'{0}: error compiling regex `{1}`: {2}'.format(config_path, pattern, str(exc))) return replacements
def commands(self): """Add the alias commands.""" if self.config['from_path'].get(bool): commands = dict(self.get_path_commands()) else: commands = {} for alias in self.config['aliases'].keys(): if alias in commands: raise confuse.ConfigError( u'alias.aliases.{0} was specified multiple times'.format( alias)) command = self.config['aliases'][alias].get() if isinstance(command, six.text_type): commands[alias] = self.get_command(alias, command) elif isinstance(command, abc.Mapping): command_text = command.get('command') if not command_text: raise confuse.ConfigError( u'alias.aliases.{0}.command not found'.format(alias)) help_text = command.get('help', command_text) commands[alias] = self.get_command(alias, command_text, help_text) else: raise confuse.ConfigError( u'alias.aliases.{0} must be a string or single-element mapping' .format(alias)) if 'alias' in commands: raise ui.UserError( u'alias `alias` is reserved for the alias plugin') alias = Subcommand('alias', help=u'Print the available alias commands.') alias_commands = dict((a, c.command) for a, c in commands.items()) alias.func = lambda lib, opts, args: self.cmd_alias( lib, opts, args, alias_commands) commands['alias'] = alias return commands.values()
def __init__(self): super(InlineHookPlugin, self).__init__() self.config.add({'hooks': [], 'argspecs': {}}) self.argspecs = dict(InlineHookPlugin.argspecs) self.argspecs.update(self.config['argspecs'].get()) inline_hooks = self.config['hooks'].get(list) for hook_index in range(len(inline_hooks)): hook = self.config['hooks'][hook_index] event = hook['event'].as_str() if event not in self.argspecs: raise confuse.ConfigError( 'inline_hook.hooks[{0}].event: `{1}` is not a handled event' .format(hook_index, event)) handler = hook['handler'].as_str() function = compile_func(handler, 'inline_hook_' + event, self.argspecs.get(event) or '') self.register_listener(event, function)
def __init__(self): super(OriginQuery, self).__init__() def fail(msg): self.error(msg) self.error('Plugin disabled.') try: self.extra_tags = config['musicbrainz']['extra_tags'].get() except confuse.NotFoundError: return fail( 'This version of beets does not support extra query tags.') if not len(self.extra_tags): return fail('Config error: musicbrainz.extra_tags not set.') config_patterns = None try: config_patterns = self.config['tag_patterns'].get() if not isinstance(config_patterns, dict): raise confuse.ConfigError() except confuse.ConfigError: return fail( 'Config error: originquery.tag_patterns must be set to a dictionary of key -> pattern mappings.' ) try: self.origin_file = Path(self.config['origin_file'].get()) except confuse.NotFoundError: return fail('Config error: originquery.origin_file not set.') self.tag_patterns = {} try: origin_type = self.config['origin_type'].as_choice( ['yaml', 'json', 'text']).lower() except confuse.NotFoundError: origin_type = self.origin_file.suffix.lower()[1:] if origin_type == 'json': self.match_fn = self.match_json elif origin_type == 'yaml': self.match_fn = self.match_yaml else: self.match_fn = self.match_text for key, pattern in config_patterns.items(): if key not in BEETS_TO_LABEL: return fail('Config error: unknown key "{0}"'.format(key)) self.error('Plugin disabled.') if origin_type == 'json' or origin_type == 'yaml': try: self.tag_patterns[key] = jsonpath_rw.parse(pattern) except Exception as e: return fail( 'Config error: invalid tag pattern for "{0}". "{1}" is not a valid JSON path ({2}).' .format(key, pattern, format(str(e)))) continue try: regex = re.compile(pattern) self.tag_patterns[key] = regex except re.error as e: return fail( 'Config error: invalid tag pattern for "{0}". "{1}" is not a valid regex ({2}).' .format(key, pattern, format(str(e)))) if regex.groups != 1: return fail( 'Config error: invalid tag pattern for "{0}". "{1}" must have exactly one capture group.' .format(key, pattern)) self.register_listener('import_task_start', self.import_task_start) self.register_listener('before_choose_candidate', self.before_choose_candidate) self.tasks = {} try: self.use_origin_on_conflict = self.config[ 'use_origin_on_conflict'].get(bool) except confuse.NotFoundError: self.use_origin_on_conflict = False