libcloud-notifications mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From to...@apache.org
Subject [2/2] libcloud git commit: Add __repr__ method to data classes in the Azure driver.
Date Mon, 06 Apr 2015 20:47:34 GMT
Add __repr__ method to data classes in the Azure driver.


Project: http://git-wip-us.apache.org/repos/asf/libcloud/repo
Commit: http://git-wip-us.apache.org/repos/asf/libcloud/commit/3745ffca
Tree: http://git-wip-us.apache.org/repos/asf/libcloud/tree/3745ffca
Diff: http://git-wip-us.apache.org/repos/asf/libcloud/diff/3745ffca

Branch: refs/heads/trunk
Commit: 3745ffcaaa6d75f5cc4c9d960e64cee9e7f821d8
Parents: a36e681
Author: Tomaz Muraus <tomaz@apache.org>
Authored: Mon Apr 6 22:40:15 2015 +0200
Committer: Tomaz Muraus <tomaz@apache.org>
Committed: Mon Apr 6 22:47:27 2015 +0200

----------------------------------------------------------------------
 libcloud/compute/drivers/azure.py | 104 ++++++++++++++++++++++++++-------
 libcloud/utils/misc.py            |  26 ++++++++-
 2 files changed, 108 insertions(+), 22 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/libcloud/blob/3745ffca/libcloud/compute/drivers/azure.py
----------------------------------------------------------------------
diff --git a/libcloud/compute/drivers/azure.py b/libcloud/compute/drivers/azure.py
index 2743093..503d8d0 100644
--- a/libcloud/compute/drivers/azure.py
+++ b/libcloud/compute/drivers/azure.py
@@ -48,6 +48,7 @@ from libcloud.utils.py3 import httplib
 from libcloud.utils.py3 import urlparse
 from libcloud.utils.py3 import ensure_string
 from libcloud.utils.py3 import urlquote as url_quote
+from libcloud.utils.misc import ReprMixin
 
 HTTPSConnection = httplib.HTTPSConnection
 
@@ -2728,9 +2729,12 @@ class CertificateSetting(WindowsAzureData):
 
 
 class StoredCertificateSettings(WindowsAzureDataTypedList):
-
     list_type = CertificateSetting
 
+    _repr_attributes = [
+        'items'
+    ]
+
 
 class SSH(WindowsAzureData):
 
@@ -2747,9 +2751,12 @@ class PublicKey(WindowsAzureData):
 
 
 class PublicKeys(WindowsAzureDataTypedList):
-
     list_type = PublicKey
 
+    _repr_attributes = [
+        'items'
+    ]
+
 
 class AzureKeyPair(WindowsAzureData):
 
@@ -2759,9 +2766,12 @@ class AzureKeyPair(WindowsAzureData):
 
 
 class KeyPairs(WindowsAzureDataTypedList):
-
     list_type = AzureKeyPair
 
+    _repr_attributes = [
+        'items'
+    ]
+
 
 class LoadBalancerProbe(WindowsAzureData):
 
@@ -2781,9 +2791,12 @@ class ConfigurationSet(WindowsAzureData):
 
 
 class ConfigurationSets(WindowsAzureDataTypedList):
-
     list_type = ConfigurationSet
 
+    _repr_attributes = [
+        'items'
+    ]
+
 
 class ConfigurationSetInputEndpoint(WindowsAzureData):
 
@@ -2804,10 +2817,13 @@ class ConfigurationSetInputEndpoint(WindowsAzureData):
 
 
 class ConfigurationSetInputEndpoints(WindowsAzureDataTypedList):
-
     list_type = ConfigurationSetInputEndpoint
     xml_element_name = 'InputEndpoint'
 
+    _repr_attributes = [
+        'items'
+    ]
+
 
 class Location(WindowsAzureData):
 
@@ -2819,9 +2835,12 @@ class Location(WindowsAzureData):
 
 
 class Locations(WindowsAzureDataTypedList):
-
     list_type = Location
 
+    _repr_attributes = [
+        'items'
+    ]
+
 
 class ComputeCapability(WindowsAzureData):
 
@@ -2851,9 +2870,12 @@ class OSImage(WindowsAzureData):
 
 
 class Images(WindowsAzureDataTypedList):
-
     list_type = OSImage
 
+    _repr_attributes = [
+        'items'
+    ]
+
 
 class VMImage(WindowsAzureData):
 
@@ -2870,9 +2892,12 @@ class VMImage(WindowsAzureData):
 
 
 class VMImages(WindowsAzureDataTypedList):
-
     list_type = VMImage
 
+    _repr_attributes = [
+        'items'
+    ]
+
 
 class VirtualIP(WindowsAzureData):
 
@@ -2883,11 +2908,18 @@ class VirtualIP(WindowsAzureData):
 
 
 class VirtualIPs(WindowsAzureDataTypedList):
-
     list_type = VirtualIP
 
+    _repr_attributes = [
+        'items'
+    ]
 
