143 lines
4.8 KiB
Python
143 lines
4.8 KiB
Python
import os
|
|
|
|
def resolveItem(action, item):
|
|
#resolver_log = os.environ.get("LOGS_RESOLVER", "false").lower() == "true"
|
|
resolver_log = True
|
|
|
|
resolved = {}
|
|
if hasattr(action, "resolve"):
|
|
for label, keys in action.resolve["get"].items():
|
|
target = item
|
|
try:
|
|
for key in keys:
|
|
if isinstance(target, dict):
|
|
target = target.get(key)
|
|
else:
|
|
target = target[key]
|
|
|
|
except (KeyError, IndexError) as error:
|
|
if resolver_log:
|
|
print(f"Resolver target key/index {error} not found during {action.name}")
|
|
target = None
|
|
|
|
resolved[label] = target
|
|
|
|
if "construct" in action.resolve:
|
|
for label2, piece in action.resolve["construct"].items():
|
|
constructed = ""
|
|
|
|
for item in piece:
|
|
if isinstance(item, dict):
|
|
for key, value in item.items():
|
|
item = resolved[value]
|
|
|
|
constructed += str(item)
|
|
|
|
resolved[label2] = constructed
|
|
|
|
if "transform" in action.resolve:
|
|
for source, mutation in action.resolve["transform"].items():
|
|
for key, value in mutation.items():
|
|
if resolved[source] == key:
|
|
resolved[source] = value
|
|
|
|
if "deconstruct" in action.resolve:
|
|
for source, mutation in action.resolve["deconstruct"].items():
|
|
deconstructed = str(resolved[source])
|
|
|
|
for item in mutation:
|
|
|
|
deconstructed = deconstructed.replace(str(item), "")
|
|
|
|
resolved[source] = deconstructed
|
|
|
|
return resolved
|
|
|
|
|
|
def resolveItem2(action, item):
|
|
resolved = {}
|
|
if hasattr(action, "resolve"):
|
|
for label, keys in action.resolve["get"].items():
|
|
target = item
|
|
|
|
for key in keys:
|
|
target = target[key]
|
|
|
|
resolved[label] = target
|
|
|
|
for mutation in action.resolve.get("mutate", []):
|
|
if "construct" in mutation:
|
|
for label2, piece in mutation["construct"].items():
|
|
constructed = ""
|
|
|
|
for part in piece:
|
|
if isinstance(part, dict):
|
|
for key, value in part.items():
|
|
part = resolved[value]
|
|
|
|
constructed += str(part)
|
|
|
|
resolved[label2] = constructed
|
|
elif "transform" in mutation:
|
|
for source, transformation in mutation["transform"].items():
|
|
for key, value in transformation.items():
|
|
if resolved[source] == key:
|
|
resolved[source] = value
|
|
|
|
elif "deconstruct" in mutation:
|
|
for source, change in mutation["deconstruct"].items():
|
|
deconstructed = str(resolved[source])
|
|
|
|
for removal in change:
|
|
|
|
deconstructed = deconstructed.replace(str(removal), "")
|
|
|
|
resolved[source] = deconstructed
|
|
|
|
return resolved
|
|
|
|
def extractItem(action, item, resolved):
|
|
#UNDER RUNTIME CONTRACT ITEM IS OUTPUTS
|
|
if hasattr(action, "extract"):
|
|
for label, keys in action.extract["get"].items():
|
|
#resolved[label] = {}
|
|
target = item
|
|
try:
|
|
for key in keys:
|
|
target = target[key]
|
|
|
|
except (KeyError, IndexError) as error:
|
|
print(f"Resolver target key/index {error} not found during {action.name}")
|
|
target = None
|
|
|
|
item[label] = target
|
|
|
|
if "construct" in action.extract:
|
|
for label2, piece in action.extract["construct"].items():
|
|
constructed = ""
|
|
|
|
for item in piece:
|
|
if isinstance(item, dict):
|
|
for key, value in item.items():
|
|
item = resolved[value]
|
|
|
|
constructed += str(item)
|
|
|
|
resolved[label2] = constructed
|
|
if "transform" in action.extract:
|
|
for source, mutation in action.extract["transform"].items():
|
|
for key, value in mutation.items():
|
|
if resolved[source] == key:
|
|
resolved[source] = value
|
|
|
|
if "deconstruct" in action.extract:
|
|
for source, mutation in action.extract["deconstruct"].items():
|
|
deconstructed = str(resolved[source])
|
|
|
|
for item in mutation:
|
|
|
|
deconstructed = deconstructed.replace(str(item), "")
|
|
|
|
resolved[source] = deconstructed
|
|
|
|
return |