Commit f065f1bb authored by Tommy Olofsson's avatar Tommy Olofsson
Browse files

Added option for getting output even if some type is never encoded.

parent 585d3f69
...@@ -56,27 +56,53 @@ def flatten(sample, _type): ...@@ -56,27 +56,53 @@ def flatten(sample, _type):
elif isinstance(_type, labcomm.primitive): elif isinstance(_type, labcomm.primitive):
print "%s," % sample, print "%s," % sample,
else: else:
print sample, _type raise Exception("Unhandled type.")
def flatten_labels(sample, _type, prefix=""): def flatten_labels(_type, prefix=""):
if isinstance(_type, labcomm.sample): if isinstance(_type, labcomm.sample):
flatten_labels(sample, _type.decl, _type.name) flatten_labels(_type.decl, _type.name)
elif isinstance(_type, labcomm.array): elif isinstance(_type, labcomm.array):
if len(_type.indices) != 1: if len(_type.indices) != 1:
raise Exception("Fix multidimensional arrays") raise Exception("Fix multidimensional arrays")
if len(sample) == 0: if len(_type.indices) == 0:
raise Exception("We dont't handle dynamical sizes yet %s" % _type) raise Exception("We dont't handle dynamical sizes yet %s" % _type)
for i in range(0, len(sample)): for i in range(0, _type.indices[0]):
flatten_labels(sample[i], _type.decl, prefix + "[%d]" % i) flatten_labels(_type.decl, prefix + "[%d]" % i)
elif isinstance(_type, labcomm.struct): elif isinstance(_type, labcomm.struct):
for name, decl in _type.field: for name, decl in _type.field:
flatten_labels(sample[name], decl, flatten_labels(decl,
prefix + "." + name) prefix + "." + name)
elif isinstance(_type, labcomm.primitive): elif isinstance(_type, labcomm.primitive):
print '"%s",' % prefix, print '"%s",' % prefix,
else: else:
print sample, _type raise Exception("Unhandled type.")
def default(type_):
if isinstance(type_, labcomm.sample):
return default(type_.decl)
elif isinstance(type_, labcomm.array):
if len(type_.indices) != 1:
raise Exception("Fix multidimensional arrays")
if len(type_.indices) == 0:
raise Exception("We dont't handle dynamical sizes yet %s" % type_)
for i in range(0, type_.indices[0]):
return [default(type_.decl) for _ in range(type_.indices[0])]
elif isinstance(type_, labcomm.struct):
return {name: default(decl) for name, decl in type_.field}
elif isinstance(type_, labcomm.STRING):
return ''
elif isinstance(type_, labcomm.BOOLEAN):
return False
elif (isinstance(type_, labcomm.FLOAT) or
isinstance(type_, labcomm.DOUBLE)):
return float('NaN')
elif isinstance(type_, labcomm.primitive):
# Must be int type.
return 0
else:
raise Exception("Unhandled type. " + str(type(type_)) + " " + str(type_))
def dump(sample, _type): def dump(sample, _type):
...@@ -85,12 +111,18 @@ def dump(sample, _type): ...@@ -85,12 +111,18 @@ def dump(sample, _type):
print print
def dump_labels(current, _type): def dump_labels(type_):
for k in sorted(_type.keys()): for k in sorted(type_.keys()):
flatten_labels(current[k], _type[k]) flatten_labels(type_[k])
print print
def defaults(current, type_):
for k in sorted(type_.keys()):
if k not in current:
current[k] = default(type_[k])
def main(main_args): def main(main_args):
parser = argparse.ArgumentParser() parser = argparse.ArgumentParser()
parser.add_argument('elc', type=str, help="The log file.") parser.add_argument('elc', type=str, help="The log file.")
...@@ -105,44 +137,42 @@ def main(main_args): ...@@ -105,44 +137,42 @@ def main(main_args):
parser.add_argument('-t', '--timeout', action="store", type=float, parser.add_argument('-t', '--timeout', action="store", type=float,
help="timeout to terminate when no changes are detected. " help="timeout to terminate when no changes are detected. "
"Requires -f.") "Requires -f.")
parser.add_argument('-d', '--default-columns', action="store_true",
help="Fill columns for which there has not come any "
"data with default values. Useful for getting output "
"even if not all registered types ara actually encoded.")
args = parser.parse_args(main_args) args = parser.parse_args(main_args)
seen = {} seen = {}
current = {} current = {}
_type = {} type_ = {}
file_ = open(args.elc) file_ = open(args.elc)
d = labcomm.Decoder(Reader(file_)) reader = FollowingReader(file_, args.interval, args.timeout)
# Do one pass through the file to find all registrations. d = labcomm.Decoder(reader)
while True: while True:
try: try:
o, t = d.decode() o, t = d.decode()
if o is None: if o is None:
seen[t.name] = 0 seen[t.name] = 0
_type[t.name] = t type_[t.name] = t
else: else:
current[t.name] = o current[t.name] = o
break
except EOFError: except EOFError:
break break
dump_labels(current, _type) dump_labels(type_)
if args.default_columns:
# Do another pass to extract the data. defaults(current, type_)
current = {}
file_.seek(0)
if args.follow:
reader = FollowingReader(file_, args.interval, args.timeout)
else:
reader = Reader(file_)
d = labcomm.Decoder(reader)
while True: while True:
try: try:
o, t = d.decode() o, t = d.decode()
if o is not None: if o is not None:
current[t.name] = o current[t.name] = o
if len(current) == len(_type): if len(current) == len(type_):
# TODO: Figure out what to trigger on...
# Assume that samples arrive at different rates. # Assume that samples arrive at different rates.
# Trigger on everything once we have a value for # Trigger on everything once we have a value for
# each column. # each column.
dump(current, _type) dump(current, type_)
except EOFError: except EOFError:
break break
......
Supports Markdown
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