Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion ros_typedb/ros_typedb/typedb_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# limitations under the License.
"""typedb_interface - python interface to interact with typedb."""

import ast
from datetime import datetime
import functools
import logging
Expand Down Expand Up @@ -99,10 +100,25 @@ def string_to_string_array(string: str) -> list[str]:
"""
Convert string to string array.

Single string values are returned as a one-item list so valid Linux paths
containing commas are not split into invalid paths. Strings that use a
Python/ROS-style list representation are parsed as lists.

:param string: string to be converted
:return: converted string
"""
return [s.strip(" '") for s in string.strip('[]').split(',')]
stripped_string = string.strip()
if stripped_string.startswith('[') and stripped_string.endswith(']'):
try:
parsed_string = ast.literal_eval(stripped_string)
except (SyntaxError, ValueError):
return [
s.strip(" '")
for s in stripped_string.strip('[]').split(',')
]
if isinstance(parsed_string, list):
return [str(value) for value in parsed_string]
return [string]


def recursively_sort_dict(obj):
Expand Down
19 changes: 19 additions & 0 deletions ros_typedb/test/test_typedb_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,28 @@
import pytest

from ros_typedb.typedb_interface import convert_py_type_to_query_type
from ros_typedb.typedb_interface import string_to_string_array
from ros_typedb.typedb_interface import TypeDBInterface


def test_string_to_string_array_preserves_raw_path_with_comma():
path = '/tmp/schema,with-comma.tql'

assert string_to_string_array(path) == [path]


def test_string_to_string_array_parses_quoted_list_with_comma_in_path():
assert string_to_string_array(
"['/tmp/schema,with-comma.tql', '/tmp/data.tql']"
) == ['/tmp/schema,with-comma.tql', '/tmp/data.tql']


def test_string_to_string_array_keeps_unbracketed_strings_as_single_values():
assert string_to_string_array('/tmp/schema.tql,/tmp/data.tql') == [
'/tmp/schema.tql,/tmp/data.tql'
]


@pytest.fixture
def typedb_interface():
typedb_interface = TypeDBInterface(
Expand Down
Loading