Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
## Note: ``\n`` will be transformed by ``universal_newlines`` mecanism for
## any platform
termination = "\0" if action.endswith("-0") else "\n"
if action == "get-value":
return str(dump(value))
elif action in ("get-values", "get-values-0"):
if isinstance(value, dict):
return "".join("".join((dump(k), termination,
dump(v), termination))
for k, v in value.items())
elif isinstance(value, list):
return "".join("".join((dump(l), termination))
for l in value)
else:
raise ActionTypeError(
action, provided=tvalue, expected=["sequence", "struct"])
elif action == "get-type":
return tvalue
elif action == "get-length":
if isinstance(value, (dict, list)):
return len(value)
else:
raise ActionTypeError(
action, provided=tvalue, expected=["sequence", "struct"])
elif action in ("keys", "keys-0",
"values", "values-0",
"key-values", "key-values-0"):
if isinstance(value, dict):
method = value.keys if action.startswith("keys") else \
value.items if action.startswith("key-values") else \
value.values
return "".join("".join((dump(k), termination,
dump(v), termination))
for k, v in value.items())
elif isinstance(value, list):
return "".join("".join((dump(l), termination))
for l in value)
else:
raise ActionTypeError(
action, provided=tvalue, expected=["sequence", "struct"])
elif action == "get-type":
return tvalue
elif action == "get-length":
if isinstance(value, (dict, list)):
return len(value)
else:
raise ActionTypeError(
action, provided=tvalue, expected=["sequence", "struct"])
elif action in ("keys", "keys-0",
"values", "values-0",
"key-values", "key-values-0"):
if isinstance(value, dict):
method = value.keys if action.startswith("keys") else \
value.items if action.startswith("key-values") else \
value.values
output = (lambda x: termination.join(str(dump(e)) for e in x)) \
if action.startswith("key-values") else \
dump
return "".join("".join((str(output(k)), termination)) for k in method())
else:
raise ActionTypeError(
action=action, provided=tvalue, expected=["struct"])
else:
else:
raise ActionTypeError(
action, provided=tvalue, expected=["sequence", "struct"])
elif action in ("keys", "keys-0",
"values", "values-0",
"key-values", "key-values-0"):
if isinstance(value, dict):
method = value.keys if action.startswith("keys") else \
value.items if action.startswith("key-values") else \
value.values
output = (lambda x: termination.join(str(dump(e)) for e in x)) \
if action.startswith("key-values") else \
dump
return "".join("".join((str(output(k)), termination)) for k in method())
else:
raise ActionTypeError(
action=action, provided=tvalue, expected=["struct"])
else:
raise InvalidAction(action)