Skip to content
GitLab
Menu
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
Tommy Olofsson
LabComm
Commits
f065f1bb
Commit
f065f1bb
authored
Feb 02, 2015
by
Tommy Olofsson
Browse files
Added option for getting output even if some type is never encoded.
parent
585d3f69
Changes
1
Show whitespace changes
Inline
Side-by-side
tools/lc2csv.py
View file @
f065f1bb
...
...
@@ -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
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment