Skip to content
This repository was archived by the owner on Jan 19, 2018. It is now read-only.

Commit f9e4ed1

Browse files
committed
Initial work on refactoring Nulecule config. #524
- Create a class representation for config data - Implement load() to load config data for a component, taking into account answers and user supplied data - Implement context() method to render flattened data to be consumed in a component artifact.
1 parent cef5ec5 commit f9e4ed1

File tree

1 file changed

+71
-0
lines changed

1 file changed

+71
-0
lines changed

atomicapp/nulecule/config.py

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import copy
2+
import logging
3+
4+
from atomicapp.constants import (NAME_KEY,
5+
GLOBAL_CONF,
6+
DEFAULTNAME_KEY,
7+
LOGGER_COCKPIT)
8+
from atomicapp.utils import Utils
9+
from collections import defaultdict
10+
11+
cockpit_logger = logging.getLogger(LOGGER_COCKPIT)
12+
13+
14+
class Config(object):
15+
"""
16+
Store config data for a Nulecule or Nulecule component.
17+
"""
18+
19+
def __init__(self, namespace='', params=None, answers=None, cli=None,
20+
data=None, is_nulecule=False):
21+
self._namespace = namespace
22+
self._parent_ns, self._current_ns = self._split_namespace(self._namespace)
23+
self._params = params or {}
24+
self._answers = answers or defaultdict(dict)
25+
self._cli = cli or {}
26+
self._data = data or defaultdict(dict)
27+
self._is_nulecule = is_nulecule or False
28+
29+
def load(self, ask=False, skip_asking=False):
30+
"""
31+
Load config data.
32+
"""
33+
for param in self._params:
34+
value = self._answers[self._namespace].get(param[NAME_KEY]) or \
35+
self._answers[self._parent_ns].get(param[NAME_KEY]) or \
36+
self._answers[GLOBAL_CONF].get(param[NAME_KEY])
37+
if value is None and (ask or (
38+
not skip_asking and param.get(DEFAULTNAME_KEY) is None)):
39+
cockpit_logger.info("%s is missing in answers.conf." % param[NAME_KEY])
40+
value = Utils.askfor(param[NAME_KEY], param)
41+
elif value is None:
42+
value = param.get(DEFAULTNAME_KEY)
43+
self._data[self._namespace][param[NAME_KEY]] = value
44+
45+
def _split_namespace(self, namespace):
46+
"""
47+
Split namespace to get parent and current namespace in a Nulecule.
48+
"""
49+
if self._is_nulecule:
50+
return '', namespace
51+
words = namespace.rsplit('.', 1)
52+
parent, current = '', ''
53+
if len(words) == 2:
54+
parent, current = words[0], words[1]
55+
else:
56+
parent, current = '', words[0]
57+
return parent, current
58+
59+
def context(self):
60+
"""
61+
Get context to render artifact files in a Nulecule component.
62+
"""
63+
context = {}
64+
65+
context.update(copy.copy(self._data[GLOBAL_CONF]))
66+
context.update(copy.copy(self._data[self._namespace]))
67+
68+
context.update(copy.copy(self._answers[GLOBAL_CONF]))
69+
context.update(copy.copy(self._answers[self._namespace]))
70+
71+
return context

0 commit comments

Comments
 (0)