Commit 01d39717 authored by Mario Hock's avatar Mario Hock
Browse files

CNLParser class:

A small "library" to parse CNL-files, to be used in plotters etc.

Open TODO: rename file..
parent 130564a3
......@@ -8,6 +8,8 @@ import json
import csv
## TODO rename file / move to other file..
def cnl_slice(file, start_delimiter, end_delimiter):
......@@ -29,6 +31,16 @@ def cnl_slice(file, start_delimiter, end_delimiter):
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
def read_header(f):
......@@ -44,39 +56,97 @@ def read_header(f):
class CNLParser:
def __init__(self, filename):
self.filename = filename
with open( filename ) as in_file:
## Check file format version.
assert( in_file.readline() == "%% CPUnetLOGv1\n" )
## Read JSON header.
self.header = read_header(in_file)
## MAIN ##
if __name__ == "__main__":
## 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)
filename = sys.argv[1]
print( filename )
with open( filename ) as in_file:
assert( in_file.readline() == "%% CPUnetLOGv1\n" )
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 ]
header = read_header(in_file)
print( json.dumps(header, sort_keys=True, indent=4) ) ## XXX
## Convenience functions ##
## XXX
print ("///")
print()
def print_json_header(self):
print( json.dumps(self.header, sort_keys=True, indent=4) )
## Read as lists
#csv_reader = csv.reader( cnl_slice(in_file, "%% Begin_Body", "%% End_Body"), skipinitialspace=True )
#csv_header = next(csv_reader)
#print( csv_header )
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 )
## Read as dicts
csv_reader = csv.DictReader( cnl_slice(in_file, "%% Begin_Body", "%% End_Body"), skipinitialspace=True )
## * Parse input file. *
cnl_file = CNLParser(filename)
## print
for l in csv_reader:
print( l )
#print( l["eth0.send"] + ", " + l["eth0.receive"] )
pass
## Display header informations.
print( cnl_file.get_type() )
print( json.dumps(cnl_file.get_json_header(), sort_keys=True, indent=4) )
print( "CPUs: " + str(cnl_file.get_cpus()) )
print( "NICs: " + str(cnl_file.get_nics()) )
## Display some csv/data fields.
names = None
names = ["eth0.send", "eth0.receive"]
print( names )
for x in cnl_file.get_csv_iterator(names):
print( ", ".join(x) )
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment