FilmSpecimenGenerator.py 2.17 KB
Newer Older
Sebastian Eibl's avatar
Sebastian Eibl committed
1
2
3
4
5
6
7
8
# -*- coding: utf-8 -*-

from jinja2 import Environment, FileSystemLoader
import os


class Parameter:
    def __init__(self, name, type, defValue="", comment=""):
9
        """Property of a data structure
Sebastian Eibl's avatar
Sebastian Eibl committed
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74

        Parameters
        ----------
        name : str
           name of the property
        type : str
           type of the property
        defValue : str
           default value the property should be initialized with
        comment : str
           comment appended to the properties in the header file
        """

        self.name = name
        self.type = type
        self.defValue = defValue
        self.comment = comment

    def __str__(self):
        return "name: {}, type: {}, defValue: {} \\\\{}".format(self.name, self.type, self.defValue, self.comment)


class Config:
    def __init__(self):
        self.parameters = []

    def parameter_exists(self, name):
        for v in self.parameters:
            if v.name == name:
                return True
        return False

    def add_parameter(self, name, type, defValue, comment):
        if self.parameter_exists(name):
            print("parameters already added: " + name)
        else:
            self.parameters.append(Parameter(name, type, defValue, comment))

    def generate_file(self, template):
        context = dict()
        context["parameters"] = self.parameters

        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'))

    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)

        self.generate_file("FilmSpecimen.templ.h")
        self.generate_file("FilmSpecimen.templ.cpp")