def test_utils_get_class_from_name(): """Tests get_class_from_name utility.""" assert (ralph_utils.get_class_from_name( "A", [module.value for module in NamedClassEnum]).name == "A") assert (ralph_utils.get_class_from_name( "B", [module.value for module in NamedClassEnum]).name == "B") with pytest.raises(ImportError, match="C class is not available"): assert (ralph_utils.get_class_from_name( "C", [module.value for module in NamedClassEnum]).name == "B")
def extract(parser): """Extracts input events from a container format using a dedicated parser.""" logger.info("Extracting events using the %s parser", parser) parser = get_class_from_name(parser, PARSERS)() for event in parser.parse(sys.stdin): click.echo(event)
def push(backend, archive, chunk_size, force, ignore_errors, **options): """Push an archive to a configured backend.""" logger.info("Pushing archive %s to the configured %s backend", archive, backend) logger.debug("Backend parameters: %s", options) backend_class = get_class_from_name(backend, BACKENDS) backend = get_instance_from_class(backend_class, **options) backend_type = get_backend_type(backend_class) if backend_type == BackendTypes.STORAGE: backend.write(archive, overwrite=force) elif backend_type == BackendTypes.DATABASE: backend.put(chunk_size=chunk_size, ignore_errors=ignore_errors) elif backend_type is None: msg = "Cannot find an implemented backend type for backend %s" logger.error(msg, backend) raise UnsupportedBackendException(msg, backend)
def wrapper(command): command = ( click.option( "-b", "--backend", type=click.Choice(backend_names), required=True, help="Backend", ) )(command) for backend_name in backend_names: backend_class = get_class_from_name(backend_name, backends) for parameter in signature(backend_class.__init__).parameters.values(): if parameter.name == "self": continue option = f"--{backend_class.name}-{parameter.name}".replace("_", "-") envvar = ( f"{ENVVAR_PREFIX}_{backend_class.name}_{parameter.name}".upper() ) option_kwargs = {} # If the parameter is a boolean, convert it to a flag option if parameter.annotation is bool: option = ( f"{option}/--no-{backend_class.name}-{parameter.name}".replace( "_", "-" ) ) option_kwargs["is_flag"] = True elif parameter.annotation is dict: option_kwargs["type"] = CommaSeparatedKeyValueParamType() command = ( optgroup.option( option, envvar=envvar, default=parameter.default, **option_kwargs, ) )(command) command = (optgroup.group(f"{backend_class.name} backend"))(command) command = (cli.command(name=name or command.__name__))(command) return command
def list_(details, new, backend, **options): """List available archives from a configured storage backend.""" logger.info("Listing archives for the configured %s backend", backend) logger.debug("Fetch details: %s", str(details)) logger.debug("Backend parameters: %s", options) storage = get_instance_from_class( get_class_from_name(backend, STORAGE_BACKENDS), **options ) archives = storage.list(details=details, new=new) counter = 0 for archive in archives: click.echo(json.dumps(archive) if details else archive) counter += 1 if counter == 0: logger.warning("Configured %s backend contains no archive", backend)
def fetch(backend, archive, chunk_size, **options): """Fetch an archive or records from a configured backend.""" logger.info( "Fetching data from the configured %s backend (archive: %s | chunk size: %s)", backend, archive, chunk_size, ) logger.debug("Backend parameters: %s", options) backend_class = get_class_from_name(backend, BACKENDS) backend = get_instance_from_class(backend_class, **options) backend_type = get_backend_type(backend_class) if backend_type == BackendTypes.STORAGE: backend.read(archive, chunk_size=chunk_size) elif backend_type == BackendTypes.DATABASE: backend.get(chunk_size=chunk_size) elif backend_type is None: msg = "Cannot find an implemented backend type for backend %s" logger.error(msg, backend) raise UnsupportedBackendException(msg, backend)