Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix: message fields with type 'string' and format 'csv' are again stored as a parsed list of strings #192

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
47 changes: 35 additions & 12 deletions lib/v2.0/python/src/pprzlink/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

from __future__ import division, print_function,annotations
import sys
import json
import json,csv
import struct
import re
import typing
Expand Down Expand Up @@ -193,10 +193,30 @@ def parse(self,strval:typing.Any) -> None:
self.val = strval
else:
try:
if self.python_simple_type == list and not(isinstance(strval,list)):
self.val = [strval]
elif self.python_simple_type == list and isinstance(strval,list):
self.val = strval

if self.python_simple_type == list:
if not(isinstance(strval,list)):
# If not a list, suppose it is a string with elements separated by commas
self.val = strval.split(',')
else:
self.val = strval

elif self.python_simple_type == str and (self.format == "csv" or self.format == ";sv"):
# Special treatment of CSV encoded strings: store them as list of strings instead of string
if self.format[0] == ';':
sep = ';'
else:
sep = ','

if isinstance(strval,list):
self.val = strval
else:
# Remove one trailing separator, if it exists
if strval[-1] == sep:
strval = strval[:-1]

# Parse using the Python CSV module
self.val = next(csv.reader([strval],delimiter=sep))
else:
self.val = self.python_simple_type(strval)

Expand Down Expand Up @@ -491,13 +511,16 @@ def ivy_string_to_payload(self, data:str) -> None:
for el in re.split('([|\"][^|\"]*[|\"])', data):
if '|' not in el and '"' not in el:
# split non-array strings further up
for e in [d for d in el.split(' ') if d != '']:
if ',' in e:
# array but not a string
values.append([x for x in e.split(',') if x != ''])
else:
# not an array
values.append(e)
for e in [d for d in el.split(' ') if d != '']:
# Instead, try to let the type-based parser decode CSV str
values.append(e)

# if ',' in e:
# # array but not a string
# values.append([x for x in e.split(',') if x != ''])
# else:
# # not an array
# values.append(e)
else:
# add string array (stripped)
values.append(str.strip(el))
Expand Down