summaryrefslogtreecommitdiff
path: root/tools/trace/tcreator/writer.py
blob: db5fc21edbb7e279efc902ba5e8ca608c0a20de7 (plain)
1
2
3
4
5
6
7
8
9
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
75
76
77
78
79
80
81
import sys
import time
import os

from lib.traceclass import *

templatedir = os.path.split (__file__)[0]

class Writer:

    """Template writer"""
    def __init__ (self):
        self.__header_name = "events.h"
        self.__file_name = "events.host.c"

    def parse_event_identifiers (self, event_list = None):
        """Parse the event list and construct a string to embedded in a
        header file."""
        if event_list != None:
            string = ""
            prefix = "    "
            for i in range (0, len (event_list)):
                string += prefix + "TRACE_" + event_list[i].name() + ",\n"
            string += prefix + "TRACE_NB"
            return string.upper()

    def __parse_event_string_cb (self, event):
        """Create a default string interpretation for callbacks events."""
        assert event.callback
        string = "\"" + event.name ()
        for i in range (event.param_nb ()):
            string += " %d"
        string += "\\n\","
        return string

    def parse_event_string (self, event_list = None):
        """Parse the event list and construct a string containing on each line
        the string interpretation for a human."""
        if event_list != None:
            string = ""
            prefix = "    "
            for i in event_list:
                if i.callback:
                    string += prefix + self.__parse_event_string_cb (i)
                else:
                    string += prefix + i.string_get () + ",\n"
            return string

    def __read_template_header (self, string, string_nb):
        f = open (templatedir + '/template.h', 'r')
        template = f.read()
        f.close()
        template = template.replace('%%ids%%', string)
        template = template.replace('%%year%%', time.strftime ('%Y'))
        template = template.replace('%%NB%%', string_nb);
        return template

    def __read_template_file (self, string_table, string_nb):
        f = open (templatedir + '/template.c', 'r')
        template = f.read()
        f.close()
        template = template.replace('%%NB%%', string_nb);
        template = template.replace('%%DATA%%', string_table)
        return template

    def __write (self, template, outfile):
        f = open (outfile, 'w')
        f.write (template)
        f.close()

    def write_file (self, id_table, string_table, string_table_nb):
        template = self.__read_template_header (id_table, string_table_nb)
        self.__write (template, self.__header_name)
        template = self.__read_template_file (string_table, string_table_nb)
        self.__write (template, self.__file_name)

    def print_file (self, id_table, string_table, string_table_nb):
        template = self.__read_template_header (id_table, string_table_nb)
        print template
        template = self.__read_template_file (string_table, string_table_nb)
        print template