simple_plotter.py 1.2 KB
Newer Older
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
#!/usr/bin/env python3
# -*- coding:utf-8 -*-

import sys
from io import StringIO

import json
import csv



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





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






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

    filename = sys.argv[1]
    print( filename )

    with open( filename ) as in_file:
        assert( in_file.readline() == "%% CPUnetLOGv1\n" )

        header = read_header(in_file)
        print( json.dumps(header, sort_keys=True, indent=4) )  ## XXX

        print ("///")
        print()

        for l in csv.reader( cnl_slice(in_file, "%% Begin_Body", "%% End_Body") ):
            print( l )
            pass