From ef067ef0e829da55b2777ecc6efb1971ef1d6282 Mon Sep 17 00:00:00 2001 From: zoidy Date: Thu, 26 Oct 2023 19:27:34 +0000 Subject: [PATCH] Improve the clarity of log messages --- figshare/Article.py | 31 +++++++++++++++++++++++-------- figshare/Collection.py | 4 ++-- 2 files changed, 25 insertions(+), 10 deletions(-) diff --git a/figshare/Article.py b/figshare/Article.py index 1df0409..9ba5c1f 100644 --- a/figshare/Article.py +++ b/figshare/Article.py @@ -600,6 +600,8 @@ def __get_single_file_hash(self, filepath): :param folder_name string """ def __save_json_in_metadata(self, version_data, folder_name): + result = False + version_no = "v" + str(version_data["version"]).zfill(2) json_folder_path = folder_name + "/" + version_no + "/METADATA" preservation_storage_location = self.preservation_storage_location @@ -633,10 +635,14 @@ def __save_json_in_metadata(self, version_data, folder_name): # Writing to json file with open(filename_path, "w") as outfile: outfile.write(json_data) + result = True except Exception as e: self.logs.write_log_in_file('error', f"{folder_name}: {e}", True) - return False - return True + result = False + + if not result: + self.logs.write_log_in_file("info", "json not saved.", True) + return result """ Copying UAL_RDM folder files to storage directory in related article version folder @@ -644,6 +650,8 @@ def __save_json_in_metadata(self, version_data, folder_name): :param folder_name string """ def __copy_files_ual_rdm(self, version_data, folder_name): + result = False + version_no = "v" + str(version_data["version"]).zfill(2) curation_storage_location = self.curation_storage_location # check curation dir is reachable @@ -667,16 +675,19 @@ def __copy_files_ual_rdm(self, version_data, folder_name): # check preservation dir is reachable self.check_access_of_directories(preservation_storage_location, "preservation") try: - self.logs.write_log_in_file("info", "Copying files to preservation folder.", True) check_path_exists = os.path.exists(complete_folder_name) if (check_path_exists is False): os.makedirs(complete_folder_name, exist_ok=True) # copying files to preservation version folder shutil.copytree(curation_dir_name, complete_folder_name, dirs_exist_ok=True) + self.logs.write_log_in_file("info", "Copied curation files to preservation folder.", True) + result = True except Exception as e: - self.logs.write_log_in_file('error', f"{e} - {complete_folder_name} error while copying files.", True) - return False - return True + self.logs.write_log_in_file('error', f"{e} - {complete_folder_name}.", True) + result = False + if not result: + self.logs.write_log_in_file("info", "No files copied to preservation folder.", True) + return result """ Find matched articles from the fetched data and curation dir @@ -758,7 +769,7 @@ def __can_copy_files(self, version_data): + "files in curation storage. Folder will be deleted.", True) copy_files = False else: - self.logs.write_log_in_file("info", "Curation files exist", True) + self.logs.write_log_in_file("info", "Curation files exist. Continuing execution.", True) copy_files = True return copy_files @@ -770,7 +781,7 @@ def __final_process(self, check_files, copy_files, check_dir, version_data, fold success = True if copy_files and not check_files: # check and create empty directories for each version - self.logs.write_log_in_file("info", "Checking and creating empty directories.", True) + self.logs.write_log_in_file("info", "Checking and creating empty directories in preservation storage.", True) success = success & self.create_required_folders(version_data, folder_name) # copy curation UAL_RDM files in storage UAL_RDM folder for each version self.logs.write_log_in_file("info", "Copying curation UAL_RDM files to preservation UAL_RDM folder.", True) @@ -970,6 +981,8 @@ def create_required_folders(self, version_data, folder_name): if (ual_path_exists is False): # create UAL_RDM directory if not exist os.makedirs(ual_folder_name, exist_ok=True) + else: + self.logs.write_log_in_file("info", "UAL_RDM directory already exists. Folder not created", True) # setup DATA directory data_folder_name = preservation_storage_location + folder_name + "/" + version_no + "/DATA" @@ -977,6 +990,8 @@ def create_required_folders(self, version_data, folder_name): if (data_path_exists is False): # create DATA directory if it does not exist os.makedirs(data_folder_name, exist_ok=True) + else: + self.logs.write_log_in_file("info", "DATA directory already exists. Folder not created", True) except Exception as e: self.logs.write_log_in_file('error', f"{folder_name}: {e}", True) return False diff --git a/figshare/Collection.py b/figshare/Collection.py index f5447b9..21e892e 100644 --- a/figshare/Collection.py +++ b/figshare/Collection.py @@ -279,14 +279,14 @@ def __save_json_in_metadata(self, collection_id, version_data, folder_name): json_data = json.dumps(version_data, indent=4) filename_path = complete_path + "/" + str(collection_id) + ".json" # Writing to json file - self.logs.write_log_in_file("info", "Saving collection data in json.", True) with open(filename_path, "w") as outfile: outfile.write(json_data) + self.logs.write_log_in_file("info", "Saved collection data in json.", True) else: storage_collection_version_dir = os.listdir(complete_path) file_name = f"{str(collection_id)}.json" if (len(storage_collection_version_dir) == 0 or file_name not in storage_collection_version_dir): - self.logs.write_log_in_file("info", f"{complete_path} path already exists but missing {file_name} file.") + self.logs.write_log_in_file("warning", f"{complete_path} path already exists but missing {file_name} file.") def get_collection_api_url(self): collections_api_url = self.api_endpoint + '/collections'