Skip to content
Snippets Groups Projects
Commit 660237a1 authored by Tommy Olofsson's avatar Tommy Olofsson
Browse files

Added an updated version of the CSV generator.

parent 6b6da1ce
No related branches found
No related tags found
No related merge requests found
#!/usr/bin/env python
import sys
import labcomm
class Reader(object):
def __init__(self, file):
self.file = open(file)
def read(self, count):
data = self.file.read(count)
if len(data) == 0:
raise EOFError()
return data
def mark(self, value, decl):
pass
def flatten(sample, _type):
if isinstance(_type, labcomm.sample):
flatten(sample, _type.decl)
elif isinstance(_type, labcomm.array):
for e in sample:
flatten(e, _type.decl)
elif isinstance(_type, labcomm.struct):
for name, decl in _type.field:
flatten(sample[name], decl)
elif isinstance(_type, labcomm.BOOLEAN):
print "%d," % sample,
elif isinstance(_type, labcomm.STRING):
print "\"%s\"," % sample,
elif isinstance(_type, labcomm.primitive):
print "%s," % sample,
else:
print sample, _type
def flatten_labels(sample, _type, prefix=""):
if isinstance(_type, labcomm.sample):
flatten_labels(sample, _type.decl, _type.name)
elif isinstance(_type, labcomm.array):
if len(_type.indices) != 1:
raise Exception("Fix multidimensional arrays")
if len(sample) == 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)
elif isinstance(_type, labcomm.struct):
for name, decl in _type.field:
flatten_labels(sample[name], decl,
prefix + "." + name)
elif isinstance(_type, labcomm.primitive):
print '"%s",' % prefix,
else:
print sample, _type
def dump(sample, _type):
for k in sorted(_type.keys()):
flatten(sample[k], _type[k])
print
def dump_labels(current, _type):
for k in sorted(_type.keys()):
flatten_labels(current[k], _type[k])
print
def main():
d = labcomm.Decoder(Reader(sys.argv[1]))
seen = {}
current = {}
_type = {}
while True:
try:
(o, t) = d.decode()
except EOFError:
break
if o == None:
# Type declaration
seen[t.name] = 0
_type[t.name] = t
else:
current[t.name] = o
if len(current) == len(_type):
# We have got one sample of each
break
dump_labels(current, _type)
n = 0
while o != None:
if seen[t.name] > n:
n = seen[t.name]
dump(current, _type)
current[t.name] = o
seen[t.name] += 1
try:
(o, t) = d.decode()
  • Anders Blomdell @anders_blomdell ·

    An encoder is allowed to add new types after sending of samples has started, maybe the code should/could handle such a case as well?

  • Author Owner

    I thought that was a bit weird as well. I'll fix that.

  • Author Owner

    It would be impractical when using it "live" during an experiment. Would it not?

  • Anders Blomdell @anders_blomdell ·

    Not necessarily, could be useful for dynamicvv logging. If used for control, one has to make sure that registration has finished (with retransmissions, etc) before switching in time-critical things using the new types. I.e. not CS101 stuff, but I can see applications for it.

  • Author Owner

    The current version tries parsing the whole file looking for registrations before outputting the header and then data. This got nowhere when following a log file... If a valid compromize would be to accept all registrations that are written when the application starts, and then continue following the file for changes, then there will be a merge request in the works.

    Feel free to deny it if there is a better way.

  • Please register or sign in to reply
except EOFError:
break
dump(current, _type)
if __name__ == "__main__":
main()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment