Skip to content

Commit

Permalink
feat: ExtensionVersion.remote() returns the default argument, if prov…
Browse files Browse the repository at this point in the history
…ided, if the file does not exist
  • Loading branch information
jpmckinney committed Nov 29, 2021
1 parent 8ba38a5 commit f5ab5ee
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 10 deletions.
5 changes: 5 additions & 0 deletions docs/changelog.rst
@@ -1,6 +1,11 @@
Changelog
=========

0.1.6 (2021-11-29)
------------------

- :class:`~ocdsextensionregistry.extension_version.ExtensionVersion`: :meth:`~ocdsextensionregistry.extension_version.ExtensionVersion.remote` returns the ``default`` argument, if provided, if the file does not exist.

0.1.5 (2021-11-24)
------------------

Expand Down
2 changes: 1 addition & 1 deletion docs/conf.py
Expand Up @@ -26,7 +26,7 @@
author = 'Open Contracting Partnership'

# The short X.Y version
version = '0.1.5'
version = '0.1.6'
# The full version, including alpha/beta/rc tags
release = version

Expand Down
17 changes: 10 additions & 7 deletions ocdsextensionregistry/extension_version.py
Expand Up @@ -57,9 +57,10 @@ def get_url(self, basename):
"""
return ''.join([self.base_url, basename])

def remote(self, basename):
def remote(self, basename, default=None):
"""
Returns the contents of the file within the extension.
Returns the contents of the file within the extension. If the ``default`` is set and the file does not exist,
returns the provided ``default`` value.
If the extension has a download URL, caches all the files' contents. Otherwise, downloads and caches the
requested file's contents. Raises an HTTPError if a download fails.
Expand All @@ -69,13 +70,15 @@ def remote(self, basename):
if basename not in self.files:
if not self.download_url:
response = session.get(self.get_url(basename))
response.raise_for_status()
self._files[basename] = response.text
if default is None or response.status_code != requests.codes.not_found:
response.raise_for_status()
self._files[basename] = response.text

try:
return self.files[basename]
except KeyError:
if default is not None:
return self.files.get(basename, default)
elif basename not in self.files:
raise DoesNotExist(f'File {basename!r} does not exist in {self}')
return self.files[basename]

@property
def files(self):
Expand Down
3 changes: 2 additions & 1 deletion ocdsextensionregistry/profile_builder.py
Expand Up @@ -119,7 +119,8 @@ def release_schema_patch(self, extension_field=None, language='en'):

# Replaces `null` with sentinel values, to preserve the null'ing of fields by extensions in the final patch.
for extension in self.extensions():
patch = json.loads(re.sub(r':\s*null\b', ': "REPLACE_WITH_NULL"', extension.remote('release-schema.json')))
patch = extension.remote('release-schema.json', default={})
patch = json.loads(re.sub(r':\s*null\b', ': "REPLACE_WITH_NULL"', patch))
if extension_field:
_add_extension_field(patch, extension.metadata['name'][language], extension_field)
json_merge_patch.merge(output, patch)
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Expand Up @@ -5,7 +5,7 @@

setup(
name='ocdsextensionregistry',
version='0.1.5',
version='0.1.6',
author='Open Contracting Partnership',
author_email='data@open-contracting.org',
url='https://github.com/open-contracting/extension_registry.py',
Expand Down
18 changes: 18 additions & 0 deletions tests/test_extension_version.py
Expand Up @@ -147,6 +147,24 @@ def test_zipfile_not_available_in_bulk():
assert str(excinfo.value) == "ExtensionVersion.zipfile() requires a download_url."


def test_remote_codelists_only_base_url():
args = dict.fromkeys(['Id', 'Date', 'Version', 'Base URL', 'Download URL'])
args['Base URL'] = 'https://raw.githubusercontent.com/contratacionesabiertas/ocds_publicNotices_extension/master/'

obj = ExtensionVersion(args)

assert obj.remote('release-schema.json', default={}) == {}


def test_remote_codelists_only_download_url():
args = dict.fromkeys(['Id', 'Date', 'Version', 'Base URL', 'Download URL'])
args['Download URL'] = 'https://api.github.com/repos/contratacionesabiertas/ocds_publicNotices_extension/zipball/master' # noqa: E501

obj = ExtensionVersion(args)

assert obj.remote('release-schema.json', default={}) == {}


def test_files():
obj = ExtensionVersion(arguments())
data = obj.files
Expand Down

0 comments on commit f5ab5ee

Please sign in to comment.