def test_parse_docstring(self): docstring = """ Return a Zone instance. Second line docsting. @type zone_id: C{str} @param zone_id: Required zone id (required) @keyword auth: Initial authentication information for the node (optional) @type auth: L{NodeAuthSSHKey} or L{NodeAuthPassword} @return: instance @rtype: L{Zone} or L{Node} """ result = parser.parse_docstring(docstring) description = result['description'] args = result['arguments'] return_type = result['return']['type_name'] return_description = result['return']['description'] self.assertTrue(description.startswith('Return')) self.assertTrue('Second line' in description) self.assertEqual(args['zone_id']['type_name'], 'C{str}') self.assertEqual(args['zone_id']['required'], True) self.assertItemsEqual(args['auth']['type_name'], 'L{NodeAuthSSHKey} or L{NodeAuthPassword}') self.assertEqual(args['auth']['required'], False) self.assertEqual(return_type, 'L{Zone} or L{Node}') self.assertEqual(return_description, 'instance')
def test_foo_create_node(self): docstring = parser.get_method_docstring(self.Foo, 'create_node') result = parser.parse_docstring(docstring, self.Foo) description = result['description'] args = result['arguments'] return_type = result['return']['type_name'] return_description = result['return']['description'] self.assertTrue(description.startswith('Create a new node ')) self.assertEqual(len(args), 2) self.assertEqual('C{str}', args['name']['type_name']) self.assertEqual('C{dict}', args['size']['type_name']) self.assertEqual(return_type, 'L{Node}') self.assertEqual(return_description, 'The newly created node.')
def test_bar_deploy_node(self): docstring = parser.get_method_docstring(self.Bar, 'deploy_node') result = parser.parse_docstring(docstring, self.Bar) description = result['description'] args = result['arguments'] return_type = result['return']['type_name'] return_description = result['return']['description'] self.assertTrue(description.startswith('Deploy bar node')) self.assertEqual(len(args), 3) self.assertEqual('L{NodeAuthSSHKey}', args['deploy']['type_name']) self.assertEqual('C{str}', args['name']['type_name']) self.assertEqual('C{dict}', args['size']['type_name']) self.assertEqual(return_type, 'L{Node}') self.assertEqual(return_description, 'The newly created node.')
def test_bar_create_node(self): docstring = parser.get_method_docstring(self.Bar, 'create_node') result = parser.parse_docstring(docstring, self.Bar) description = result['description'] args = result['arguments'] return_type = result['return']['type_name'] return_description = result['return']['description'] self.assertTrue(description.startswith('Create a new bar node')) self.assertEqual(len(args), 3) self.assertEqual('C{str}', args['name']['type_name']) self.assertEqual('C{dict}', args['size']['type_name']) self.assertEqual('C{str}', args['ex_fqdn']['type_name']) self.assertEqual(return_type, 'L{Node}') self.assertEqual(return_description, 'New bar node')
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)