From 25238d030cc0ae34c16b7d37844306ece8d65c28 Mon Sep 17 00:00:00 2001 From: Debela Gemechu Date: Fri, 10 Jan 2025 14:34:53 +0000 Subject: [PATCH] bug fix --- src/turninator.py | 76 +++++++++++++++++++++++------------------------ 1 file changed, 38 insertions(+), 38 deletions(-) diff --git a/src/turninator.py b/src/turninator.py index 23d9816..02d2a81 100644 --- a/src/turninator.py +++ b/src/turninator.py @@ -67,46 +67,46 @@ def get_aif_(self, format='xAIF'): return json.dumps(aif) def get_aif(self, format='xAIF'): - """ - This function reads the file, processes it, and returns the xAIF or AIF section in the correct format. - If the input file format is not valid, it attempts to correct it and then returns the desired format. - """ - try: - # Open the file and read its contents - with open(self.f_name, 'r') as file: - data = file.read() - - # Try to load the data as JSON - try: - x_aif = json.loads(data) - except JSONDecodeError as e: - logging.error(f"Error decoding JSON: {e}") - return {"error": "Invalid JSON format"} - - # Ensure x_aif is a dictionary, handle if it's a string - if isinstance(x_aif, str): + with open(self.f_name) as file: + data = file.read() + + # Check if the file content is a valid JSON string (it might be a JSON object or an array) + try: + data = json.loads(data) # Try to load it as a JSON object + except ValueError as e: + print(f"Error decoding JSON from file: {e}") + return None + + # Function to handle string entries (decoding double escaping if needed) + def process_data(entry): + if isinstance(entry, str): try: - x_aif = json.loads(x_aif) # Try to load it again if it's a string - except JSONDecodeError as e: - logging.error(f"Error re-decoding JSON: {e}") - return {"error": "Invalid JSON format after re-processing string"} - - # If format is "xAIF", return the whole xAIF structure - if format == "xAIF": - return json.dumps(x_aif) - - # Otherwise, extract the 'aif' section, if available - aif = x_aif.get('aif') - if not aif: - logging.warning("The 'aif' section is missing from the input JSON.") - return {"error": "'aif' section is missing"} - + # Try to decode the string if it's double-escaped JSON + decoded = json.loads(entry) + return decoded + except ValueError: + # If decoding fails, return the string as-is + return entry + elif isinstance(entry, dict): + # Process each key-value pair in the dictionary + for key, value in entry.items(): + entry[key] = process_data(value) + elif isinstance(entry, list): + # Process each item in the list + for index, item in enumerate(entry): + entry[index] = process_data(item) + return entry + + # Process the entire data structure (file contents) + data = process_data(data) + + # Convert the processed data back to JSON string if necessary + if format == "xAIF": + return json.dumps(data) + else: + # Extract 'aif' section from the data, if it exists + aif = data.get('aif', data) return json.dumps(aif) - - except Exception as e: - logging.error(f"An error occurred while processing the file: {e}") - return {"error": "Unexpected error occurred while processing the file"} - def turninator_default(self,): # Get the file path from the path object