Skip to content

The createcsv modul

gamslib.objectcsv.create_csv

Create object.csv and datastreams.csv files for GAMS objects.

This module generates object.csv and datastreams.csv files for one or more object folders. It uses data from the DC.xml file and the project configuration to populate metadata fields. If information is missing, fields are left blank or filled with default values.

logger module-attribute

logger = logging.getLogger()

NAMESPACES module-attribute

NAMESPACES = {'dc': 'http://purl.org/dc/elements/1.1/'}

is_datastream_file

is_datastream_file(ds_file, configuration)

Determine if a file should be treated as a datastream file.

Excludes files named 'object.csv' or 'datastreams.csv', and files matching ignore patterns specified in the configuration.

PARAMETER DESCRIPTION
ds_file

Path to the candidate datastream file.

TYPE: Path

configuration

Project configuration containing ignore patterns.

TYPE: Configuration

RETURNS DESCRIPTION
bool

True if the file should be used as a datastream, False otherwise.

TYPE: bool

get_rights

get_rights(config, dc)

Retrieve the rights information for an object or datastream.

Lookup order:

  1. Value from Dublin Core metadata (DC.xml).
  2. Value from project configuration.
  3. Default value if none found.

Args:

  • config (Configuration): Project configuration.
  • dc (DublinCore): Dublin Core metadata object.
RETURNS DESCRIPTION
str

Rights value.

TYPE: str

extract_dsid

extract_dsid(datastream, keep_extension=True)

Extract and validate the datastream ID from a file path.

If keep_extension is False, attempts to remove the file extension from the ID. Validates the resulting ID format.

PARAMETER DESCRIPTION
datastream

Path or filename of the datastream.

TYPE: Path | str

keep_extension

Whether to keep the file extension in the ID.

TYPE: bool DEFAULT: True

RETURNS DESCRIPTION
str

The extracted datastream ID.

TYPE: str

RAISES DESCRIPTION
ValueError

If the resulting ID is invalid.

detect_languages

detect_languages(ds_file, delimiter=' ')

Detect the language(s) of a file.

Returns detected language(s) as a string separated by the given delimiter. (Currently returns an empty string; language detection is not implemented.)

PARAMETER DESCRIPTION
ds_file

Path to the file.

TYPE: Path

delimiter

Delimiter for joining detected languages.

TYPE: str DEFAULT: ' '

RETURNS DESCRIPTION
str

Detected languages, or empty string if none.

TYPE: str

collect_object_data

collect_object_data(pid, config, dc, use_subjects_as_tags=False)

Collect metadata for an object to populate object.csv.

Resolves values from Dublin Core metadata and project configuration.

PARAMETER DESCRIPTION
pid

Object identifier.

TYPE: str

config

Project configuration.

TYPE: Configuration

dc

Dublin Core metadata object.

TYPE: DublinCore

use_subjects_as_tags

Whether to use dc:subjects as tags. (Default: False)

TYPE: bool DEFAULT: False

RETURNS DESCRIPTION
ObjectData

Populated object metadata.

TYPE: ObjectData

make_ds_title

make_ds_title(dsid, format_info)

Generate a title for a datastream based on its ID and format.

PARAMETER DESCRIPTION
dsid

Datastream ID.

TYPE: str

format_info

Format information for the datastream.

TYPE: FormatInfo

RETURNS DESCRIPTION
str

Generated datastream title.

TYPE: str

make_ds_description

make_ds_description(dsid, format_info)

Generate a description for a datastream based on its ID and format.

Uses the format subtype as the description if available.

PARAMETER DESCRIPTION
dsid

Datastream ID.

TYPE: str

format_info

Format information for the datastream.

TYPE: FormatInfo

RETURNS DESCRIPTION
str

Datastream description, or empty string if not available.

TYPE: str

collect_datastream_data

collect_datastream_data(ds_file, config, dc)

Collect metadata for a single datastream to populate datastreams.csv.

Uses file information, format detection, and configuration values.

PARAMETER DESCRIPTION
ds_file

Path to the datastream file.

TYPE: Path

config

Project configuration.

TYPE: Configuration

dc

Dublin Core metadata object.

TYPE: DublinCore

RETURNS DESCRIPTION
DSData

Populated datastream metadata.

TYPE: DSData

create_csv

create_csv(object_directory, configuration, force_overwrite=False, use_subjects_as_tags=False)

Generate object.csv and datastreams.csv for a single object directory.

Existing CSV files are not overwritten unless 'force_overwrite' is True. Metadata is collected from DC.xml and configuration.

PARAMETER DESCRIPTION
object_directory

Path to the object directory.

TYPE: Path

configuration

Project configuration.

TYPE: Configuration

force_overwrite

Whether to overwrite existing CSV files.

TYPE: bool DEFAULT: False

RETURNS DESCRIPTION
ObjectCSVManager | None

ObjectCSVManager | None: Manager for the created CSV files, or None if not created.

update_csv

update_csv(object_directory, configuration, use_subjects_as_tags=False)

Update existing CSV files for an object directory with new metadata.

Adds new datastreams and updates metadata if configuration or DC.xml has changed. Existing CSV files are updated, not overwritten.

PARAMETER DESCRIPTION
object_directory

Path to the object directory.

TYPE: Path

configuration

Project configuration.

TYPE: Configuration

RETURNS DESCRIPTION
ObjectCSVManager | None

ObjectCSVManager | None: Manager for the updated CSV files, or None if not updated.

create_csv_files

create_csv_files(root_folder, config, force_overwrite=False, update=False, use_subjects_as_tags=False)

Create or update CSV files for all objects under the given root folder.

Iterates through all object directories found below root_folder and creates or updates their object.csv and datastreams.csv files.

PARAMETER DESCRIPTION
root_folder

Root directory containing object folders.

TYPE: Path

config

Project configuration.

TYPE: Configuration

force_overwrite

If True, overwrite existing CSV files.

TYPE: bool DEFAULT: False

update

If True, update existing CSV files instead of creating new ones.

TYPE: bool DEFAULT: False

use_subjects_as_tags

If True, insert all dc:subject entries as tags in object.csv

TYPE: bool DEFAULT: False

RETURNS DESCRIPTION
list[ObjectCSVManager]

list[ObjectCSVManager]: List of managers for the processed object directories.