Skip to content

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 target_dir.

migrate

Migrate files shipped with the Moose package to the user directory.

download_databases

Download databases from the Moose repository to the moose-spectra folder in the user directory.

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

GITHUB_API_URL module-attribute

GITHUB_API_URL = 'https://api.github.com'

REPO module-attribute

REPO = 'AntoineTUE/Moose'

SRC_DATA_PATH module-attribute

SRC_DATA_PATH = 'src/Moose/data'

USER_DIR module-attribute

USER_DIR = Path.home().joinpath('moose-spectra')

HAS_BEEN_NOTIFIED module-attribute

HAS_BEEN_NOTIFIED = False

FileExistsBehavior

Bases: Flag

Enum to define behavior when a file already exists in the target directory.

Attributes:

Name Type Description
OVERWRITE
SKIP
VALIDATE
RAISE
Source code in src/Moose/utils/db_io.py
class FileExistsBehavior(Flag):
    """Enum to define behavior when a file already exists in the target directory."""

    OVERWRITE = auto()
    SKIP = auto()
    VALIDATE = auto()
    RAISE = auto()

OVERWRITE class-attribute instance-attribute

OVERWRITE = auto()

SKIP class-attribute instance-attribute

SKIP = auto()

VALIDATE class-attribute instance-attribute

VALIDATE = auto()

RAISE class-attribute instance-attribute

RAISE = auto()

generate_chunks_and_hash

