blob: 27cbf7d1f22ec401e0ac23562c495e715c2fb8b5 [file] [log] [blame]
martin f. krafftf37f0682013-06-14 16:36:20 +02001#
2# -*- coding: utf-8 -*-
3#
4# This file is part of reclass (http://github.com/madduck/reclass)
5#
6# Copyright © 2007–13 martin f. krafft <madduck@madduck.net>
7# Released under the terms of the Artistic Licence 2.0
8#
9class NodeStorageBase(object):
10
martin f. krafft9a9b0ac2013-06-21 21:24:18 +020011 def __init__(self, nodes_uri, classes_uri, applications_postfix):
martin f. krafftf37f0682013-06-14 16:36:20 +020012 self._nodes_uri = nodes_uri
13 self._classes_uri = classes_uri
martin f. krafft9a9b0ac2013-06-21 21:24:18 +020014 self._applications_postfix = applications_postfix
martin f. krafftf37f0682013-06-14 16:36:20 +020015
16 nodes_uri = property(lambda self: self._nodes_uri)
17 classes_uri = property(lambda self: self._classes_uri)
18
19 def _read_entity(self, node, base_uri, seen={}):
20 raise NotImplementedError, "Storage class not implement node info retrieval"
21
22 def nodeinfo(self, node):
23 entity, uri = self._read_nodeinfo(node, self.nodes_uri, {})
24 return {'RECLASS' : {'node': node, 'node_uri': uri},
25 'classes': list(entity.classes),
26 'applications': list(entity.applications),
27 'parameters': dict(entity.parameters)
28 }
29
30 def _list_inventory(self):
31 raise NotImplementedError, "Storage class does not implement inventory listing"
32
33 def inventory(self):
34 entity, applications, classes = self._list_inventory()
35 ret = classes
martin f. krafft9a9b0ac2013-06-21 21:24:18 +020036 ret.update([(k + self._applications_postfix,v) for k,v in applications.iteritems()])
martin f. krafftf37f0682013-06-14 16:36:20 +020037 return ret
38
39class StorageBackendLoader(object):
40
41 def __init__(self, storage_type):
42 self._name = storage_type
43 try:
44 self._module = __import__(storage_type, globals(), locals(), storage_type)
45 except ImportError:
46 raise NotImplementedError
47
48 def load(self, attr='ExternalNodeStorage'):
49 klass = getattr(self._module, attr, None)
50 if klass is None:
51 raise AttributeError, \
52 'Storage backend class {0} does not export "{1}"'.format(self._name, klass)
53 return klass