Skip to content

Commit

Permalink
Merge pull request #55 from WenjieDu/dev
Browse files Browse the repository at this point in the history
Apply a new strategy to determine the home path
  • Loading branch information
WenjieDu authored Jan 15, 2024
2 parents b50eb9b + c387ebe commit 507ed23
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 12 deletions.
21 changes: 16 additions & 5 deletions tsdb/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
11 changes: 4 additions & 7 deletions tsdb/utils/downloading.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 507ed23

Please sign in to comment.