Moose.utils.db_io
Module for downloading line-by-line databases for use in Moose.
Currently Moose ships with some database files bundled.
This has some issues
- The size of the package is large (80 MB).
- Each install or update of Moose downloads or copies the data
- The default search path for the databases is the install location of Moose, which would be opaque to the user.
- Searching for the install location in the current environment can be very slow, increasing import times.
To address these issues, this module handles downloading of these database files, and will store them in a more standardized location.
The location is a subfolder in the user directory, depending on the OS:
%USERPROFILE%/moose-spectra
~/moose-spectra
~/moose-spectra
To get started with Moose in the future, a user will first need to call the [download_databases][Moose.utils.download_databases] function, or put their own files in the database folder.
This will become the norm in a future release, after a grace period (to be decided).
In the meantime, it will be possible to migrate the bundled databases to the new location, using either [migrate][Moose.utils.migrate] or [download_databases][Moose.utils.download_databases]
Classes:
| Name | Description |
|---|---|
FileExistsBehavior |
Enum to define behavior when a file already exists in the target directory. |
Functions:
| Name | Description |
|---|---|
generate_chunks_and_hash |
Stream the contents of a file-like object or Response, while hashing the contents. |
hash_file |
Compute a file hash, for comparing files based on contents. |
list_data_files_in_repo |
Retrieve the list of files in the data directory in the Moose repository. |
download_file |
Download a file from the Moose repo based on information returned from the GitHub API. |
download_databases_from_repo |
Download all databases and ancillary files from the Moose git repository to the target directory. |
migrate_file |
Copy a file to the directory |
migrate |
Migrate files shipped with the Moose package to the user directory. |
download_databases |
Download databases from the Moose repository to the |
set_database_path |
Set the path where Moose searches for databases by default. |
get_database_path |
Get the path where Moose searches for databases by default. |
get_database_files |
List the database names available to Moose out-of-the-box. |
notify_data_dir_migration |
Notify user to migrate data. |
Attributes:
| Name | Type | Description |
|---|---|---|
GITHUB_API_URL |
|
|
REPO |
|
|
SRC_DATA_PATH |
|
|
USER_DIR |
|
|
HAS_BEEN_NOTIFIED |
|
FileExistsBehavior
generate_chunks_and_hash
Stream the contents of a file-like object or Response, while hashing the contents.
Uses the same hashing as git, so it can be compared to files in the repo.
See also: https://git-scm.com/book/ms/v2/Git-Internals-Git-Objects
Note
The hash digest should only be called after the iterator has been exhausted and the full content has been processed.
If the iterator is exhausted but the size is different from size, a ValueError is raised.
Example:
file = Path("test.tmp")
with file.open("rb") as fo:
chunks, sha1 = stream_with_hash(fo, file.stat().st_size)
for chunk in chunks:
pass
digest = sha1.hexdigest()
Source code in src/Moose/utils/db_io.py
hash_file
hash_file(file: Path)
Compute a file hash, for comparing files based on contents.
Source code in src/Moose/utils/db_io.py
list_data_files_in_repo
Retrieve the list of files in the data directory in the Moose repository.
Uses the public GitHub API, without need for auth.
For each file, returns a dictionary with useful keys such as: * name * sha * size * download_url * type
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ses
|
Session
|
A persistent requests Session |
required |
branch
|
str
|
The name of the git branch used for the look up, default: |
'main'
|
Returns:
| Name | Type | Description |
|---|---|---|
info |
list[dict]
|
A list of dictionaries with info per file that existst under the path. |
Source code in src/Moose/utils/db_io.py
download_file
download_file(
ses: Session, info: dict[str, str | int], target_dir: Path, mode: FileExistsBehavior = VALIDATE
)
Download a file from the Moose repo based on information returned from the GitHub API.
If mode=FileExistsBehavior.SKIP and the file already exists, the download will be skipped.
Similarly, if mode=FileExistsBehavior.VALIDATE, but the file hash on disk is the same as upstream, the files are already the same and nothing will be downloaded.
Finally, if data is downloaded, it checks if the file SHA hash matches the expected hash to verify the download.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ses
|
Session
|
A persistent requests Session |
required |
info
|
dict
|
A dict with file path information as retrieved from the GitHub API. |
required |
target_dir
|
Path
|
The target directory to store the file in, if download succeeds |
required |
mode
|
FileExistsBehavior
|
Behavior when the file already exists (default: FileExistsBehavior.VALIDATE) |
VALIDATE
|
Raises:
| Type | Description |
|---|---|
OSError
|
raised when the target file already exists and |
ValueError
|
raised when there is a mismatch in the SHA hash reported by GitHub and the actual download. |
Source code in src/Moose/utils/db_io.py
download_databases_from_repo
download_databases_from_repo(target_dir: Path | None = None, mode: FileExistsBehavior = VALIDATE)
Download all databases and ancillary files from the Moose git repository to the target directory.
These are the files in the repo path src/Moose/data.
Note
This will download all database files, but also files like data_sources.txt.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
target_dir
|
Path | None
|
The target directory to download to. Must be a directory, not a file path. If missing, falls back to USER_DIR |
None
|
mode
|
FileExistsBehavior
|
Behavior when the file already exists (default: FileExistsBehavior.VALIDATE) |
VALIDATE
|
Source code in src/Moose/utils/db_io.py
migrate_file
migrate_file(file: Path, target_dir: Path, mode: FileExistsBehavior = SKIP)
Copy a file to the directory target_dir.
This fuction is intended to migrate bundled files to the new moose-spectra folder in the user directory.
Source code in src/Moose/utils/db_io.py
migrate
migrate(mode: FileExistsBehavior = SKIP)
Migrate files shipped with the Moose package to the user directory.
Will create the directory ~/moose-spectra if missing.
Source code in src/Moose/utils/db_io.py
download_databases
download_databases(mode: FileExistsBehavior = VALIDATE)
Download databases from the Moose repository to the moose-spectra folder in the user directory.
Will first attempt to migrate the files shipped with Moose to avoid downloading.
This latter behaviour will be deprecated after a transition period, when Moose stops shipping the databases.
Source code in src/Moose/utils/db_io.py
set_database_path
set_database_path(path: Path, mkdir=False)
Set the path where Moose searches for databases by default.
The default location is ~/moose-spectra, but this function lets you point to another location.
Note that this does not persist and you will need to do this each session.
Use this over setting [DEFAULT_USER_DIR][Moose.utils.db_io.DEFAULT_USER_DIR] directly, as this function will allow the change to propagate.
Source code in src/Moose/utils/db_io.py
get_database_path
get_database_path() -> Path
Get the path where Moose searches for databases by default.
Defaults to the directory ~/moose-spectra, but can be changed with set_database_path.
If this folder does not exist, it falls back to package folder.
Source code in src/Moose/utils/db_io.py
get_database_files
List the database names available to Moose out-of-the-box.
Relies on the [DEFAULT_USER_DIR][Moose.utils.db_io.DEFAULT_USER_DIR] and/or set_database_path and get_database_path to find the available files.
Source code in src/Moose/utils/db_io.py
notify_data_dir_migration
Notify user to migrate data.