def __add_resource_specific_arguments(self, obj, specific_options, line, key=None): gdt = TypeHelper.getDecoratorType obj_type = gdt(TypeHelper.to_singular(obj)) args = TypeHelper.get_actionable_types(expendNestedTypes=True) gom = MethodHelper.get_object_methods actions = [] method = self.__get_method_name_by_args(obj, line.split(' '), args, line) # collect resource args if yet not used if not method and obj_type and hasattr(brokers, obj_type): #add base resource actions actions = gom(getattr(brokers, obj_type), exceptions=self.complete_exceptions) if args.has_key(obj) and args[obj]: #add sub-resources actions self.__add_sub_resourse_actions(obj, args, line, actions) specific_options[obj if key == None else key] = actions
def execute(self): """Execute the action command.""" args = self.arguments opts = self.options if not (TypeHelper.isKnownType(args[0])) and \ ParseHelper.getXmlType(args[0]) == None: self.error(Messages.Error.NO_SUCH_TYPE % args[0]) # scope = '%s:%s' % (ParseHelper.getXmlWrapperType(args[0]), args[2]) actionable_types = TypeHelper.get_actionable_types( expendNestedTypes=True, groupOptions=True) resource = self.get_object(args[0], args[1], self.resolve_base(opts), context_variants=actionable_types[args[0]]) if resource is None: self.error(Messages.Error.NO_SUCH_OBJECT % (args[0], args[1])) elif hasattr(resource, args[2]) and \ type(getattr(resource, args[2])) == types.MethodType: try: result = self.execute_method(resource, args[2], opts) except Exception, e: self.error(str(e)) if result.status.state == 'failed': self.error(Messages.Info.ACTION_STATUS % result.status.state)
def __get_action_args(self, line): args = TypeHelper.get_actionable_types(expendNestedTypes=True) spl = line.rstrip().split(' ') gom = MethodHelper.get_object_methods gdt = TypeHelper.getDecoratorType if len(spl) >= 3: new_cons = '' obj = spl[1].strip() #top level resource in possibilities if args.has_key( obj) and args[obj] and args[obj].find('None') != -1: act = spl[3].strip() if len(spl) > 3 \ else spl[-1].strip() #possibilities refer only to the sub-resource else: for item in spl: if item.endswith('-identifier'): args[obj] = new_cons return args return args for cons in args[obj].split(', '): if cons == 'None': obj_type = gdt(obj) if not obj_type: continue actions = gom(getattr(brokers, obj_type), exceptions=self.complete_exceptions) if actions and act in actions: new_cons += 'None, ' else: tmp_spl = spl[:] tmp_spl.append(self.identifier_template % cons) base = self._resolve_base(tmp_spl[1:]) if base: obj_type = gdt(TypeHelper.to_singular(base)) if not obj_type: continue actions = gom(getattr(brokers, obj_type), exceptions=self.complete_exceptions) if actions and act in actions: new_cons += cons + ', ' args[obj] = new_cons[:len(new_cons) - 2] if len(new_cons) > 3 \ else '' return args
def __get_action_args(self, line): args = TypeHelper.get_actionable_types(expendNestedTypes=True) spl = line.rstrip().split(' ') gom = MethodHelper.get_object_methods gdt = TypeHelper.getDecoratorType if len(spl) >= 3: new_cons = '' obj = spl[1].strip() #top level resource in possibilities if args.has_key(obj) and args[obj] and args[obj].find('None') != -1: act = spl[3].strip() if len(spl) > 3 \ else spl[-1].strip() #possibilities refer only to the sub-resource else: for item in spl: if item.endswith('-identifier'): args[obj] = new_cons return args return args for cons in args[obj].split(', '): if cons == 'None': obj_type = gdt(obj) if not obj_type: continue actions = gom(getattr(brokers, obj_type), exceptions=self.complete_exceptions) if actions and act in actions: new_cons += 'None, ' else: tmp_spl = spl[:] tmp_spl.append(self.identifier_template % cons) base = self._resolve_base(tmp_spl[1:]) if base: obj_type = gdt(TypeHelper.to_singular(base)) if not obj_type: continue actions = gom(getattr(brokers, obj_type), exceptions=self.complete_exceptions) if actions and act in actions: new_cons += cons + ', ' args[obj] = new_cons[:len(new_cons) - 2] if len(new_cons) > 3 \ else '' return args
def execute(self): """Execute the action command.""" args = self.arguments opts = self.options if not (TypeHelper.isKnownType(args[0])) and \ ParseHelper.getXmlType(args[0]) == None: self.error( Messages.Error.NO_SUCH_TYPE % args[0] ) # scope = '%s:%s' % (ParseHelper.getXmlWrapperType(args[0]), args[2]) actionable_types = TypeHelper.get_actionable_types( expendNestedTypes=True, groupOptions=True ) resource = self.get_object( args[0], args[1], self.resolve_base(opts), context_variants=actionable_types[args[0]] ) if resource is None: self.error( Messages.Error.NO_SUCH_OBJECT % (args[0], args[1]) ) elif hasattr(resource, args[2]) and \ type(getattr(resource, args[2])) == types.MethodType: try: result = self.execute_method(resource, args[2], opts) except Exception, e: self.error(str(e)) if result.status.state == 'failed': self.error( Messages.Info.ACTION_STATUS % result.status.state )
def show_help(self): """Show help for the action command.""" args = self.arguments opts = self.options types = TypeHelper.get_actionable_types(expendNestedTypes=True, groupOptions=True) subst = {} if not args or self.is_supported_type(types.keys(), args[0]): if len(args) == 2 and len(opts) == 2: helptext = self.helptext1 subst['type'] = args[0] subst['id'] = args[1] base = self.resolve_base(self.options) obj = self.get_object(args[0], args[1], base, context_variants=types[args[0]]) if obj is None: self.error(Messages.Error.NO_SUCH_OBJECT % (args[1], args[1])) actions = self._get_action_methods(obj) subst['actions'] = self.format_list(actions) if len(args) == 3 and len(opts) == 2: helptext = self.helptext1 subst['type'] = args[0] subst['id'] = args[1] subst['action'] = args[0] base = self.resolve_base(self.options) obj = self.get_object(args[1], args[2], base, context_variants=types[args[0]]) if obj is None: self.error(Messages.Error.NO_SUCH_OBJECT % (args[0], args[1])) actions = self._get_action_methods(obj) if args[0] not in actions: self.error(Messages.Error.NO_SUCH_ACTION % args[2]) options = self.get_options(method=args[0], resource=obj, context_variants=types[args[0]]) subst['actions'] = self.format_list(actions) subst['options'] = self.format_list(options, bullet='', sort=False) elif len(args) == 1: helptext = self.helptext0 subst['types'] = self.format_map({args[0]: types[args[0]]}) subst['type'] = args[0] elif len(args) == 2: helptext = self.helptext1 subst['type'] = args[0] subst['id'] = args[1] base = self.resolve_base(opts) obj = self.get_object(args[0], args[1], base, context_variants=types[args[0]]) if obj is None: self.error(Messages.Error.NO_SUCH_OBJECT % (args[0], args[1])) actions = self._get_action_methods(obj) subst['actions'] = self.format_list(actions) elif len(args) == 3: helptext = self.helptext1 subst['type'] = args[0] subst['id'] = args[1] subst['action'] = args[2] base = self.resolve_base(self.options) obj = self.get_object(args[0], args[1], base, context_variants=types[args[0]]) if obj is None: self.error(Messages.Error.NO_SUCH_OBJECT % (args[0], args[1])) actions = self._get_action_methods(obj) if args[2] not in actions: self.error(Messages.Error.NO_SUCH_ACTION % args[2]) options = self.get_options(method=args[2], resource=obj, sub_resource=base, context_variants=types[args[0]]) subst['actions'] = self.format_list(actions) subst['options'] = self.format_list(options, bullet='', sort=False) else: helptext = self.helptext0 subst['types'] = self.format_map(types) # scope = '%s:%s' % (type(obj).__name__, args[2]) statuses = self.get_statuses() subst['statuses'] = self.format_list(statuses) helptext = self.format_help(helptext, subst) self.write(helptext)
def show_help(self): """Show help for the action command.""" args = self.arguments opts = self.options types = TypeHelper.get_actionable_types( expendNestedTypes=True, groupOptions=True ) subst = {} if not args or self.is_supported_type(types.keys(), args[0]): if len(args) == 2 and len(opts) == 2: helptext = self.helptext1 subst['type'] = args[0] subst['id'] = args[1] base = self.resolve_base(self.options) obj = self.get_object( args[0], args[1], base, context_variants=types[args[0]] ) if obj is None: self.error( Messages.Error.NO_SUCH_OBJECT % (args[1], args[1]) ) actions = self._get_action_methods(obj) subst['actions'] = self.format_list(actions) if len(args) == 3 and len(opts) == 2: helptext = self.helptext1 subst['type'] = args[0] subst['id'] = args[1] subst['action'] = args[0] base = self.resolve_base(self.options) obj = self.get_object( args[1], args[2], base, context_variants=types[args[0]] ) if obj is None: self.error( Messages.Error.NO_SUCH_OBJECT % (args[0], args[1]) ) actions = self._get_action_methods(obj) if args[0] not in actions: self.error( Messages.Error.NO_SUCH_ACTION % args[2] ) options = self.get_options( method=args[0], resource=obj, context_variants=types[args[0]] ) subst['actions'] = self.format_list(actions) subst['options'] = self.format_list(options, bullet='', sort=False) elif len(args) == 1: helptext = self.helptext0 subst['types'] = self.format_map({args[0]:types[args[0]]}) subst['type'] = args[0] elif len(args) == 2: helptext = self.helptext1 subst['type'] = args[0] subst['id'] = args[1] base = self.resolve_base(opts) obj = self.get_object( args[0], args[1], base, context_variants=types[args[0]] ) if obj is None: self.error( Messages.Error.NO_SUCH_OBJECT % (args[0], args[1]) ) actions = self._get_action_methods(obj) subst['actions'] = self.format_list(actions) elif len(args) == 3: helptext = self.helptext1 subst['type'] = args[0] subst['id'] = args[1] subst['action'] = args[2] base = self.resolve_base(self.options) obj = self.get_object( args[0], args[1], base, context_variants=types[args[0]] ) if obj is None: self.error( Messages.Error.NO_SUCH_OBJECT % (args[0], args[1]) ) actions = self._get_action_methods(obj) if args[2] not in actions: self.error(Messages.Error.NO_SUCH_ACTION % args[2]) options = self.get_options( method=args[2], resource=obj, sub_resource=base, context_variants=types[args[0]] ) subst['actions'] = self.format_list(actions) subst['options'] = self.format_list(options, bullet='', sort=False) else: helptext = self.helptext0 subst['types'] = self.format_map(types) # scope = '%s:%s' % (type(obj).__name__, args[2]) statuses = self.get_statuses() subst['statuses'] = self.format_list(statuses) helptext = self.format_help(helptext, subst) self.write(helptext)