ConfigGenerator.py 1.99 KB
Newer Older
Sebastian Eibl's avatar
Sebastian Eibl committed
1
2
3
4
5
# -*- coding: utf-8 -*-

from jinja2 import Environment, FileSystemLoader
import os

6

Sebastian Eibl's avatar
Sebastian Eibl committed
7
class Parameter:
8
9
10
11
12
13
14
15
16
17
18
19
    def __init__(self, name, type, defValue=""):
        """Propery of a data strcuture

        Parameters
        ----------
        name : str
           name of the property
        type : str
           type of the property
        defValue : str
           default value the property should be initialized with
        """
Sebastian Eibl's avatar
Sebastian Eibl committed
20

21
22
23
        self.name = name
        self.type = type
        self.defValue = defValue
Sebastian Eibl's avatar
Sebastian Eibl committed
24

25
26
    def __str__(self):
        return "name: {}, type: {}, defValue: {}".format(self.name, self.type, self.defValue)
Sebastian Eibl's avatar
Sebastian Eibl committed
27
28
29


class Config:
30
31
    def __init__(self):
        self.parameters = []
Sebastian Eibl's avatar
Sebastian Eibl committed
32

33
34
35
36
37
    def parameterExists(self, name):
        for v in self.parameters:
            if v.name == name:
                return True
        return False
Sebastian Eibl's avatar
Sebastian Eibl committed
38

39
40
41
42
43
    def addParameter(self, name, type, defValue):
        if self.parameterExists(name):
            print("parameters already added: " + name)
        else:
            self.parameters.append(Parameter(name, type, defValue))
Sebastian Eibl's avatar
Sebastian Eibl committed
44

45
46
47
    def generateFile(self, template):
        context = dict()
        context["parameters"] = self.parameters
Sebastian Eibl's avatar
Sebastian Eibl committed
48

49
50
51
52
53
54
55
56
        path = ""
        filename = template.replace(".templ", "")
        dirname = os.path.dirname(__file__)
        env = Environment(loader=FileSystemLoader(dirname))
        print("generating: " + path + filename)
        with open(path + filename, "wb") as fout:
            content = env.get_template(template).render(context)
            fout.write(content.encode('utf8'))
Sebastian Eibl's avatar
Sebastian Eibl committed
57

58
59
60
61
62
63
64
65
66
    def generate(self):
        print("=" * 90)
        print("Config File:")
        print("")
        print("{0: <30}{1: <30}{2: <30}".format("Name", "Type", "Def. Value"))
        print("=" * 90)
        for param in self.parameters:
            print("{0: <30.29}{1: <30.29}{2: <30.29}".format(param.name, param.type, param.defValue))
        print("=" * 90)
Sebastian Eibl's avatar
Sebastian Eibl committed
67

68
69
        self.generateFile("Parameters.templ.h")
        self.generateFile("Parameters.templ.cpp")