-class HostedService(WindowsAzureData):
+
+class HostedService(WindowsAzureData, ReprMixin):
+    _repr_attributes = [
+        'service_name',
+        'url'
+    ]
 
     def __init__(self):
         self.url = ''
@@ -2896,10 +2928,13 @@ class HostedService(WindowsAzureData):
         self.deployments = Deployments()
 
 
-class HostedServices(WindowsAzureDataTypedList):
-
+class HostedServices(WindowsAzureDataTypedList, ReprMixin):
     list_type = HostedService
 
+    _repr_attributes = [
+        'items'
+    ]
+
 
 class HostedServiceProperties(WindowsAzureData):
 
@@ -2948,9 +2983,12 @@ class Deployment(WindowsAzureData):
 
 
 class Deployments(WindowsAzureDataTypedList):
-
     list_type = Deployment
 
+    _repr_attributes = [
+        'items'
+    ]
+
 
 class UpgradeStatus(WindowsAzureData):
 
@@ -2979,9 +3017,12 @@ class RoleInstance(WindowsAzureData):
 
 
 class RoleInstanceList(WindowsAzureDataTypedList):
-
     list_type = RoleInstance
 
+    _repr_attributes = [
+        'items'
+    ]
+
 
 class InstanceEndpoint(WindowsAzureData):
 
@@ -2994,9 +3035,12 @@ class InstanceEndpoint(WindowsAzureData):
 
 
 class InstanceEndpoints(WindowsAzureDataTypedList):
-
     list_type = InstanceEndpoint
 
+    _repr_attributes = [
+        'items'
+    ]
+
 
 class InputEndpoint(WindowsAzureData):
 
@@ -3007,9 +3051,12 @@ class InputEndpoint(WindowsAzureData):
 
 
 class InputEndpoints(WindowsAzureDataTypedList):
-
     list_type = InputEndpoint
 
+    _repr_attributes = [
+        'items'
+    ]
+
 
 class Role(WindowsAzureData):
 
@@ -3019,9 +3066,12 @@ class Role(WindowsAzureData):
 
 
 class RoleList(WindowsAzureDataTypedList):
-
     list_type = Role
 
+    _repr_attributes = [
+        'items'
+    ]
+
 
 class PersistentVMDowntimeInfo(WindowsAzureData):
 
@@ -3054,9 +3104,12 @@ class Disk(WindowsAzureData):
 
 
 class Disks(WindowsAzureDataTypedList):
-
     list_type = Disk
 
+    _repr_attributes = [
+        'items'
+    ]
+
 
 class AttachedTo(WindowsAzureData):
 
@@ -3105,9 +3158,12 @@ class OSDiskConfiguration(WindowsAzureData):
 
 
 class OperatingSystems(WindowsAzureDataTypedList):
-
     list_type = OperatingSystem
 
+    _repr_attributes = [
+        'items'
+    ]
+
 
 class OperatingSystemFamily(WindowsAzureData):
 
@@ -3118,9 +3174,12 @@ class OperatingSystemFamily(WindowsAzureData):
 
 
 class OperatingSystemFamilies(WindowsAzureDataTypedList):
-
     list_type = OperatingSystemFamily
 
+    _repr_attributes = [
+        'items'
+    ]
+
 
 class Subscription(WindowsAzureData):
 
@@ -3157,9 +3216,12 @@ class SubscriptionCertificate(WindowsAzureData):
 
 
 class SubscriptionCertificates(WindowsAzureDataTypedList):
-
     list_type = SubscriptionCertificate
 
+    _repr_attributes = [
+        'items'
+    ]
+
 
 class AzureHTTPRequest(object):
     def __init__(self):

http://git-wip-us.apache.org/repos/asf/libcloud/blob/3745ffca/libcloud/utils/misc.py
----------------------------------------------------------------------
diff --git a/libcloud/utils/misc.py b/libcloud/utils/misc.py
index fbca4cb..3e2eeac 100644
--- a/libcloud/utils/misc.py
+++ b/libcloud/utils/misc.py
@@ -27,7 +27,9 @@ __all__ = [
     'dict2str',
     'reverse_dict',
     'lowercase_keys',
-    'get_secure_random_string'
+    'get_secure_random_string',
+
+    'ReprMixin'
 ]
 
 
@@ -252,3 +254,25 @@ def get_secure_random_string(size):
     value = binascii.hexlify(value)
     value = value.decode('utf-8')[:size]
     return value
+
+
+class ReprMixin(object):
+    """
+    Mixin class which adds __repr__ and __str__ methods for the attributes
+    specified on the class.
+    """
+
+    _repr_attributes = []
+
+    def __repr__(self):
+        attributes = []
+        for attribute in self._repr_attributes:
+            value = getattr(self, attribute, None)
+            attributes.append('%s=%s' % (attribute, value))
+
+        values = (self.__class__.__name__, ', '.join(attributes))
+        result = '<%s %s>' % values
+        return result
+
+    def __str__(self):
+        return str(self.__repr__())


Mime
View raw message