Skip to content
Snippets Groups Projects
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
No related branches found
No related tags found
No related merge requests found
......@@ -56,27 +56,53 @@ def flatten(sample, _type):
elif isinstance(_type, labcomm.primitive):
print "%s," % sample,
else:
print sample, _type
raise Exception("Unhandled type.")
def flatten_labels(sample, _type, prefix=""):
def flatten_labels(_type, prefix=""):
if isinstance(_type, labcomm.sample):
flatten_labels(sample, _type.decl, _type.name)
flatten_labels(_type.decl, _type.name)
elif isinstance(_type, labcomm.array):
if len(_type.indices) != 1:
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)
for i in range(0, len(sample)):
flatten_labels(sample[i], _type.decl, prefix + "[%d]" % i)
for i in range(0, _type.indices[0]):
flatten_labels(_type.decl, prefix + "[%d]" % i)
elif isinstance(_type, labcomm.struct):
for name, decl in _type.field:
flatten_labels(sample[name], decl,
flatten_labels(decl,
prefix + "." + name)
elif isinstance(_type, labcomm.primitive):
print '"%s",' % prefix,
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):
......@@ -85,12 +111,18 @@ def dump(sample, _type):
print
def dump_labels(current, _type):
for k in sorted(_type.keys()):
flatten_labels(current[k], _type[k])
def dump_labels(type_):
for k in sorted(type_.keys()):
flatten_labels(type_[k])
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):
parser = argparse.ArgumentParser()
parser.add_argument('elc', type=str, help="The log file.")
......@@ -105,44 +137,42 @@ def main(main_args):
parser.add_argument('-t', '--timeout', action="store", type=float,
help="timeout to terminate when no changes are detected. "
"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)
seen = {}
current = {}
_type = {}
type_ = {}
file_ = open(args.elc)
d = labcomm.Decoder(Reader(file_))
# Do one pass through the file to find all registrations.
reader = FollowingReader(file_, args.interval, args.timeout)
d = labcomm.Decoder(reader)
while True:
try:
o, t = d.decode()
if o is None:
seen[t.name] = 0
_type[t.name] = t
type_[t.name] = t
else:
current[t.name] = o
break
except EOFError:
break
dump_labels(current, _type)
# Do another pass to extract the data.
current = {}
file_.seek(0)
if args.follow:
reader = FollowingReader(file_, args.interval, args.timeout)
else:
reader = Reader(file_)
d = labcomm.Decoder(reader)
dump_labels(type_)
if args.default_columns:
defaults(current, type_)
while True:
try:
o, t = d.decode()
if o is not None:
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.
# Trigger on everything once we have a value for
# each column.
dump(current, _type)
dump(current, type_)
except EOFError:
break
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment