Python click.Context() Examples
The following are 30
code examples of click.Context().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
click
, or try the search function
.
Example #1
Source File: test_cli.py From profiling with BSD 3-Clause "New" or "Revised" License | 6 votes |
def test_customized_cli(): cli = ProfilingCLI(default='bar') @cli.command(aliases=['fooo', 'foooo']) def foo(): pass @cli.command() @click.argument('l', default='answer') @click.option('-n', type=int, default=0) def bar(l, n=0): click.echo('%s: %d' % (l, n)) assert len(cli.commands) == 2 ctx = click.Context(cli) assert cli.get_command(ctx, 'foo').name == 'foo' assert cli.get_command(ctx, 'fooo').name == 'foo' assert cli.get_command(ctx, 'foooo').name == 'foo' assert cli.get_command(ctx, 'bar').name == 'bar' assert cli.get_command(ctx, 'hello.txt').name == 'bar' assert 'Usage:' in cli_runner.invoke(cli, []).output assert cli_runner.invoke(cli, ['zero']).output == 'zero: 0\n' assert cli_runner.invoke(cli, ['one', '-n', '1']).output == 'one: 1\n' assert cli_runner.invoke(cli, ['-n', '42']).output == 'answer: 42\n' assert 'no such option' in cli_runner.invoke(cli, ['-x']).output
Example #2
Source File: test_subcommand.py From pygreynoise with MIT License | 6 votes |
def test_input_file_invalid_ip_addresses_passsed(self, api_client): """Error returned if only invalid IP addresses are passed in input file.""" runner = CliRunner() expected = ( "Error: at least one valid IP address must be passed either as an " "argument (IP_ADDRESS) or through the -i/--input_file option." ) result = runner.invoke( subcommand.interesting, ["-i", StringIO("not-an-ip")], parent=Context(main, info_name="greynoise"), ) assert result.exit_code == -1 assert "Usage: greynoise interesting" in result.output assert expected in result.output api_client.interesting.assert_not_called()
Example #3
Source File: test_subcommand.py From pygreynoise with MIT License | 6 votes |
def test_input_file_invalid_ip_addresses_passsed(self, api_client): """Error returned if only invalid IP addresses are passed in input file.""" runner = CliRunner() expected = ( "Error: at least one valid IP address must be passed either as an " "argument (IP_ADDRESS) or through the -i/--input_file option." ) result = runner.invoke( subcommand.ip, ["-i", StringIO("not-an-ip")], parent=Context(main, info_name="greynoise"), ) assert result.exit_code == -1 assert "Usage: greynoise ip" in result.output assert expected in result.output api_client.ip.assert_not_called()
Example #4
Source File: test_subcommand.py From pygreynoise with MIT License | 6 votes |
def test_empty_input_file(self, api_client): """Error is returned if empty input fle is passed.""" runner = CliRunner() expected = ( "Error: at least one query must be passed either as an argument " "(QUERY) or through the -i/--input_file option." ) result = runner.invoke( subcommand.query, ["-i", StringIO()], parent=Context(main, info_name="greynoise"), ) assert result.exit_code == -1 assert "Usage: greynoise query" in result.output assert expected in result.output api_client.query.assert_not_called()
Example #5
Source File: launch.py From agents-aea with Apache License 2.0 | 6 votes |
def _launch_agents( click_context: click.core.Context, agents: List[str], multithreaded: bool ) -> None: """ Run multiple agents. :param click_context: click context object. :param agents: agents names. :param multithreaded: bool flag to run as multithreads. :return: None. """ agents_directories = list(map(Path, list(OrderedDict.fromkeys(agents)))) if multithreaded: failed = _launch_threads(click_context, agents_directories) else: failed = _launch_subprocesses(click_context, agents_directories) logger.debug(f"Exit cli. code: {failed}") sys.exit(failed)
Example #6
Source File: helper.py From pygreynoise with MIT License | 6 votes |
def get_queries(context, input_file, query): """Get queries passed as argument or via input file. :param context: Subcommand context :type context: click.Context :param input_file: Input file :type input_file: click.File | None :param query: GNQL query :type query: str | None """ if input_file is None and not sys.stdin.isatty(): input_file = click.open_file("-") if input_file is None and not query: click.echo(context.get_help()) context.exit(-1) queries = [] if input_file is not None: queries.extend([line.strip() for line in input_file]) if query: queries.append(query) if not queries: output = [ context.command.get_usage(context), ( "Error: at least one query must be passed either as an argument " "(QUERY) or through the -i/--input_file option." ), ] click.echo("\n\n".join(output)) context.exit(-1) return queries
Example #7
Source File: test_subcommand.py From pygreynoise with MIT License | 6 votes |
def test_input_file_invalid_ip_addresses_passsed(self, api_client): """Error returned if only invalid IP addresses are passed in input file.""" runner = CliRunner() expected = ( "Error: at least one valid IP address must be passed either as an " "argument (IP_ADDRESS) or through the -i/--input_file option." ) result = runner.invoke( subcommand.quick, ["-i", StringIO("not-an-ip")], parent=Context(main, info_name="greynoise"), ) assert result.exit_code == -1 assert "Usage: greynoise quick" in result.output assert expected in result.output api_client.quick.assert_not_called()
Example #8
Source File: test_subcommand.py From pygreynoise with MIT License | 6 votes |
def test_empty_input_file(self, api_client): """Error is returned if empty input fle is passed.""" runner = CliRunner() expected = ( "Error: at least one query must be passed either as an argument " "(QUERY) or through the -i/--input_file option." ) result = runner.invoke( subcommand.stats, ["-i", StringIO()], parent=Context(main, info_name="greynoise"), ) assert result.exit_code == -1 assert "Usage: greynoise stats" in result.output assert expected in result.output api_client.query.assert_not_called()
Example #9
Source File: eval.py From efficientdet-tf with GNU General Public License v3.0 | 6 votes |
def VOC(ctx: click.Context, **kwargs: Any) -> None: kwargs.update(ctx.obj['common']) model, _ = ctx.obj['model'], ctx.obj['params'] config = model.config im_size = config.input_size class2idx = efficientdet.data.voc.LABEL_2_IDX im_size = config.input_size ds = efficientdet.data.voc.build_dataset( kwargs['root_test'], im_input_size=im_size, shuffle=False) ds = ds.padded_batch(batch_size=kwargs['batch_size'], padded_shapes=((*im_size, 3), ((None,), (None, 4))), padding_values=(0., (-1, -1.))) gtCOCO = coco.tf_data_to_COCO(ds, class2idx) coco.evaluate( model=model, dataset=ds, steps=sum(1 for _ in ds), gtCOCO=gtCOCO)
Example #10
Source File: cli.py From chaostoolkit with Apache License 2.0 | 6 votes |
def discover(ctx: click.Context, package: str, discovery_path: str = "./discovery.json", no_system_info: bool = False, no_install: bool = False) -> Discovery: """Discover capabilities and experiments.""" settings = load_settings(ctx.obj["settings_path"]) try: notify(settings, DiscoverFlowEvent.DiscoverStarted, package) discovery = disco( package_name=package, discover_system=not no_system_info, download_and_install=not no_install) except DiscoveryFailed as err: notify(settings, DiscoverFlowEvent.DiscoverFailed, package, err) logger.debug("Failed to discover {}".format(package), exc_info=err) logger.fatal(str(err)) return with open(discovery_path, "w") as d: d.write(json.dumps(discovery, indent=2, default=encoder)) logger.info("Discovery outcome saved in {p}".format( p=discovery_path)) notify(settings, DiscoverFlowEvent.DiscoverCompleted, discovery) return discovery
Example #11
Source File: cli.py From chaostoolkit with Apache License 2.0 | 6 votes |
def get_settings_value(ctx: click.Context, key: str, fmt: str = "json"): """ Show a settings value. The key must be dotted path to its location in the settings file. """ if not os.path.isfile(ctx.obj["settings_path"]): ctx.exit(1) settings = load_settings(ctx.obj["settings_path"]) or {} item = locate_settings_entry(settings, key) if not item: ctx.exit(1) parent, entry, key_tail, index = item if fmt == "json": click.echo(json.dumps(entry, indent=2)) elif fmt == "string": click.echo(str(entry)) elif fmt == "yaml": click.echo(yaml.dump(entry, indent=2))
Example #12
Source File: cli.py From chaostoolkit with Apache License 2.0 | 6 votes |
def set_settings_value(ctx: click.Context, key: str, value: str = None): """ Set a settings value. The value must be a valid JSON string so that it can be interpreted with the appropriate type. The key must be dotted path to its location in the settings file. """ if not os.path.isfile(ctx.obj["settings_path"]): ctx.exit(1) settings = load_settings(ctx.obj["settings_path"]) or {} item = locate_settings_entry(settings, key) if not item: ctx.exit(1) parent, entry, key_tail, index = item value = json.loads(value) if key_tail is not None: parent[key_tail] = value elif index is not None: parent[index] = value save_settings(settings, ctx.obj["settings_path"])
Example #13
Source File: method_terminal_commands.py From cellphonedb with MIT License | 6 votes |
def check_subsampling_params(ctx: Context, argument: Argument, value) -> Any: subsampling = ctx.params.get('subsampling') if not subsampling and value is not None: tpl = 'This parameter ({}) only applies to subsampling, to enable it add `--subsampling` to your command' app_logger.error(tpl.format(argument.name)) ctx.abort() if argument.name == 'subsampling_log' and subsampling and value is None: app_logger.error('''In order to perform subsampling you need to specify whether to log1p input counts or not: to do this specify in your command as --subsampling-log [true|false]''') ctx.abort() defaults = { 'subsampling_num_pc': 100, 'subsampling_num_cells': None } if subsampling and value is None: return defaults.get(argument.name, None) return value
Example #14
Source File: cli.py From chaostoolkit with Apache License 2.0 | 6 votes |
def validate(ctx: click.Context, source: str, no_verify_tls: bool = False) -> Experiment: """Validate the experiment at SOURCE.""" settings = load_settings(ctx.obj["settings_path"]) try: experiment = load_experiment( source, settings, verify_tls=not no_verify_tls) except InvalidSource as x: logger.error(str(x)) logger.debug(x) ctx.exit(1) try: notify(settings, ValidateFlowEvent.ValidateStarted, experiment) ensure_experiment_is_valid(experiment) notify(settings, ValidateFlowEvent.ValidateCompleted, experiment) logger.info("experiment syntax and semantic look valid") except ChaosException as x: notify(settings, ValidateFlowEvent.ValidateFailed, experiment, x) logger.error(str(x)) logger.debug(x) ctx.exit(1) return experiment
Example #15
Source File: client.py From gordo with GNU Affero General Public License v3.0 | 6 votes |
def metadata( ctx: click.Context, output_file: typing.Optional[typing.IO[str]], target: typing.List[str], ): """ Get metadata from a given endpoint """ client = Client(*ctx.obj["args"], **ctx.obj["kwargs"]) metadata = { k: v.to_dict() for k, v in client.get_metadata(targets=target).items() # type: ignore } if output_file: json.dump(metadata, output_file) click.secho(f"Saved metadata json to file: '{output_file}'") else: pprint(metadata) return metadata
Example #16
Source File: client.py From gordo with GNU Affero General Public License v3.0 | 6 votes |
def download_model(ctx: click.Context, output_dir: str, target: typing.List[str]): """ Download the actual model from the target and write to an output directory """ client = Client(*ctx.obj["args"], **ctx.obj["kwargs"]) models = client.download_model(targets=target) # Iterate over mapping of models and save into their own sub dirs of the output_dir for model_name, model in models.items(): model_out_dir = os.path.join(output_dir, model_name) os.mkdir(model_out_dir) click.secho( f"Writing model '{model_name}' to directory: '{model_out_dir}'...", nl=False ) serializer.dump(model, model_out_dir) click.secho(f"done") click.secho(f"Wrote all models to directory: {output_dir}", fg="green")
Example #17
Source File: geocube.py From geocube with BSD 3-Clause "New" or "Revised" License | 6 votes |
def check_version(ctx, _, value): """ Print current version, and check for latest version. Called via 'geocube --version' :param ctx: Application context object (click.Context) :param value: Passed in by Click :return None """ if not value or ctx.resilient_parsing: return click.echo(f"geocube v{__version__}") ctx.exit()
Example #18
Source File: cli.py From quart with MIT License | 6 votes |
def get_command(self, ctx: click.Context, name: str) -> click.Command: """Return the relevant command given the context and name. .. warning:: This differs substantially from Flask in that it allows for the inbuilt commands to be overridden. """ info = ctx.ensure_object(ScriptInfo) command = None try: command = info.load_app().cli.get_command(ctx, name) except NoAppException: pass if command is None: command = super().get_command(ctx, name) return command
Example #19
Source File: cli.py From chaostoolkit with Apache License 2.0 | 5 votes |
def show_settings(ctx: click.Context, fmt: str = "json"): """ Show the entire content of the settings file. Be aware this will not obfuscate secret data. """ if not os.path.isfile(ctx.obj["settings_path"]): click.abort( "No settings file found at {}".format(ctx.obj["settings_path"])) settings = load_settings(ctx.obj["settings_path"]) or {} if fmt == "json": click.echo(json.dumps(settings, indent=2)) elif fmt == "yaml": click.echo(yaml.dump(settings, indent=2))
Example #20
Source File: cli.py From apprise with MIT License | 5 votes |
def print_help_msg(command): """ Prints help message when -h or --help is specified. """ with click.Context(command) as ctx: click.echo(command.get_help(ctx))
Example #21
Source File: macros.py From cfripper with Apache License 2.0 | 5 votes |
def define_env(env): @env.macro def cfripper_rules(): severity_map = {RuleRisk.HIGH: "**High**", RuleRisk.MEDIUM: "Medium", RuleRisk.LOW: "Low"} rules_inspection = inspect.getmembers(rules, inspect.isclass) results = [] for _, klass in rules_inspection: doc = inspect.getdoc(klass) parsed_doc = parse_doc_string(doc) content = "" for paragraph_title, paragraph_text in parsed_doc.items(): if paragraph_title == "Description": # Remove ABCMeta default docstring if not paragraph_text.startswith("Helper class that"): content += paragraph_text content += f"\n\n>Severity: {severity_map[klass.RISK_VALUE]}\n" if klass.RULE_MODE == RuleMode.MONITOR: content += "\n>Defaults to monitor mode (rule not enforced)\n" if klass.RULE_MODE == RuleMode.DEBUG: content += "\n>Defaults to debug mode (rule not enforced)\n" else: content += f"\n#### {paragraph_title}\n" content += f"{paragraph_text}\n" results.append((klass.__name__, content)) return sorted(results) @env.macro def inline_source(reference): obj = get_object_from_reference(reference) source = "".join(inspect.getsourcelines(obj)[0]) return f"```python3\n{source}```" @env.macro def cfripper_cli_help(): with click.Context(cli) as ctx: return cli.get_help(ctx)
Example #22
Source File: utils.py From pros-cli2 with Mozilla Public License 2.0 | 5 votes |
def verbose(content, level: int = 1, ctx=None): if ctx is None: try: ctx = click.get_current_context() except Exception: ctx = State() if ctx is not None and isinstance(ctx, click.Context): ctx = ctx.obj elif not isinstance(ctx, State): ctx = State() if ctx.verbosity >= level: click.echo(content)
Example #23
Source File: utils.py From pros-cli2 with Mozilla Public License 2.0 | 5 votes |
def debug(content, ctx=None, debug_flag=None): if debug_flag is None: if ctx is None: try: ctx = click.get_current_context() except Exception: ctx = State() if ctx is not None and isinstance(ctx, click.Context): ctx = ctx.obj else: ctx = State() debug_flag = ctx.debug if debug_flag: click.echo('\tDEBUG: {}'.format(content))
Example #24
Source File: cli.py From pipenv with MIT License | 5 votes |
def unset(ctx, key): # type: (click.Context, Any) -> None '''Removes the given key.''' file = ctx.obj['FILE'] quote = ctx.obj['QUOTE'] success, key = unset_key(file, key, quote) if success: click.echo("Successfully removed %s" % key) else: exit(1)
Example #25
Source File: command_line.py From clearly with MIT License | 5 votes |
def get_command(self, ctx: click.Context, cmd_name: str) -> Optional[click.Command]: rv = click.Group.get_command(self, ctx, cmd_name) if rv is not None: return rv matches = [x for x in self.list_commands(ctx) if x.startswith(cmd_name)] if not matches: return if len(matches) > 1: ctx.fail('Too many matches: %s' % ', '.join(sorted(matches))) return click.Group.get_command(self, ctx, matches[0])
Example #26
Source File: common.py From xcube with MIT License | 5 votes |
def cli_option_traceback(func): """Decorator for adding a pre-defined, reusable CLI option `--traceback`.""" # noinspection PyUnusedLocal def _callback(ctx: click.Context, param: click.Option, value: bool): ctx_obj = ctx.ensure_object(dict) if ctx_obj is not None: ctx_obj["traceback"] = value return value return click.option( '--traceback', is_flag=True, help="Enable tracing back errors by dumping the Python call stack. " "Pass as very first option to also trace back error during command-line validation.", callback=_callback)(func)
Example #27
Source File: common.py From xcube with MIT License | 5 votes |
def cli_option_scheduler(func): """Decorator for adding a pre-defined, reusable CLI option `--scheduler`.""" # noinspection PyUnusedLocal def _callback(ctx: click.Context, param: click.Option, value: Optional[str]): if not value: return address_and_kwargs = value.split("?", 2) if len(address_and_kwargs) == 2: address, kwargs_string = address_and_kwargs kwargs = parse_cli_kwargs(kwargs_string, metavar="SCHEDULER") else: address, = address_and_kwargs kwargs = dict() try: # The Dask Client registers itself as the default Dask scheduler, and so runs dask.array used by xarray import distributed scheduler_client = distributed.Client(address, **kwargs) ctx_obj = ctx.ensure_object(dict) if ctx_obj is not None: ctx_obj["scheduler"] = scheduler_client return scheduler_client except ValueError as e: raise click.BadParameter(f'Failed to create Dask scheduler client: {e}') from e return click.option( '--scheduler', metavar='SCHEDULER', help="Enable distributed computing using the Dask scheduler identified by SCHEDULER. " "SCHEDULER can have the form <address>?<keyword>=<value>,... where <address> " "is <host> or <host>:<port> and specifies the scheduler's address in your network. " "For more information on distributed computing " "using Dask, refer to http://distributed.dask.org/. " "Pairs of <keyword>=<value> are passed to the Dask client. " "Refer to http://distributed.dask.org/en/latest/api.html#distributed.Client", callback=_callback)(func)
Example #28
Source File: cli.py From chaostoolkit with Apache License 2.0 | 5 votes |
def cli(ctx: click.Context, verbose: bool = False, no_version_check: bool = False, change_dir: str = None, no_log_file: bool = False, log_file: str = "chaostoolkit.log", log_format: str = "string", settings: str = CHAOSTOOLKIT_CONFIG_PATH): if no_log_file: configure_logger( verbose=verbose, log_format=log_format, context_id=str(uuid.uuid4())) else: configure_logger( verbose=verbose, log_file=log_file, log_format=log_format, context_id=str(uuid.uuid4())) subcommand = ctx.invoked_subcommand # make it nicer for going through the log file logger.debug("#" * 79) logger.debug("Running command '{}'".format(subcommand)) ctx.obj = {} ctx.obj["settings_path"] = click.format_filename(settings) logger.debug("Using settings file '{}'".format(ctx.obj["settings_path"])) if not no_version_check: check_newer_version(command=subcommand) if change_dir: logger.warning("Moving to {d}".format(d=change_dir)) os.chdir(change_dir)
Example #29
Source File: launch.py From agents-aea with Apache License 2.0 | 5 votes |
def _launch_threads(click_context: click.Context, agents: List[Path]) -> int: """ Launch many agents, multithreaded. :param agents: the click context. :param agents: list of paths to agent projects. :return: exit status """ aeas = [] # type: List[AEA] for agent_directory in agents: with cd(agent_directory): aeas.append(AEABuilder.from_aea_project(".").build()) threads = [Thread(target=agent.start) for agent in aeas] for t in threads: t.start() try: while sum([t.is_alive() for t in threads]) != 0: # exit when all threads are not alive. # done to avoid block on joins for t in threads: t.join(0.1) except KeyboardInterrupt: logger.info("Keyboard interrupt detected.") finally: for idx, agent in enumerate(aeas): if not agent.is_stopped: agent.stop() threads[idx].join() logger.info("Agent {} has been stopped.".format(agent.name)) return 0
Example #30
Source File: cli.py From quart with MIT License | 5 votes |
def with_appcontext(fn: Optional[Callable] = None) -> Callable: # decorator was used with parenthesis if fn is None: return with_appcontext @click.pass_context def decorator(__ctx: click.Context, *args: Any, **kwargs: Any) -> Any: async def _inner() -> Any: async with __ctx.ensure_object(ScriptInfo).load_app().app_context(): return __ctx.invoke(fn, *args, **kwargs) return asyncio.run(_inner()) return functools.update_wrapper(decorator, fn)