cnl_library.py 3.59 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
#!/usr/bin/env python3
# -*- coding:utf-8 -*-

import sys
from io import StringIO

import json
import csv


Mario Hock's avatar
Mario Hock committed
11
12
## TODO rename file / move to other file..

13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33

def cnl_slice(file, start_delimiter, end_delimiter):

    ## Find beginning
    for line in file:
        if ( line.startswith(start_delimiter) ):
            break

    ## Skip comments and quit on end
    for line in file:
        if ( line.startswith(end_delimiter) ):
            return

        # skip empty or commented lines
        if ( not line or line[0] == "%" or line[0] == "#" ):
            continue

        yield line



Mario Hock's avatar
Mario Hock committed
34
35
36
37
38
39
40
41
42
43
def create_csv_index(csv_header):
    ## Create an index that maps csv_header names to tuple indices.
    csv_field_index = dict()
    i = 0
    for field in csv_header:
        csv_field_index[field] = i
        i += 1

    return csv_field_index

44
45
46
47
48
49
50
51
52
53
54
55
56
57
58


def read_header(f):
    str_io = StringIO()

    for line in cnl_slice(f, "%% Begin_Header", "%% End_Header"):
        str_io.write(line)

    str_io.seek(0)
    header = json.load( str_io )

    return header



Mario Hock's avatar
Mario Hock committed
59
60
61
class CNLParser:
    def __init__(self, filename):
        self.filename = filename
62

Mario Hock's avatar
Mario Hock committed
63
64
65
        with open( filename ) as in_file:
            ## Check file format version.
            assert( in_file.readline() == "%% CPUnetLOGv1\n" )
66

Mario Hock's avatar
Mario Hock committed
67
68
            ## Read JSON header.
            self.header = read_header(in_file)
69

Mario Hock's avatar
Mario Hock committed
70
71
72
73
            ## Read CSV "header"
            csv_reader = csv.reader( cnl_slice(in_file, "%% Begin_Body", "%% End_Body"), skipinitialspace=True )
            self.csv_header = next(csv_reader)
            self.csv_index = create_csv_index(self.csv_header)
74
75


Mario Hock's avatar
Mario Hock committed
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
    def get_json_header(self):
        return self.header


    def get_csv_iterator(self, fields=None):
        indices = None

        ## Only return selected columns (if the |fields| option is set).
        if ( fields ):
            indices = cnl_file.get_csv_indices_of(names)


        ## Read from file.
        with open( filename ) as in_file:
            ## Find start of the CSV part.
            csv_reader = csv.reader( cnl_slice(in_file, "%% Begin_Body", "%% End_Body"), skipinitialspace=True )
            csv_header = next(csv_reader)
            assert( csv_header == self.csv_header )

            ## Yield line by line.
            for line in csv_reader:
                if ( not indices ):
                    yield line
                else:
                    yield [ line[ind] for ind in indices ]

102

Mario Hock's avatar
Mario Hock committed
103
    ## Convenience functions ##
104

Mario Hock's avatar
Mario Hock committed
105
106
    def print_json_header(self):
        print( json.dumps(self.header, sort_keys=True, indent=4) )
107

Mario Hock's avatar
Mario Hock committed
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
    def get_csv_index_of(self, field_name):
        return self.csv_index[field_name]

    def get_csv_indices_of(self, field_names):
        return [ self.get_csv_index_of(name) for name in field_names ]

    # Specific getters:

    def get_type(self):
        return self.header["General"]["Type"]

    def get_cpus(self):
        return self.header["ClassDefinitions"]["CPU"]["Siblings"]

    def get_nics(self):
        return self.header["ClassDefinitions"]["NIC"]["Siblings"]



## MAIN ##
if __name__ == "__main__":

    ### DEMO:

    filename = sys.argv[1]
    print( filename )
Mario Hock's avatar
Mario Hock committed
134

Mario Hock's avatar
Mario Hock committed
135
136
    ## * Parse input file. *
    cnl_file = CNLParser(filename)
Mario Hock's avatar
Mario Hock committed
137

138

Mario Hock's avatar
Mario Hock committed
139
140
141
    ## Display header informations.
    print( cnl_file.get_type() )
    print( json.dumps(cnl_file.get_json_header(), sort_keys=True, indent=4) )
142

Mario Hock's avatar
Mario Hock committed
143
144
    print( "CPUs: " + str(cnl_file.get_cpus()) )
    print( "NICs: " + str(cnl_file.get_nics()) )
145

Mario Hock's avatar
Mario Hock committed
146
147
148
149
    ## Display some csv/data fields.
    names = None
    names = ["eth0.send", "eth0.receive"]
    print( names )
150

Mario Hock's avatar
Mario Hock committed
151
152
    for x in cnl_file.get_csv_iterator(names):
        print( ", ".join(x) )