Upper level functions in utils.py
These are the functions that are called by modules and not only by other functions in utils.py
utils.get_current_datetime()
-
Parameters:
-
None.
-
Context for Use:
-
Returns the current date and timestamp as a dictionary with keys 'date-str' for the date in "yyyy-MM-dd" format and 'timestamp' for the UNIX timestamp.
-
Example Output:
-
python { 'date-str': '2024-05-10', 'timestamp': 1715328608 }
utils.get_data_root()
-
Parameters:
-
None.
-
Context for Use:
-
Returns the root directory path for data storage, obtained from the environment variable 'WDM_DATA_ROOT' and if the environnment variable doesn't exist then returns the default value:
W:/wdm-test. -
Example Output:
-
python #When run on Windows Systems W:\wdm-test -
python #When run on Linux Based Systems W:/wdm-test
utils.get_data_root_not_backed_up()
-
Parameters:
-
None.
-
Context for Use:
-
Returns the root directory path for data storage, specifically for temporary data that is not backed up, obtained from the environment variable 'WDM_DATA_ROOT_TEMP' and if the environnment variable doesn't exist then returns the default value:
X:/wdm-test. -
Example Output:
-
python #When run on Windows Systems X:\wdm-test -
python #When run on Linux Based Systems X:/wdm-test
utils.get_date_yesterday()
-
Parameters:
-
None.
-
Context for Use:
-
Returns the date of the previous day in
yyyy-MM-ddformat. -
Example Output:
2024-05-22
utils.get_journal_dir_path(issn)
-
Parameters:
-
issn: str - The International Standard Serial Number of the journal. -
Context for Use:
-
This function generates the directory path for storing articles of a journal based on its ISSN.
-
Example Output:
-
python # When run on Windows Systems X:\wdm-test\Automatische_Prozessierung\2673-6411 -
python # When run on Linux Based Systems X:/wdm-test/Automatische_Prozessierung/2673-6411
utils.create_json_file(file_path, **kwargs)
-
Parameters:
-
file_path: str - The path to the JSON file to be created. -
**kwargs: Any - Key-value pairs to be serialized into JSON format. -
Context for Use:
-
This function creates a JSON file at the specified path and writes the provided key-value pairs into it.
-
Example Usage:
file_path = 'X:\\wdm-test\\Automatische_Prozessierung\\2673-6411\\metadata-journal.json'
kwargs = {"publisher": "MDPI AG"}
utils.create_json_file(file_path, **kwargs)
-
Example Output:
-
A JSON file named
metadata-journal.jsoncontaining:{ "publisher": "MDPI AG" }
utils.get_article_dir_path(doi, issn, is_temp=False)
-
Parameters:
-
doi: str - The Digital Object Identifier of the article. issn: str - The International Standard Serial Number of the journal.-
is_temp: bool, optional - Flag indicating whether the directory is temporary or permanent. Default isFalse. -
Context for Use:
-
This function generates the directory path for storing an article based on its DOI and journal ISSN.
-
Example Usage:
doi = '10.3390/app14104206'
issn = '2673-6411'
article_dir = utils.get_article_dir_path(doi, issn, is_temp=False)
-
Example Output:
-
python # When run on Windows Systems X:\wdm-test\Automatische_Prozessierung\2673-6411\app14104206 -
python # When run on Linux Based Systems X:/wdm-test/Automatische_Prozessierung/2673-6411/app14104206
utils.get_date_from_crossref_timestamp(timestamp_in_milliseconds)
-
Parameters:
-
timestamp_in_milliseconds: int - Timestamp in milliseconds. -
Context for Use:
-
This function converts a Unix timestamp in milliseconds to a date string in the format "YYYY-MM-DD".
-
Example Usage:
timestamp_in_milliseconds = 1715810490959
current_time = utils.get_date_from_crossref_timestamp(timestamp_in_milliseconds)
- Example Output:
'2024-05-16'
utils.get_module_dir_path(article_dir_path, deposit_date, module_name, is_temp=False, should_stop=False)
-
Parameters:
-
article_dir_path:pathlib.Path- The path to the article directory. deposit_date: str - The deposit date.module_name: str - The name of the module.is_temp: bool, optional - Whether the directory is temporary or permanent. Default isFalse.-
should_stop: bool, optional - Whether to indicate that processing should stop. Default isFalse. -
Context for Use:
-
This function constructs the directory path for a module, which can be either temporary or permanent. It can also mark the directory to indicate that processing should stop.
-
Example Usage:
doi = "10.3390/app14104206"
issn = "2673-6411"
article_dir = utils.get_article_dir_path(doi, issn, is_temp=False)
deposit_date = "2024-07-31"
module_name = "relaxng_scanner"
module_temp_dir = utils.get_module_dir_path(article_dir, deposit_date, module_name, is_temp=True)
module_temp_stop_dir = utils.get_module_dir_path(article_dir, deposit_date, module_name, should_stop=True)
-
Example Output:
-
```python # When run on Windows Systems X:\wdm-test\Automatische_Prozessierung\2673-6411\app14104206\temp-relaxng_scanner_2024-07-31
X:\wdm-test\Automatische_Prozessierung\2673-6411\app14104206\stop-processing-relaxng_scanner_2024-07-31 ```
-
```python # When run on Linux Based Systems X:/wdm-test/Automatische_Prozessierung/2673-6411/app14104206/temp-relaxng_scanner_2024-07-31
X:/wdm-test/Automatische_Prozessierung/2673-6411/app14104206/stop-processing-relaxng_scanner_2024-07-31 ```
utils.get_path_article_root()
-
Parameters:
-
None
-
Context for Use:
-
Returns the root path for articles from a non-backed-up data root.
-
Example Output:
-
python #When run on Windows Systems X:\wdm-test\Automatische_Prozessierung -
python #When run on Linux Based Systems X:/wdm-test/Automatische_Prozessierung
utils.call_api_with_error_handling(logger, func, *args, **args2)
-
Parameters:
-
logger: Prefect logger for logging messages. func: The function that handles the API calls.args: Parameters to be passed to the function.-
args2: Keyword arguments to be passed to the function. -
Context for Use:
-
Calls a remote API with exponential back-offs and exception handling to manage retries and log any errors
-
Example Usage:
def example_api_call(param1, param2):
# Simulate API call
return {"result": "success"}
fatal, result = call_api_with_error_handling(prefect_logger, example_api_call, "param1_value", param2="param2_value")
- Example Output:
False, {'result': 'success'}
utils.work_on_folder_copy(journal_folder, work_on_copy=False)
-
Parameters:
-
journal_folder:pathlib.Path- The path to the journal folder. -
work_on_copy: bool, optional - Indicates whether to work on a copy of the folder. Defaults toFalse. -
Context for Use:
-
Creates a copy of a given folder, appending the current date and timestamp to the copied folder's name if
work_on_copyisTrueotherwise returns thejournal_foldervalue. -
Example Usage:
issn = "2673-6411"
journal_path = utils.get_journal_dir_path(issn)
jorunal_copy_path = utils.work_on_folder_copy(test_journal_path, work_on_copy=True)
-
Example Output:
-
python #When run on Windows Systems X:\wdm-test\Automatische_Prozessierung\2673-6411-2024-05-24-1716540809 -
python #When run on Linux Based Systems X:/wdm-test/Automatische_Prozessierung/2673-6411-2024-05-24-1716540809
utils.delete_temp_module_directories(module_name: str, article_dir_path: pathlib.Path)
-
Parameters:
-
module_name: str - The name of the module. -
article_dir_path:pathlib.Path- The path to the article directory. -
Context for Use:
-
Deletes temporary directories created by failed runs of the specified module.
-
Example Usage:
module_name = "relaxng_scanner"
issn = "2673-6411"
journal_path = utils.get_journal_dir_path(issn)
for item in os.scandir(journal_path):
if os.path.isdir(item):
article_dir_path = pathlib.Path(item)
utils.delete_temp_module_directories(module_name, article_dir_path)
-
Example Output:
-
No output, deletes directories
utils.get_previous_module_latest(previous_module: str, article_dir_path: pathlib.Path)
-
Parameters:
-
previous_module: str - The name of the previous module. -
article_dir_path:pathlib.Path- The path to the article directory. -
Context for Use:
-
Retrieves the latest directory for a given previous module within the specified article directory.
-
Example Usage:
previous_module = "relaxng_scanner"
article_dir_path = pathlib.Path("X:\\wdm-test\\Automatische_Prozessierung\\2673-6411\\biochem1020007")
latest_previous_module = utils.get_previous_module_latest(previous_module, article_dir_path)
-
Example Output:
-
python #When run on Windows Systems X:\wdm-test\Automatische_Prozessierung\2673-6411\biochem1020007\relaxng_scanner_2021-07-28 -
python #When run on Linux Based Systems X:/wdm-test/Automatische_Prozessierung/2673-6411/biochem1020007/relaxng_scanner_2021-07-28
utils.is_previous_module_updated(module_name: str, latest_previous_module: pathlib.Path)
-
Parameters:
-
module_name: str - The name of the module. -
latest_previous_module:pathlib.Path- The path to the latest previous module directory. -
Context for Use:
-
Checks if the previous module has been updated by looking for a success indicator prefix (
processed-by-module-). -
Example Usage:
previous_module = "relaxng_scanner"
article_dir_path = pathlib.Path("X:\\wdm-test\\Automatische_Prozessierung\\2673-6411\\biochem1020007")
latest_previous_module = utils.get_previous_module_latest(previous_module, article_dir_path)
previous_module_updated = utils.is_previous_module_updated(module_name, latest_previous_module)
-
Example Output:
-
TrueorFalse
utils.is_previous_module_marked_not_be_processed(latest_previous_module: pathlib.Path)
-
Parameters:
-
latest_previous_module:pathlib.Path- The path to the latest previous module directory. -
Context for Use:
-
Checks if the previous module is marked with a
stop-processing-indicator. -
Example Usage:
previous_module = "relaxng_scanner"
article_dir_path = pathlib.Path("X:\\wdm-test\\Automatische_Prozessierung\\2673-6411\\biochem1020007")
latest_previous_module = utils.get_previous_module_latest(previous_module, article_dir_path)
previous_module_marked_not_to_be_processed = utils.is_previous_module_marked_not_be_processed(latest_previous_module)
-
Example Output:
-
TrueorFalse
utils.copy_content_from_previous_module(module_name: str, latest_previous_module: pathlib.Path)
-
Parameters:
-
module_name: str - The name of the module. -
latest_previous_module:pathlib.Path- The path to the latest previous module directory. -
Context for Use:
-
Copies the contents of the latest previous module directory to a new temporary directory for the current module.
-
Example Usage:
module_name = "toolbox_ids"
latest_previous_module = pathlib.Path("X:\\wdm-test\\Automatische_Prozessierung\\2673-6411\\biochem1020007\\relaxng_scanner_2021-07-28")
utils.copy_content_from_previous_module(module_name, latest_previous_module)
-
Example Output:
-
No output, copies directory contents.
utils.rename_module_temp_directory(module_name: str, latest_previous_module: pathlib.Path, mark_stop=False)
-
Parameters:
-
module_name: str - The name of the module. latest_previous_module:pathlib.Path- The path to the latest previous module directory.-
mark_stop: bool, optional - Indicates whether to mark the directory asstop-processing-. Defaults toFalse. -
Context for Use:
-
Renames the temporary directory for the current module to its permanent name.
-
Example Usage:
module_name = "toolbox_ids"
latest_previous_module = pathlib.Path("X:\\wdm-test\\Automatische_Prozessierung\\2673-6411\\biochem1020007\\relaxng_scanner_2021-07-28")
utils.rename_module_temp_directory(module_name, latest_previous_module, mark_stop=False)
-
Example Output:
-
No output, renames directory.
utils.delete_contents_previous_module(module_name: str, latest_previous_module: pathlib.Path)
-
Parameters:
-
module_name: str - The name of the module. -
latest_previous_module:pathlib.Path- The path to the latest previous module directory. -
Context for Use:
-
Deletes the contents of the latest previous module directory.
-
Example Usage:
module_name = "toolbox_ids"
latest_previous_module = pathlib.Path("X:\\wdm-test\\Automatische_Prozessierung\\2673-6411\\biochem1020007\\relaxng_scanner_2021-07-28")
utils.delete_contents_previous_module(module_name, latest_previous_module)
-
Example Output:
-
No output, deletes directory contents.
utils.mark_previous_module_processed(module_name: str, latest_previous_module: pathlib.Path)
-
Parameters:
-
module_name: str - The name of the module. -
latest_previous_module:pathlib.Path- The path to the latest previous module directory. -
Context for Use:
-
Creates a directory with the prefix
processed-by-module-plus themodule_name, within thelatest_previous_moduledirectory. -
Example Usage:
module_name = "toolbox_ids"
latest_previous_module = pathlib.Path("X:\\wdm-test\\Automatische_Prozessierung\\2673-6411\\biochem1020007\\relaxng_scanner_2021-07-28")
utils.mark_previous_module_processed(module_name, latest_previous_module)
-
Example Output:
-
No output, creates a directory like the following
X:\wdm-test\Automatische_Prozessierung\2673-6411\biochem1020007\relaxng_scanner_2021-07-28\processed-by-module-toolbox_ids.
utils.get_config_json()
-
Parameters:
-
None.
-
Context for Use:
-
Reads the config JSON file which contains the credentials for the different systems.
-
Example Usage:
config_harvesting = utils.get_config_json()
utils.get_module_stop_folder(module: str, article_dir_path: pathlib.Path)
-
Parameters:
-
module: str - The name of the module. -
article_dir_path:pathlib.Path- Path to the article directory. -
Context for Use:
-
Retrieves the stop folder for the specified module within the article directory if it exists.
-
Example Usage:
module_name = "relaxng_scanner"
article_dir = pathlib.Path("X:\\wdm-test\\Automatische_Prozessierung\\2673-6411\\biochem1010003")
stop_folder = utils.get_module_stop_folder(module_name, article_dir)
-
Example Output:
-
python #When run on Windows Systems X:/wdm-test/Automatische_Prozessierung/2673-6411/biochem1010003/stop-processing-relaxng_scanner_2021-04-05 -
python #When run on Linux Based Systems X:\wdm-test\Automatische_Prozessierung\2673-6411\biochem1010003\stop-processing-relaxng_scanner_2021-04-05
utils.write_harvesting_log(logger, module_name: str, module_dir_path: pathlib.Path, log_data: Dict[str, str])
-
Parameters:
-
logger: Logger instance for logging messages. module_name: str - Name of the module.module_dir_path:pathlib.Path- Path to the module directory.-
log_data:Dict[str, str]- Dictionary containing the log data. -
Context for Use:
-
Writes a harvesting log for the specified module.
-
Example Usage:
utils.write_harvesting_log(prefect_logger, module_name, temp_current_module_dir,{constants.HARVESTING_LOG_TOOLBOX_ID_KEY: text_id})
-
Example Output:
-
No output, creates or updates the log file with the provided log data.
Lower level functions in utils.py
These are functions that are solely called by other functions in the utils.py and should not be called from the modules itself
utils.get_path_article_root()
-
Parameters:
-
None
-
Context for Use:
-
Retrieves and returns the root directory path for storing articles.
-
Example Output:
-
python #When run on Windows Systems X:\wdm-test\Automatische_Prozessierung -
python #When run on Linux Based Systems X:/wdm-test/Automatische_Prozessierung
utils.get_journal_directory_name(issn)
-
Parameters:
-
issn: str - The International Standard Serial Number of the journal. -
Context for Use:
-
This function is intended to generate a directory name for a journal based on its ISSN. However, it currently returns the ISSN itself as the directory name.
-
Example Output:
-
python '2673-6411'
utils.get_article_directory_name(doi)
-
Parameters:
-
doi: str - The Digital Object Identifier of the article. -
Context for Use:
-
This function generates a directory name for an article based on its DOI.
-
Example Usage:
doi = '10.3390/app14104206'
article_dir_name = utils.get_article_directory_name(doi)
- Example Output:
'app14104206'
utils.get_article_temp_directory_name(doi)
-
Parameters:
-
doi: str - The Digital Object Identifier of the article. -
Context for Use:
-
This function generates a temporary directory name for storing the temporary data of an article based on its DOI.
-
Example Usage:
doi = '10.3390/app14104206'
article_dir_name = utils.get_article_temp_directory_name(doi)
- Example Output:
'temp-app14104206'
utils.get_module_temp_directory_name(module_name, deposit_date)
-
Parameters:
-
module_name: str - The name of the module. -
deposit_date: str - The deposit date. -
Context for Use:
-
This function generates a name for a temporary module directory, used during processing before finalizing the module directory.
-
Example Usage:
module_name = "crossrefcrawler"
deposit_date = "2024-07-31"
module_dir_name = utils.get_module_temp_directory_name(module_name, deposit_date)
- Example Output:
'temp-crossrefcrawler_2024-07-31'
utils.get_module_directory_name(module_name, deposit_date)
-
Parameters:
-
module_name: str - The name of the module. -
deposit_date: str - The deposit date. -
Context for Use:
-
This function generates a name for a module directory based on the module name and deposit date.
-
Example Usage:
module_name = "relaxng_scanner"
deposit_date = "2024-07-31"
module_dir_name = utils.get_module_directory_name(module_name, deposit_date)
- Example Output:
'relaxng_scanner_2024-07-31'
utils.get_previous_modules(previous_module: str, article_dir_path: pathlib.Path)
-
Parameters:
-
previous_module: str - The name of the previous module. -
article_dir_path:pathlib.Path- The path to the article directory. -
Context for Use:
-
Retrieves a list of directories for a given previous module within the specified article directory.
-
Example Usage:
previous_module = "relaxng_scanner"
article_dir_path = pathlib.Path("X:\\wdm-test\\Automatische_Prozessierung\\2673-6411\\biochem1020007")
folders_list = utils.get_previous_modules(previous_module, article_dir_path)
- Example Output:
['X:\\wdm-test\\Automatische_Prozessierung\\2673-6411\\biochem1020007\\relaxng_scanner_2021-07-28']
utils.get_module_suffix_from_path(latest_previous_module: pathlib.Path)
-
Parameters:
-
latest_previous_module:pathlib.Path- The path to the latest previous module directory. -
Context for Use:
-
Extracts the module suffix from the latest previous module directory's name.
-
Example Usage:
latest_previous_module = pathlib.Path("X:\\wdm-test\\Automatische_Prozessierung\\2673-6411\\biochem1020007\\relaxng_scanner_2021-07-28")
module_suffix = utils.get_module_suffix_from_path(latest_previous_module)
- Example Output:
'2021-07-28'
utils.get_module_temp_directory(module_name: str, latest_previous_module: pathlib.Path)
-
Parameters:
-
module_name: str - The name of the module. -
latest_previous_module:pathlib.Path- The path to the latest previous module directory. -
Context for Use:
-
Constructs the path to the temporary directory for the current module based on the suffix of the latest previous module directory.
-
Example Usage:
module_name = "toolbox_ids"
latest_previous_module = pathlib.Path("X:\\wdm-test\\Automatische_Prozessierung\\2673-6411\\biochem1020007\\relaxng_scanner_2021-07-28")
temp_current_module_dir = utils.get_module_temp_directory(module_name, latest_previous_module)
-
Example Output:
-
python #When run on Windows Systems X:\wdm-test\Automatische_Prozessierung\2673-6411\biochem1020007\temp-toolbox_ids_2021-07-28 -
python #When run on Linux Based Systems X:/wdm-test/Automatische_Prozessierung/2673-6411/biochem1020007/temp-toolbox_ids_2021-07-28
utils.get_module_perm_directory(module_name: str, latest_previous_module: pathlib.Path, mark_stop=False)
-
Parameters:
-
module_name: str - The name of the module. latest_previous_module:pathlib.Path- The path to the latest previous module directory.-
mark_stop: bool, optional - Indicates whether to mark the directory asstop-processing-. Defaults toFalse. -
Context for Use:
-
Constructs the path to the permanent directory for the current module based on the suffix of the latest previous module directory.
-
Example Usage:
module_name = "toolbox_ids"
latest_previous_module = pathlib.Path("X:\\wdm-test\\Automatische_Prozessierung\\2673-6411\\biochem1020007\\relaxng_scanner_2021-07-28")
perm_current_module_dir = utils.get_module_perm_directory(module_name, latest_previous_module, mark_stop=False)
-
Example Output:
-
python #When run on Windows Systems X:\wdm-test\Automatische_Prozessierung\2673-6411\biochem1020007\toolbox_ids_2021-07-28 -
python #When run on Linux Based Systems X:/wdm-test/Automatische_Prozessierung/2673-6411/biochem1020007/toolbox_ids_2021-07-28
utils.get_harvesting_log_dir()
-
Parameters:
-
None.
-
Context for Use:
-
Returns the directory where the harvesting logs are stored. Creates the directory if it does not exist.
-
Example Usage:
harvesting_log_dir = utils.get_harvesting_log_dir()
-
Example Output:
-
python #When run on Windows Systems W:\wdm-test\harvesting_logs -
python #When run on Linux Based Systems W:/wdm-test/harvesting_logs