generate_chunks_and_hash(buff, size: int, chunk_size=_CHUNK_SIZE) -> tuple[Iterable[bytes], 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
def generate_chunks_and_hash(buff, size: int, chunk_size=_CHUNK_SIZE) -> tuple[Iterable[bytes], 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:
    ```python
    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()
    ```
    """
    sha1: HASH = hashlib.sha1()
    sha1.update(f"blob {size}\0".encode())

    def iterator():
        bytes_read = 0
        if hasattr(buff, "read"):  # file-like object
            while chunk := buff.read(chunk_size):
                sha1.update(chunk)
                bytes_read += len(chunk)
                yield chunk
        else:  # requests.Response
            for chunk in buff.iter_content(chunk_size=chunk_size):
                if not chunk:
                    continue
                sha1.update(chunk)
                bytes_read += len(chunk)
                yield chunk
        if bytes_read != size:
            raise ValueError(f"Size mismatch when reading stream. Expected {size=}, {bytes_read=}.")

    return iterator(), sha1

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
def hash_file(file: Path):
    """Compute a file hash, for comparing files based on contents."""
    with file.open("rb") as fo:
        chunks, hashed = generate_chunks_and_hash(fo, file.stat().st_size)
        # iterate over chucks to compute the hash
        for _ in chunks:
            pass
    return hashed.hexdigest()

list_data_files_in_repo

list_data_files_in_repo(ses: Session, branch='main') -> list[dict]

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'.

'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
def list_data_files_in_repo(ses: Session, branch="main") -> list[dict]:
    """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

    Args:
        ses (Session):  A persistent requests Session
        branch (str):   The name of the git branch used for the look up, default: `'main'`.

    Returns:
        info (list[dict]):  A list of dictionaries with info per file that existst under the path.
    """
    response = ses.get(f"{GITHUB_API_URL}/repos/{REPO}/contents/{SRC_DATA_PATH}", params={"ref": branch})
    response.raise_for_status()
    return response.json()

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 mode=FileExistsBehavior.RAISE

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
def download_file(
    ses: Session, info: dict[str, str | int], target_dir: Path, mode: FileExistsBehavior = 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.

    Args:
        ses (Session):  A persistent requests Session
        info (dict):    A dict with file path information as retrieved from the GitHub API.
        target_dir (Path):  The target directory to store the file in, if download succeeds
        mode (FileExistsBehavior): Behavior when the file already exists (default: FileExistsBehavior.VALIDATE)

    Raises:
        OSError:    raised when the target file already exists and `mode=FileExistsBehavior.RAISE`
        ValueError: raised when there is a mismatch in the SHA hash reported by GitHub and the actual download.
    """
    name: str = info["name"]  # ty:ignore[invalid-assignment]
    file_path = target_dir.joinpath(name)
    if file_path.exists():
        if mode == FileExistsBehavior.RAISE:
            raise OSError(f"File {name} already exists.")
        elif mode == FileExistsBehavior.SKIP:
            return  # skip download
        elif mode == FileExistsBehavior.VALIDATE:
            existing_hash = hash_file(file_path)
            if existing_hash == info["sha"]:
                return  # skip download if hash matches

    response: Response = ses.get(info["download_url"], stream=True, timeout=30)
    response.raise_for_status()

    with file_path.open("wb") as f, tqdm(total=info["size"], unit="B", unit_scale=True, desc=name, leave=False) as pbar:
        chunks, sha1 = generate_chunks_and_hash(response, info["size"])  # ty:ignore[invalid-argument-type]
        for chunk in chunks:
            f.write(chunk)
            pbar.update(len(chunk))
    if sha1.hexdigest() != info["sha"]:
        raise ValueError(
            f"Hash of downloaded file {file_path} does not match {info['sha']}, this indicates a download error."
        )

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
def download_databases_from_repo(
    target_dir: Path | None = None, mode: FileExistsBehavior = 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`.

    Args:
        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][..]
        mode (FileExistsBehavior): Behavior when the file already exists (default: FileExistsBehavior.VALIDATE)
    """
    target_dir = USER_DIR if target_dir is None else target_dir
    if not target_dir.exists():
        raise OSError(f"{target_dir} does not exist, please create it first")
    session = Session()
    file_info = list_data_files_in_repo(session)
    errors = {}
    for info in tqdm(file_info):
        try:
            download_file(session, info, target_dir, mode=mode)
        except (OSError, ValueError) as e:
            errors[info["name"]] = e.args[0]
    if errors != {}:
        print("Notice:")
        for file, error in errors.items():
            print(f"\t- {file}: {error}")

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
def migrate_file(file: Path, target_dir: Path, mode: FileExistsBehavior = 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.
    """
    if mode == FileExistsBehavior.VALIDATE:
        raise ValueError(
            "Cannot validate file when migrating, please use `mode=FileExistsBehavior.SKIP` or `mode=FileExistsBehavior.OVERWRITE`."
        )
    if file.is_dir():
        raise OSError(f"{file=} is a directory, please provide a file path.")
    if not target_dir.is_dir() or not target_dir.exists():
        raise OSError(f"{target_dir=} is not an existing directory.")
    target_file = target_dir.joinpath(file.name)
    if target_file.exists():
        if mode == FileExistsBehavior.RAISE:
            raise OSError(
                f"A file named {file.name} already exists in {target_dir}. Set `mode=FileExistsBehavior.SKIP` if you want to ignore this error."
            )
        elif mode == FileExistsBehavior.SKIP:
            return  # skip migration
    shutil.copy2(file, target_file)

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
def migrate(mode: FileExistsBehavior = FileExistsBehavior.SKIP):
    """Migrate files shipped with the Moose package to the user directory.

    Will create the directory `~/moose-spectra` if missing.
    """
    if mode == FileExistsBehavior.VALIDATE:
        raise ValueError(
            "Cannot validate files when migrating, please use `mode=FileExistsBehavior.SKIP` or `mode=FileExistsBehavior.OVERWRITE`."
        )
    if _dir_missing := not USER_DIR.exists():
        USER_DIR.mkdir(exist_ok=True)  # Do not raise if exist, though it should not be needed at this point.
    _, pkg_files = _find_packaged_data()
    for file in pkg_files:
        try:
            migrate_file(file, target_dir=USER_DIR, mode=mode)
        except OSError as e:
            if "already exists" in e.args[0] and mode == FileExistsBehavior.SKIP:
                continue
            else:
                raise

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
def download_databases(mode: FileExistsBehavior = 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.
    """
    if not USER_DIR.exists():
        USER_DIR.mkdir(exist_ok=True, parents=True)  # Do not raise if exist, though it should not at this point.
    if mode != FileExistsBehavior.OVERWRITE:
        # only migrate if this will not be overwritten
        migrate(mode=FileExistsBehavior.SKIP)
    download_databases_from_repo(USER_DIR, mode=mode)

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
def 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][..] directly, as this function will allow the change to propagate.
    """
    if not path.is_dir():
        if not mkdir:
            raise ValueError(f"{path} is not a directory, or does not exist.")
        path.mkdir(parents=True, exist_ok=True)
    global USER_DIR
    USER_DIR = path

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
def 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.
    """
    global USER_DIR
    if not USER_DIR.exists():
        # Avoid updating DEFAULT_USER_DIR, so migrations still can happen.
        package_dir, _ = _find_packaged_data()
        notify_data_dir_migration()
        return package_dir
    return USER_DIR

get_database_files

get_database_files() -> list[str]

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
def get_database_files() -> list[str]:
    """List the database names available to Moose out-of-the-box.

    Relies on the [DEFAULT_USER_DIR][..] and/or [set_database_path][..] and [get_database_path][..] to find the available files.
    """
    path = get_database_path()
    return [p.stem for p in path.glob("*.db")]

notify_data_dir_migration

notify_data_dir_migration()

Notify user to migrate data.

Source code in src/Moose/utils/db_io.py
def notify_data_dir_migration():
    """Notify user to migrate data."""
    global HAS_BEEN_NOTIFIED
    if not HAS_BEEN_NOTIFIED:
        print("Moose is moving towards storing databases in the user folder, instead of bundling them.")
        print("See:\n\thttps://github.com/AntoineTUE/Moose/issues/2\n")
        print(f"The user folder is: {USER_DIR}.")
        print("This folder does not exist or is empty.\nRun either of these functions to migrate:\n")
        print("\t> Moose.utils.db_io.migrate")
        print("\t> Moose.utils.db_io.download_databases")
        HAS_BEEN_NOTIFIED = True