Skip to content

The projectconfiguration subpackage

The projectconfiguration submodule provides code to create and use configuration settings for GAMS projects.

The package provides a few useful functions:

  • get_configuration(): Returns a (cached) Configuration object
  • configuration_needs_update(): Returns True, if the format of the existing configuration is deprecated (e.g. because we have added new fields)
  • update_configuration(): Updates the existing configuration by inserting new fields from the new schema.

Here is an example how to retrieve the configuration object:

from gamslib import projectconfiguration

cfg = get_configuration()

Check the docstrings for more information!

Here is what I have extracted from the docstrings:

gamslib.projectconfiguration

A helper module for GAMS project configuration management.

This module provides a central way to access and update project configuration data for GAMS projects. On subpackage level, it provides the following functions:

  • get_configuration(): Retrieve the current project configuration as a Configuration object. Call this function to access configuration values in your code. It is implemented as module level function with caching to ensure that the configuration is loaded only once per session. The configuration is sourced from multiple locations with a clear precedence order. See below for more details.
  • MissingConfigurationException: Exception raised when no configuration file is found.
  • configuration_needs_update(): Check if the current configuration file is missing required keys compared to the template.
  • update_configuration(): Update the configuration file with missing keys from the template. This might become useful when the template is updated with new keys and you want to update your existing configuration file without losing existing values.

The get_configuration() function returns a Configuration object which represents the complete configuration for a GAMS project. The coniguration data is organized in tables (e.g., metadata, general) and each table contains specific fields(e.g.,metadata.publisher,general.loglevel`).

Currently, these fields are defined in the Configuration class:

  • metadata.project_id: the id of the project (eg.: hsa)
  • metadata.creator: the creator of the project (e.g. the name of the principal investigator)
  • metadata.funder: the funder of the project (e.g. FWF)
  • metadata.rights: the default license for the project (can be overridden in project.csv and datastream.csv for single objects/data streams. Default is 'CC BY-NC 4.0', which should be kept if you are using GAMS to publish your data)
  • metadata.publisher: the publisher of the project (default is GAMS, which should be kept if you are using GAMS to publish your data)

  • general.loglevel: the logging level. Default is "info". Possible values are "debug", "info", "warning", "error" and "critical".

  • general.dsid_keep_extension: whether to keep the file extension for dsid. We suggest to keep the extension, because it makes it easier to identify the file type of the datastreams. Set to false if you want to strip the extension for dsid. This field is used when creating datastreams.csv.
  • general.format_detector="siegfried: the format detector to use. You might want to keep this unless for good reasons because the older detectors might be removed in the future.
  • general.format_detector_url: the URL for the format detector service. Currently not used, so keep it empty.
  • general.ds_ignore_files: a list of filenames/filename patterns which should be ignored when creating datastreams.csv. This is useful to exclude files which might be in the object directory but but should not be added as datastreams. object.csvand datastreams.csv are automatically ignored.

Main features:

This object aggregates configuration data from multiple sources, including the gamsproject.toml file, .env file, and environment variables in this order of precedence, meaning that values from .env override those in project.toml, and environment variables override both.

To override configuration values in the .env file, create a .env file in the current working directory. Each field in the configuration can be overridden by adding a line in the .env file in the format GAMSCFG_<TABLE>.<FIELD>=value. For example, to override the creator, add GAMSCFG_METADATA.CREATOR=John Doe.

To override configuration values using environment variables, set environment variables with the prefix GAMSCFG_ followed by the table and field name in uppercase. For example, to override the creator, set GAMSCFG_METADATA.CREATOR=Jane Doe.

The gamsconfig.toml file containing the project configuration should be located in the project directory, the current working directory or a a location specified by the GAMSCFG_PROJECT_TOML environment variable.

__all__ module-attribute

__all__ = ['Configuration', 'MissingConfigurationException', 'configuration_needs_update', 'get_configuration', 'update_configuration']

MissingConfigurationException

MissingConfigurationException(message="You must provide a configuration file. Set it when calling the get_configuration() function, use the 'GAMSCFG_PROJECT_TOML' environment variable or set 'project_toml' in the .env file.")

Bases: Exception

Raised if the configuration is missing.

message instance-attribute

message = message

configuration_needs_update

configuration_needs_update(config_file)

Check if the given configuration file is missing required keys compared to the template.

This function compares the structure of the provided config file against the template 'project.toml' included with the package. It returns True if any required keys from the template are missing in the config file, and False if all required keys are present.

PARAMETER DESCRIPTION
config_file

Path to the configuration file to check.

TYPE: Path

RETURNS DESCRIPTION
bool

True if the config file is missing keys and needs to be updated, False otherwise.

TYPE: bool

Notes
  • Only missing keys are detected; extra keys or differing values are ignored.
  • If the config file does not exist, returns False.
  • Nested keys are checked recursively.

update_configuration

update_configuration(config_file)

Update the configuration file by adding missing entries from the template.

Compares the provided config file to the template 'project.toml' included with the package. Any keys present in the template but missing from the config file are added, preserving existing values and comments. The function does not remove or modify existing entries—only additions are handled.

A backup of the original config file is created before any changes are made.

PARAMETER DESCRIPTION
config_file

Path to the configuration file to update.

TYPE: Path

Notes
  • Only missing keys are added; existing values are not overwritten.
  • Comments and formatting in the config file are preserved.
  • The original config file is backed up as '.bak' in the same directory.

get_configuration cached

get_configuration(config_file=None)

Load and return the project configuration.

The configuration is determined in the following order:

  1. If config_file is provided, use it.
  2. If the environment variable GAMSCFG_PROJECT_TOML is set, use its value as the path.
  3. If a .env file exists in the current directory and contains a project_toml field, use that.
RAISES DESCRIPTION
MissingConfigurationException

If no configuration file is found.

Note

Values from project.toml are overridden by those in .env, which are further overridden by environment variables. For example:

  • project.toml sets metadata.publisher = "foo" → used by default.
  • .env sets metadata.publisher = "bar" → overrides project.toml.
  • Environment variable GAMSCFG_METADATA_PUBLISHER = "baz" → overrides both.