d3a1dc44a8a902feff3a406cd6a59f7f4bc47fd1
[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         self._logger = logging.getLogger("neco.execution.resource.Resource.%s" % 
72             self.guid)
73
74     @property
75     def guid(self):
76         return self._guid
77
78     @property
79     def ec(self):
80         return self._ec()
81
82     def connect(self, guid):
83         if (self._validate_connection(guid)):
84             self._connections.add(guid)
85
86     @property
87     def connections(self):
88         return self._connections
89
90     def discover(self, filters):
91         pass
92
93     def provision(self, filters):
94         pass
95
96     def set(self, name, value):
97         attr = self._attrs[name]
98         attr.value = value
99
100     def get(self, name):
101         attr = self._attrs[name]
102         return attr.value
103
104     def start_after(self, time, after_status, guid):
105         pass
106
107     def stop_after(self, time, after_status, guid):
108         pass
109
110     def set_after(self, name, value, time, after_status, guid):
111         pass
112
113     def next_step(self):
114         pass
115
116     def stop(self):
117         pass
118
119     def _validate_connection(self, guid):
120         # TODO: Validate!
121         return True
122
123 class ResourceFactory(object):
124     _resource_types = dict()
125
126     @classmethod
127     def resource_types(cls):
128         return cls._resource_types
129
130     @classmethod
131     def register_type(cls, rclass):
132         cls._resource_types[rclass.rtype()] = rclass
133
134     @classmethod
135     def create(cls, rtype, ec, guid, creds):
136         rclass = cls._resource_types[rtype]
137         return rclass(ec, guid, creds)
138