diff options
| author | Alexander Gordeev <agordeev@mirantis.com> | 2014-05-05 15:49:03 +0400 |
|---|---|---|
| committer | Alexander Gordeev <agordeev@mirantis.com> | 2014-05-06 18:02:45 +0400 |
| commit | ed4460990e9e71994c631dde545f50005b5ef769 (patch) | |
| tree | 09e1e1b085b0d24f1dbda1dd4558a927a9fc2261 | |
| parent | f95c8a9e6d6bb2a9c834b0bd8c4819bf25815af8 (diff) | |
| download | ironic-python-agent-ed4460990e9e71994c631dde545f50005b5ef769.tar.gz | |
Make encoding.serialize() more programmatical
Introduce `serializable_fields` to express which class attributes
to be serialized.
Get rid of OrderedDict. Just replacing it with regular dict.
Change-Id: I3f7639dab171d3d62e92d0d1bb6d7b071cf963ad
| -rw-r--r-- | ironic_python_agent/agent.py | 10 | ||||
| -rw-r--r-- | ironic_python_agent/encoding.py | 4 | ||||
| -rw-r--r-- | ironic_python_agent/errors.py | 14 | ||||
| -rw-r--r-- | ironic_python_agent/extensions/base.py | 13 | ||||
| -rw-r--r-- | ironic_python_agent/hardware.py | 35 |
5 files changed, 22 insertions, 54 deletions
diff --git a/ironic_python_agent/agent.py b/ironic_python_agent/agent.py index 39c8368c..7a75f0a3 100644 --- a/ironic_python_agent/agent.py +++ b/ironic_python_agent/agent.py @@ -27,7 +27,6 @@ from ironic_python_agent.extensions import base from ironic_python_agent import hardware from ironic_python_agent import ironic_api_client from ironic_python_agent.openstack.common import log -from ironic_python_agent import utils def _time(): @@ -36,17 +35,12 @@ def _time(): class IronicPythonAgentStatus(encoding.Serializable): + serializable_fields = ('started_at', 'version') + def __init__(self, started_at, version): self.started_at = started_at self.version = version - def serialize(self): - """Turn the status into a dict.""" - return utils.get_ordereddict([ - ('started_at', self.started_at), - ('version', self.version), - ]) - class IronicPythonAgentHeartbeater(threading.Thread): # If we could wait at most N seconds between heartbeats (or in case of an diff --git a/ironic_python_agent/encoding.py b/ironic_python_agent/encoding.py index acf0cfed..bd521d3e 100644 --- a/ironic_python_agent/encoding.py +++ b/ironic_python_agent/encoding.py @@ -18,9 +18,11 @@ import uuid class Serializable(object): """Base class for things that can be serialized.""" + serializable_fields = () + def serialize(self): """Turn this object into a dict.""" - raise NotImplementedError() + return dict((f, getattr(self, f)) for f in self.serializable_fields) class RESTJSONEncoder(json.JSONEncoder): diff --git a/ironic_python_agent/errors.py b/ironic_python_agent/errors.py index 151d8cea..0cb9ad4a 100644 --- a/ironic_python_agent/errors.py +++ b/ironic_python_agent/errors.py @@ -13,7 +13,6 @@ # limitations under the License. from ironic_python_agent import encoding -from ironic_python_agent import utils class RESTError(Exception, encoding.Serializable): @@ -21,15 +20,12 @@ class RESTError(Exception, encoding.Serializable): message = 'An error occurred' details = 'An unexpected error occurred. Please try back later.' status_code = 500 + serializable_fields = ('type', 'code', 'message', 'details') - def serialize(self): - """Turn a RESTError into a dict.""" - return utils.get_ordereddict([ - ('type', self.__class__.__name__), - ('code', self.status_code), - ('message', self.message), - ('details', self.details), - ]) + def __init__(self, *args, **kwargs): + super(RESTError, self).__init__(*args, **kwargs) + self.type = self.__class__.__name__ + self.code = self.status_code class InvalidContentError(RESTError): diff --git a/ironic_python_agent/extensions/base.py b/ironic_python_agent/extensions/base.py index c9b351bd..3ef5c4ea 100644 --- a/ironic_python_agent/extensions/base.py +++ b/ironic_python_agent/extensions/base.py @@ -31,6 +31,9 @@ class AgentCommandStatus(object): class BaseCommandResult(encoding.Serializable): + serializable_fields = ('id', 'command_name', 'command_params', + 'command_status', 'command_error', 'command_result') + def __init__(self, command_name, command_params): self.id = six.text_type(uuid.uuid4()) self.command_name = command_name @@ -39,16 +42,6 @@ class BaseCommandResult(encoding.Serializable): self.command_error = None self.command_result = None - def serialize(self): - return dict(( - (u'id', self.id), - (u'command_name', self.command_name), - (u'command_params', self.command_params), - (u'command_status', self.command_status), - (u'command_error', self.command_error), - (u'command_result', self.command_result), - )) - def is_done(self): return self.command_status != AgentCommandStatus.RUNNING diff --git a/ironic_python_agent/hardware.py b/ironic_python_agent/hardware.py index 8c466d65..b1165457 100644 --- a/ironic_python_agent/hardware.py +++ b/ironic_python_agent/hardware.py @@ -45,18 +45,17 @@ class HardwareType(object): class BlockDevice(encoding.Serializable): + serializable_fields = ('name', 'size') + def __init__(self, name, size): self.name = name self.size = size - def serialize(self): - return utils.get_ordereddict([ - ('name', self.name), - ('size', self.size), - ]) - class NetworkInterface(encoding.Serializable): + serializable_fields = ('name', 'mac_address', 'switch_port_descr', + 'switch_chassis_descr') + def __init__(self, name, mac_addr): self.name = name self.mac_address = mac_addr @@ -64,38 +63,22 @@ class NetworkInterface(encoding.Serializable): self.switch_port_descr = None self.switch_chassis_descr = None - def serialize(self): - return utils.get_ordereddict([ - ('name', self.name), - ('mac_address', self.mac_address), - ('switch_port_descr', self.switch_port_descr), - ('switch_chassis_descr', self.switch_port_descr), - ]) - class CPU(encoding.Serializable): + serializable_fields = ('model_name', 'frequency', 'count') + def __init__(self, model_name, frequency, count): self.model_name = model_name self.frequency = frequency self.count = count - def serialize(self): - return utils.get_ordereddict([ - ('model_name', self.model_name), - ('frequency', self.frequency), - ('count', self.count), - ]) - class Memory(encoding.Serializable): + serializable_fields = ('total', ) + def __init__(self, total): self.total = total - def serialize(self): - return utils.get_ordereddict([ - ('total', self.total), - ]) - @six.add_metaclass(abc.ABCMeta) class HardwareManager(object): |
