Skip to content

Commit

Permalink
stateengine plugin: introduce internal methods to convert a string to…
Browse files Browse the repository at this point in the history
… list or dict
  • Loading branch information
onkelandy committed Oct 3, 2023
1 parent 66911c4 commit 8917f4f
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions stateengine/StateEngineTools.py
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,48 @@ def partition_strip(value, splitchar):
return part1.strip(), part2.strip()


# return list representation of string
# value: list as string
# returns: list or original value
def convert_str_to_list(value):
if isinstance(value, str) and ("," in value and value.startswith("[")):
value = value.strip("[]")
if isinstance(value, str) and "," in value:
try:
elements = re.findall(r"'([^']+)'|([^,]+)", value)
flattened_elements = [element[0] if element[0] else element[1] for element in elements]
formatted_str = "[" + ", ".join(
["'" + element.strip(" '\"") + "'" for element in flattened_elements]) + "]"
return literal_eval(formatted_str)
except Exception as ex:
raise ValueError("Problem converting string to list: {}".format(ex))
elif isinstance(value, list):
return value
else:
return [value]

# return dict representation of string
# value: OrderedDict as string
# returns: OrderedDict or original value
def convert_str_to_dict(value):
if isinstance(value, str) and value.startswith("["):
value = re.split('(, (?![^(]*\)))', value.strip(']['))
value = [s for s in value if s != ', ']
result = []
for s in value:
m = re.match(r'^OrderedDict\((.+)\)$', s)
if m:
result.append(dict(literal_eval(m.group(1))))
else:
result.append(literal_eval(s))
value = result
else:
return value
try:
return literal_eval(value)
except Exception as ex:
raise ValueError("Problem converting string to OrderedDict: {}".format(ex))

# return string representation of eval function
# eval_func: eval function
# returns: string representation
Expand Down

0 comments on commit 8917f4f

Please sign in to comment.