From c50cb0b3a08158d02cf42e95cf77124aa7bfd1f7 Mon Sep 17 00:00:00 2001 From: Alina Quereilhac Date: Sat, 2 Mar 2013 13:06:13 +0100 Subject: [PATCH] Isolating Resource derived classes class-attributes state --- src/neco/execution/ec.py | 2 +- src/neco/execution/resource.py | 51 ++++++++++++++++++++++++++++------ 2 files changed, 44 insertions(+), 9 deletions(-) diff --git a/src/neco/execution/ec.py b/src/neco/execution/ec.py index 6d365f62..0ac35e73 100644 --- a/src/neco/execution/ec.py +++ b/src/neco/execution/ec.py @@ -4,7 +4,7 @@ import sys import time from neco.util import guid -from neco.resources import ResourceFactory +from neco.execution.resource import ResourceFactory class ExperimentController(object): def __init__(self, root_dir = "/tmp", loglevel = 'error'): diff --git a/src/neco/execution/resource.py b/src/neco/execution/resource.py index aba9d080..80ac444b 100644 --- a/src/neco/execution/resource.py +++ b/src/neco/execution/resource.py @@ -2,12 +2,16 @@ import copy import logging import weakref +def clsinit(cls): + cls._clsinit() + return cls + +# Decorator to invoke class initialization method +@clsinit class Resource(object): - # static template for resource filters - _filters = dict() - - # static template for resource attributes - _attributes = dict() + _rtype = "Resource" + _filters = None + _attributes = None @classmethod def _register_filter(cls, attr): @@ -16,11 +20,37 @@ class Resource(object): cls._filters[attr.name] = attr @classmethod - def _register_attributes(cls, attr): + def _register_attribute(cls, attr): """ Resource subclasses will invoke this method to add a resource attribute""" cls._attributes[attr.name] = attr + @classmethod + def _register_filters(cls): + """ Resource subclasses will invoke this method to add a + filter attribute""" + pass + + @classmethod + def _register_attributes(cls): + """ Resource subclasses will invoke this method to add a + resource attribute""" + pass + + @classmethod + def _clsinit(cls): + # static template for resource filters + cls._filters = dict() + cls._register_filters() + + # static template for resource attributes + cls._attributes = dict() + cls._register_attributes() + + @classmethod + def rtype(cls): + return cls._rtype + @classmethod def get_filters(cls): return copy.deepcopy(cls._filters.values()) @@ -89,10 +119,15 @@ class ResourceFactory(object): _resource_types = dict() @classmethod - def register_type(cls, rtype, rclass): - cls._resource_types[rtype] = rclass + def resource_types(cls): + return cls._resource_types + + @classmethod + def register_type(cls, rclass): + cls._resource_types[rclass.rtype()] = rclass @classmethod def create(cls, rtype, ec, guid): rclass = cls._resource[rtype] return rclass(ec, guid) + -- 2.47.0