Added attributes for resources. Implemented new API for ec and resource
[nepi.git] / src / neco / execution / resource.py
1 import copy
2 import logging
3 import weakref
4
5 class Resource(object):
6     # static template for resource filters
7     _filters = dict()
8     
9     # static template for resource attributes
10     _attributes = dict()
11
12     @classmethod
13     def _register_filter(cls, attr):
14         """ Resource subclasses will invoke this method to add a 
15         filter attribute"""
16         cls._filters[attr.name] = attr
17
18     @classmethod
19     def _register_attributes(cls, attr):
20         """ Resource subclasses will invoke this method to add a 
21         resource attribute"""
22         cls._attributes[attr.name] = attr
23
24     @classmethod
25     def get_filters(cls):
26         return copy.deepcopy(cls._filters.values())
27
28     @classmethod
29     def get_attributes(cls):
30         return copy.deepcopy(cls._attributes.values())
31
32     def __init__(self, ec, guid):
33         self._guid = guid
34         self._ec = weakref.ref(ec)
35         self._connections = set()
36         # the resource instance gets a copy of all attributes
37         # that can modify
38         self._attrs = copy.deepcopy(self._attributes)
39
40         # Logging
41         loglevel = "debug"
42         self._logger = logging.getLogger("neco.execution.resource.Resource.%s" % 
43             self.guid)
44         self._logger.setLevel(getattr(logging, loglevel.upper()))
45
46     @property
47     def guid(self):
48         return self._guid
49
50     @property
51     def ec(self):
52         return self._ec()
53
54     def connect(self, guid):
55         if (self._validate_connection(guid)):
56             self._connections.add(guid)
57
58     def discover(self, filters):
59         pass
60
61     def provision(self, filters):
62         pass
63
64     def set(self, name, value):
65         attr = self._attrs[name]
66         attr.value = value
67
68     def get(self, name):
69         attr = self._attrs[name]
70         return attr.value
71
72     def start_after(self, time, after_status, guid):
73         pass
74
75     def stop_after(self, time, after_status, guid):
76         pass
77
78     def set_after(self, name, value, time, after_status, guid):
79         pass
80
81     def stop(self):
82         pass
83
84     def _validate_connection(self, guid):
85         # TODO: Validate!
86         return True
87
88 class ResourceFactory(object):
89     def __init__(self):
90         self._resource_types = dict()
91
92     def register_type(self, rtype, rclass):
93         self._resource_types[rtype] = rclass
94
95     def create(self, rtype, ec, guid):
96         rclass = self._resource[rtype]
97         return rclass(ec, guid)
98