#!/usr/bin/python

## Tool to run the nova config generating code and spit out a dummy
## version.  Useful for testing that code in isolation.

import os
import sys

possible_topdir = os.path.normpath(os.path.join(os.path.abspath(sys.argv[0]),
                                   os.pardir,
                                   os.pardir))

if os.path.exists(os.path.join(possible_topdir,
                               'anvil',
                               '__init__.py')):
    sys.path.insert(0, possible_topdir)

from  anvil.components.helpers.nova import ConfConfigurator
from  anvil.trace import TraceWriter

class DummyInstaller(object):
    def get_option(self, option, *options, **kwargs):
        if option == 'db':
            return {'host': 'localhost',
                    'port': 3306,
                    'type': 'mysql',
                    'user': 'root'}
        return "dummy"

    def get_bool_option(self, option, *options, **kwargs):
        return False

    def get_password(self, option, *options, **kwargs):
        return "forbinus"

    def target_config(self, config_fn):
        return None

    def __init__(self):
        self.tracewriter = TraceWriter("dummy-config-trace")

d = DummyInstaller()
c = ConfConfigurator(d)
print c.generate("foo.conf")

@@ -157,16 +157,46 @@
 port sys
+%0Aimport tempfile%0Aimport atexit
 %0A%0Apossib
@@ -539,25 +539,24 @@
 pdir)%0A%0Afrom 
- 
 anvil.compon
@@ -602,17 +602,16 @@
 or%0Afrom 
- 
 anvil.tr
@@ -634,16 +634,402 @@
 Writer%0A%0A
+from anvil import utils%0Afrom anvil import shell as sh%0A%0ADUMMY_FILE = tempfile.mktemp()%0A%0A%0Adef at_exit_cleaner():%0A    sh.unlink(DUMMY_FILE)%0A%0Aatexit.register(at_exit_cleaner)%0A%0A%0Adef make_fakey(all_opts, last='dummy'):%0A    src = %7B%7D%0A    tmp = src%0A    last_opt = all_opts%5B-1%5D%0A    for opt in all_opts%5B0:-1%5D:%0A        tmp%5Bopt%5D = %7B%7D%0A        tmp = tmp%5Bopt%5D%0A    tmp%5Blast_opt%5D = last%0A    return src%0A%0A%0A
 class Du
@@ -1147,16 +1147,62 @@
     
-return %7B
+src = %7B%0A                option: %7B%0A                    
 'hos
@@ -1327,32 +1327,390 @@
 oot'
-%7D%0A        return %22dummy%22
+%0A                %7D,%0A            %7D%0A        elif option == 'ip':%0A            return utils.get_host_ip()%0A        elif utils.has_any(option, 'extra_flags', 'extra_opts', 'instances_path'):%0A            return ''%0A        else:%0A            # Make a fake dictionary hierachy%0A            src = make_fakey(%5Boption%5D + list(options))%0A        return utils.get_deep(src, %5Boption%5D + list(options))
 %0A%0A  
@@ -1999,29 +1999,21 @@
 ter(
-%22dummy-config-trace%22)
+DUMMY_FILE)%0A%0A
 %0A%0Ad 

Enhance the dummy configuration maker to be a little more workable