From 088d272e61dfc948b203f84cdce514b1f97be142 Mon Sep 17 00:00:00 2001 From: James McKinney <26463+jpmckinney@users.noreply.github.com> Date: Thu, 29 Aug 2019 21:15:00 -0400 Subject: [PATCH] Move annotate parameter onto release_schema_patch and patched_release_schema methods --- docs/changelog.rst | 2 +- ocdsextensionregistry/profile_builder.py | 33 ++++++++++++------------ tests/test_profile_builder.py | 4 +-- 3 files changed, 20 insertions(+), 19 deletions(-) diff --git a/docs/changelog.rst b/docs/changelog.rst index 83d1f00..9a1e2d2 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -5,7 +5,7 @@ Changelog ~~~~~~~~~~~~~~~~~~~ - Unregistered extensions are now supported by the profile builder. The ``extension_versions`` argument to ``ProfileBuilder`` can be a list of extensions' metadata URLs, base URLs and/or download URLs. -- Add an ``annotate`` parameter to ``ProfileBuilder`` to annotate all definitions and properties with extension names. +- Add an ``annotate`` parameter to ``ProfileBuilder``'s ``release_schema_patch`` and ``patched_release_schema`` methods to annotate all definitions and properties with extension names. - Add a ``get_latest_version`` function to ``ocdsextensionregistry.utils``, to return the identifier of the latest version from a list of versions of the same extension. 0.0.11 (2019-06-26) diff --git a/ocdsextensionregistry/profile_builder.py b/ocdsextensionregistry/profile_builder.py index f270e12..edeca8c 100644 --- a/ocdsextensionregistry/profile_builder.py +++ b/ocdsextensionregistry/profile_builder.py @@ -20,8 +20,7 @@ class ProfileBuilder: - def __init__(self, standard_tag, extension_versions, registry_base_url=None, schema_base_url=None, - annotate=False): + def __init__(self, standard_tag, extension_versions, registry_base_url=None, schema_base_url=None): """ Accepts an OCDS version and either a dictionary of extension identifiers and versions, or a list of extensions' metadata URLs, base URLs and/or download URLs, and initializes a reader of the extension registry. @@ -32,14 +31,12 @@ def __init__(self, standard_tag, extension_versions, registry_base_url=None, sch ``'https://raw.githubusercontent.com/open-contracting/extension_registry/master/'`` :param str schema_base_url: the schema's base URL, e.g. ``'https://standard.open-contracting.org/profiles/ppp/schema/1__0__0__beta/'`` - :param bool annotate: whether to annotate all definitions and properties with extension names :type extension_versions: dict or list : """ self.standard_tag = standard_tag self.extension_versions = extension_versions self.schema_base_url = schema_base_url - self.annotate = annotate self._file_cache = {} # Allows setting the registry URL to e.g. a pull request, when working on a profile. @@ -66,31 +63,35 @@ def extensions(self): data['Download URL'] = url yield ExtensionVersion(data) - def release_schema_patch(self): + def release_schema_patch(self, annotate=False): """ Returns the consolidated release schema patch. + + :param bool annotate: whether to annotate all definitions and properties with extension names """ - profile_patch = OrderedDict() + output = OrderedDict() # Replaces `null` with sentinel values, to preserve the null'ing of fields by extensions in the final patch. for extension in self.extensions(): - data = json_loads(re.sub(r':\s*null\b', ': "REPLACE_WITH_NULL"', extension.remote('release-schema.json'))) - if self.annotate: - add_extension_name(data, extension.metadata['name']['en']) - json_merge_patch.merge(profile_patch, data) + patch = json_loads(re.sub(r':\s*null\b', ': "REPLACE_WITH_NULL"', extension.remote('release-schema.json'))) + if annotate: + add_extension_name(patch, extension.metadata['name']['en']) + json_merge_patch.merge(output, patch) - return json_loads(json.dumps(profile_patch).replace('"REPLACE_WITH_NULL"', 'null')) + return json_loads(json.dumps(output).replace('"REPLACE_WITH_NULL"', 'null')) - def patched_release_schema(self): + def patched_release_schema(self, annotate=False): """ Returns the patched release schema. + + :param bool annotate: whether to annotate all definitions and properties with extension names """ - content = self.get_standard_file_contents('release-schema.json') - patched = json_merge_patch.merge(json_loads(content), self.release_schema_patch()) + output = json_loads(self.get_standard_file_contents('release-schema.json')) + json_merge_patch.merge(output, self.release_schema_patch(annotate=annotate)) if self.schema_base_url: - patched['id'] = urljoin(self.schema_base_url, 'release-schema.json') + output['id'] = urljoin(self.schema_base_url, 'release-schema.json') - return patched + return output def release_package_schema(self): """ diff --git a/tests/test_profile_builder.py b/tests/test_profile_builder.py index 153b52f..a416d31 100644 --- a/tests/test_profile_builder.py +++ b/tests/test_profile_builder.py @@ -83,8 +83,8 @@ def test_patched_release_schema(): def test_patched_release_schema_with_annotate(): - builder = ProfileBuilder('1__1__3', OrderedDict([('location', 'v1.1.3')]), annotate=True) - result = builder.patched_release_schema() + builder = ProfileBuilder('1__1__3', OrderedDict([('location', 'v1.1.3')])) + result = builder.patched_release_schema(annotate=True) definition = result['definitions']['Location'] assert definition['extension'] == 'Location'