← Back to team overview

launchpad-reviewers team mailing list archive

[Merge] lp:~allenap/maas/pserv-xmlrpc-cobblerobject-metaclass into lp:maas

 

Gavin Panella has proposed merging lp:~allenap/maas/pserv-xmlrpc-cobblerobject-metaclass into lp:maas with lp:~allenap/maas/pserv-xmlrpc-cobblerclient as a prerequisite.

Requested reviews:
  Launchpad code reviewers (launchpad-reviewers)

For more details, see:
https://code.launchpad.net/~allenap/maas/pserv-xmlrpc-cobblerobject-metaclass/+merge/91631

Uses a metaclass, CobblerObjectType, to ensure that CobblerObject and all its descendants use frozensets for known_attributes and required_attributes.
-- 
https://code.launchpad.net/~allenap/maas/pserv-xmlrpc-cobblerobject-metaclass/+merge/91631
Your team Launchpad code reviewers is requested to review the proposed merge of lp:~allenap/maas/pserv-xmlrpc-cobblerobject-metaclass into lp:maas.
=== modified file 'src/provisioningserver/cobblerclient.py'
--- src/provisioningserver/cobblerclient.py	2012-02-06 10:45:32 +0000
+++ src/provisioningserver/cobblerclient.py	2012-02-06 10:45:32 +0000
@@ -222,6 +222,20 @@
         returnValue(result)
 
 
+class CobblerObjectType(type):
+    """Metaclass of Cobbler objects."""
+
+    def __new__(mtype, name, bases, attributes):
+        if "known_attributes" in attributes:
+            attributes["known_attributes"] = frozenset(
+                attributes["known_attributes"])
+        if "required_attributes" in attributes:
+            attributes["required_attributes"] = frozenset(
+                attributes["required_attributes"])
+        return super(CobblerObjectType, mtype).__new__(
+            mtype, name, bases, attributes)
+
+
 class CobblerObject:
     """Abstract base class: a type of object in Cobbler's XMLRPC API.
 
@@ -230,6 +244,8 @@
     through its API.  Implement a new type by inheriting from this class.
     """
 
+    __metaclass__ = CobblerObjectType
+
     # What are objects of this type called in the Cobbler API?
     object_type = None
 

=== modified file 'src/provisioningserver/tests/test_cobblersession.py'
--- src/provisioningserver/tests/test_cobblersession.py	2012-02-06 10:45:32 +0000
+++ src/provisioningserver/tests/test_cobblersession.py	2012-02-06 10:45:32 +0000
@@ -484,3 +484,21 @@
         values_observed = yield (
             cobblerclient.CobblerDistro.get_all_values(session))
         self.assertEqual(values_expected, values_observed)
+
+    def test_known_attributes(self):
+        # The known_attributes class attribute is always a frozenset.
+        self.assertIsInstance(
+            cobblerclient.CobblerObject.known_attributes,
+            frozenset)
+        self.assertIsInstance(
+            cobblerclient.CobblerProfile.known_attributes,
+            frozenset)
+
+    def test_required_attributes(self):
+        # The known_attributes class attribute is always a frozenset.
+        self.assertIsInstance(
+            cobblerclient.CobblerObject.required_attributes,
+            frozenset)
+        self.assertIsInstance(
+            cobblerclient.CobblerDistro.required_attributes,
+            frozenset)