mixed storage type
diff --git a/reclass/storage/mixed/__init__.py b/reclass/storage/mixed/__init__.py
new file mode 100644
index 0000000..238ce74
--- /dev/null
+++ b/reclass/storage/mixed/__init__.py
@@ -0,0 +1,58 @@
+#
+# -*- coding: utf-8 -*-
+#
+# This file is part of reclass
+
+import collections
+import copy
+
+import reclass.errors
+from reclass import get_storage
+from reclass.storage import NodeStorageBase
+
+def path_mangler(inventory_base_uri, nodes_uri, classes_uri):
+ if nodes_uri == classes_uri:
+ raise errors.DuplicateUriError(nodes_uri, classes_uri)
+ return nodes_uri, classes_uri
+
+STORAGE_NAME = 'mixed'
+
+class ExternalNodeStorage(NodeStorageBase):
+
+ MixedUri = collections.namedtuple('MixedURI', 'storage_type options')
+
+ def __init__(self, nodes_uri, classes_uri):
+ super(ExternalNodeStorage, self).__init__(STORAGE_NAME)
+
+ self._nodes_uri = self._uri(nodes_uri)
+ self._nodes_storage = get_storage(self._nodes_uri.storage_type, self._nodes_uri.options, None)
+ self._classes_default_uri = self._uri(classes_uri)
+ self._classes_default_storage = get_storage(self._classes_default_uri.storage_type, None, self._classes_default_uri.options)
+
+ self._classes_storage = dict()
+ if 'env_overrides' in classes_uri:
+ for override in classes_uri['env_overrides']:
+ for env, options in override.iteritems():
+ uri = classes_uri
+ uri.update(options)
+ uri = self._uri(uri)
+ self._classes_storage[env] = get_storage(uri.storage_type, None, uri.options)
+
+ def _uri(self, uri):
+ ret = copy.deepcopy(uri)
+ ret['storage_type'] = uri['storage_type']
+ if 'env_overrides' in ret:
+ del ret['env_overrides']
+ if uri['storage_type'] == 'yaml_fs':
+ ret = ret['uri']
+ return self.MixedUri(uri['storage_type'], ret)
+
+ def get_node(self, name):
+ return self._nodes_storage.get_node(name)
+
+ def get_class(self, name, environment=None):
+ if environment is None:
+ storage = self._classes_default_storage
+ else:
+ storage = self._classes_storage[environment]
+ return storage.get_class(name, environment=environment)
diff --git a/reclass/storage/yaml_fs/__init__.py b/reclass/storage/yaml_fs/__init__.py
index 30bfed0..d06b406 100644
--- a/reclass/storage/yaml_fs/__init__.py
+++ b/reclass/storage/yaml_fs/__init__.py
@@ -53,11 +53,13 @@
def __init__(self, nodes_uri, classes_uri):
super(ExternalNodeStorage, self).__init__(STORAGE_NAME)
- self._nodes_uri = nodes_uri
- self._nodes = self._enumerate_inventory(nodes_uri, NameMangler.nodes)
+ if nodes_uri is not None:
+ self._nodes_uri = nodes_uri
+ self._nodes = self._enumerate_inventory(nodes_uri, NameMangler.nodes)
- self._classes_uri = classes_uri
- self._classes = self._enumerate_inventory(classes_uri, NameMangler.classes)
+ if classes_uri is not None:
+ self._classes_uri = classes_uri
+ self._classes = self._enumerate_inventory(classes_uri, NameMangler.classes)
nodes_uri = property(lambda self: self._nodes_uri)
classes_uri = property(lambda self: self._classes_uri)
@@ -96,7 +98,7 @@
entity = YamlData.from_file(path).get_entity(name)
return entity
- def get_class(self, name, nodename=None, environment=None):
+ def get_class(self, name, environment=None):
vvv('GET CLASS {0}'.format(name))
try:
path = os.path.join(self.classes_uri, self._classes[name])
diff --git a/reclass/storage/yaml_git/__init__.py b/reclass/storage/yaml_git/__init__.py
index fdf2258..cd85ed1 100644
--- a/reclass/storage/yaml_git/__init__.py
+++ b/reclass/storage/yaml_git/__init__.py
@@ -105,25 +105,27 @@
def __init__(self, nodes_uri, classes_uri):
super(ExternalNodeStorage, self).__init__(STORAGE_NAME)
- self._nodes_uri = GitURI({ 'branch': 'master', 'repo': None })
- self._nodes_uri.update(nodes_uri)
- self._load_repo(self._nodes_uri.repo)
- self._nodes = self._repos[self._nodes_uri.repo].nodes(self._nodes_uri.branch)
+ if nodes_uri is not None:
+ self._nodes_uri = GitURI({ 'branch': 'master', 'repo': None })
+ self._nodes_uri.update(nodes_uri)
+ self._load_repo(self._nodes_uri.repo)
+ self._nodes = self._repos[self._nodes_uri.repo].nodes(self._nodes_uri.branch)
- self._classes_default_uri = GitURI({ 'branch': '__env__', 'repo': None })
- self._classes_default_uri.update(classes_uri)
- self._load_repo(self._classes_default_uri.repo)
+ if classes_uri is not None:
+ self._classes_default_uri = GitURI({ 'branch': '__env__', 'repo': None })
+ self._classes_default_uri.update(classes_uri)
+ self._load_repo(self._classes_default_uri.repo)
- self._classes_uri = []
- if 'env_overrides' in classes_uri:
- for override in classes_uri['env_overrides']:
- for env, options in override.iteritems():
- uri = GitURI({ 'branch': env, 'repo': self._classes_default_uri.repo })
- uri.update(options)
- self._classes_uri.append((env, uri))
- self._load_repo(uri.repo)
+ self._classes_uri = []
+ if 'env_overrides' in classes_uri:
+ for override in classes_uri['env_overrides']:
+ for env, options in override.iteritems():
+ uri = GitURI({ 'branch': env, 'repo': self._classes_default_uri.repo })
+ uri.update(options)
+ self._classes_uri.append((env, uri))
+ self._load_repo(uri.repo)
- self._classes_uri.append(('*', self._classes_default_uri))
+ self._classes_uri.append(('*', self._classes_default_uri))
nodes_uri = property(lambda self: self._nodes_uri)
classes_uri = property(lambda self: self._classes_uri)
@@ -134,7 +136,7 @@
entity = YamlData.from_string(blob.data, 'git_fs://{0}#{1}/{2}'.format(self._nodes_uri.repo, self._nodes_uri.branch, file.path)).get_entity(name)
return entity
- def get_class(self, name, nodename=None, environment=None):
+ def get_class(self, name, environment=None):
uri = self._env_to_uri(environment)
file = self._repos[uri.repo].files[uri.branch][name]
blob = self._repos[uri.repo].get(file.id)