def __call__(self, args, parsed_globals): # args is the remaining unparsed args. # We might be able to parse these args so we need to create # an arg parser and parse them. self._subcommand_table = self._build_subcommand_table() self._arg_table = self._build_arg_table() event = 'before-building-argument-table-parser.%s' % \ ".".join(self.lineage_names) self._session.emit(event, argument_table=self._arg_table, args=args, session=self._session) parser = ArgTableArgParser(self.arg_table, self.subcommand_table) parsed_args, remaining = parser.parse_known_args(args) # Unpack arguments for key, value in vars(parsed_args).items(): cli_argument = None # Convert the name to use dashes instead of underscore # as these are how the parameters are stored in the # `arg_table`. xformed = key.replace('_', '-') if xformed in self.arg_table: cli_argument = self.arg_table[xformed] value = unpack_argument(self._session, 'custom', self.name, cli_argument, value) # If this parameter has a schema defined, then allow plugins # a chance to process and override its value. if self._should_allow_plugins_override(cli_argument, value): override = self._session\ .emit_first_non_none_response( 'process-cli-arg.%s.%s' % ('custom', self.name), cli_argument=cli_argument, value=value, operation=None) if override is not None: # A plugin supplied a conversion value = override else: # Unpack the argument, which is a string, into the # correct Python type (dict, list, etc) value = unpack_cli_arg(cli_argument, value) self._validate_value_against_schema( cli_argument.argument_model, value) setattr(parsed_args, key, value) if hasattr(parsed_args, 'help'): self._display_help(parsed_args, parsed_globals) elif getattr(parsed_args, 'subcommand', None) is None: # No subcommand was specified so call the main # function for this top level command. if remaining: raise ValueError("Unknown options: %s" % ','.join(remaining)) return self._run_main(parsed_args, parsed_globals) else: return self.subcommand_table[parsed_args.subcommand]( remaining, parsed_globals)
def __call__(self, args, parsed_globals): # args is the remaining unparsed args. # We might be able to parse these args so we need to create # an arg parser and parse them. subcommand_table = self._build_subcommand_table() arg_table = self.arg_table parser = ArgTableArgParser(arg_table, subcommand_table) parsed_args, remaining = parser.parse_known_args(args) # Unpack arguments for key, value in vars(parsed_args).items(): param = None if key in arg_table: param = arg_table[key] setattr( parsed_args, key, unpack_argument(self._session, 'custom', self.name, param, value)) if hasattr(parsed_args, 'help'): self._display_help(parsed_args, parsed_globals) elif getattr(parsed_args, 'subcommand', None) is None: # No subcommand was specified so call the main # function for this top level command. return self._run_main(parsed_args, parsed_globals) else: return subcommand_table[parsed_args.subcommand](remaining, parsed_globals)
def _create_operation_parser(self, parameter_table): """ This creates the ArgTableArgParser for the command. It adds an extra argument to the parser, paths, which represents a required the number of positional argument that must follow the command's name. """ parser = ArgTableArgParser(parameter_table) parser.add_argument("paths", **self.options) return parser
def __call__(self, args, parsed_globals): # args is the remaining unparsed args. # We might be able to parse these args so we need to create # an arg parser and parse them. subcommand_table = self._build_subcommand_table() arg_table = self.arg_table parser = ArgTableArgParser(arg_table, subcommand_table) parsed_args, remaining = parser.parse_known_args(args) # Unpack arguments for key, value in vars(parsed_args).items(): param = None # Convert the name to use dashes instead of underscore # as these are how the parameters are stored in the # `arg_table`. xformed = key.replace('_', '-') if xformed in arg_table: param = arg_table[xformed] value = unpack_argument(self._session, 'custom', self.name, param, value) # If this parameter has a schema defined, then allow plugins # a chance to process and override its value. if param and getattr(param, 'argument_object', None) is not None \ and value is not None: param_object = param.argument_object # Allow a single event handler to process the value override = self._session\ .emit_first_non_none_response( 'process-cli-arg.%s.%s' % ('custom', self.name), param=param_object, value=value, operation=None) if override is not None: # A plugin supplied a conversion value = override else: # Unpack the argument, which is a string, into the # correct Python type (dict, list, etc) value = unpack_cli_arg(param_object, value) # Validate param types, required keys, etc param_object.validate(value) setattr(parsed_args, key, value) if hasattr(parsed_args, 'help'): self._display_help(parsed_args, parsed_globals) elif getattr(parsed_args, 'subcommand', None) is None: # No subcommand was specified so call the main # function for this top level command. return self._run_main(parsed_args, parsed_globals) else: return subcommand_table[parsed_args.subcommand](remaining, parsed_globals)
def __call__(self, args, parsed_globals): # args is the remaining unparsed args. # We might be able to parse these args so we need to create # an arg parser and parse them. parser = ArgTableArgParser(self.arg_table) parsed_args = parser.parse_args(args) if hasattr(parsed_args, 'help'): self._display_help(parsed_args, parsed_globals) else: self._run_main(parsed_args, parsed_globals)
def __call__(self, args, parsed_globals): # args is the remaining unparsed args. # We might be able to parse these args so we need to create # an arg parser and parse them. subcommand_table = self._build_subcommand_table() parser = ArgTableArgParser(self.arg_table, subcommand_table) parsed_args, remaining = parser.parse_known_args(args) if hasattr(parsed_args, 'help'): self._display_help(parsed_args, parsed_globals) elif getattr(parsed_args, 'subcommand', None) is None: # No subcommand was specified so call the main # function for this top level command. return self._run_main(parsed_args, parsed_globals) else: return subcommand_table[parsed_args.subcommand](remaining, parsed_globals)
def __call__(self, args, parsed_globals): if args: subcommand_parser = ArgTableArgParser({}, self.subcommand_table) parsed, remaining = subcommand_parser.parse_known_args(args) if getattr(parsed, 'subcommand', None) is not None: return self.subcommand_table[parsed.subcommand](remaining, parsed_globals) # Create an event handler for a Provider Document instance = self.EventHandlerClass(self) # Now generate all of the events for a Provider document. # We pass ourselves along so that we can, in turn, get passed # to all event handlers. docevents.generate_events(self.session, self) self.renderer.render(self.doc.getvalue()) instance.unregister()
def _create_operation_parser(self, arg_table): parser = ArgTableArgParser(arg_table) return parser