Skip to content

Commit

Permalink
Move annotate parameter onto release_schema_patch and patched_release…
Browse files Browse the repository at this point in the history
…_schema methods
  • Loading branch information
jpmckinney committed Aug 30, 2019
1 parent 346be08 commit 088d272
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 19 deletions.
2 changes: 1 addition & 1 deletion docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
33 changes: 17 additions & 16 deletions ocdsextensionregistry/profile_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand All @@ -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):
"""
Expand Down
4 changes: 2 additions & 2 deletions tests/test_profile_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down

0 comments on commit 088d272

Please sign in to comment.