def test_parse_args(self): class A(object): def update_zone(self, zone, domain, type='master', ttl=None, extra=None): pass result = parser.parse_args(A.update_zone) self.assertEqual(result.keys(), ['zone', 'domain', 'type', 'ttl', 'extra']) self.assertEqual(result['zone']['required'], True) self.assertEqual(result['type']['required'], False) self.assertFalse('default' in result['zone']) self.assertTrue('default' in result['ttl'])
def __init__(self, driver_obj, method_name): if inspect.isclass(driver_obj): self.driver_cls = driver_obj else: self.driver_cls = driver_obj.__class__ self.driver_obj = driver_obj self.method_name = method_name self.method = getattr(self.driver_obj, method_name, None) if not inspect.ismethod(self.method): raise NoSuchOperationError() method_doc = get_method_docstring(self.driver_cls, method_name) if not method_doc: raise MethodParsingException('Empty docstring') argspec_arg = parse_args(self.method) docstring_parse_result = parse_docstring(method_doc, self.driver_cls) self.description = docstring_parse_result['description'] docstring_args = docstring_parse_result['arguments'] #check vargs self.vargs_entries = [] for name, arg_info in argspec_arg.iteritems(): if name in docstring_args: docstring_arg = docstring_args[name] entry_kwargs = { 'name': name, 'description': docstring_arg['description'], 'type_name': docstring_arg['type_name'], 'required': (docstring_arg['required'] or arg_info['required']), } if not entry_kwargs['required'] and 'default' in arg_info: entry_kwargs['default'] = arg_info['default'] self.vargs_entries.append(Entry(**entry_kwargs)) else: raise MethodParsingException( '%s %s not described in docstring' % (method_name, name)) #update kwargs kwargs = set(docstring_args).difference(argspec_arg) self.kwargs_entries = [ Entry(arg_name, **docstring_args[arg_name]) for arg_name in kwargs ] method_return = docstring_parse_result['return'] self.result_entry = Entry('', method_return['type_name'], method_return['description'], True)
def __init__(self, driver_obj, method_name): if inspect.isclass(driver_obj): self.driver_cls = driver_obj else: self.driver_cls = driver_obj.__class__ self.driver_obj = driver_obj self.method_name = method_name self.method = getattr(self.driver_obj, method_name, None) if not inspect.ismethod(self.method): raise NoSuchOperationError() method_doc = get_method_docstring(self.driver_cls, method_name) if not method_doc: raise MethodParsingException('Empty docstring') argspec_arg = parse_args(self.method) docstring_parse_result = parse_docstring(method_doc, self.driver_cls) self.description = docstring_parse_result['description'] docstring_args = docstring_parse_result['arguments'] #check vargs self.vargs_entries = [] for name, arg_info in argspec_arg.iteritems(): if name in docstring_args: docstring_arg = docstring_args[name] entry_kwargs = { 'name': name, 'description': docstring_arg['description'], 'type_name': docstring_arg['type_name'], 'required': (docstring_arg['required'] or arg_info['required']), } if not entry_kwargs['required'] and 'default' in arg_info: entry_kwargs['default'] = arg_info['default'] self.vargs_entries.append(Entry(**entry_kwargs)) else: raise MethodParsingException( '%s %s not described in docstring' % (method_name, name)) #update kwargs kwargs = set(docstring_args).difference(argspec_arg) self.kwargs_entries = [Entry(arg_name, **docstring_args[arg_name]) for arg_name in kwargs] method_return = docstring_parse_result['return'] self.result_entry = Entry('', method_return['type_name'], method_return['description'], True)