NACK
This is personal preference. We could argue the merits of both styles like we could argue
Emacs vs Vi.
On Jun 1, 2011, at 6:22 AM, Mark McLoughlin wrote:
We have a lot of copy and paste code in our property definitions.
Split this code out into utility methods and re-use it.
(As the cool kids would say, "DRY up the property definitions").
---
imagefactory/ApplicationConfiguration.py | 12 +---
imagefactory/ImageWarehouse.py | 63 ++---------------
imagefactory/Template.py | 37 +---------
imagefactory/builders/BaseBuilder.py | 109 +++-------------------------
imagefactory/props.py | 47 ++++++++++++
imagefactory/qmfagent/BuildAdaptor.py | 74 ++-----------------
imagefactory/qmfagent/ImageFactory.py | 25 +------
imagefactory/qmfagent/ImageFactoryAgent.py | 19 +----
8 files changed, 82 insertions(+), 304 deletions(-)
create mode 100644 imagefactory/props.py
diff --git a/imagefactory/ApplicationConfiguration.py
b/imagefactory/ApplicationConfiguration.py
index 6b0e5ea..8c113de 100644
--- a/imagefactory/ApplicationConfiguration.py
+++ b/imagefactory/ApplicationConfiguration.py
@@ -23,20 +23,12 @@ import os
import argparse
import json
import logging
+import props
class ApplicationConfiguration(object):
instance = None
- def configuration():
- doc = "The configuration property."
- def fget(self):
- return self._configuration
- def fset(self, value):
- self._configuration = value
- def fdel(self):
- del self._configuration
- return locals()
- configuration = property(**configuration())
+ configuration = props.prop("_configuration", "The configuration
property.")
def __new__(cls, *p, **k):
if cls.instance is None:
diff --git a/imagefactory/ImageWarehouse.py b/imagefactory/ImageWarehouse.py
index ecf22eb..0a67f99 100644
--- a/imagefactory/ImageWarehouse.py
+++ b/imagefactory/ImageWarehouse.py
@@ -24,68 +24,17 @@ import httplib2
import urllib
import uuid
import os
+import props
from imagefactory.ApplicationConfiguration import ApplicationConfiguration
class ImageWarehouse(object):
- # Properties
- def url():
- doc = "The url property."
- def fget(self):
- return self._url
- def fset(self, value):
- self._url = value
- def fdel(self):
- del self._url
- return locals()
- url = property(**url())
-
- def image_bucket():
- doc = "The image_bucket property."
- def fget(self):
- return self._image_bucket
- def fset(self, value):
- self._image_bucket = value
- def fdel(self):
- del self._image_bucket
- return locals()
- image_bucket = property(**image_bucket())
-
- def template_bucket():
- doc = "The template_bucket property."
- def fget(self):
- return self._template_bucket
- def fset(self, value):
- self._template_bucket = value
- def fdel(self):
- del self._template_bucket
- return locals()
- template_bucket = property(**template_bucket())
-
- def icicle_bucket():
- doc = "The icicle_bucket property."
- def fget(self):
- return self._icicle_bucket
- def fset(self, value):
- self._icicle_bucket = value
- def fdel(self):
- del self._icicle_bucket
- return locals()
- icicle_bucket = property(**icicle_bucket())
-
- def provider_image_bucket():
- doc = "The provider_image_bucket property."
- def fget(self):
- return self._provider_image_bucket
- def fset(self, value):
- self._provider_image_bucket = value
- def fdel(self):
- del self._provider_image_bucket
- return locals()
- provider_image_bucket = property(**provider_image_bucket())
-
- # Properties end
+ url = props.prop("_url", "The url property.")
+ image_bucket = props.prop("_image_bucket", "The image_bucket
property.")
+ template_bucket = props.prop("_template_bucket", "The template_bucket
property.")
+ icicle_bucket = props.prop("_icicle_bucket", "The icicle_bucket
property.")
+ provider_image_bucket = props.prop("_provider_image_bucket", "The
provider_image_bucket property.")
def __repr__(self):
return "%s - %r" % (super(ImageWarehouse, self).__repr__(),
self.__dict__)
diff --git a/imagefactory/Template.py b/imagefactory/Template.py
index 0bba587..263dccc 100644
--- a/imagefactory/Template.py
+++ b/imagefactory/Template.py
@@ -22,45 +22,16 @@ import logging
import httplib2
import re
import os.path
+import props
from imagefactory.ApplicationConfiguration import ApplicationConfiguration
from imagefactory.ImageWarehouse import ImageWarehouse
class Template(object):
uuid_pattern =
'([0-9a-f]{8})-([0-9a-f]{4})-([0-9a-f]{4})-([0-9a-f]{4})-([0-9a-f]{12})'
- # Properties
- def identifier():
- doc = "The identifier property."
- def fget(self):
- return self._identifier
- def fset(self, value):
- self._identifier = value
- def fdel(self):
- del self._identifier
- return locals()
- identifier = property(**identifier())
-
- def url():
- doc = "The url property."
- def fget(self):
- return self._url
- def fset(self, value):
- self._url = value
- def fdel(self):
- del self._url
- return locals()
- url = property(**url())
-
- def xml():
- doc = "The xml property."
- def fget(self):
- return self._xml
- def fset(self, value):
- self._xml = value
- def fdel(self):
- del self._xml
- return locals()
- xml = property(**xml())
+ identifier = props.prop("_identifier", "The identifier
property.")
+ url = props.prop("_url", "The url property.")
+ xml = props.prop("_xml", "The xml property.")
def __repr__(self):
diff --git a/imagefactory/builders/BaseBuilder.py b/imagefactory/builders/BaseBuilder.py
index 3109f18..e74df56 100644
--- a/imagefactory/builders/BaseBuilder.py
+++ b/imagefactory/builders/BaseBuilder.py
@@ -22,6 +22,7 @@ import uuid
import os
import httplib2
from IBuilder import IBuilder
+from imagefactory import props
from imagefactory.ApplicationConfiguration import ApplicationConfiguration
from imagefactory.ImageWarehouse import ImageWarehouse
from imagefactory.Template import Template
@@ -32,72 +33,15 @@ class BaseBuilder(object):
deploying images."""
zope.interface.implements(IBuilder)
- # Properties
- def template():
- doc = "An instance of the Template class."
- def fget(self):
- return self._template
- def fset(self, value):
- self._template = value
- def fdel(self):
- del self._template
- return locals()
- template = property(**template())
-
- def target():
- doc = "The target cloud for which to build this image."
- def fget(self):
- return self._target
- def fset(self, value):
- self._target = value
- def fdel(self):
- del self._target
- return locals()
- target = property(**target())
-
- def target_id():
- doc = "The identifier provided by the target."
- def fget(self):
- return self._target_id
- def fset(self, value):
- self._target_id = value
- def fdel(self):
- del self._target_id
- return locals()
- target_id = property(**target_id())
-
- def provider():
- doc = "The a string name of the target region or provider."
- def fget(self):
- return self._provider
- def fset(self, value):
- self._provider = value
- def fdel(self):
- del self._provider
- return locals()
- provider = property(**provider())
-
- def image_id():
- doc = "The uuid of the image."
- def fget(self):
- return self._image_id
- def fset(self, value):
- self._image_id = value
- def fdel(self):
- del self._image_id
- return locals()
- image_id = property(**image_id())
-
- def image():
- doc = "The image file path."
- def fget(self):
- return self._image
- def fset(self, value):
- self._image = value
- def fdel(self):
- del self._image
- return locals()
- image = property(**image())
+ template = props.prop("_template", "An instance of the Template
class.")
+ target = props.prop("_target", "The target cloud for which to build
this image.")
+ target_id = props.prop("_target_id", "The identifier provided by the
target.")
+ provider = props.prop("_provider", "The a string name of the target
region or provider.")
+ image_id = props.prop("_image_id", "The uuid of the image.")
+ image = props.prop("_image", "The image file path.")
+ output_descriptor = props.prop("_output_descriptor", "An XML string
describing the completed image, aka: CDL or ICICLE.")
+ delegate = props.prop("_delegate", "An object that responds to
IBuilderDelegate methods.")
+ warehouse = props.prop("_warehouse", "A warehouse object used to
store/fetch images, templates, icicle, provider_images, etc...")
def status():
doc = "A string value."
@@ -154,39 +98,6 @@ class BaseBuilder(object):
return locals()
percent_complete = property(**percent_complete())
- def output_descriptor():
- doc = "An XML string describing the completed image, aka: CDL or
ICICLE."
- def fget(self):
- return self._output_descriptor
- def fset(self, value):
- self._output_descriptor = value
- def fdel(self):
- del self._output_descriptor
- return locals()
- output_descriptor = property(**output_descriptor())
-
- def delegate():
- doc = "An object that responds to IBuilderDelegate methods."
- def fget(self):
- return self._delegate
- def fset(self, value):
- self._delegate = value
- def fdel(self):
- del self._delegate
- return locals()
- delegate = property(**delegate())
-
- def warehouse():
- doc = "A warehouse object used to store/fetch images, templates, icicle,
provider_images, etc..."
- def fget(self):
- return self._warehouse
- def fset(self, value):
- self._warehouse = value
- def fdel(self):
- del self._warehouse
- return locals()
- warehouse = property(**warehouse())
-
# Initializer
def __init__(self, template, target):
super(BaseBuilder, self).__init__()
diff --git a/imagefactory/props.py b/imagefactory/props.py
new file mode 100644
index 0000000..c0cb89c
--- /dev/null
+++ b/imagefactory/props.py
@@ -0,0 +1,47 @@
+# Copyright (C) 2010-2011 Red Hat, Inc.
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; version 2 of the License.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+# MA 02110-1301, USA. A copy of the GNU General Public License is
+# also available at
http://www.gnu.org/copyleft/gpl.html.
+
+#
+# Return a copy of dict d with the specified keys filtered out
+#
+def filter_dict(d, keys):
+ return dict(map(lambda k: [k, d[k]], filter(lambda k: not k in keys, d.keys())))
+
+#
+# Return a property backed by the given attribute
+#
+def prop(attr, doc = None):
+ def fget(self):
+ return getattr(self, attr)
+ def fset(self, value):
+ setattr(self, attr, value)
+ def fdel(self):
+ delattr(self, attr)
+ return property(**filter_dict(locals().copy(), ['attr']))
+
+#
+# A variant of the above where the property is backed by an
+# attribute of an attribute
+#
+def subprop(attr, subattr, doc = None):
+ def fget(self):
+ return getattr(getattr(self, attr), subattr)
+ def fset(self, value):
+ setattr(getattr(self, attr), subattr, value)
+ def fdel(self):
+ delattr(getattr(self, attr), subattr)
+ return property(**filter_dict(locals().copy(), ['attr',
'subattr']))
diff --git a/imagefactory/qmfagent/BuildAdaptor.py
b/imagefactory/qmfagent/BuildAdaptor.py
index 7825405..4da8832 100644
--- a/imagefactory/qmfagent/BuildAdaptor.py
+++ b/imagefactory/qmfagent/BuildAdaptor.py
@@ -21,6 +21,7 @@ import logging
import cqpid
from qmf2 import *
from threading import Thread, Lock
+from imagefactory import props
from imagefactory.builders import *
from imagefactory.BuildDispatcher import *
@@ -61,73 +62,12 @@ class BuildAdaptor(object):
"COMPLETED":()
}
-
- ### Properties
- def template():
- doc = "The template property."
- def fget(self):
- return self._template
- def fset(self, value):
- self._template = value
- def fdel(self):
- del self._template
- return locals()
- template = property(**template())
-
- def target():
- doc = "The target property."
- def fget(self):
- return self._target
- def fset(self, value):
- self._target = value
- def fdel(self):
- del self._target
- return locals()
- target = property(**target())
-
- def status():
- doc = "The status property."
- def fget(self):
- return self.qmf_object.status
- def fset(self, value):
- self.qmf_object.status = value
- def fdel(self):
- del self.qmf_object.status
- return locals()
- status = property(**status())
-
- def percent_complete():
- doc = "The percent_complete property."
- def fget(self):
- return self.qmf_object.percent_complete
- def fset(self, value):
- self.qmf_object.percent_complete = value
- def fdel(self):
- del self.qmf_object.percent_complete
- return locals()
- percent_complete = property(**percent_complete())
-
- def image_id():
- doc = "The image property."
- def fget(self):
- return self.qmf_object.image_id
- def fset(self, value):
- self.qmf_object.image_id = value
- def fdel(self):
- del self.qmf_object.image_id
- return locals()
- image = property(**image_id())
-
- def qmf_object():
- doc = "The qmf_object property."
- def fget(self):
- return self._qmf_object
- def fset(self, value):
- self._qmf_object = value
- def fdel(self):
- del self._qmf_object
- return locals()
- qmf_object = property(**qmf_object())
+ template = props.prop("_template", "The template property.")
+ target = props.prop("_target", "The target property.")
+ status = props.subprop("qmf_object", "status", "The status
property.")
+ percent_complete = props.subprop("qmf_object",
"percent_complete", "The percent_complete property.")
+ image = props.subprop("qmf_object", "image_id", "The image
property.")
+ qmf_object = props.prop("_qmf_object", "The qmf_object
property.")
def __init__(self, template, target, agent=None):
super(BuildAdaptor, self).__init__()
diff --git a/imagefactory/qmfagent/ImageFactory.py
b/imagefactory/qmfagent/ImageFactory.py
index ce1ec32..a07d441 100644
--- a/imagefactory/qmfagent/ImageFactory.py
+++ b/imagefactory/qmfagent/ImageFactory.py
@@ -21,6 +21,7 @@ import cqpid
from qmf2 import *
import BuildAdaptor
import httplib2
+from imagefactory import props
from imagefactory.ApplicationConfiguration import ApplicationConfiguration
from imagefactory.ImageWarehouse import ImageWarehouse
from imagefactory.Template import Template
@@ -57,28 +58,8 @@ class ImageFactory(object):
"""Returns a dictionary representing the finite state machine for
instances of this class."""
return {}
- ## Properties
- def qmf_object():
- doc = "The qmf_object property."
- def fget(self):
- return self._qmf_object
- def fset(self, value):
- self._qmf_object = value
- def fdel(self):
- del self._qmf_object
- return locals()
- qmf_object = property(**qmf_object())
-
- def agent():
- """The property agent"""
- def fget(self):
- return self._agent
- def fset(self, value):
- self._agent = value
- def fdel(self):
- del self._agent
- return locals()
- agent = property(**agent())
+ qmf_object = props.prop("_qmf_object", "The qmf_object
property.")
+ agent = props.prop("_agent", "The property agent")
def __new__(cls, *p, **k):
if cls.instance is None:
diff --git a/imagefactory/qmfagent/ImageFactoryAgent.py
b/imagefactory/qmfagent/ImageFactoryAgent.py
index 62f5452..9502361 100644
--- a/imagefactory/qmfagent/ImageFactoryAgent.py
+++ b/imagefactory/qmfagent/ImageFactoryAgent.py
@@ -21,32 +21,19 @@ import cqpid
from qmf2 import *
from ImageFactory import ImageFactory
from BuildAdaptor import BuildAdaptor
+from imagefactory import props
class ImageFactoryAgent(AgentHandler):
- ## Properties
- def qmf_object():
- doc = "The qmf_object property."
- def fget(self):
- return self._qmf_object
- def fset(self, value):
- self._qmf_object = value
- def fdel(self):
- del self._qmf_object
- return locals()
- qmf_object = property(**qmf_object())
+
+ qmf_object = props.prop("_qmf_object", "The qmf_object
property.")
def managedObjects():
doc = "The managedObjects property."
def fget(self):
return self._managedObjects
- # def fset(self, value):
- # self._managedObjects = value
- # def fdel(self):
- # del self._managedObjects
return locals()
managedObjects = property(**managedObjects())
-
def __init__(self, url):
self.log = logging.getLogger('%s.%s' % (__name__,
self.__class__.__name__))
self._managedObjects = {}
--
1.7.4.4