From 7159bf207c694c3279c8a6b03607c7144b05b644 Mon Sep 17 00:00:00 2001 From: frikky Date: Tue, 12 Oct 2021 18:41:47 +0200 Subject: [PATCH] Moved prints to logging in python --- backend/app_sdk/app_base.py | 786 ++++++++++++++++++------------------ 1 file changed, 399 insertions(+), 387 deletions(-) diff --git a/backend/app_sdk/app_base.py b/backend/app_sdk/app_base.py index 252885b2..ce954c25 100644 --- a/backend/app_sdk/app_base.py +++ b/backend/app_sdk/app_base.py @@ -52,7 +52,7 @@ class AppBase: self.action = json.loads(self.action) self.original_action = json.loads(self.action) except: - print("[WARNING] Failed parsing action as JSON") + self.logger.info("[WARNING] Failed parsing action as JSON") if len(self.base_url) == 0: self.base_url = self.url @@ -63,45 +63,48 @@ class AppBase: if action_result["status"] == "EXECUTING": action_result["status"] = "FAILURE" + self.logger.info(f"""[DEBUG] Inside Send result with status {action_result["status"]}""") + # FIXME: Add cleanup of parameters to not send to frontend here params = {} #action = action_result["action"] #try: # for item in action["authentication"]: # for action["parameters"] - # print("AUTH: ", key, value) + # self.logger.info("AUTH: ", key, value) # params[item["key"]] = item["value"] #except KeyError: - # print("No authentication specified!") + # self.logger.info("No authentication specified!") # pass # I wonder if this actually works - self.logger.info("Before last stream result") + self.logger.info(f"[DEBUG] Before last stream result") url = "%s%s" % (self.base_url, stream_path) - #print("[INFO] URL (URL): %s" % url) + #self.logger.info("[INFO] URL (URL): %s" % url) try: ret = requests.post(url, headers=headers, json=action_result) - self.logger.info("Result: %d" % ret.status_code) + self.logger.info(f"[DEBUG] Result: {res.status_code}") if ret.status_code != 200: - self.logger.info(ret.text) + self.logger.info(f"[DEBUG] Shuffle Response: {ret.text}") + + self.logger.info("[DEBUG] Stream result was sent!") except requests.exceptions.ConnectionError as e: - #self.logger.exception("ConnectionError: %s" % e) - self.logger.info("Expected ConnectionError happened") + self.logger.info(f"[DEBUG] Unexpected ConnectionError happened: {e}") return except TypeError as e: #self.logger.exception(e) action_result["status"] = "FAILURE" - action_result["result"] = "POST error: %s" % e - self.logger.info("Before typeerror stream result") + action_result["result"] = f"POST error: {e}" + self.logger.info(f"[DEBUG] Before typeerror stream result: {e}") ret = requests.post("%s%s" % (self.base_url, stream_path), headers=headers, json=action_result) - self.logger.info("Result: %d" % ret.status_code) + self.logger.info(f"[DEBUG] Result: {ret.status_code}") if ret.status_code != 200: self.logger.info(ret.text) except http.client.RemoteDisconnected as e: - self.logger.info("Expected Remotedisconnect happened") + self.logger.info(f"[DEBUG] Expected Remotedisconnect happened: {e}") return except urllib3.exceptions.ProtocolError as e: - self.logger.info("Expected ProtocolError happened") + self.logger.info(f"[DEBUG] Expected ProtocolError happened: {e}") return async def cartesian_product(self, L): @@ -112,19 +115,19 @@ class AppBase: # Handles unique fields by negoiating with the backend def validate_unique_fields(self, params): - #print("IN THE UNIQUE FIELDS PLACE!") + #self.logger.info("IN THE UNIQUE FIELDS PLACE!") newlist = [params] if isinstance(params, list): - #print("ITS A LIST!") + #self.logger.info("ITS A LIST!") newlist = params #self.full_execution = os.getenv("FULL_EXECUTION", "") - #print(len(params)) - #print(params.items()) - #print(list(params.items())) - #print(f"PARAM: {params}") - #print(f"NEWLIST: {newlist}") + #self.logger.info(len(params)) + #self.logger.info(params.items()) + #self.logger.info(list(params.items())) + #self.logger.info(f"PARAM: {params}") + #self.logger.info(f"NEWLIST: {newlist}") # FIXME: Also handle MULTI PARAM values = [] @@ -133,21 +136,21 @@ class AppBase: index = 0 for outerparam in newlist: - #print(f"INNERTYPE: {type(outerparam)}") - #print(f"HANDLING PARAM {key}") + #self.logger.info(f"INNERTYPE: {type(outerparam)}") + #self.logger.info(f"HANDLING PARAM {key}") param_value = "" for key, value in outerparam.items(): - #print("KEY: %s" % key) + #self.logger.info("KEY: %s" % key) #value = params[key] for param in self.action["parameters"]: try: if param["name"] == key and param["unique_toggled"]: - print(f"FOUND: {key} with param {param}!") + self.logger.info(f"[DEBUG] FOUND: {key} with param {param}!") if isinstance(value, dict) or isinstance(value, list): try: value = json.dumps(value) except json.decoder.JSONDecodeError as e: - print(f"Error in json decode for param {value}: {e}") + self.logger.info(f"[WARNING] Error in json decode for param {value}: {e}") continue elif isinstance(value, int) or isinstance(value, float): value = str(value) @@ -156,15 +159,15 @@ class AppBase: elif value == True: value = "True" - print(f"VALUE APPEND: {value}") + self.logger.info(f"[DEBUG] VALUE APPEND: {value}") param_value += value if param["name"] not in param_names: param_names.append(param["name"]) except (KeyError, NameError) as e: - print(f"""Key/NameError in param handler for {param["name"]}: {e}""") + self.logger.info(f"""Key/NameError in param handler for {param["name"]}: {e}""") - print(f"[DEBUG] OUTER VALUE: {param_value}") + self.logger.info(f"[DEBUG] OUTER VALUE: {param_value}") if len(param_value) > 0: md5 = hashlib.md5(param_value.encode('utf-8')).hexdigest() values.append(md5) @@ -201,12 +204,12 @@ class AppBase: }] } - #print(f"DATA: {data}") + #self.logger.info(f"DATA: {data}") # 1594869a676630b397bc34f7dc0951a3 - #print(f"VALUE URL: {url}") - #print(f"RET: {ret.text}") - #print(f"ID: {ret.status_code}") + #self.logger.info(f"VALUE URL: {url}") + #self.logger.info(f"RET: {ret.text}") + #self.logger.info(f"ID: {ret.status_code}") url = f"{self.url}/api/v1/orgs/{org_id}/validate_app_values" ret = requests.post(url, json=data) if ret.status_code == 200: @@ -214,27 +217,27 @@ class AppBase: if len(json_value["found"]) > 0: modifier = 0 for item in json_value["found"]: - print(f"Should remove {item}") + self.logger.info(f"Should remove {item}") try: - print(f"FOUND: {all_values[item]}") - print(f"SHOULD REMOVE INDEX: {all_values[item]['index']}") + self.logger.info(f"FOUND: {all_values[item]}") + self.logger.info(f"SHOULD REMOVE INDEX: {all_values[item]['index']}") try: newlist.pop(all_values[item]["index"]-modifier) modifier += 1 except IndexError as e: - print(f"Error popping value from array: {e}") + self.logger.info(f"Error popping value from array: {e}") except (NameError, KeyError) as e: - print(f"Failed removal: {e}") + self.logger.info(f"Failed removal: {e}") #return False else: - print("None of the items were found!") + self.logger.info("None of the items were found!") return newlist else: - print(f"[WARNING] Failed checking values with status code {ret.status_code}!") + self.logger.info(f"[WARNING] Failed checking values with status code {ret.status_code}!") #return True return newlist @@ -295,19 +298,19 @@ class AppBase: #self.action = action loopnames = [] - #print(f"Baseparams to check!!: {baseparams}") + #self.logger.info(f"Baseparams to check!!: {baseparams}") for key, value in baseparams.items(): check_value = "" for param in self.original_action["parameters"]: if param["name"] == key: - #print("PARAM: %s" % param) + #self.logger.info("PARAM: %s" % param) check_value = param["value"] # self.result_wrapper_count = 0 octothorpe_count = param["value"].count(".#") if octothorpe_count > self.result_wrapper_count: self.result_wrapper_count = octothorpe_count - print("[INFO] NEW OCTOTHORPE WRAPPER: %d" % octothorpe_count) + self.logger.info("[INFO] NEW OCTOTHORPE WRAPPER: %d" % octothorpe_count) # This whole thing is hard. @@ -328,13 +331,13 @@ class AppBase: # FIXME: Check the above, and fix so that nested looped items can be # Skipped if wanted - #print("\nCHECK: %s" % check_value) + #self.logger.info("\nCHECK: %s" % check_value) #try: # values = parameter["value_replace"] # if values != None: - # print(values) + # self.logger.info(values) # for val in values: - # print(val) + # self.logger.info(val) #except: # pass @@ -343,15 +346,15 @@ class AppBase: should_merge = True # Specific for OpenAPI body replacement - #print("\n\n\nDOING STUFF BELOW HERE") + #self.logger.info("\n\n\nDOING STUFF BELOW HERE") if not should_merge: for parameter in self.original_action["parameters"]: if parameter["name"] == key: - #print("CHECKING BODY FOR VALUE REPLACE DATA!") + #self.logger.info("CHECKING BODY FOR VALUE REPLACE DATA!") try: values = parameter["value_replace"] if values != None: - print(values) + self.logger.info(values) for val in values: if "#" in val["value"]: should_merge = True @@ -359,9 +362,9 @@ class AppBase: except: pass - #print(f"MERGE: {should_merge}") + #self.logger.info(f"MERGE: {should_merge}") if isinstance(value, list): - print(f"[DEBUG] Item {value} is a list.") + self.logger.info(f"[DEBUG] Item {value} is a list.") if len(value) <= 1: if len(value) == 1: baseparams[key] = value[0] @@ -370,7 +373,7 @@ class AppBase: # should_merge = True else: if not should_merge: - print("[DEBUG] Adding WITHOUT looping list") + self.logger.info("[DEBUG] Adding WITHOUT looping list") else: if len(value) not in listlengths: listlengths.append(len(value)) @@ -384,41 +387,41 @@ class AppBase: all_list_keys.append(key) all_lists.append(baseparams[key]) else: - #print(f"{value} is not a list") + #self.logger.info(f"{value} is not a list") pass - print("[DEBUG] Listlengths: %s" % listlengths) + self.logger.info("[DEBUG] Listlengths: %s" % listlengths) if len(listlengths) == 0: - print("[DEBUG] NO multiplier. Running a single iteration.") + self.logger.info("[DEBUG] NO multiplier. Running a single iteration.") paramlist.append(baseparams) elif len(listlengths) == 1: - print("[DEBUG] NO MULTIPLIER NECESSARY. Length is %d" % len(listitems)) + self.logger.info("[DEBUG] NO MULTIPLIER NECESSARY. Length is %d" % len(listitems)) for item in listitems: # This loops should always be length 1 for key, value in item.items(): if isinstance(value, int): - print("\n[DEBUG] Should run key %s %d times from %s" % (key, value, baseparams[key])) + self.logger.info("\n[DEBUG] Should run key %s %d times from %s" % (key, value, baseparams[key])) if len(paramlist) == value: - print("[DEBUG] List ALREADY exists - just changing values") + self.logger.info("[DEBUG] List ALREADY exists - just changing values") for subloop in range(value): baseitem = copy.deepcopy(baseparams) paramlist[subloop][key] = baseparams[key][subloop] else: - print("[DEBUG] List DOESNT exist - ADDING values") + self.logger.info("[DEBUG] List DOESNT exist - ADDING values") for subloop in range(value): baseitem = copy.deepcopy(baseparams) baseitem[key] = baseparams[key][subloop] paramlist.append(baseitem) else: - print("[DEBUG] Multipliers to handle: %s" % listitems) + self.logger.info("[DEBUG] Multipliers to handle: %s" % listitems) newlength = 1 for item in listitems: for key, value in item.items(): newlength = newlength * value - print("[DEBUG] Newlength of array: %d. Lists: %s" % (newlength, all_lists)) + self.logger.info("[DEBUG] Newlength of array: %d. Lists: %s" % (newlength, all_lists)) # Get the cartesian product of the arrays cartesian = await self.cartesian_product(all_lists) newlist = [] @@ -433,7 +436,7 @@ class AppBase: paramlist.append(baseitem) - #print("PARAMLIST: %s" % paramlist) + #self.logger.info("PARAMLIST: %s" % paramlist) #newlist[subitem[0]] #if len(newlist) > 0: @@ -441,22 +444,22 @@ class AppBase: # How do we get it back, ordered? #for item in cartesian: - #print("Listlengths: %s" % listlengths) + #self.logger.info("Listlengths: %s" % listlengths) #paramlist = [baseparams] - #print("[INFO] Return paramlist: %s" % paramlist) + #self.logger.info("[INFO] Return paramlist: %s" % paramlist) return paramlist # Runs recursed versions with inner loops and such async def run_recursed_items(self, func, baseparams, loop_wrapper): - #print(f"RECURSED ITEMS: {baseparams}") + #self.logger.info(f"RECURSED ITEMS: {baseparams}") has_loop = False newparams = {} for key, value in baseparams.items(): if isinstance(value, list) and len(value) > 0: - print(f"[DEBUG] In list check for {key}") + self.logger.info(f"[DEBUG] In list check for {key}") try: # Added skip for body (OpenAPI) which uses data= in requests @@ -464,36 +467,36 @@ class AppBase: if key != "body": value[0] = json.loads(value[0]) except json.decoder.JSONDecodeError as e: - print("[WARNING] JSON casting error (1): %s" % e) + self.logger.info("[WARNING] JSON casting error (1): %s" % e) except TypeError as e: - print("[WARNING] TypeError: %s" % e) + self.logger.info("[WARNING] TypeError: %s" % e) - print("[DEBUG] POST initial list check") + self.logger.info("[DEBUG] POST initial list check") try: if isinstance(value, list) and len(value) == 1 and isinstance(value[0], list): try: loop_wrapper[key] += 1 except Exception as e: - print("[WARNING] Exception in loop wrapper: {e}") + self.logger.info("[WARNING] Exception in loop wrapper: {e}") loop_wrapper[key] = 1 - print(f"[DEBUG] Key {key} is a list: {value}") + self.logger.info(f"[DEBUG] Key {key} is a list: {value}") newparams[key] = value[0] has_loop = True else: - #print(f"Key {key} is NOT a list within a list. Value: {value}") + #self.logger.info(f"Key {key} is NOT a list within a list. Value: {value}") newparams[key] = value except Exception as e: - print(f"[WARNING] Error in baseparams list: {e}") + self.logger.info(f"[WARNING] Error in baseparams list: {e}") newparams[key] = value results = [] if has_loop: - print(f"[DEBUG] Should run inner loop: {newparams}") + self.logger.info(f"[DEBUG] Should run inner loop: {newparams}") ret = await self.run_recursed_items(func, newparams, loop_wrapper) else: - print(f"[DEBUG] Should run multiplier check with params (inner): {newparams}") + self.logger.info(f"[DEBUG] Should run multiplier check with params (inner): {newparams}") # 1. Find the loops that are required and create new multipliers # If here: check for multipliers within this scope. ret = [] @@ -501,9 +504,9 @@ class AppBase: # FIXME: This does a deduplication of the data new_params = self.validate_unique_fields(param_multiplier) - #print(f"NEW PARAMS: {new_params}") + #self.logger.info(f"NEW PARAMS: {new_params}") if len(new_params) == 0: - print("[WARNING] SHOULD STOP MULTI-EXECUTION BECAUSE FIELDS AREN'T UNIQUE") + self.logger.info("[WARNING] SHOULD STOP MULTI-EXECUTION BECAUSE FIELDS AREN'T UNIQUE") self.action_result = { "action": self.action, "authorization": self.authorization, @@ -519,24 +522,24 @@ class AppBase: #return else: #subparams = new_params - #print(f"NEW PARAMS: {new_params}") + #self.logger.info(f"NEW PARAMS: {new_params}") param_multiplier = new_params - #print("Returned with newparams of length %d", len(new_params)) + #self.logger.info("Returned with newparams of length %d", len(new_params)) #if isinstance(new_params, list) and len(new_params) == 1: # params = new_params[0] #else: - # print("[WARNING] SHOULD STOP EXECUTION BECAUSE FIELDS AREN'T UNIQUE") + # self.logger.info("[WARNING] SHOULD STOP EXECUTION BECAUSE FIELDS AREN'T UNIQUE") # action_result["status"] = "SKIPPED" # action_result["result"] = f"A non-unique value was found" # action_result["completed_at"] = int(time.time()) # self.send_result(action_result, headers, stream_path) # return - print("[INFO] Multiplier length: %d" % len(param_multiplier)) + self.logger.info("[INFO] Multiplier length: %d" % len(param_multiplier)) #tmp = "" for subparams in param_multiplier: - #print(f"SUBPARAMS IN MULTI: {subparams}") + #self.logger.info(f"SUBPARAMS IN MULTI: {subparams}") try: #tmp = await func(**subparams) @@ -545,7 +548,7 @@ class AppBase: tmp = await func(**subparams) break except TypeError as e: - print("BASE TYPEERROR: %s" % e) + self.logger.info("BASE TYPEERROR: %s" % e) errorstring = "%s" % e if "got an unexpected keyword argument" in errorstring: fieldsplit = errorstring.split("'") @@ -554,7 +557,7 @@ class AppBase: try: del subparams[field] - print("Removed field invalid field %s" % field) + self.logger.info("Removed field invalid field %s" % field) except KeyError: break else: @@ -565,12 +568,12 @@ class AppBase: try: e = sys.exc_info()[1] except: - print("Exc check fail: %s" % e) + self.logger.info("Exc check fail: %s" % e) pass tmp = "An error occured during execution: %s" % e - #print("RET from execution: %s" % ret) + #self.logger.info("RET from execution: %s" % ret) new_value = tmp if tmp == None: new_value = "" @@ -589,7 +592,7 @@ class AppBase: pass except: pass - #print("Json: %s" % e) + #self.logger.info("Json: %s" % e) #ret.append(tmp) #if self.result_wrapper_count > 0: @@ -597,12 +600,12 @@ class AppBase: #else: ret.append(new_value) - print("[INFO] Ret length: %d" % len(ret)) + self.logger.info("[INFO] Ret length: %d" % len(ret)) if len(ret) == 1: #ret = ret[0] - print("[DEBUG] DONT make list of 1 into 0!!") + self.logger.info("[DEBUG] DONT make list of 1 into 0!!") - print("Return from execution: %s" % ret) + self.logger.info("Return from execution: %s" % ret) if ret == None: results.append("") json_object = False @@ -619,7 +622,7 @@ class AppBase: results.append(json.loads(ret)) json_object = True except json.decoder.JSONDecodeError as e: - #print("Json: %s" % e) + #self.logger.info("Json: %s" % e) results.append(ret) except TypeError as e: results.append(ret) @@ -628,9 +631,9 @@ class AppBase: if len(results) == 1: #results = results[0] - print("DONT MAKE LIST FROM 1 TO 0!!") + self.logger.info("DONT MAKE LIST FROM 1 TO 0!!") - print("\nLOOP: %s\nRESULTS: %s" % (loop_wrapper, results)) + self.logger.info("\nLOOP: %s\nRESULTS: %s" % (loop_wrapper, results)) return results # Downloads all files from a namespace @@ -658,10 +661,10 @@ class AppBase: full_execution = self.full_execution org_id = full_execution["workflow"]["execution_org"]["id"] - print("SHOULD GET FILES BASED ON ORG %s, workflow %s and value(s) %s" % (org_id, full_execution["workflow"]["id"], value)) + self.logger.info("SHOULD GET FILES BASED ON ORG %s, workflow %s and value(s) %s" % (org_id, full_execution["workflow"]["id"], value)) if isinstance(value, list): - print("IS LIST!") + self.logger.info("IS LIST!") #if len(value) == 1: # value = value[0] else: @@ -669,9 +672,9 @@ class AppBase: returns = [] for item in value: - print("VALUE: %s" % item) + self.logger.info("VALUE: %s" % item) if len(item) != 36: - print("Bad length for file value %s" % item) + self.logger.info("Bad length for file value %s" % item) continue #return { # "filename": "", @@ -686,7 +689,7 @@ class AppBase: } ret1 = requests.get("%s%s" % (self.url, get_path), headers=headers) - print("RET1 (file get): %s" % ret1.text) + self.logger.info("RET1 (file get): %s" % ret1.text) if ret1.status_code != 200: returns.append({ "filename": "", @@ -697,7 +700,7 @@ class AppBase: content_path = "/api/v1/files/%s/content?execution_id=%s" % (item, full_execution["execution_id"]) ret2 = requests.get("%s%s" % (self.url, content_path), headers=headers) - print("RET2 (file get) done") + self.logger.info("RET2 (file get) done") if ret2.status_code == 200: tmpdata = ret1.json() returndata = { @@ -707,7 +710,7 @@ class AppBase: } returns.append(returndata) - print("RET3 (file get done)") + self.logger.info("RET3 (file get done)") if len(returns) == 0: return { @@ -739,7 +742,7 @@ class AppBase: allvalues["value"] = str(value) return allvalues except: - print("Value couldn't be parsed") + self.logger.info("Value couldn't be parsed") #return response.json() return {"success": False} @@ -757,19 +760,19 @@ class AppBase: value = requests.post(url, json=data) try: allvalues = value.json() - print("VAL1: ", allvalues) + self.logger.info("VAL1: ", allvalues) allvalues["key"] = key - print("VAL2: ", allvalues) + self.logger.info("VAL2: ", allvalues) try: parsedvalue = json.loads(allvalues["value"]) allvalues["value"] = parsedvalue except: - print("Parsing of value as JSON failed. Continue anyway!") + self.logger.info("Parsing of value as JSON failed. Continue anyway!") return allvalues except: - print("Value couldn't be parsed, or json dump of value failed") + self.logger.info("Value couldn't be parsed, or json dump of value failed") #return value.json() return {"success": False} @@ -803,28 +806,28 @@ class AppBase: data["filename"] = curfile["filename"] filename = curfile["filename"] except KeyError as e: - print(f"KeyError in file setup: {e}") + self.logger.info(f"KeyError in file setup: {e}") pass ret = requests.post("%s%s" % (self.url, create_path), headers=headers, json=data) - print(f"Ret CREATE: {ret.text}") + self.logger.info(f"Ret CREATE: {ret.text}") cur_id = "" if ret.status_code == 200: - print("RET: %s" % ret.text) + self.logger.info("RET: %s" % ret.text) ret_json = ret.json() if not ret_json["success"]: - print("Not success in file upload creation.") + self.logger.info("Not success in file upload creation.") continue - print("Should handle ID %s" % ret_json["id"]) + self.logger.info("Should handle ID %s" % ret_json["id"]) file_ids.append(ret_json["id"]) cur_id = ret_json["id"] else: - print("Bad status code: %d" % ret.status_code) + self.logger.info("Bad status code: %d" % ret.status_code) continue if len(cur_id) == 0: - print("No file ID specified from backend") + self.logger.info("No file ID specified from backend") continue new_headers = { @@ -832,16 +835,16 @@ class AppBase: } upload_path = "/api/v1/files/%s/upload?execution_id=%s" % (cur_id, full_execution["execution_id"]) - print("Create path: %s" % create_path) + self.logger.info("Create path: %s" % create_path) files={"shuffle_file": (filename, curfile["data"])} #open(filename,'rb')} ret = requests.post("%s%s" % (self.url, upload_path), files=files, headers=new_headers) - print("Ret UPLOAD: %s" % ret.text) - print("Ret2 UPLOAD: %d" % ret.status_code) + self.logger.info("Ret UPLOAD: %s" % ret.text) + self.logger.info("Ret2 UPLOAD: %d" % ret.status_code) - print("IDS TO RETURN: %s" % file_ids) + self.logger.info("IDS TO RETURN: %s" % file_ids) return file_ids async def execute_action(self, action): @@ -875,19 +878,19 @@ class AppBase: } if len(self.action) == 0: - print("ACTION env not defined") + self.logger.info("ACTION env not defined") self.action_result["result"] = "Error in setup ENV: ACTION not defined" self.send_result(self.action_result, headers, stream_path) return if len(self.authorization) == 0: - print("AUTHORIZATION env not defined") + self.logger.info("AUTHORIZATION env not defined") self.action_result["result"] = "Error in setup ENV: AUTHORIZATION not defined" self.send_result(self.action_result, headers, stream_path) return if len(self.current_execution_id) == 0: - print("EXECUTIONID env not defined") + self.logger.info("EXECUTIONID env not defined") self.action_result["result"] = "Error in setup ENV: EXECUTIONID not defined" self.send_result(self.action_result, headers, stream_path) return @@ -904,7 +907,7 @@ class AppBase: # if ret.status_code != 200: # self.logger.info(ret.text) #except requests.exceptions.ConnectionError as e: - # print("Connectionerror: %s" % e) + # self.logger.info("Connectionerror: %s" % e) # action_result["result"] = "Bad setup during startup: %s" % e # self.send_result(action_result, headers, stream_path) @@ -914,7 +917,7 @@ class AppBase: # If found, we get the full results list from backend fullexecution = {} if len(self.full_execution) == 0: - print("[DEBUG] NO EXECUTION - LOADING!") + self.logger.info("[DEBUG] NO EXECUTION - LOADING!") try: tmpdata = { "authorization": self.authorization, @@ -949,12 +952,12 @@ class AppBase: try: fullexecution = json.loads(self.full_execution) except json.decoder.JSONDecodeError as e: - print("[WARNING] Json decode execution error: %s" % e) + self.logger.info("[WARNING] Json decode execution error: %s" % e) self.action_result["result"] = "Json error during startup: %s" % e self.send_result(self.action_result, headers, stream_path) return - print("") + self.logger.info("") self.full_execution = fullexecution @@ -1016,7 +1019,7 @@ class AppBase: try: return int(data) except ValueError: - print("[DEBUG] ValueError while casting %s to int" % data) + self.logger.info("[DEBUG] ValueError while casting %s to int" % data) return data if "lower" in thistype: @@ -1030,7 +1033,7 @@ class AppBase: if "split" in thistype: return data.split() if "replace" in thistype: - print("Running replace!") + self.logger.info("Running replace!") splitvalues = data.split(",") if len(splitvalues) > 2: @@ -1058,47 +1061,47 @@ class AppBase: else: return f"replace({data})" if "join" in thistype: - print(f"SHOULD JOIN: {data}") + self.logger.info(f"SHOULD JOIN: {data}") try: splitvalues = data.split(",") if "," not in data: return f"join({data})" if len(splitvalues) >= 2: - print(f"SPLITVALUE: {splitvalues[-1]}") + self.logger.info(f"SPLITVALUE: {splitvalues[-1]}") # 1. Take the list and parse it from string # 2. Take all the items and join them # 3. Parse them back as string and return values = ",".join(splitvalues[0:-1]) - print(f"VALUES: {values}") + self.logger.info(f"VALUES: {values}") tmp = json.loads(values) - print(f"TMP: {tmp}") + self.logger.info(f"TMP: {tmp}") #tmp = tmp[1:-1] - #print(f"TMP2: {tmp}") + #self.logger.info(f"TMP2: {tmp}") try: newvalues = splitvalues[-1].join(str(item).strip() for item in tmp) except TypeError: newvalues = splitvalues[-1].join(json.dumps(item).strip() for item in tmp) - print(f"new: {newvalues}") + self.logger.info(f"new: {newvalues}") return newvalues else: - print("Returning default") + self.logger.info("Returning default") return f"join({data})" except (KeyError, IndexError) as e: - print(f"ERROR in join(): {e}") + self.logger.info(f"ERROR in join(): {e}") except json.decoder.JSONDecodeError as e: - print(f"JSON ERROR in join(): {e}") + self.logger.info(f"JSON ERROR in join(): {e}") if "len" in thistype or "length" in thistype or "lenght" in thistype: - print(f"Trying to length-parse: {data}") + self.logger.info(f"Trying to length-parse: {data}") try: tmp_len = json.loads(data, parse_float=str, parse_int=str, parse_constant=str) except (NameError, KeyError, TypeError, json.decoder.JSONDecodeError) as e: try: - print(f"[WARNING] INITIAL Parsing bug for length in app sdk: {e}") + self.logger.info(f"[WARNING] INITIAL Parsing bug for length in app sdk: {e}") # data = data.replace("\'", "\"") data = data.replace("True", "true") data = data.replace("False", "false") @@ -1139,7 +1142,7 @@ class AppBase: else: tmp = json.loads(parsedlist)[lastsplit[0]] - #print(tmp) + #self.logger.info(tmp) return tmp except IndexError as e: return default_error @@ -1162,8 +1165,8 @@ class AppBase: inner_value = parse_nested_param(data, maxDepth(data) - 0) outer_value = parse_nested_param(data, maxDepth(data) - 1) - #print("[DEBUG] INNER: ", inner_value) - #print("[DEBUG] OUTER: ", outer_value) + #self.logger.info("[DEBUG] INNER: ", inner_value) + #self.logger.info("[DEBUG] OUTER: ", outer_value) wrapper_group = "|".join(wrappers) parse_string = data @@ -1194,7 +1197,7 @@ class AppBase: c_parentheses = parse_nested_param(parse_string, 0)[0] match_string = re.escape(c_parentheses) custom_casting = re.findall(fr"({wrapper_group})\({match_string}", parse_string) - print("In ELSE: %s" % custom_casting) + self.logger.info("In ELSE: %s" % custom_casting) # check if a wrapper was found if len(custom_casting) != 0: inner_result = parse_type(c_parentheses, custom_casting[0]) @@ -1206,7 +1209,7 @@ class AppBase: else: parse_string = inner_result - print("PARSE STRING: %s" % parse_string) + self.logger.info("PARSE STRING: %s" % parse_string) return parse_string, True # Looks for parantheses to grab special cases within a string, e.g: @@ -1228,7 +1231,7 @@ class AppBase: if isinstance(data, str) and len(data) > 4: if (data[0] == "{" or data[0] == "[") and (data[len(data)-1] == "]" or data[len(data)-1] == "}"): - print("Skipping parser because use of {[ and ]}") + self.logger.info("Skipping parser because use of {[ and ]}") return data newdata = [] @@ -1284,12 +1287,12 @@ class AppBase: return data if len(parsedlist) > 0 and not non_string: - #print("Returning parsed list: ", parsedlist) + #self.logger.info("Returning parsed list: ", parsedlist) return " ".join(parsedlist) elif len(parsedlist) == 1 and non_string: return parsedlist[0] else: - #print("Casting back to string because multi: ", parsedlist) + #self.logger.info("Casting back to string because multi: ", parsedlist) newlist = [] for item in parsedlist: try: @@ -1305,7 +1308,7 @@ class AppBase: # $nodename.data.#min-max.info.id def recurse_json(basejson, parsersplit): match = "#([0-9a-z]+):?-?([0-9a-z]+)?#?" - #print("Split: %s\n%s" % (parsersplit, basejson)) + #self.logger.info("Split: %s\n%s" % (parsersplit, basejson)) try: outercnt = 0 @@ -1314,9 +1317,9 @@ class AppBase: #if " " in value: # value = value.replace(" ", "_", -1) - #print("VALUE: %s\n" % value) + #self.logger.info("VALUE: %s\n" % value) actualitem = re.findall(match, value, re.MULTILINE) - #print("ACTUAL RECURSE: (%s) %s" % (value, actualitem)) + #self.logger.info("ACTUAL RECURSE: (%s) %s" % (value, actualitem)) if value == "#": newvalue = [] for innervalue in basejson: @@ -1336,17 +1339,17 @@ class AppBase: return newvalue, True elif len(actualitem) > 0: - #print("[INFO] In recursion v2: ", actualitem) + #self.logger.info("[INFO] In recursion v2: ", actualitem) is_loop = True newvalue = [] firstitem = actualitem[0][0] seconditem = actualitem[0][1] - print("[DEBUG] ACTUAL PARSED: %s" % actualitem) + self.logger.info("[DEBUG] ACTUAL PARSED: %s" % actualitem) # Means it's a single item -> continue if seconditem == "": - print("[INFO] In first - handling %s. Len: %d" % (firstitem, len(basejson))) + self.logger.info("[INFO] In first - handling %s. Len: %d" % (firstitem, len(basejson))) if firstitem.lower() == "max" or firstitem.lower() == "last": firstitem = len(basejson)-1 elif firstitem.lower() == "min" or firstitem.lower() == "first": @@ -1354,14 +1357,14 @@ class AppBase: else: firstitem = int(firstitem) - print(f"[DEBUG] Post lower checks with item {firstitem}") + self.logger.info(f"[DEBUG] Post lower checks with item {firstitem}") tmpitem = basejson[int(firstitem)] try: newvalue, is_loop = recurse_json(tmpitem, parsersplit[outercnt+1:]) except IndexError: newvalue, is_loop = (tmpitem, parsersplit[outercnt+1:]) else: - print("[INFO] In ELSE - handling %s and %s" % (firstitem, seconditem)) + self.logger.info("[INFO] In ELSE - handling %s and %s" % (firstitem, seconditem)) if firstitem.lower() == "max" or firstitem.lower() == "last": firstitem = len(basejson)-1 elif firstitem.lower() == "min" or firstitem.lower() == "first": @@ -1376,7 +1379,7 @@ class AppBase: else: seconditem = int(seconditem) - print(f"[DEBUG] Post lower checks 2: {firstitem} AND {seconditem}") + self.logger.info(f"[DEBUG] Post lower checks 2: {firstitem} AND {seconditem}") newvalue = [] if int(seconditem) > len(basejson): seconditem = len(basejson) @@ -1384,68 +1387,68 @@ class AppBase: for i in range(int(firstitem), int(seconditem)+1): # 1. Check the next item (message) # 2. Call this function again - #print("Base: %s" % basejson[i]) + #self.logger.info("Base: %s" % basejson[i]) try: ret, tmp_loop = recurse_json(basejson[i], parsersplit[outercnt+1:]) except IndexError: - print("[DEBUG] INDEXERROR: ", parsersplit[outercnt]) + self.logger.info("[DEBUG] INDEXERROR: ", parsersplit[outercnt]) #ret = innervalue ret, tmp_loop = recurse_json(innervalue, parsersplit[outercnt:]) - #print("IN LIST: %s" % ret) + #self.logger.info("IN LIST: %s" % ret) #exit() newvalue.append(ret) - #print("Returning %s" % newvalue) + #self.logger.info("Returning %s" % newvalue) return newvalue, is_loop else: - #print("BEFORE NORMAL VALUE: ", basejson, value) + #self.logger.info("BEFORE NORMAL VALUE: ", basejson, value) if len(value) == 0: return basejson, False try: if isinstance(basejson, list): - print("[WARNING] VALUE IN ISINSTANCE IS NOT TO BE USED (list): %s" % value) + self.logger.info("[WARNING] VALUE IN ISINSTANCE IS NOT TO BE USED (list): %s" % value) return basejson, False elif isinstance(basejson[value], str): - #print(f"[INFO] LOADING STRING '%s' AS JSON" % basejson[value]) + #self.logger.info(f"[INFO] LOADING STRING '%s' AS JSON" % basejson[value]) try: basejson = json.loads(basejson[value]) - #print("[DEBUG] BASEJSON: %s" % basejson) + #self.logger.info("[DEBUG] BASEJSON: %s" % basejson) except json.decoder.JSONDecodeError as e: - #print("[DEBUG] RETURNING BECAUSE '%s' IS A NORMAL STRING (0)" % basejson[value]) + #self.logger.info("[DEBUG] RETURNING BECAUSE '%s' IS A NORMAL STRING (0)" % basejson[value]) return basejson[value], False else: basejson = basejson[value] except KeyError as e: - print("[WARNING] Running secondary value check with replacement of underscore in %s: %s" % (value, e)) + self.logger.info("[WARNING] Running secondary value check with replacement of underscore in %s: %s" % (value, e)) if "_" in value: value = value.replace("_", " ", -1) elif " " in value: value = value.replace(" ", "_", -1) if isinstance(basejson, list): - print("[WARNING] VALUE IN ISINSTANCE IS NOT TO BE USED (list): %s" % value) + self.logger.info("[WARNING] VALUE IN ISINSTANCE IS NOT TO BE USED (list): %s" % value) return basejson, False elif isinstance(basejson[value], str): - print(f"[INFO] LOADING STRING '%s' AS JSON" % basejson[value]) + self.logger.info(f"[INFO] LOADING STRING '%s' AS JSON" % basejson[value]) try: basejson = json.loads(basejson[value]) - print("[DEBUG] BASEJSON: %s" % basejson) + self.logger.info("[DEBUG] BASEJSON: %s" % basejson) except json.decoder.JSONDecodeError as e: - print("[DEBUG] RETURNING BECAUSE '%s' IS A NORMAL STRING (1)" % basejson[value]) + self.logger.info("[DEBUG] RETURNING BECAUSE '%s' IS A NORMAL STRING (1)" % basejson[value]) return basejson[value], False else: basejson = basejson[value] - #print("Parsed BASEJSON: %s" % basejson) + #self.logger.info("Parsed BASEJSON: %s" % basejson) outercnt += 1 except KeyError as e: - print("[INFO] Lower keyerror: %s" % e) + self.logger.info("[INFO] Lower keyerror: %s" % e) return "", False #return basejson @@ -1460,13 +1463,13 @@ class AppBase: actionname_lower = parsersplit[0][1:].lower() #Actionname: Start_node - print(f"\n[INFO] Actionname: {actionname_lower}") + self.logger.info(f"\n[INFO] Actionname: {actionname_lower}") # 1. Find the action baseresult = "" appendresult = "" - print("[INFO] Parsersplit length: %d" % len(parsersplit)) + self.logger.info("[INFO] Parsersplit length: %d" % len(parsersplit)) if (actionname_lower.startswith("exec ") or actionname_lower.startswith("webhook ") or actionname_lower.startswith("schedule ") or actionname_lower.startswith("userinput ") or actionname_lower.startswith("email_trigger ") or actionname_lower.startswith("trigger ")) and len(parsersplit) == 1: record = False for char in actionname_lower: @@ -1486,21 +1489,21 @@ class AppBase: if actionname_lower == "exec" or actionname_lower == "webhook" or actionname_lower == "schedule" or actionname_lower == "userinput" or actionname_lower == "email_trigger" or actionname_lower == "trigger": baseresult = execution_data["execution_argument"] elif actionname_lower == "shuffle_cache": - print("SHOULD GET CACHE KEY: %s" % parsersplit) + self.logger.info("SHOULD GET CACHE KEY: %s" % parsersplit) if len(parsersplit) > 1: actual_key = parsersplit[1] - print("KEY: %s" % actual_key) + self.logger.info("KEY: %s" % actual_key) cachedata = self.get_cache(actual_key) - print("CACHE: %s" % cachedata) + self.logger.info("CACHE: %s" % cachedata) parsersplit.pop(1) try: baseresult = json.dumps(cachedata) except json.decoder.JSONDecodeError as e: - print("Failed json dumping: %s" % e) + self.logger.info("Failed json dumping: %s" % e) #returndata = str(baseresult)+str(appendresult) else: - #print("Within execution data check. Execution data: %s", execution_data["results"]) + #self.logger.info("Within execution data check. Execution data: %s", execution_data["results"]) if execution_data["results"] != None: for result in execution_data["results"]: resultlabel = result["action"]["label"].replace(" ", "_", -1).lower() @@ -1508,13 +1511,13 @@ class AppBase: baseresult = result["result"] break else: - print("No results to get values from.") + self.logger.info("No results to get values from.") baseresult = "$" + parsersplit[0][1:] - print("BEFORE VARIABLES!") + self.logger.info("BEFORE VARIABLES!") if len(baseresult) == 0: try: - #print("WF Variables: %s" % execution_data["workflow"]["workflow_variables"]) + #self.logger.info("WF Variables: %s" % execution_data["workflow"]["workflow_variables"]) for variable in execution_data["workflow"]["workflow_variables"]: variablename = variable["name"].replace(" ", "_", -1).lower() @@ -1523,47 +1526,47 @@ class AppBase: break except KeyError as e: - print("[INFO] KeyError wf variables: %s" % e) + self.logger.info("[INFO] KeyError wf variables: %s" % e) pass except TypeError as e: - print("[INFO] TypeError wf variables: %s" % e) + self.logger.info("[INFO] TypeError wf variables: %s" % e) pass - print("BEFORE EXECUTION VAR") + self.logger.info("BEFORE EXECUTION VAR") if len(baseresult) == 0: try: - #print("Execution Variables: %s" % execution_data["execution_variables"]) + #self.logger.info("Execution Variables: %s" % execution_data["execution_variables"]) for variable in execution_data["execution_variables"]: variablename = variable["name"].replace(" ", "_", -1).lower() if variablename.lower() == actionname_lower: baseresult = variable["value"] break except KeyError as e: - print("[INFO] KeyError exec variables: %s" % e) + self.logger.info("[INFO] KeyError exec variables: %s" % e) pass except TypeError as e: - print("[INFO] TypeError exec variables: %s" % e) + self.logger.info("[INFO] TypeError exec variables: %s" % e) pass except KeyError as error: - print(f"[DEBUG] KeyError in JSON: {error}") + self.logger.info(f"[DEBUG] KeyError in JSON: {error}") - print(f"[INFO] After first trycatch. Baseresult")#, baseresult) + self.logger.info(f"[INFO] After first trycatch. Baseresult")#, baseresult) # 2. Find the JSON data if len(baseresult) == 0: return ""+appendresult, False - print("[INFO] After second return") + self.logger.info("[INFO] After second return") if len(parsersplit) == 1: returndata = str(baseresult)+str(appendresult) - print("RETURNING!")#: %s" % returndata) + self.logger.info("RETURNING!")#: %s" % returndata) return returndata, False baseresult = baseresult.replace(" True,", " true,") baseresult = baseresult.replace(" False", " false,") - print("[INFO] After third parser return - Formatted")#, baseresult) + self.logger.info("[INFO] After third parser return - Formatted")#, baseresult) basejson = {} try: basejson = json.loads(baseresult) @@ -1572,10 +1575,10 @@ class AppBase: baseresult = baseresult.replace("\'", "\"") basejson = json.loads(baseresult) except json.decoder.JSONDecodeError as e: - print("Parser issue with JSON: %s" % e) + self.logger.info("Parser issue with JSON: %s" % e) return str(baseresult)+str(appendresult), False - print("[INFO] After fourth parser return as JSON") + self.logger.info("[INFO] After fourth parser return as JSON") data, is_loop = recurse_json(basejson, parsersplit[1:]) parseditem = data @@ -1583,52 +1586,52 @@ class AppBase: try: parseditem = json.dumps(parseditem) except json.decoder.JSONDecodeError as e: - print("Parseditem issue: %s" % e) + self.logger.info("Parseditem issue: %s" % e) pass - print("DATA: (%s) %s" % (type(data), data)) + self.logger.info("DATA: (%s) %s" % (type(data), data)) if is_loop: - print("DATA IS A LOOP - SHOULD WRAP") + self.logger.info("DATA IS A LOOP - SHOULD WRAP") if parsersplit[-1] == "#": - print("SET DATA WRAPPER TO NORMAL!") + self.logger.info("SET DATA WRAPPER TO NORMAL!") parseditem = "${SHUFFLE_NO_SPLITTER%s}$" % json.dumps(data) else: # Return value: ${id[12345, 45678]}$ - print("SET DATA WRAPPER TO %s!" % parsersplit[-1]) + self.logger.info("SET DATA WRAPPER TO %s!" % parsersplit[-1]) parseditem = "${%s%s}$" % (parsersplit[-1], json.dumps(data)) - print("Before last return with %s" % appendresult) + self.logger.info("Before last return with %s" % appendresult) returndata = str(parseditem)+str(appendresult) # New in 0.8.97: Don't return items without lists - print("RETURNDATA: %s" % returndata) + self.logger.info("RETURNDATA: %s" % returndata) #return returndata, is_loop try: return json.dumps(json.loads(returndata)), is_loop except json.decoder.JSONDecodeError as e: - print("Error in decoder: %s" % e) + self.logger.info("Error in decoder: %s" % e) return returndata, is_loop # Sending self as it's not a normal function def parse_liquid(template, self): - #print("Inside liquid with glob: %s" % globals()) + #self.logger.info("Inside liquid with glob: %s" % globals()) try: if len(template) > 5000000: - print("[DEBUG] Skipping liquid - size too big (%d)" % len(template)) + self.logger.info("[DEBUG] Skipping liquid - size too big (%d)" % len(template)) return template #if not "{{" in template or not "}}" in template: # if not "{%" in template or not "%}" in template: - # print("Skipping liquid - missing {{ }} and {% %}") + # self.logger.info("Skipping liquid - missing {{ }} and {% %}") # return template #if not "{{" in template or not "}}" in template: # return template - #print(globals()) - print("[DEBUG] Running liquid with data of length %d" % len(template)) + #self.logger.info(globals()) + self.logger.info("[DEBUG] Running liquid with data of length %d" % len(template)) run = Liquid(template, mode="wild", from_file=False) # Can't handle self yet (?) @@ -1642,13 +1645,13 @@ class AppBase: # ret = run.render(**globals()) # return ret #except liquid.exceptions.LiquidRenderError as e: - # print("Render error: %s" % e) + # self.logger.info("Render error: %s" % e) except jinja2.exceptions.TemplateNotFound as e: - print("[ERROR] Template error: %s" % e) + self.logger.info("[ERROR] Template error: %s" % e) except jinja2.exceptions.TemplateSyntaxError as e: - print("[ERROR] Syntax error: %s" % e) + self.logger.info("[ERROR] Syntax error: %s" % e) except: - print("[ERROR] General exception for liquid") + self.logger.info("[ERROR] General exception for liquid") return template @@ -1656,6 +1659,7 @@ class AppBase: # Should have a regex which looks for the value, then goes out and cleans up the key def recurse_cleanup_script(data): deletekeys = [] + newvalue = data try: if not isinstance(data, dict): newvalue = json.loads(data) @@ -1663,7 +1667,7 @@ class AppBase: newvalue = data for key, value in newvalue.items(): - #print("%s: %s" % (key, value)) + #self.logger.info("%s: %s" % (key, value)) if isinstance(value, str) and len(value) == 0: deletekeys.append(key) continue @@ -1671,17 +1675,17 @@ class AppBase: if isinstance(value, list): try: value = json.dumps(value) - print(value) + self.logger.info(value) except: - print("Json parsing issue in recursed value") + self.logger.info("[WARNING] Json parsing issue in recursed value") pass if value == "${%s}" % key: - print("Deleting %s because key = value" % key) + self.logger.info("[WARNING] Deleting %s because key = value" % key) deletekeys.append(key) continue elif "${" in value and "}" in value: - print("Deleting %s because it contains ${ and }" % key) + self.logger.info("[WARNING] Deleting %s because it contains ${ and }" % key) deletekeys.append(key) continue @@ -1689,16 +1693,22 @@ class AppBase: newvalue[key] = recurse_cleanup_script(value) except json.decoder.JSONDecodeError as e: - print("Failed JSON replacement for OpenAPI keys (3) {e}") + self.logger.info(f"[WARNING] Failed JSON replacement for OpenAPI keys (3) {e}") + except Exception as e: + self.logger.info(f"[WARNING] Failed as an exception (1): {e}") - for deletekey in deletekeys: - try: - del newvalue[deletekey] - except: - pass + try: + for deletekey in deletekeys: + try: + del newvalue[deletekey] + except: + pass + except Exception as e: + self.logger.info(f"[WARNING] Failed in deletekeys: {e}") + return data try: - #print("Post delete: %s" % newvalue) + #self.logger.info("Post delete: %s" % newvalue) for key, value in newvalue.items(): if isinstance(value, bool): continue @@ -1709,26 +1719,28 @@ class AppBase: value = json.loads(value) newvalue[key] = value except json.decoder.JSONDecodeError as e: - #print("Inner overwrite issue for \"%s\": %s" % (key, e)) + #self.logger.info("Inner overwrite issue for \"%s\": %s" % (key, e)) continue except Exception as e: - #print("General error in newvalue items loop: %s" % e) + #self.logger.info("General error in newvalue items loop: %s" % e) continue try: data = json.dumps(newvalue) except json.decoder.JSONDecodeError as e: - print("[WARNING] JsonDecodeError: %s" % e) + self.logger.info("[WARNING] JsonDecodeError: %s" % e) data = newvalue except json.decoder.JSONDecodeError as e: - print("Failed JSON replacement for OpenAPI keys (2) {e}") + self.logger.info("[WARNING] Failed JSON replacement for OpenAPI keys (2) {e}") + except Exception as e: + self.logger.info(f"[WARNING] Failed as an exception (2): {e}") #if isinstance(data, str): # tmpdata = json.dumps(data) - # print(tmpdata) + # self.logger.info(tmpdata) # foundvalue = re.findall(".*?(${\w+})", tmpdata, re.MULTILINE) - # print("FOUND: %s", foundvalue) + # self.logger.info("FOUND: %s", foundvalue) return data @@ -1750,13 +1762,13 @@ class AppBase: escape_replacement = "\\%\\%\\%\\%\\%" end_variable = "^_^" - #print("Input value: %s" % parameter["value"]) + #self.logger.info("Input value: %s" % parameter["value"]) try: parameter["value"] = parameter["value"].replace(escaped_dollar, escape_replacement, -1) except: - print("Error in initial replacement of escaped dollar!") + self.logger.info("Error in initial replacement of escaped dollar!") - #print("POST input value: %s" % parameter["value"]) + #self.logger.info("POST input value: %s" % parameter["value"]) # Regex to find all the things if parameter["variant"] == "STATIC_VALUE": @@ -1765,7 +1777,7 @@ class AppBase: #self.logger.debug(f"\n\nHandle static data with JSON: {data}\n\n") #self.logger.info("STATIC PARSED: %s" % actualitem) if len(actualitem) > 0: - print("ACTUAL: ", actualitem) + self.logger.info("ACTUAL: ", actualitem) for replace in actualitem: try: to_be_replaced = replace[0] @@ -1777,8 +1789,8 @@ class AppBase: # Trying without string dumping. value, is_loop = get_json_value(fullexecution, to_be_replaced) - #print("\n\nType of value: %s. Value: %s" % (type(value), value)) - print("\n\nType of value: %s" % type(value)) + #self.logger.info("\n\nType of value: %s. Value: %s" % (type(value), value)) + self.logger.info("\n\nType of value: %s" % type(value)) if isinstance(value, str): parameter["value"] = parameter["value"].replace(to_be_replaced, value) elif isinstance(value, dict) or isinstance(value, list): @@ -1790,18 +1802,18 @@ class AppBase: # parameter["value"] = parameter["value"].replace(to_be_replaced, str(value)) #except: # parameter["value"] = parameter["value"].replace(to_be_replaced, json.dumps(value)) - # print("Failed parsing value as string?") + # self.logger.info("Failed parsing value as string?") else: - print("Unknown type %s" % type(value)) + self.logger.info("Unknown type %s" % type(value)) try: parameter["value"] = parameter["value"].replace(to_be_replaced, json.dumps(value)) except json.decoder.JSONDecodeError as e: parameter["value"] = parameter["value"].replace(to_be_replaced, value) - #print("VALUE: %s" % parameter["value"]) + #self.logger.info("VALUE: %s" % parameter["value"]) if parameter["variant"] == "WORKFLOW_VARIABLE": - print("Handling workflow variable") + self.logger.info("Handling workflow variable") found = False try: for item in fullexecution["workflow"]["workflow_variables"]: @@ -1810,10 +1822,10 @@ class AppBase: parameter["value"] = item["value"] break except KeyError as e: - print("KeyError WF variable 1: %s" % e) + self.logger.info("KeyError WF variable 1: %s" % e) pass except TypeError as e: - print("TypeError WF variables 1: %s" % e) + self.logger.info("TypeError WF variables 1: %s" % e) pass if not found: @@ -1823,10 +1835,10 @@ class AppBase: parameter["value"] = item["value"] break except KeyError as e: - print("KeyError WF variable 2: %s" % e) + self.logger.info("KeyError WF variable 2: %s" % e) pass except TypeError as e: - print("TypeError WF variables 2: %s" % e) + self.logger.info("TypeError WF variables 2: %s" % e) pass elif parameter["variant"] == "ACTION_RESULT": @@ -1859,44 +1871,44 @@ class AppBase: try: to_be_replaced = replace[0] except IndexError: - print("Nothing to replace?: " % e) + self.logger.info("Nothing to replace?: " % e) continue # This will never be a loop aka multi argument parameter["value"] = to_be_replaced value, is_loop = get_json_value(fullexecution, to_be_replaced) - print("Loop: %s" % is_loop) + self.logger.info("Loop: %s" % is_loop) if isinstance(value, str): parameter["value"] = parameter["value"].replace(to_be_replaced, value) elif isinstance(value, dict): parameter["value"] = parameter["value"].replace(to_be_replaced, json.dumps(value)) else: - print("Unknown type %s" % type(value)) + self.logger.info("Unknown type %s" % type(value)) try: parameter["value"] = parameter["value"].replace(to_be_replaced, json.dumps(value)) except json.decoder.JSONDecodeError as e: parameter["value"] = parameter["value"].replace(to_be_replaced, value) - #print("PRE Replaced data: %s" % parameter["value"]) + #self.logger.info("PRE Replaced data: %s" % parameter["value"]) try: parameter["value"] = parameter["value"].replace(end_variable, "", -1) parameter["value"] = parameter["value"].replace(escape_replacement, "$", -1) except: - print("Error in datareplacement") + self.logger.info("Error in datareplacement") # Just here in case it breaks # Implemented 02.08.2021 - #print("Pre liquid: %s" % parameter["value"]) + #self.logger.info("Pre liquid: %s" % parameter["value"]) try: parameter["value"] = parse_liquid(parameter["value"], self) except: pass - #print("Replaced data: %s" % parameter["value"]) + #self.logger.info("Replaced data: %s" % parameter["value"]) - #print("POST liquid: %s" % parameter["value"]) + #self.logger.info("POST liquid: %s" % parameter["value"]) return "", parameter["value"], is_loop def run_validation(sourcevalue, check, destinationvalue): @@ -1952,7 +1964,7 @@ class AppBase: continue if item.strip() in sourcevalue: - print("[INFO] Found %s in %s" % (item, sourcevalue)) + self.logger.info("[INFO] Found %s in %s" % (item, sourcevalue)) return True return False @@ -1978,7 +1990,7 @@ class AppBase: try: found = re.search(destinationvalue, sourcevalue) except re.error as e: - print("[WARNING] Regex error in condition: %s" % e) + self.logger.info("[WARNING] Regex error in condition: %s" % e) return False if found == None: @@ -2055,7 +2067,7 @@ class AppBase: self.logger.warning("Skipping %s %s %s because %s is invalid." % (sourcevalue, condition["condition"]["value"], destinationvalue, condition["condition"]["value"])) continue - #print(destinationvalue) + #self.logger.info(destinationvalue) # NEGATE # Configuration = negated because of WorkflowAppActionParam.. @@ -2082,12 +2094,12 @@ class AppBase: # Checks whether conditions are met, otherwise set branchcheck, tmpresult = check_branch_conditions(action, fullexecution, self) if isinstance(tmpresult, object) or isinstance(tmpresult, list): - print("[DEBUG] Fixing branch return as object -> string") + self.logger.info("[DEBUG] Fixing branch return as object -> string") try: #tmpresult = tmpresult.replace("'", "\"") tmpresult = json.dumps(tmpresult) except json.decoder.JSONDecodeError as e: - print(f"[WARNING] Failed condition parsing {tmpresult} to string") + self.logger.info(f"[WARNING] Failed condition parsing {tmpresult} to string") if not branchcheck: self.logger.info("Failed one or more branch conditions.") @@ -2101,7 +2113,7 @@ class AppBase: except requests.exceptions.ConnectionError as e: self.logger.exception(e) - print("\n\n[DEBUG] RETURNING BECAUSE A BRANCH FAILED: %s\n\n" % tmpresult) + self.logger.info("\n\n[DEBUG] RETURNING BECAUSE A BRANCH FAILED: %s\n\n" % tmpresult) return # Replace name cus there might be issues @@ -2129,16 +2141,16 @@ class AppBase: # Potentially parse JSON here # FIXME - add potential authentication as first parameter(s) here # params[parameter["name"]] = parameter["value"] - #print(fullexecution["authentication"] + #self.logger.info(fullexecution["authentication"] # What variables are necessary here tho hmm params = {} try: for item in action["authentication"]: - #print("AUTH: ", key, value) + #self.logger.info("AUTH: ", key, value) params[item["key"]] = item["value"] except KeyError: - print("[DEBUG] No authentication specified!") + self.logger.info("[DEBUG] No authentication specified!") pass #action["authentication"] @@ -2152,16 +2164,16 @@ class AppBase: # Hack for key:value in options using || try: if parameter["options"] != None and len(parameter["options"]) > 0: - #print(f'OPTIONS: {parameter["options"]}') - #print(f'OPTIONS VAL: {parameter}') + #self.logger.info(f'OPTIONS: {parameter["options"]}') + #self.logger.info(f'OPTIONS VAL: {parameter}') if "||" in parameter["value"]: splitvalue = parameter["value"].split("||") if len(splitvalue) > 1: - #print(f'[INFO] Parsed split || options of actions["parameters"]["name"]') + #self.logger.info(f'[INFO] Parsed split || options of actions["parameters"]["name"]') action["parameters"][counter]["value"] = splitvalue[1] except (IndexError, KeyError, TypeError) as e: - print("[WARNING] Options err: {e}") + self.logger.info("[WARNING] Options err: {e}") # This part is purely for OpenAPI accessibility. # It replaces the data back into the main item @@ -2169,7 +2181,7 @@ class AppBase: # but this has changed to do lists within items and such if parameter["name"] == "body": bodyindex = counter - #print("PARAM: %s" % parameter) + #self.logger.info("PARAM: %s" % parameter) try: values = parameter["value_replace"] if values != None: @@ -2178,36 +2190,36 @@ class AppBase: replace_value = val["value"] replace_key = val["key"] if (val["value"].startswith("{") and val["value"].endswith("}")) or (val["value"].startswith("[") and val["value"].endswith("]")): - print(f"""Trying to parse as JSON: {val["value"]}""") + self.logger.info(f"""Trying to parse as JSON: {val["value"]}""") try: value_replace = json.loads(val["value"]) # If it gets here, remove the "" infront and behind the key as well since this is preventing the JSON from being loaded replace_key = f"\"{replace_key}\"" except json.decoder.JSONDecodeError as e: - print("Failed JSON replacement for OpenAPI %s", val["key"]) + self.logger.info("Failed JSON replacement for OpenAPI %s", val["key"]) elif val["value"].lower() == "true" or val["value"].lower() == "false": replace_key = f"\"{replace_key}\"" action["parameters"][counter]["value"] = action["parameters"][counter]["value"].replace(replace_key, replace_value, 1) - print(f'[INFO] Added param {val["key"]} for body (using OpenAPI)') + self.logger.info(f'[INFO] Added param {val["key"]} for body (using OpenAPI)') added += 1 #action["parameters"]["body"] - print("ADDED %d parameters for body" % added) + self.logger.info("ADDED %d parameters for body" % added) except KeyError as e: - print("KeyError body OpenAPI: %s" % e) + self.logger.info("KeyError body OpenAPI: %s" % e) pass - print(f"""HANDLING {action["parameters"][counter]["value"]}""") + self.logger.info(f"""HANDLING {action["parameters"][counter]["value"]}""") action["parameters"][counter]["value"] = recurse_cleanup_script(action["parameters"][counter]["value"]) - #print(action["parameters"]) + #self.logger.info(action["parameters"]) # This seems redundant now - print("Pre parameters") + self.logger.info("Pre parameters") for parameter in newparams: action["parameters"].append(parameter) @@ -2217,7 +2229,7 @@ class AppBase: try: self.original_action = json.loads(json.dumps(action)) except Exception as e: - print(f"[ERROR] Failed parsing action as JSON to original action. This COULD have bad effects on LOOPED executions: {e}") + self.logger.info(f"[ERROR] Failed parsing action as JSON to original action. This COULD have bad effects on LOOPED executions: {e}") # calltimes is used to handle forloops in the app itself. # 2 kinds of loop - one in gui with one app each, and one like this, @@ -2229,7 +2241,7 @@ class AppBase: # Multi_parameter has the data for each. variable minlength = 0 - print("Pre-loading parameters") + self.logger.info("Pre-loading parameters") multi_parameters = json.loads(json.dumps(params)) multiexecution = False multi_execution_lists = [] @@ -2241,34 +2253,34 @@ class AppBase: # Custom format for ${name[0,1,2,...]}$ #submatch = "([${]{2}([0-9a-zA-Z_-]+)(\[.*\])[}$]{2})" - #print(f"Returnedvalue: {value}") + #self.logger.info(f"Returnedvalue: {value}") # OLD: Used until 13.03.2021: submatch = "([${]{2}#?([0-9a-zA-Z_-]+)#?(\[.*\])[}$]{2})" # \${[0-9a-zA-Z_-]+#?(\[.*?]}\$) submatch = "([${]{2}#?([0-9a-zA-Z_-]+)#?(\[.*?]}\$))" actualitem = re.findall(submatch, value, re.MULTILINE) try: if action["skip_multicheck"]: - print("Skipping multicheck") + self.logger.info("Skipping multicheck") actualitem = [] except KeyError: pass - #print("Return value: %s" % value) + #self.logger.info("Return value: %s" % value) actionname = action["name"] - #print("Multicheck ", actualitem) - #print("ITEM LENGTH: %d, Actual item: %s" % (len(actualitem), actualitem)) + #self.logger.info("Multicheck ", actualitem) + #self.logger.info("ITEM LENGTH: %d, Actual item: %s" % (len(actualitem), actualitem)) if len(actualitem) > 0: multiexecution = True # Loop WITHOUT JSON variables go here. # Loop WITH variables go in else. - print("Before first part in multiexec!") + self.logger.info("Before first part in multiexec!") handled = False # Has a loop without a variable used inside if len(actualitem[0]) > 2 and actualitem[0][1] == "SHUFFLE_NO_SPLITTER": - print("(1) Pre replacement: %s" % actualitem[0][2]) + self.logger.info("(1) Pre replacement: %s" % actualitem[0][2]) tmpitem = value index = 0 @@ -2279,10 +2291,10 @@ class AppBase: if replacement.startswith("\"") and replacement.endswith("\""): replacement = replacement[1:len(replacement)-1] - print("POST replacement: %s" % replacement) + self.logger.info("POST replacement: %s" % replacement) #json_replacement = tmpitem.replace(actualitem[index][0], replacement, 1) - #print("AFTER POST replacement: %s" % json_replacement) + #self.logger.info("AFTER POST replacement: %s" % json_replacement) json_replacement = replacement try: json_replacement = json.loads(replacement) @@ -2291,12 +2303,12 @@ class AppBase: replacement = replacement.replace("\'", "\"", -1) json_replacement = json.loads(replacement) except: - print("JSON error singular: %s" % e) + self.logger.info("JSON error singular: %s" % e) if len(json_replacement) > minlength: minlength = len(json_replacement) - print("PRE new_replacement") + self.logger.info("PRE new_replacement") new_replacement = [] for i in range(len(json_replacement)): @@ -2309,12 +2321,12 @@ class AppBase: try: newvalue = json.loads(newvalue) except json.decoder.JSONDecodeError as e: - print("DECODER ERROR: %s" % e) + self.logger.info("DECODER ERROR: %s" % e) pass new_replacement.append(newvalue) - print("New replacement: %s" % new_replacement) + self.logger.info("New replacement: %s" % new_replacement) # New tmpitem = tmpitem.replace(actualitem[index][0], replacement, 1) @@ -2323,47 +2335,47 @@ class AppBase: resultarray = [] isfile = False try: - print("(1) ------------ PARAM: %s" % parameter["schema"]["type"]) + self.logger.info("(1) ------------ PARAM: %s" % parameter["schema"]["type"]) if parameter["schema"]["type"] == "file" and len(value) > 0: - print("(1) SHOULD HANDLE FILE IN MULTI. Get based on value %s" % tmpitem) + self.logger.info("(1) SHOULD HANDLE FILE IN MULTI. Get based on value %s" % tmpitem) # This is silly :) # Q: Is there something wrong with the download system? # It seems to return "FILE CONTENT: %s" with the ID as %s for tmp_file_split in json.loads(tmpitem): - print("(1) PRE GET FILE %s" % tmp_file_split) + self.logger.info("(1) PRE GET FILE %s" % tmp_file_split) file_value = self.get_file(tmp_file_split) - print("(1) POST AWAIT %s" % file_value) + self.logger.info("(1) POST AWAIT %s" % file_value) resultarray.append(file_value) - print("(1) FILE VALUE FOR VAL %s: %s" % (tmp_file_split, file_value)) + self.logger.info("(1) FILE VALUE FOR VAL %s: %s" % (tmp_file_split, file_value)) isfile = True except NameError as e: - print("(1) SCHEMA NAMEERROR IN FILE HANDLING: %s" % e) + self.logger.info("(1) SCHEMA NAMEERROR IN FILE HANDLING: %s" % e) except KeyError as e: - print("(1) SCHEMA KEYERROR IN FILE HANDLING: %s" % e) + self.logger.info("(1) SCHEMA KEYERROR IN FILE HANDLING: %s" % e) except json.decoder.JSONDecodeError as e: - print("(1) JSON ERROR IN FILE HANDLING: %s" % e) + self.logger.info("(1) JSON ERROR IN FILE HANDLING: %s" % e) if not isfile: - print("Resultarray (NOT FILE): %s" % resultarray) + self.logger.info("Resultarray (NOT FILE): %s" % resultarray) params[parameter["name"]] = tmpitem multi_parameters[parameter["name"]] = new_replacement else: - print("Resultarray (FILE): %s" % resultarray) + self.logger.info("Resultarray (FILE): %s" % resultarray) params[parameter["name"]] = resultarray multi_parameters[parameter["name"]] = resultarray #if len(resultarray) == 0: - # print("[WARNING] Returning empty array because the array length to be looped is 0 (1)") + # self.logger.info("[WARNING] Returning empty array because the array length to be looped is 0 (1)") # action_result["status"] = "SUCCESS" # action_result["result"] = "[]" # self.send_result(action_result, headers, stream_path) # return multi_execution_lists.append(new_replacement) - #print("MULTI finished: %s" % json_replacement) + #self.logger.info("MULTI finished: %s" % json_replacement) else: - print(f"(2) Pre replacement (loop with variables). Variables: {actualitem}") #% actualitem) + self.logger.info(f"(2) Pre replacement (loop with variables). Variables: {actualitem}") #% actualitem) # This is here to handle for loops within variables.. kindof # 1. Find the length of the longest array # 2. Build an array with the base values based on parameter["value"] @@ -2379,12 +2391,12 @@ class AppBase: actualitem = actualitem[:-2] except IndexError: - print("[WARNING] Indexerror") + self.logger.info("[WARNING] Indexerror") continue - #print(f"\n\nTMPITEM: {actualitem}\n\n") + #self.logger.info(f"\n\nTMPITEM: {actualitem}\n\n") #actualitem = parse_wrapper_start(actualitem) - #print(f"\n\nTMPITEM2: {actualitem}\n\n") + #self.logger.info(f"\n\nTMPITEM2: {actualitem}\n\n") try: itemlist = json.loads(actualitem) @@ -2395,7 +2407,7 @@ class AppBase: curminlength = len(itemlist) except json.decoder.JSONDecodeError as e: - print("JSON Error (replace): %s in %s" % (e, actualitem)) + self.logger.info("JSON Error (replace): %s in %s" % (e, actualitem)) replacements[to_be_replaced] = actualitem @@ -2403,7 +2415,7 @@ class AppBase: # Parses the data as string with length, split etc. before moving on. - #print("In second part of else: %s" % (len(itemlist))) + #self.logger.info("In second part of else: %s" % (len(itemlist))) # This is a result array for JUST this value.. # What if there are more? resultarray = [] @@ -2415,81 +2427,81 @@ class AppBase: replacement = replacement[1:len(replacement)-1] #except json.decoder.JSONDecodeError as e: - #print("REPLACING %s with %s" % (key, replacement)) + #self.logger.info("REPLACING %s with %s" % (key, replacement)) #replacement = parse_wrapper_start(replacement) tmpitem = tmpitem.replace(key, replacement, -1) # This code handles files. - print("(2) ------------ PARAM: %s" % parameter["schema"]["type"]) + self.logger.info("(2) ------------ PARAM: %s" % parameter["schema"]["type"]) isfile = False try: if parameter["schema"]["type"] == "file" and len(value) > 0: - print("(2) SHOULD HANDLE FILE IN MULTI. Get based on value %s" % parameter["value"]) + self.logger.info("(2) SHOULD HANDLE FILE IN MULTI. Get based on value %s" % parameter["value"]) for tmp_file_split in json.loads(parameter["value"]): - print("(2) PRE GET FILE %s" % tmp_file_split) + self.logger.info("(2) PRE GET FILE %s" % tmp_file_split) file_value = self.get_file(tmp_file_split) - print("(2) POST AWAIT %s" % file_value) + self.logger.info("(2) POST AWAIT %s" % file_value) resultarray.append(file_value) - print("(2) FILE VALUE FOR VAL %s: %s" % (tmp_file_split, file_value)) + self.logger.info("(2) FILE VALUE FOR VAL %s: %s" % (tmp_file_split, file_value)) isfile = True except KeyError as e: - print("(2) SCHEMA ERROR IN FILE HANDLING: %s" % e) + self.logger.info("(2) SCHEMA ERROR IN FILE HANDLING: %s" % e) except json.decoder.JSONDecodeError as e: - print("(2) JSON ERROR IN FILE HANDLING: %s" % e) + self.logger.info("(2) JSON ERROR IN FILE HANDLING: %s" % e) if not isfile: tmpitem = tmpitem.replace("\\\\", "\\", -1) resultarray.append(tmpitem) # With this parameter ready, add it to... a greater list of parameters. Rofl - print("LENGTH OF ARR: %d" % len(resultarray)) + self.logger.info("LENGTH OF ARR: %d" % len(resultarray)) if len(resultarray) == 0: - print("[WARNING] Returning empty array because the array length to be looped is 0 (0)") + self.logger.info("[WARNING] Returning empty array because the array length to be looped is 0 (0)") self.action_result["status"] = "SUCCESS" self.action_result["result"] = "[]" self.send_result(self.action_result, headers, stream_path) return - #print("RESULTARRAY: %s" % resultarray) + #self.logger.info("RESULTARRAY: %s" % resultarray) if resultarray not in multi_execution_lists: multi_execution_lists.append(resultarray) multi_parameters[parameter["name"]] = resultarray else: # Parses things like int(value) - print("[DEBUG] Normal parsing (not looping)")#with data %s" % value) + self.logger.info("[DEBUG] Normal parsing (not looping)")#with data %s" % value) # This part has fucked over so many random JSON usages because of weird paranthesis parsing value = parse_wrapper_start(value, self) - print("[DEBUG] Post return: %s" % value) + self.logger.info("[DEBUG] Post return: %s" % value) - #print("POST data value: %s" % value) + #self.logger.info("POST data value: %s" % value) params[parameter["name"]] = value multi_parameters[parameter["name"]] = value # This code handles files. try: if parameter["schema"]["type"] == "file" and len(value) > 0: - print("\n SHOULD HANDLE FILE. Get based on value %s. <--- is this a valid ID?" % parameter["value"]) + self.logger.info("\n SHOULD HANDLE FILE. Get based on value %s. <--- is this a valid ID?" % parameter["value"]) file_value = self.get_file(value) - print("FILE VALUE: %s \n" % file_value) + self.logger.info("FILE VALUE: %s \n" % file_value) params[parameter["name"]] = file_value multi_parameters[parameter["name"]] = file_value except KeyError as e: - print("SCHEMA ERROR IN FILE HANDLING: %s" % e) + self.logger.info("SCHEMA ERROR IN FILE HANDLING: %s" % e) #remove_params.append(parameter["name"]) # Fix lists here # FIXME: This doesn't really do anything anymore - print("[DEBUG] CHECKING multi execution list: %d!" % len(multi_execution_lists)) + self.logger.info("[DEBUG] CHECKING multi execution list: %d!" % len(multi_execution_lists)) if len(multi_execution_lists) > 0: - print("\n [DEBUG] Multi execution list has more data: %d" % len(multi_execution_lists)) + self.logger.info("\n [DEBUG] Multi execution list has more data: %d" % len(multi_execution_lists)) filteredlist = [] for listitem in multi_execution_lists: if listitem in filteredlist: @@ -2504,55 +2516,55 @@ class AppBase: #else: # filteredlist.append(listitem) - #print("New list length: %d" % len(filteredlist)) + #self.logger.info("New list length: %d" % len(filteredlist)) if len(filteredlist) > 1: - print(f"Calculating new multi-loop length with {len(filteredlist)} lists") + self.logger.info(f"Calculating new multi-loop length with {len(filteredlist)} lists") tmplength = 1 for innerlist in filteredlist: tmplength = len(innerlist)*tmplength - print("List length: %d. %d*%d" % (tmplength, len(innerlist), tmplength)) + self.logger.info("List length: %d. %d*%d" % (tmplength, len(innerlist), tmplength)) minlength = tmplength - print("New multi execution length: %d\n" % tmplength) + self.logger.info("New multi execution length: %d\n" % tmplength) # Cleaning up extra list params for subparam in remove_params: - #print(f"DELETING {subparam}") + #self.logger.info(f"DELETING {subparam}") try: del params[subparam] except: pass - #print(f"Error with subparam deletion of {subparam} in {params}") + #self.logger.info(f"Error with subparam deletion of {subparam} in {params}") try: del multi_parameters[subparam] except: - #print(f"Error with subparam deletion of {subparam} in {multi_parameters} (2)") + #self.logger.info(f"Error with subparam deletion of {subparam} in {multi_parameters} (2)") pass - #print() - #print(f"Param: {params}") - #print(f"Multiparams: {multi_parameters}") - #print() + #self.logger.info() + #self.logger.info(f"Param: {params}") + #self.logger.info(f"Multiparams: {multi_parameters}") + #self.logger.info() if not multiexecution: # Runs a single iteration here new_params = self.validate_unique_fields(params) - print(f"Returned with newparams of length {len(new_params)}") + self.logger.info(f"Returned with newparams of length {len(new_params)}") if isinstance(new_params, list) and len(new_params) == 1: params = new_params[0] else: - print("[WARNING] SHOULD STOP EXECUTION BECAUSE FIELDS AREN'T UNIQUE") + self.logger.info("[WARNING] SHOULD STOP EXECUTION BECAUSE FIELDS AREN'T UNIQUE") self.action_result["status"] = "SKIPPED" self.action_result["result"] = f"A non-unique value was found" self.action_result["completed_at"] = int(time.time()) self.send_result(self.action_result, headers, stream_path) return - print("[INFO] Running normal execution (not loop)\n") + self.logger.info("[INFO] Running normal execution (not loop)\n") #newres = await func(**params) - #print("PARAMS: %s" % params) + #self.logger.info("PARAMS: %s" % params) #newres = "" while True: try: @@ -2568,32 +2580,32 @@ class AppBase: try: del params[field] - print("Removed field invalid field %s" % field) + self.logger.info("Removed field invalid field %s" % field) except KeyError: break else: raise e #break - print("\n[INFO] Returned from execution with types %s" % type(newres)) - #print("\n[INFO] Returned from execution with %s of types %s" % (newres, type(newres)))#, newres) + self.logger.info("\n[INFO] Returned from execution with types %s" % type(newres)) + #self.logger.info("\n[INFO] Returned from execution with %s of types %s" % (newres, type(newres)))#, newres) if isinstance(newres, tuple): - print("[INFO] Handling return as tuple") + self.logger.info("[INFO] Handling return as tuple") # Handles files. filedata = "" file_ids = [] - print("TUPLE: %s" % newres[1]) + self.logger.info("TUPLE: %s" % newres[1]) if isinstance(newres[1], list): - print("[INFO] HANDLING LIST FROM RET") + self.logger.info("[INFO] HANDLING LIST FROM RET") file_ids = self.set_files(newres[1]) elif isinstance(newres[1], object): - print("[INFO] Handling JSON from ret") + self.logger.info("[INFO] Handling JSON from ret") file_ids = self.set_files([newres[1]]) elif isinstance(newres[1], str): - print("[INFO] Handling STRING from ret") + self.logger.info("[INFO] Handling STRING from ret") file_ids = self.set_files([newres[1]]) else: - print("[INFO] NO FILES TO HANDLE") + self.logger.info("[INFO] NO FILES TO HANDLE") tmp_result = { "result": newres[0], @@ -2602,33 +2614,33 @@ class AppBase: result = json.dumps(tmp_result) elif isinstance(newres, str): - print("[INFO] Handling return as string of length %d" % len(newres)) + self.logger.info("[INFO] Handling return as string of length %d" % len(newres)) result += newres elif isinstance(newres, dict) or isinstance(newres, list): try: result += json.dumps(newres, indent=4) except json.JSONDecodeError as e: - print("Failed decoding result: %s" % e) + self.logger.info("Failed decoding result: %s" % e) try: result += str(newres) except ValueError: result += "Failed autocasting. Can't handle %s type from function. Must be string" % type(newres) - print("Can't handle type %s value from function" % (type(newres))) + self.logger.info("Can't handle type %s value from function" % (type(newres))) else: try: result += str(newres) except ValueError: result += "Failed autocasting. Can't handle %s type from function. Must be string" % type(newres) - print("Can't handle type %s value from function" % (type(newres))) + self.logger.info("Can't handle type %s value from function" % (type(newres))) - print("[INFO] POST NEWRES RESULT!")#, result) + self.logger.info("[INFO] POST NEWRES RESULT!")#, result) else: - #print("[INFO] APP_SDK DONE: Starting MULTI execution (length: %d) with values %s" % (minlength, multi_parameters)) + #self.logger.info("[INFO] APP_SDK DONE: Starting MULTI execution (length: %d) with values %s" % (minlength, multi_parameters)) # 1. Use number of executions based on the arrays being similar # 2. Find the right value from the parsed multi_params - print("[INFO] Running WITHOUT outer loop (looping)") + self.logger.info("[INFO] Running WITHOUT outer loop (looping)") json_object = False results = await self.run_recursed_items(func, multi_parameters, {}) if isinstance(results, dict) or isinstance(results, list): @@ -2636,14 +2648,14 @@ class AppBase: #for i in range(0, minlength): # # To be able to use the results as a list: - # print("1: %s" % multi_parameters) + # self.logger.info("1: %s" % multi_parameters) # #baseparams = json.loads(json.dumps(multi_parameters)) # baseparams = copy.deepcopy(multi_parameters) - # print("2: %s: %s" % (type(baseparams), baseparams)) + # self.logger.info("2: %s: %s" % (type(baseparams), baseparams)) - # print("4") - # print("Running with params (1): %s" % baseparams) + # self.logger.info("4") + # self.logger.info("Running with params (1): %s" % baseparams) # results = await self.run_recursed_items(func, baseparams, {}) # if isinstance(results, dict) or isinstance(results, list): @@ -2659,7 +2671,7 @@ class AppBase: #try: # firstlist = True # for key, value in baseparams.items(): - # print("Itemtype: %s" % type(value)) + # self.logger.info("Itemtype: %s" % type(value)) # if isinstance(value, list): # try: # newvalue = value[i] @@ -2668,16 +2680,16 @@ class AppBase: # if len(value) != minlength and len(value) > 0: # newarray = [] - # print("VALUE: ", value) + # self.logger.info("VALUE: ", value) # additiontime = minlength/len(value) - # print("Bad length for value: %d - should be %d. Additiontime: %d" % (len(value), minlength, additiontime)) + # self.logger.info("Bad length for value: %d - should be %d. Additiontime: %d" % (len(value), minlength, additiontime)) # if firstlist: - # print("Running normal list (FIRST)") + # self.logger.info("Running normal list (FIRST)") # for subvalue in value: # for number in range(int(additiontime)): # newarray.append(subvalue) # else: - # #print("Running secondary lists") + # #self.logger.info("Running secondary lists") # ## 1. Set up length of array # ## 2. Put values spread out # # FIXME: This works well, except if lists are same length @@ -2687,31 +2699,31 @@ class AppBase: # for number in range(int(additiontime)): # for subvaluerange in range(len(value)): # # newlocation = number+(additiontime*subvaluerange) - # # print("%d+(%d*%d) = %d. VAL: %s" % (number, additiontime, subvaluerange, newlocation, value[subvaluerange])) + # # self.logger.info("%d+(%d*%d) = %d. VAL: %s" % (number, additiontime, subvaluerange, newlocation, value[subvaluerange])) # # Reverse if same length? # if int(minlength/len(value)) == len(value): # tmp = int(len(value)-subvaluerange-1) - # print("NEW: %d" % tmp) + # self.logger.info("NEW: %d" % tmp) # newarray[cnt] = value[tmp] # else: # newarray[cnt] = value[subvaluerange] # cnt += 1 - # #print("Newarray =", newarray) + # #self.logger.info("Newarray =", newarray) # newvalue = newarray[i] # firstlist = False # baseparams[key] = newvalue - # print("3") + # self.logger.info("3") #except IndexError as e: - # print("IndexError: %s" % e) + # self.logger.info("IndexError: %s" % e) # baseparams[key] = "IndexError: %s" % e #except KeyError as e: - # print("KeyError: %s" % e) + # self.logger.info("KeyError: %s" % e) # baseparams[key] = "KeyError: %s" % e - #print("4") - #print("Running with params (1): %s" % baseparams) + #self.logger.info("4") + #self.logger.info("Running with params (1): %s" % baseparams) #results = await self.run_recursed_items(func, baseparams, {}) #if isinstance(results, dict) or isinstance(results, list): @@ -2720,7 +2732,7 @@ class AppBase: # Check the structure here. If "isloop", try to recurse? # ret, is_loop = recurse_json(innervalue, parsersplit[outercnt+1:]) #ret = await func(**baseparams) - #print("Return from execution: %s" % ret) + #self.logger.info("Return from execution: %s" % ret) #if ret == None: # results.append("") # json_object = False @@ -2734,22 +2746,22 @@ class AppBase: # results.append(json.loads(ret)) # json_object = True # except json.decoder.JSONDecodeError as e: - # #print("Json: %s" % e) + # #self.logger.info("Json: %s" % e) # results.append(ret) - #print("Inner ret parsed: %s" % ret) + #self.logger.info("Inner ret parsed: %s" % ret) # Dump the result as a string of a list - #print("RESULTS: %s" % results) + #self.logger.info("RESULTS: %s" % results) if isinstance(results, list) or isinstance(results, dict): - print("JSON OBJECT? ", json_object) + self.logger.info("JSON OBJECT? ", json_object) # This part is weird lol if json_object: try: result = json.dumps(results) except json.JSONDecodeError as e: - print(f"Failed to decode: {e}") + self.logger.info(f"Failed to decode: {e}") result = results else: result = "[" @@ -2759,7 +2771,7 @@ class AppBase: result += item except json.decoder.JSONDecodeError as e: # Common nested issue which puts " around everything - print("Decodingerror: %s" % e) + self.logger.info("Decodingerror: %s" % e) try: tmpitem = item.replace("\\\"", "\"", -1) json.loads(tmpitem) @@ -2773,7 +2785,7 @@ class AppBase: result = result[:-2] result += "]" else: - print("Normal result - no list?") + self.logger.info("Normal result - no list?") result = results self.action_result["status"] = "SUCCESS" @@ -2784,18 +2796,18 @@ class AppBase: self.logger.debug(f"Executed {action['label']}-{action['id']}")#with result: {result}") #self.logger.debug(f"Data: %s" % action_result) except TypeError as e: - print("TypeError issue: %s" % e) + self.logger.info("TypeError issue: %s" % e) self.action_result["status"] = "FAILURE" self.action_result["result"] = "TypeError: %s" % str(e) else: - print("Function %s doesn't exist?" % action["name"]) + self.logger.info("Function %s doesn't exist?" % action["name"]) self.logger.error(f"App {self.__class__.__name__}.{action['name']} is not callable") self.action_result["status"] = "FAILURE" self.action_result["result"] = "Function %s is not callable." % actionname # https://ptb.discord.com/channels/747075026288902237/882017498550112286/882043773138382890 except (requests.exceptions.RequestException, TimeoutError) as e: - print(f"Failed to execute request: {e}") + self.logger.info(f"Failed to execute request: {e}") self.logger.exception(f"Failed to execute {e}-{action['id']}") self.action_result["status"] = "SUCCESS" try: @@ -2807,7 +2819,7 @@ class AppBase: self.action_result["result"] = f"Request error: {e}" except Exception as e: - print(f"Failed to execute: {e}") + self.logger.info(f"Failed to execute: {e}") self.logger.exception(f"Failed to execute {e}-{action['id']}") self.action_result["status"] = "FAILURE" self.action_result["result"] = f"General exception: {e}" @@ -2824,14 +2836,14 @@ class AppBase: logger = logging.getLogger(f"{cls.__name__}") logger.setLevel(logging.DEBUG) - #print("Started execution: %s!!" % cls) - #print("Action: %s" % action) + #self.logger.info("Started execution: %s!!" % cls) + #self.logger.info("Action: %s" % action) #if isinstance(cls, object): # self.action = cls app = cls(redis=None, logger=logger, console_logger=logger) if isinstance(action, str): - print("Normal execution. Action is a string.") + self.logger.info("Normal execution. Action is a string.") elif isinstance(action, object): app.action = action @@ -2851,6 +2863,6 @@ class AppBase: except: pass else: - print("ACTION TYPE (unhandled): %s" % type(action)) + self.logger.info("ACTION TYPE (unhandled): %s" % type(action)) await app.execute_action(app.action)