API Reference#

Merge#

class ocdsmerge.merge.Merger(schema=None, merge_rules=None, rule_overrides=None)[source]#
__init__(schema=None, merge_rules=None, rule_overrides=None)[source]#

Initializes a reusable Merger instance for creating merged releases.

Parameters
  • schema (dict or str) – the release schema (if not provided, will default to the latest version of OCDS)

  • merge_rules (dict) – the merge rules (if not provided, will determine the rules from the schema)

  • rule_overrides (dict) – any rule overrides, in which keys are field paths as tuples, and values are either ocdsmerge.APPEND or ocdsmerge.MERGE_BY_POSITION

create_compiled_release(releases)[source]#

Merges a list of releases into a compiled release.

create_versioned_release(releases)[source]#

Merges a list of releases into a versioned release.

class ocdsmerge.merge.MergedRelease(data=None, schema=None, merge_rules=None, rule_overrides=None)[source]#

Whether the class is for merging versioned releases.

versioned = None#
__init__(data=None, schema=None, merge_rules=None, rule_overrides=None)[source]#

Initializes a merged release.

Parameters
  • data (dict) – the latest copy of the merged release, if any

  • schema (dict or str) – the release schema (if not provided, will default to the latest version of OCDS)

  • merge_rules (dict) – the merge rules (if not provided, will determine the rules from the schema)

  • rule_overrides (dict) – any rule overrides, in which keys are field paths as tuples, and values are either ocdsmerge.APPEND or ocdsmerge.MERGE_BY_POSITION

asdict()[source]#

Returns the merged release as a dictionary.

extend(releases)[source]#

Sorts and merges many releases into the merged release.

append(release)[source]#

Merges one release into the merged release.

flat_append(flat, ocid, release_id, date, tag)[source]#
class ocdsmerge.merge.CompiledRelease(data=None, **kwargs)[source]#
versioned = False#
__init__(data=None, **kwargs)[source]#

Initializes a merged release.

Parameters
  • data (dict) – the latest copy of the merged release, if any

  • schema (dict or str) – the release schema (if not provided, will default to the latest version of OCDS)

  • merge_rules (dict) – the merge rules (if not provided, will determine the rules from the schema)

  • rule_overrides (dict) – any rule overrides, in which keys are field paths as tuples, and values are either ocdsmerge.APPEND or ocdsmerge.MERGE_BY_POSITION

flat_append(flat, ocid, release_id, date, tag)[source]#
class ocdsmerge.merge.VersionedRelease(data=None, schema=None, merge_rules=None, rule_overrides=None)[source]#
versioned = True#
flat_append(flat, ocid, release_id, date, tag)[source]#

Rules#

ocdsmerge.rules.get_merge_rules(schema=None)[source]#

Returns merge rules as key-value pairs, in which the key is a JSON path as a tuple, and the value is a list of merge properties whose values are true.

Flatten#

class ocdsmerge.flatten.MergeStrategy(value)[source]#

An enumeration.

APPEND = 1#
MERGE_BY_POSITION = 2#
class ocdsmerge.flatten.IdValue(identifier)[source]#

A string with identifier and original_value properties.

property original_value#
class ocdsmerge.flatten.IdDict[source]#

A dictionary with an identifier property.

property identifier#
ocdsmerge.flatten.is_versioned_value(value)[source]#

Returns whether the value is a versioned value.

ocdsmerge.flatten.flatten(obj, merge_rules, rule_overrides, flattened, path=(), rule_path=(), versioned=False)[source]#

Flattens a JSON object into key-value pairs, in which the key is the JSON path as a tuple. For example:

Replaces numbers in JSON paths (representing positions in arrays) with special objects. This ensures that objects in arrays with different id values have different JSON paths – and makes it easy to identify such arrays.

{
    "a": "I am a",
    "b": ["A", "list"],
    "c": [
        {"id": 1, "cb": "I am ca"},
        {"id": 2, "ca": "I am cb"}
    ]
}

flattens to:

{
    ('a',): 'I am a',
    ('b',): ['A', 'list'],
    ('a', '1', 'cb'): 'I am ca',
    ('a', '1', 'id'): 1,
    ('a', '2', 'ca'): 'I am cb',
    ('a', '2', 'id'): 2,
}
ocdsmerge.flatten.unflatten(flattened)[source]#

Unflattens a flattened object into a JSON object.

Utilities#

ocdsmerge.util.get_tags()[source]#

Returns the tags of all versions of OCDS in alphabetical order.

ocdsmerge.util.get_release_schema_url(tag)[source]#

Returns the URL of the release schema in the given version of OCDS.

ocdsmerge.util.sorted_releases(releases)[source]#

Sorts a list of releases by date.

Exceptions#

exception ocdsmerge.exceptions.OCDSMergeError[source]#

Base class for exceptions from within this package

exception ocdsmerge.exceptions.MissingDateKeyError(key, message)[source]#

Raised when a release is missing a ‘date’ key

exception ocdsmerge.exceptions.NonObjectReleaseError[source]#

Raised when a release is not an object

exception ocdsmerge.exceptions.NullDateValueError[source]#

Raised when a release has a null ‘date’ value

exception ocdsmerge.exceptions.NonStringDateValueError[source]#

Raised when a release has a non-string ‘date’ value

exception ocdsmerge.exceptions.InconsistentTypeError[source]#

Raised when a path is a literal and an object in different releases

exception ocdsmerge.exceptions.OCDSMergeWarning[source]#

Base class for warnings from within this package

exception ocdsmerge.exceptions.DuplicateIdValueWarning(path, id, message)[source]#

Used when at least two objects in the same array have the same value for the ‘id’ field