martin f. krafft | f37f068 | 2013-06-14 16:36:20 +0200 | [diff] [blame] | 1 | # |
| 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 | # |
| 9 | class NodeStorageBase(object): |
| 10 | |
martin f. krafft | 9a9b0ac | 2013-06-21 21:24:18 +0200 | [diff] [blame^] | 11 | def __init__(self, nodes_uri, classes_uri, applications_postfix): |
martin f. krafft | f37f068 | 2013-06-14 16:36:20 +0200 | [diff] [blame] | 12 | self._nodes_uri = nodes_uri |
| 13 | self._classes_uri = classes_uri |
martin f. krafft | 9a9b0ac | 2013-06-21 21:24:18 +0200 | [diff] [blame^] | 14 | self._applications_postfix = applications_postfix |
martin f. krafft | f37f068 | 2013-06-14 16:36:20 +0200 | [diff] [blame] | 15 | |
| 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. krafft | 9a9b0ac | 2013-06-21 21:24:18 +0200 | [diff] [blame^] | 36 | ret.update([(k + self._applications_postfix,v) for k,v in applications.iteritems()]) |
martin f. krafft | f37f068 | 2013-06-14 16:36:20 +0200 | [diff] [blame] | 37 | return ret |
| 38 | |
| 39 | class 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 |