Isolating Resource derived classes class-attributes state
[nepi.git] / src / neco / execution / resource.py
1 import copy
2 import logging
3 import weakref
4
5 def clsinit(cls):
6     cls._clsinit()
7     return cls
8
9 # Decorator to invoke class initialization method
10 @clsinit
11 class Resource(object):
12     _rtype = "Resource"
13     _filters = None
14     _attributes = None
15
16     @classmethod
17     def _register_filter(cls, attr):
18         """ Resource subclasses will invoke this method to add a 
19         filter attribute"""
20         cls._filters[attr.name] = attr
21
22     @classmethod
23     def _register_attribute(cls, attr):
24         """ Resource subclasses will invoke this method to add a 
25         resource attribute"""
26         cls._attributes[attr.name] = attr
27
28     @classmethod
29     def _register_filters(cls):
30         """ Resource subclasses will invoke this method to add a 
31         filter attribute"""
32         pass
33
34     @classmethod
35     def _register_attributes(cls):
36         """ Resource subclasses will invoke this method to add a 
37         resource attribute"""
38         pass
39
40     @classmethod
41     def _clsinit(cls):
42         # static template for resource filters
43         cls._filters = dict()
44         cls._register_filters()
45
46         # static template for resource attributes
47         cls._attributes = dict()
48         cls._register_attributes()
49
50     @classmethod
51     def rtype(cls):
52         return cls._rtype
53
54     @classmethod
55     def get_filters(cls):
56         return copy.deepcopy(cls._filters.values())
57
58     @classmethod
59     def get_attributes(cls):
60         return copy.deepcopy(cls._attributes.values())
61
62     def __init__(self, ec, guid):
63         self._guid = guid
64         self._ec = weakref.ref(ec)
65         self._connections = set()
66         # the resource instance gets a copy of all attributes
67         # that can modify
68         self._attrs = copy.deepcopy(self._attributes)
69
70         # Logging
71         loglevel = "debug"
72         self._logger = logging.getLogger("neco.execution.resource.Resource.%s" % 
73             self.guid)
74         self._logger.setLevel(getattr(logging, loglevel.upper()))
75
76     @property
77     def guid(self):
78         return self._guid
79
80     @property
81     def ec(self):
82         return self._ec()
83
84     def connect(self, guid):
85         if (self._validate_connection(guid)):
86             self._connections.add(guid)
87
88     def discover(self, filters):
89         pass
90
91     def provision(self, filters):
92         pass
93
94     def set(self, name, value):
95         attr = self._attrs[name]
96         attr.value = value
97
98     def get(self, name):
99         attr = self._attrs[name]
100         return attr.value
101
102     def start_after(self, time, after_status, guid):
103         pass
104
105     def stop_after(self, time, after_status, guid):
106         pass
107
108     def set_after(self, name, value, time, after_status, guid):
109         pass
110
111     def stop(self):
112         pass
113
114     def _validate_connection(self, guid):
115         # TODO: Validate!
116         return True
117
118 class ResourceFactory(object):
119     _resource_types = dict()
120
121     @classmethod
122     def resource_types(cls):
123         return cls._resource_types
124
125     @classmethod
126     def register_type(cls, rclass):
127         cls._resource_types[rclass.rtype()] = rclass
128
129     @classmethod
130     def create(cls, rtype, ec, guid):
131         rclass = cls._resource[rtype]
132         return rclass(ec, guid)
133