Zum Inhalt

constants.py

Purpose

constants.py is a single file of module-level string/number (sensitive information) assignments. It is imported by nearly every module in the repository (import constants) and exists to give every hardcoded string in the codebase a single, named, searchable definition.

The general format in which this file must be structured as, can be found at harvesting-config(skeleton).json

How this keeps secrets out of the codebase

constants.py never contains a real credential, URL, or API key. What it contains instead are the key names the code will use to look one up at runtime. For example:

# constants.py
ARCHIVEMATICA_API_KEY = "archivematica_api_key"
TOOLBOX_USERNAME = "toolbox_username"
TOOLBOX_PASSWORD = "toolbox_password"

These are just strings that happen to be dictionary keys. The actual API key, username, and password live only in one place: a JSON config file on the NAS (config_harvesting.json), which is not part of this git repository. A module retrieves the real secret like this:

config = utils.get_config_json()          # reads the NAS-hosted config JSON at runtime
api_key = config[constants.ARCHIVEMATICA_API_KEY]   # looks up the real value by key name

utils.get_config_json() (in utils.py) is what actually opens and parses that file:

def get_config_json():
    data_root = get_data_root()
    config_file_path = data_root.joinpath(constants.HARVESTING_CONFIG_FILE)
    with open(config_file_path, 'r') as json_file:
        config = json.load(json_file)
    return config

So the split of responsibilities is:

Lives in the repo Lives only on the NAS
What The name of each config key, e.g. "archivematica_api_key" The value behind that key, e.g. the real API key string
Where constants.py config_harvesting.json (path defined by HARVESTING_CONFIG_FILE)
Accessed via constants.ARCHIVEMATICA_API_KEY config[constants.ARCHIVEMATICA_API_KEY] after calling utils.get_config_json()

This pattern shows up throughout the repo — e.g. TOOLBOX_USERNAME/TOOLBOX_PASSWORD (toolbox_ids, toolbox_conversion), ARCHIVEMATICA_BASE_URL/ARCHIVEMATICA_API_KEY (archivematica_ingest, archivematica_parallel_ingest), GITLAB_KONVERTIERUNGSSKRIPTE_URL (used to fetch the latest commit hash for history.csv's Version field).

If writing a new module that needs a secret or an environment-specific value (eg, an API key, a hostname, a username): add a new constant here naming the config key, add the real value to the NAS config JSON (never to a file that gets committed), and read it via utils.get_config_json()[constants.YOUR_NEW_CONSTANT].

The other things this file defines

Besides config-key names, constants.py also centralizes:

  • CrossRef metadata field names (e.g. CROSSREF_DOI_ARTICLE = "DOI") — used when reading metadata-article.json, so field names aren't retyped as raw strings in every module that touches metadata.
  • Folder-naming and lifecycle conventions (e.g. TEMP_DIRECTORY_IDENTIFIER = "temp-", DONT_PROCESS_INDICATOR = 'stop-processing-', MODULE_PROCESSING_SUCCESS_PREFIX = "processed-by-module-") — the building blocks utils.py's folder-path functions (get_module_temp_directory, get_module_perm_directory, etc.) are built from. If you're writing a new module, you generally won't need to reference these directly — you'll call the utils.py functions that already use them.
  • Standard file names written/read by specific modules (ARTICLE_METADATA_FILE_NAME, DOCUMENTATION_FILE_NAME for history.csv, TOOLBOX_TEXT_ID_FILE, etc.).