Commit 16091a6e authored by Michael Hanselmann's avatar Michael Hanselmann
Browse files

Fix type descriptions in RAPI documentation



This patch adds descriptors to the “_CheckCIDR*” functions in opcodes
and improves the descriptions generated by “ht.TInstanceOf”, thereby
indirectly fixing bad type descriptions in the RAPI documentation.

Before this patch:
- (String and (<function _CheckCIDRAddrNotation at 0x2f…>))
- (Instance of (<class 'ganeti.opcodes.OpInstanceCreate'>))

After this patch:
- (String and (IPv4 address))
- (Instance of (ganeti.opcodes.OpInstanceCreate))
Signed-off-by: default avatarMichael Hanselmann <hansmi@google.com>
Reviewed-by: default avatarHelga Velroyen <helgav@google.com>
parent 853c570b
......@@ -364,12 +364,18 @@ TNumber = TOr(TInt, TFloat)
TRelativeJobId = WithDesc("RelativeJobId")(TNegativeInt)
def TInstanceOf(my_inst):
"""Checks if a given value is an instance of my_inst.
def TInstanceOf(cls):
"""Checks if a given value is an instance of C{cls}.
@type cls: class
@param cls: Class object
"""
desc = WithDesc("Instance of %s" % (Parens(my_inst), ))
return desc(lambda val: isinstance(val, my_inst))
name = "%s.%s" % (cls.__module__, cls.__name__)
desc = WithDesc("Instance of %s" % (Parens(name), ))
return desc(lambda val: isinstance(val, cls))
def TListOf(my_type):
......
......@@ -353,8 +353,9 @@ _PNetworkType = ("network_type", None, ht.TMaybe(_CheckNetworkType),
"Network type")
@ht.WithDesc("IPv4 network")
def _CheckCIDRNetNotation(value):
"""Ensure a given cidr notation type is valid.
"""Ensure a given CIDR notation type is valid.
"""
try:
......@@ -364,8 +365,9 @@ def _CheckCIDRNetNotation(value):
return True
@ht.WithDesc("IPv4 address")
def _CheckCIDRAddrNotation(value):
"""Ensure a given cidr notation type is valid.
"""Ensure a given CIDR notation type is valid.
"""
try:
......@@ -375,8 +377,9 @@ def _CheckCIDRAddrNotation(value):
return True
@ht.WithDesc("IPv6 address")
def _CheckCIDR6AddrNotation(value):
"""Ensure a given cidr notation type is valid.
"""Ensure a given CIDR notation type is valid.
"""
try:
......@@ -386,8 +389,9 @@ def _CheckCIDR6AddrNotation(value):
return True
@ht.WithDesc("IPv6 network")
def _CheckCIDR6NetNotation(value):
"""Ensure a given cidr notation type is valid.
"""Ensure a given CIDR notation type is valid.
"""
try:
......
......@@ -275,6 +275,13 @@ class TestTypeChecks(unittest.TestCase):
self.assertTrue(fn([{}, -938210858947, None]))
self.assertFalse(fn(["Hello", []]))
def testInstanceOf(self):
fn = ht.TInstanceOf(self.__class__)
self.assertTrue(fn(self))
self.assertTrue(str(fn).startswith("Instance of "))
self.assertFalse(fn(None))
if __name__ == "__main__":
testutils.GanetiTestProgram()
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment