Skip to content

The utils module

This module provides some utility function needed when dealing with GAMS bags.

gamslib.sip.utils

Utility functions for the GAMS SIP package creation and validation.

Provides helpers for validating object directories, extracting IDs, calculating hashes, counting files and bytes, and fetching JSON schemas for validation.

Features
  • Validates object directory structure and required files.
  • Extracts and validates object and datastream IDs.
  • Calculates MD5, SHA512 hashes for files.
  • Counts files and bytes in a directory tree.
Usage

Use validate_object_dir(object_path) to check an object directory. Use extract_id(path) to extract and validate an object or datastream ID. Use md5hash(file), sha512hash(file), or sha256hash(file) for file checksums. Use count_bytes(root_dir) or count_files(root_dir) for directory statistics.

count_bytes

count_bytes(root_dir)

Count the number of bytes of all files below root_dir.

PARAMETER DESCRIPTION
root_dir

Directory to count bytes in.

TYPE: Path

RETURNS DESCRIPTION
int

Total number of bytes in all files.

TYPE: int

count_files

count_files(root_dir)

Count the number of all files below root_dir.

PARAMETER DESCRIPTION
root_dir

Directory to count files in.

TYPE: Path

RETURNS DESCRIPTION
int

Total number of files.

TYPE: int

fetch_json_schema cached

fetch_json_schema(url)

Fetch a JSON schema from a URL.

PARAMETER DESCRIPTION
url

URL to fetch the JSON schema from.

TYPE: str

RETURNS DESCRIPTION
dict

Parsed JSON schema.

TYPE: dict

RAISES DESCRIPTION
BagValidationError

If the schema cannot be fetched or is not valid JSON.

is_bag

is_bag(bag_path)

Check if the given path points to a Bag.

It does not check the validity of the Bag, only if the structure indicates that it looks like a Bag.

bag_path can be either a directory or a file (zip).

PARAMETER DESCRIPTION
bag_path

The path to the directory to check.

TYPE: Path

RETURNS DESCRIPTION
bool

True if the path points to a Bag, False otherwise.

TYPE: bool

md5hash

md5hash(file)

Calculate the MD5 hash of a file.

PARAMETER DESCRIPTION
file

Path to the file.

TYPE: Path

RETURNS DESCRIPTION
str

MD5 hash as a hexadecimal string.

TYPE: str

sha512hash

sha512hash(file)

Calculate the SHA512 hash of a file.

PARAMETER DESCRIPTION
file

Path to the file.

TYPE: Path

RETURNS DESCRIPTION
str

SHA512 hash as a hexadecimal string.

TYPE: str