Skip to content

Commit

Permalink
feat(merge): Add ignore_version argument
Browse files Browse the repository at this point in the history
  • Loading branch information
jpmckinney committed May 1, 2024
1 parent abd264f commit 9718ce2
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 16 deletions.
8 changes: 8 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
Changelog
=========

1.1.11 (2024-05-01)
-------------------

Added
~~~~~

- :meth:`~ocdskit.combine.merge` accepts a ``ignore_version`` argument.

1.1.10 (2024-04-15)
-------------------

Expand Down
2 changes: 1 addition & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
author = "Open Contracting Partnership"

# The short X.Y version
version = "1.1.10"
version = "1.1.11"
# The full version, including alpha/beta/rc tags
release = version

Expand Down
36 changes: 24 additions & 12 deletions ocdskit/combine.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,19 @@ def combine_release_packages(packages, uri='', publisher=None, published_date=''
return output


def merge(data, uri='', publisher=None, published_date='', version=DEFAULT_VERSION, schema=None,
return_versioned_release=False, return_package=False, use_linked_releases=False, streaming=False):
def merge(
data,
uri: str = '',
publisher: dict = None,
published_date: str = '',
version: str = DEFAULT_VERSION,
schema: dict = None,
return_versioned_release: bool = False,
return_package: bool = False,
use_linked_releases: bool = False,
streaming: bool = False,
ignore_version: bool = False,
):
"""
Merges release packages and individual releases.
Expand All @@ -145,23 +156,24 @@ def merge(data, uri='', publisher=None, published_date='', version=DEFAULT_VERSI
the last input release package.
:param data: an iterable of release packages and individual releases
:param str uri: if ``return_package`` is ``True``, the record package's ``uri``
:param dict publisher: if ``return_package`` is ``True``, the record package's ``publisher``
:param str published_date: if ``return_package`` is ``True``, the record package's ``publishedDate``
:param str version: if ``return_package`` is ``True``, the record package's ``version``
:param dict schema: the URL, path or dict of the patched release schema to use
:param bool return_package: wrap the compiled releases in a record package
:param bool use_linked_releases: if ``return_package`` is ``True``, use linked releases instead of full releases,
:param uri: if ``return_package`` is ``True``, the record package's ``uri``
:param publisher: if ``return_package`` is ``True``, the record package's ``publisher``
:param published_date: if ``return_package`` is ``True``, the record package's ``publishedDate``
:param version: if ``return_package`` is ``True``, the record package's ``version``
:param schema: the URL, path or dict of the patched release schema to use
:param return_package: wrap the compiled releases in a record package
:param use_linked_releases: if ``return_package`` is ``True``, use linked releases instead of full releases,
if the input is a release package
:param bool return_versioned_release: if ``return_package`` is ``True``, include versioned releases in the record
:param return_versioned_release: if ``return_package`` is ``True``, include versioned releases in the record
package; otherwise, yield versioned releases instead of compiled releases
:param bool streaming: if ``return_package`` is ``True``, set the package's records to a generator (this only works
:param streaming: if ``return_package`` is ``True``, set the package's records to a generator (this only works
if the calling code exhausts the generator before ``merge`` returns)
:param ignore_version: do not raise an error if the versions are inconsistent across packages to merge
:raises InconsistentVersionError: if the versions are inconsistent across packages to merge
:raises MissingOcidKeyError: if the release is missing an ``ocid`` field
:raises UnknownVersionError: if the OCDS version is not recognized
"""
with Packager() as packager:
with Packager(ignore_version=ignore_version) as packager:
packager.add(data)

if not schema and packager.version:
Expand Down
5 changes: 3 additions & 2 deletions ocdskit/packager.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,10 @@ class Packager:
same version of OCDS.
"""

def __init__(self):
def __init__(self, ignore_version=False):
self.package = _empty_record_package()
self.version = None
self.ignore_version = ignore_version

if USING_SQLITE:
self.backend = SQLiteBackend()
Expand All @@ -74,7 +75,7 @@ def add(self, data):
for i, item in enumerate(data):
version = get_ocds_minor_version(item)
if self.version:
if version != self.version:
if not self.ignore_version and version != self.version:
# OCDS 1.1 and OCDS 1.0 have different merge rules for `awards.suppliers`. Also, mixing new and
# deprecated fields can lead to inconsistencies (e.g. transaction `amount` and `value`).
# https://standard.open-contracting.org/latest/en/schema/changelog/#advisories
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[metadata]
name = ocdskit
version = 1.1.10
version = 1.1.11
author = Open Contracting Partnership
author_email = data@open-contracting.org
license = BSD
Expand Down
18 changes: 18 additions & 0 deletions tests/test_combine.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from ocdsmerge.exceptions import DuplicateIdValueWarning

from ocdskit.combine import compile_release_packages, merge, package_records
from ocdskit.exceptions import InconsistentVersionError
from tests import read


Expand Down Expand Up @@ -53,6 +54,23 @@ def test_merge_warning():
]


def test_merge_version_mismatch():
def data():
yield json.loads(read('realdata/release-package_1.1-1.json'))
yield json.loads(read('realdata/release-package_1.0-1.json'))

with pytest.raises(InconsistentVersionError):
list(merge(data()))


def test_merge_version_mismatch_ignore_version():
def data():
yield json.loads(read('realdata/release-package_1.1-1.json'))
yield json.loads(read('realdata/release-package_1.0-1.json'))

list(merge(data(), ignore_version=True)) # no error


def test_compile_release_packages():
with pytest.warns(DeprecationWarning) as records:
compiled_releases = list(compile_release_packages([]))
Expand Down

0 comments on commit 9718ce2

Please sign in to comment.