From 4417737dd5d93f182b8192795341552662efc738 Mon Sep 17 00:00:00 2001 From: Wenjie Du Date: Sun, 7 Jan 2024 11:06:18 +0800 Subject: [PATCH 1/2] refactor: raise error directly; --- tsdb/utils/downloading.py | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/tsdb/utils/downloading.py b/tsdb/utils/downloading.py index fff06d6..5479cd1 100644 --- a/tsdb/utils/downloading.py +++ b/tsdb/utils/downloading.py @@ -57,19 +57,16 @@ def _download_and_extract(url: str, saving_path: str) -> Optional[str]: except Exception as e: shutil.rmtree(saving_path, ignore_errors=True) shutil.rmtree(raw_data_saving_path, ignore_errors=True) - logger.info(f"Exception: {e}\n" f"Download failed. Aborting.") - raise + raise RuntimeError(f"Exception: {e}\n" f"Download failed. Aborting.") except KeyboardInterrupt: shutil.rmtree(saving_path, ignore_errors=True) shutil.rmtree(raw_data_saving_path, ignore_errors=True) - logger.info("Download cancelled by the user.") - raise + raise KeyboardInterrupt("Download cancelled by the user.") logger.info(f"Successfully downloaded data to {raw_data_saving_path}") - if ( - suffix in supported_compression_format - ): # if the file is compressed, then unpack it + # if the file is compressed, then unpack it + if suffix in supported_compression_format: try: os.makedirs(saving_path, exist_ok=True) shutil.unpack_archive(raw_data_saving_path, saving_path) From c387ebe2ccf5946c2f3ee94c90c583cca156605e Mon Sep 17 00:00:00 2001 From: Wenjie Du Date: Sun, 7 Jan 2024 15:53:20 +0800 Subject: [PATCH 2/2] feat: apply new strategy to determine the home dir path; --- tsdb/database.py | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/tsdb/database.py b/tsdb/database.py index 5af1380..41fce85 100644 --- a/tsdb/database.py +++ b/tsdb/database.py @@ -14,18 +14,29 @@ tsdb_config_path = os.path.join(os.path.dirname(__file__), "config.ini") config.read(tsdb_config_path) -old_cached_dataset_dir = os.path.join(os.path.expanduser("~"), ".tsdb_cached_datasets") -CACHED_DATASET_DIR = os.path.join( - os.path.expanduser("~"), config.get("path", "data_home") -) +data_home_path = os.path.abspath(config.get("path", "data_home")) +old_cached_dataset_dir = os.path.abspath("~/.tsdb_cached_datasets") + if os.path.exists(old_cached_dataset_dir): + # use the old path and warn the user logger.warning( "‼️ Detected the home dir of the old version TSDB. " "Since v0.3, TSDB has changed the default cache dir to '~/.tsdb'. " "You can migrate downloaded datasets by invoking the new function " - f"tsdb.migrate(old='~/.tsdb_cached_datasets', new={CACHED_DATASET_DIR})" + "tsdb.migrate(old='~/.tsdb_cached_datasets', new='~/.tsdb')" ) CACHED_DATASET_DIR = old_cached_dataset_dir +elif os.path.exists(data_home_path): + # use the path directly, may be in a portable disk + CACHED_DATASET_DIR = data_home_path +else: + # use the default path + default_path = os.path.abspath("~/.tsdb") + CACHED_DATASET_DIR = default_path + logger.warning( + f"‼️ The preset data_home path '{data_home_path}' doesn't exist. " + f"Using the default path '{default_path}'." + ) _DATABASE = {