def generate_query(self, bbox: str = None, zoom: str = None): queries = [] found_layers = set() for layer_id, layer in self.get_layers(): found_layers.add(layer_id) query = self.generate_layer(layer, zoom, bbox) queries.append(query) if self.layers_ids and self.layers_ids != found_layers: unknown = sorted(self.layers_ids - found_layers) raise DocoptExit( f"Unable to find layer [{', '.join(unknown)}]. Available layers:\n" + '\n'.join(f"* {v['layer']['id']}" + (f"\n{v['layer']['description']}" if v['layer']. get('description') else '') for v in self.tileset.layers)) if not queries: raise DocoptExit('Could not find any layer definitions') query = "SELECT STRING_AGG(mvtl, '') AS mvt FROM (\n " + \ "\n UNION ALL\n ".join(queries) + \ "\n) AS all_layers" if self.key_column: query = f"SELECT mvt, md5(mvt) AS key FROM ({query}) AS mvt_data" return query + '\n'
def check_arguments(arguments): if not arguments['--spec']: error( '--spec flag not set and no .nbinteract.json file found. Rerun ' 'this command with the --spec flag or run `nbinteract init` to ' 'resolve this issue.' ) raise DocoptExit() if not SPEC_REGEX.match(arguments['--spec']): error( 'Spec must be in the format {username}/{repo}/{branch} but got ' + arguments['--spec'] + '.\n' 'Exiting...' ) raise DocoptExit() if arguments['--images'] and not arguments['--output']: error( 'If --images is specified, --output must also be specified. ' 'Exiting...' ) raise DocoptExit() if arguments['--template'] not in VALID_TEMPLATES: error( 'Unsupported template: "{}". Template must be one of: \n{}' .format(arguments['--template'], VALID_TEMPLATES) ) raise DocoptExit()
def get_layers(self) -> Iterable[Tuple[str, Layer]]: all_layers = [(v.id, v) for v in self.tileset.layers] if not all_layers: raise DocoptExit('Could not find any layer definitions') duplicates = find_duplicates([k for k, v in all_layers]) if duplicates: raise DocoptExit(f'Duplicate layer IDs found: {", ".join(duplicates)}') if not self.layer_ids: yield from all_layers return found_layers = set() skipped_layers = set() for layer_id, layer in all_layers: if (layer_id in self.layer_ids) != self.exclude_layers: found_layers.add(layer_id) yield layer_id, layer else: skipped_layers.add(layer_id) expected_result = skipped_layers if self.exclude_layers else found_layers if self.layer_ids != expected_result: unknown = sorted(self.layer_ids - expected_result) raise DocoptExit( f"Unable to find layer [{', '.join(unknown)}]. Available layers:\n" + '\n'.join(f"* {k}" + ( f"\n{v.description}" if v.description else '' ) for k, v in all_layers) )
def main(): """ Create a client, parse the arguments received on the command line, and call the appropriate method on the client. """ cli = DeisClient() args = docopt(__doc__, version='Deis CLI {}'.format(__version__), options_first=True) cmd = args['<command>'] cmd, help_flag = parse_args(cmd) # print help if it was asked for if help_flag: if cmd != 'help': if cmd in dir(cli): print trim(getattr(cli, cmd).__doc__) return docopt(__doc__, argv=['--help']) # re-parse docopt with the relevant docstring if cmd in dir(cli): docstring = trim(getattr(cli, cmd).__doc__) if 'Usage: ' in docstring: args.update(docopt(docstring)) # find the method for dispatching if hasattr(cli, cmd): method = getattr(cli, cmd) else: raise DocoptExit('Found no matching command') # dispatch the CLI command try: method(args) except EnvironmentError: print 'Could not find git remote for deis' raise DocoptExit()
def validate_duration(duration: str): if duration is not None: try: duration = int(duration) if duration <= 0: raise DocoptExit("Duration must be positive") except ValueError: raise DocoptExit("Duration must be a number")
def validate_timeout(timeout: str): if timeout is not None: try: timeout = int(timeout) except ValueError: raise DocoptExit("Timeout must be a number") if timeout < 0: raise DocoptExit("Timeout must be positive")
def check_options(options): if not options['language']: raise DocoptExit( 'Error: No language selected, use "--language=fr,en,de" or "--language=all"' ) if not options['name']: raise DocoptExit( 'Error: No name selected, use "--name=google,facebook" or "--name=all"' ) if not options['variant']: raise DocoptExit( 'Error: No variant selected, use "--variant=black,eco" or "--variant=all"' )
def main(): args = docopt(__doc__, version='0.0.3', options_first=True) # Retrieve the command to execute. command_name = args.pop('<command>').capitalize() # Retrieve the command arguments. command_args = args.pop('<args>') if command_args is None: command_args = {} # After 'poping' '<command>' and '<args>', what is left in the args dictionary are the global arguments. # Retrieve the class from the 'commands' module. try: command_class = getattr(commands, command_name) except AttributeError: print('Unknown command. RTFM!.') raise DocoptExit() # Create an instance of the command. command = command_class(command_args, args) # Execute the command. command.execute()
def main(): args = docopt(__doc__) src = args.get('<src>') dest = args.get('<dest>') if args.get('-'): src_text = sys.stdin.read() elif src: with open(src, "rb") as f: src_text = f.read() else: raise DocoptExit() src_text = src_text.decode('utf-8') params = {} rendered = render(src_text, params) # just a little bit walkaround (this is a mako's spec?) rendered = rendered[1:] if rendered.startswith('\n') else rendered # same as the above rendered = rendered + '\n' if not rendered.endswith('\n') else rendered rendered = rendered.encode('utf-8') if dest is None: sys.stdout.write(rendered) else: with open(dest, "wb") as f: f.write(rendered)
def do_list(self): list_filter = self.args["--filter"] or "my" if list_filter not in ["my", "all", "auto"]: raise DocoptExit("--filter value must be in [my, all, auto]") show_ended = self.args["--show-ended"] count = self.args.get("--count", 25) try: sandbox_list = self.manager.list(filter_opt=list_filter, count=count) except Exception as e: logger.exception(e, exc_info=False) sandbox_list = None return self.die() result_table = [] for sb in sandbox_list: if sb.sandbox_status == "Ended" and not show_ended: continue result_table.append({ "Sandbox ID": sb.sandbox_id, "Sandbox Name": sb.name, "Blueprint Name": sb.blueprint_name, "Status": sb.sandbox_status, }) self.message(tabulate.tabulate(result_table, headers="keys"))
def _get_cli_args(): """Retrieve set of CLI arguments.""" cli_argspec = CLI_ARGSPEC.format( cmd_setup=CLI_COMMAND.SETUP, cmd_upgrade=CLI_COMMAND.UPGRADE, cmd_start=CLI_COMMAND.START, valid_cmd_targets=', '.join(VALID_CLI_CMDTARGETS), default_cmd_target=CLI_CMDTARGET.PKGALL, default_root_dpath=storage.DCOS_INST_ROOT_DPATH_DFT, default_config_dpath=storage.DCOS_INST_CFG_DPATH_DFT, default_state_dpath=storage.DCOS_INST_STATE_DPATH_DFT, default_repository_dpath=storage.DCOS_INST_PKGREPO_DPATH_DFT, default_var_dpath=storage.DCOS_INST_VAR_DPATH_DFT, default_clustercfg_fpath=cm_const.DCOS_CLUSTER_CFG_FNAME_DFT) cli_args = docopt(cli_argspec) # Verify actual command target value cmdtarget = cli_args.get('--target') if cmdtarget not in VALID_CLI_CMDTARGETS: LOG.critical(f'CLI: Invalid option value: --target: {cmdtarget}') raise DocoptExit() LOG.debug(f'cli_args: {cli_args}') return cli_args
def parse_args(argv=None): cli_args = docopt(__doc__, argv, version=VERSION, options_first=True) if cli_args['--log-level'] not in \ (None, 'info', 'debug', 'critical', 'warning', 'error', 'INFO', 'DEBUG', 'CRITICAL', 'WARNING', 'ERROR'): raise DocoptExit() return cli_args
def get_connection(self) -> ColonyConnection: # first try to get them as options or from env variable token = self._args_parser.token space = self._args_parser.space account = self._args_parser.account # then try to load them from file if not all([token, space]): logger.debug( "Couldn't fetch token/space neither from command line nor environment variables" ) profile = self._args_parser.profile config_file = self._args_parser.get_config_path() logger.debug( "Trying to obtain unset values from configuration file") try: colony_conn = ColonyConfigProvider( config_file).load_connection(profile) token = token or colony_conn[ColonyConfigKeys.TOKEN] space = space or colony_conn[ColonyConfigKeys.SPACE] if ColonyConfigKeys.ACCOUNT in colony_conn: account = colony_conn[ColonyConfigKeys.ACCOUNT] except ConfigError as e: raise DocoptExit( f"Unable to read Colony credentials. Reason: {e}") return ColonyConnection(token=token, space=space, account=account)
def main(): """Main CLI entrypoint.""" import domba.clis options = docopt(__doc__, version=VERSION, options_first=True) command_name = "" args = "" command_class ="" command_name = options.pop('<command>') args = options.pop('<args>') if args is None: args = {} try: module = getattr(domba.clis, command_name) domba.clis = getmembers(module, isclass) command_class = [command[1] for command in domba.clis if command[0] != 'Base'][0] except AttributeError as e: print(e) raise DocoptExit() command = command_class(options, args) command.execute()
def _map_argv(argv, application_module): try: # by default docopt uses sys.argv[1:]; ensure correct args passed args = docopt(__doc__, argv=argv[1:], version=application_module.__version__) if argv[2] == "help": raise DocoptExit() except DocoptExit: if argv[2] == "help": raise args = docopt(__doc__, argv[1:3], version=application_module.__version__) args["--cms"] = "--cms" in argv args["--persistent"] = "--persistent" in argv for arg in argv: if arg.startswith("--extra-settings="): args["--extra-settings"] = arg.split("=")[1] if arg.startswith("--runner="): args["--runner"] = arg.split("=")[1] if arg.startswith("--persistent-path="): args["--persistent-path"] = arg.split("=")[1] args["--persistent"] = True args["options"] = [argv[0]] + argv[2:] if args["test"] and "--native" in args["options"]: args["test"] = False args["<command>"] = "test" args["options"].remove("--native") return args
def __init__(self, *argv, **kwargs): try: docstring = self.__doc__.format(version=__version__) options = docopt(docstring, *argv, **kwargs) except DocoptExit: raise SystemExit(docstring) command = options["COMMAND"] if command is None: raise SystemExit(docstring) if not hasattr(self, command): if hasattr(self, "_%s" % command): command = "_%s" % command else: raise NoExistCommand(command, self) command_handler = getattr(self, command) command_docstring = inspect.getdoc(command_handler) template = Template(command_docstring, trim_blocks=True, lstrip_blocks=True) command_docstring = template.render(attrs=Host.attrs) command_options = docopt(command_docstring, options["ARGS"], options_first=True) # options, command_handler, command_options try: command_handler(options, command_options) except Exception as e: raise DocoptExit(str(e))
def do_desk(self, arg): """ Usage: desk --action <action> --name "<name>" --i2c_address <i2c_address> Options: --action Action on a desk [create] --name Name of desk to execute action on --i2c_address Address on the I2C bus """ action = arg['--action'] name = arg['--name'] i2c_address = arg['--i2c_address'] if action == 'create': status, response = DeskHandlerService.create( name=name, i2c_address=i2c_address) if status == DeskHandlerService.SUCCESS_STATUS: print(Fore.GREEN + DeskHandlerService.SUCCESSFUL_CREATE + Style.RESET_ALL) else: print(Fore.RED + DeskHandlerService.FAILED_CREATE) print(response + Style.RESET_ALL) else: raise DocoptExit('Action on desk is not allowed')
def load_robot(robot_name, parts=None): modules = [robot_name] if not parts else parts loaded = [] for name in modules: try: print bcolors.OKBLUE + '\tloading %s' % name + bcolors.ENDC module = importlib.import_module('robot_skills.' + name) constructor = module.__getattribute__(name.title()) if parts: instance = constructor(robot_name, wait_services=True) else: instance = constructor(wait_services=False) loaded.append(instance) # register as global globals()[name] = instance print bcolors.OKGREEN + '\tSuccesfully loaded "%s"' % name + bcolors.ENDC except: msg = '\n"%s" could not be found!!!\n' % name print bcolors.WARNING + msg + bcolors.ENDC traceback.print_exc() if not len(loaded): raise DocoptExit("error: no robots or parts loaded")
def main(argv=sys.argv): # pragma: no cover # Command is executed in the main directory of the plugin, and we must # include it in the current path for the imports to work sys.path.insert(0, '.') if len(argv) > 1: application = argv[1] application_module = import_module(application) try: args = docopt(__doc__, version=application_module.__version__) if argv[2] == 'help': raise DocoptExit() except DocoptExit: if argv[2] == 'help': raise args = docopt(__doc__, argv[1:3], version=application_module.__version__) args['--cms'] = '--cms' in argv for arg in argv: if arg.startswith('--extra-settings='): args['--extra-settings'] = arg.split('=')[1] if arg.startswith('--runner='): args['--runner'] = arg.split('=')[1] args['options'] = [argv[0]] + argv[2:] if args['test'] and '--native' in args['options']: args['test'] = False args['<command>'] = 'test' args['options'].remove('--native') core(args=args, application=application) else: args = docopt(__doc__, version=__version__)
def _map_argv(argv, application_module): try: # by default docopt uses sys.argv[1:]; ensure correct args passed args = docopt(__doc__, argv=argv[1:], version=application_module.__version__) if argv[2] == 'help': raise DocoptExit() except DocoptExit: if argv[2] == 'help': raise args = docopt(__doc__, argv[1:3], version=application_module.__version__) args['--cms'] = '--cms' in argv args['--persistent'] = '--persistent' in argv for arg in argv: if arg.startswith('--extra-settings='): args['--extra-settings'] = arg.split('=')[1] if arg.startswith('--runner='): args['--runner'] = arg.split('=')[1] if arg.startswith('--persistent-path='): args['--persistent-path'] = arg.split('=')[1] args['--persistent'] = True args['options'] = [argv[0]] + argv[2:] if args['test'] and '--native' in args['options']: args['test'] = False args['<command>'] = 'test' args['options'].remove('--native') return args
def do_remove(self): profile_to_remove = self.args["<profile>"] if not profile_to_remove: raise DocoptExit("Please provide a profile name to remove") config_file = GlobalInputParser.get_config_path() config_provider = ColonyConfigProvider(config_file) config_provider.remove_profile(profile_to_remove) return self.success()
def docopt(doc, argv=None, help=True, version=None, options_first=False): # @ReservedAssignment help """Re-implementation of docopt.docopt() function to parse ANYTHING at the end (for proxying django options).""" if argv is None: argv = sys.argv[1:] DocoptExit.usage = printable_usage(doc) options = parse_defaults(doc) pattern = parse_pattern(formal_usage(DocoptExit.usage), options) argv = parse_argv(TokenStream(argv, DocoptExit), list(options), options_first) pattern_options = set(pattern.flat(Option)) for ao in pattern.flat(AnyOptions): doc_options = parse_defaults(doc) ao.children = list(set(doc_options) - pattern_options) extras(help, version, argv, doc) __matched, __left, collected = pattern.fix().match(argv) # if matched and left == []: # better error message if left? if collected: # better error message if left? result = Dict((a.name, a.value) for a in (pattern.flat() + collected)) collected_django_options = len(result.get('DJANGO_OPTIONS', [])) result['DJANGO_OPTIONS'] = (result.get('DJANGO_OPTIONS', []) + sys.argv[len(collected) + (collected_django_options or 1):]) # If any of the collected arguments are also in the DJANGO_OPTIONS, # then exit because we don't want users to have put options for kalite # at the end of the command if any( map( lambda x: x.name in map(lambda x: x.split("=")[0], result[ 'DJANGO_OPTIONS']), collected)): sys.stderr.write( "Cannot mix django manage command options with kalite options. " "Always put django management options last.\n\n") raise DocoptExit() return result raise DocoptExit()
def main(): """ Create a client, parse the arguments received on the command line, and call the appropriate method on the client. """ cli = DanaboxClient() args = docopt(__doc__, version='Danabox CLI {}'.format(__version__), options_first=True) cmd = args['<command>'] cmd, help_flag = parse_args(cmd) # print help if it was asked for if help_flag: if cmd != 'help': if cmd in dir(cli): print(trim(getattr(cli, cmd).__doc__)) return docopt(__doc__, argv=['--help']) # unless cmd needs to use sys.argv directly if hasattr(cli, cmd): method = getattr(cli, cmd) else: raise DocoptExit('Found no matching command, try `danabox help`') # re-parse docopt with the relevant docstring unless it needs sys.argv if cmd not in ('apps_run', ): docstring = trim(getattr(cli, cmd).__doc__) if 'Usage: ' in docstring: args.update(docopt(docstring)) # dispatch the CLI command try: method(args) except EnvironmentError as err: raise DocoptExit(err.message) except ResponseError as err: resp = err.message print('{} {}'.format(resp.status_code, resp.reason)) try: msg = resp.json() except: msg = resp.text print(msg) sys.exit(1)
def main(args=None): args = docopt(__doc__, argv=args, version=VERSION) # from the whole input bag, split media files from transcriptions # media and its transcript must be paired (i.e same order) # for example, supose a folder with a video file macri_gato.mp4 and # its transcription is macri_gato.txt # # *.mp4 *.txt # macri_gato.* # macri_gato.mp4 macri_gato.txt media = [] transcripts = [] for filename in chain.from_iterable( glob.iglob(pattern) for pattern in args['<input_files>']): output_extension = os.path.splitext(filename)[1][1:] if output_extension in extensions_dict: media.append(filename) else: transcripts.append(filename) media_str = ' \n'.join(media) transcripts_str = ' \n'.join(transcripts) info = "Audio/Video:\n {}\nTranscripts/subtitle:\n {}".format( media_str, transcripts_str) logging.info(info) if not media or len(media) != len(transcripts): raise DocoptExit( "Input mismatch: the quantity of inputs and transcriptions differs" ) try: return miau(media, transcripts, args['--remix'], args['--output'], args['--dump'], debug=args['--debug'], force_language=args['--lang']) except ValueError as e: raise DocoptExit(str(e))
def main(): arguments = docopt(doc, version=version_string, options_first=True) command = arguments['<command>'] command_arguments = [command] + arguments['<arguments>'] config = Config() if command not in COMMANDS: raise DocoptExit('Unknown command: ' + command) if command not in COMMANDS_NO_CONFIG and len(config.keys()) == 0: raise DocoptExit('Not configured, try running: i2 configure') client = ApiClient(config.url, verify=False) # TODO: Verify option client.authenticate(username=config.username, password=config.password) module = 'icinga2client.cli.{}'.format(command) invoke = importlib.import_module(module).invoke invoke(client, command_arguments, porcelain=arguments['--porcelain'])
def checkargs(args): """Check arguments for validity :param args: parsed arguments from :class:`docopt.docopt` :type args: dict :raises: :class:`docopt.DocoptExit`, :class:`FileNotFoundError` :return: """ molden_file = args['<molden_file>'] if molden_file is None: raise DocoptExit() if not os.path.exists(molden_file): raise FileNotFoundError(molden_file)
def __init__(self, *args, **kwargs): # Parse args self._args = docopt(trim(self.__doc__), *args, **kwargs) # Populate Arguments from args for f in dir(self): arg = getattr(self, f) if isinstance(arg, Argument): try: arg.set_value(self._args) except (ValueError, TypeError) as e: print("Error for option {}: {}".format(arg._key, e)) raise DocoptExit()
def do_remove(self): profile_to_remove = self.input_parser.configure_remove.profile if not profile_to_remove: raise DocoptExit("Please provide a profile name to remove") try: config_file = GlobalInputParser.get_config_path() config_provider = ColonyConfigProvider(config_file) config_provider.remove_profile(profile_to_remove) except Exception as e: logger.exception(e, exc_info=False) return self.die() return self.success()
def parse(self): """ Parse the doc string given on instantiation, determine current command Returns: the parser self """ try: self.args = safe_docopt(self.docs, argv=self.argv, version=self.version, help=False) except DocoptExit: # by specifying docopt(help=False) we get to control what happens on parse failure if self.argv: # if help requested, show help. this simulates a valid Usage: help <action> if self.argv[0] in ('help', '--help'): action = self.argv[1] if len(self.argv) > 1 else None self.args = { '<command>': 'help', '<action>': action, } self.help(usage_only=False) # raise SystemExit because raise SystemExit # custom handling should be provided by a Command with command='catchall' elif self.argv[0] == '--copyright': self.args = { '--copyright': True, } else: # this re-raises DocoptExit which prints usage raise else: # this re-raises DocoptExit which prints usage raise # if we get here it means docopt has decided all the arguments are valid # -- see if we have a command self.command = self.parse_command() # -- no command?! if not self.command: self.help() raise DocoptExit() if self.should_debug: print("*** docopt parsed args", self.args) print("*** docopt using command class {}".format(repr( self.command))) print("*** docopt using command method {}".format( repr(self.command.get_command_method()))) return self
def main(self): args = docopt(__doc__, version=molecule.__version__, options_first=True) command_name = args.get('<command>').capitalize() command_args = {} if args.get('<args>') is None else args.pop('<args>') try: command_class = getattr(molecule.commands, command_name) except AttributeError: raise DocoptExit() c = command_class(command_args, args) sys.exit(c.execute())