working config tree generation.
This commit is contained in:
parent
c89d534b10
commit
8be8141060
|
@ -7,10 +7,19 @@ from pystache.context import KeyNotFoundError
|
|||
class CornfigException(Exception):
|
||||
pass
|
||||
|
||||
def install_cornfig(config_path, template_root):
|
||||
def install_cornfig(config_path, template_root, output_path='/'):
|
||||
config = read_config(config_path)
|
||||
for in_file, out_file in template_paths(template_root):
|
||||
print render_template(in_file, config)
|
||||
tree = build_tree( template_paths(template_root), config )
|
||||
for path, contents in tree.items():
|
||||
write_file( os.path.join(output_path, path[1:]), contents)
|
||||
|
||||
def write_file(path, contents):
|
||||
d = os.path.dirname(path)
|
||||
if not os.path.exists(d):
|
||||
os.makedirs(d)
|
||||
out = open(path, 'w')
|
||||
out.write(contents)
|
||||
out.close()
|
||||
|
||||
def build_tree(templates, config):
|
||||
res = {}
|
||||
|
@ -44,7 +53,7 @@ def flatten(d, prefix='', res=None):
|
|||
res = res or {}
|
||||
for k, v in d.items():
|
||||
key = (prefix + '.' + k) if len(prefix) > 0 else k
|
||||
if isinstance(v, str):
|
||||
if isinstance(v, str) or isinstance(v, unicode):
|
||||
res[key] = v
|
||||
elif isinstance(v, dict):
|
||||
res = dict(res.items() + flatten(v, key, res).items())
|
||||
|
|
|
@ -4,12 +4,14 @@ import tempfile
|
|||
from nose.tools import *
|
||||
from cornfig.cornfig import *
|
||||
|
||||
# example template tree
|
||||
TEMPLATES = os.path.join(os.path.dirname(__file__), 'templates')
|
||||
TEMPLATE_PATHS = [
|
||||
"/etc/glance/script.conf",
|
||||
"/etc/keystone/keystone.conf"
|
||||
]
|
||||
|
||||
# config for example tree
|
||||
CONFIG = {
|
||||
"x": "foo",
|
||||
"database": {
|
||||
|
@ -17,6 +19,12 @@ CONFIG = {
|
|||
}
|
||||
}
|
||||
|
||||
# expected output for example tree
|
||||
OUTPUT = {
|
||||
"/etc/glance/script.conf": "foo\n",
|
||||
"/etc/keystone/keystone.conf": "[foo]\ndatabase = sqlite:///blah\n"
|
||||
}
|
||||
|
||||
def setup():
|
||||
pass
|
||||
|
||||
|
@ -26,13 +34,20 @@ def teardown():
|
|||
def template(relpath):
|
||||
return os.path.join(TEMPLATES, relpath[1:])
|
||||
|
||||
|
||||
def test_install_cornfig():
|
||||
t = tempfile.NamedTemporaryFile()
|
||||
t.write(json.dumps(CONFIG))
|
||||
t.flush()
|
||||
tmpdir = tempfile.mkdtemp()
|
||||
install_cornfig(t.name, TEMPLATES, output_path=tmpdir)
|
||||
for path, contents in OUTPUT.items():
|
||||
full_path = os.path.join(tmpdir, path[1:])
|
||||
assert os.path.exists(full_path)
|
||||
assert_equal( open(full_path).read(), contents )
|
||||
|
||||
def test_build_tree():
|
||||
print flatten(CONFIG)
|
||||
assert_equals( build_tree(template_paths(TEMPLATES), CONFIG),
|
||||
{
|
||||
"/etc/keystone/keystone.conf": "[foo]\ndatabase = sqlite:///blah\n",
|
||||
"/etc/glance/script.conf": "foo\n"
|
||||
})
|
||||
assert_equals( build_tree(template_paths(TEMPLATES), CONFIG), OUTPUT )
|
||||
|
||||
def test_flatten():
|
||||
assert_equals( flatten({"x": {"a": "b", "c": "d"}, "y": "z"}), {"x.a": "b", "x.c": "d", "y": "z"} )
|
||||
|
|
Loading…
Reference in New Issue