From 9718ce285ce17a8cdcd03b58cb104317c8aafdc3 Mon Sep 17 00:00:00 2001 From: James McKinney <26463+jpmckinney@users.noreply.github.com> Date: Wed, 1 May 2024 01:17:55 -0400 Subject: [PATCH] feat(merge): Add ignore_version argument --- docs/changelog.rst | 8 ++++++++ docs/conf.py | 2 +- ocdskit/combine.py | 36 ++++++++++++++++++++++++------------ ocdskit/packager.py | 5 +++-- setup.cfg | 2 +- tests/test_combine.py | 18 ++++++++++++++++++ 6 files changed, 55 insertions(+), 16 deletions(-) diff --git a/docs/changelog.rst b/docs/changelog.rst index 0651a9d..5a8a434 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -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) ------------------- diff --git a/docs/conf.py b/docs/conf.py index d849cbd..c471c01 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -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 diff --git a/ocdskit/combine.py b/ocdskit/combine.py index 3539f44..0205bef 100644 --- a/ocdskit/combine.py +++ b/ocdskit/combine.py @@ -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. @@ -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: diff --git a/ocdskit/packager.py b/ocdskit/packager.py index e6f31b6..538ae56 100644 --- a/ocdskit/packager.py +++ b/ocdskit/packager.py @@ -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() @@ -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 diff --git a/setup.cfg b/setup.cfg index 41bf59d..c86daab 100644 --- a/setup.cfg +++ b/setup.cfg @@ -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 diff --git a/tests/test_combine.py b/tests/test_combine.py index ff09470..e275030 100644 --- a/tests/test_combine.py +++ b/tests/test_combine.py @@ -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 @@ -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([]))