Skip to content

Commit

Permalink
chore: Run ruff check .
Browse files Browse the repository at this point in the history
  • Loading branch information
jpmckinney committed Sep 14, 2024
1 parent 7ac64ef commit 6d435a4
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 29 deletions.
36 changes: 13 additions & 23 deletions extension_explorer/util.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
Module to keep ``views.py`` simple and high-level.
"""
import contextlib
import json
import os
import re
Expand Down Expand Up @@ -146,10 +147,7 @@ def identify_headings(html):
continue

slug = slugify(element.text_content())
if slug in slug_counts:
heading_id = f'{slug}-{slug_counts[slug]}'
else:
heading_id = slug
heading_id = f'{slug}-{slug_counts[slug]}' if slug in slug_counts else slug
element.attrib['id'] = heading_id

headings.append({'id': heading_id, 'level': heading_level, 'text': element.text_content()})
Expand Down Expand Up @@ -232,10 +230,7 @@ def get_codelist_tables(extension_version, lang):

rows.append(new_row)

if name.startswith(('+', '-')):
basename = name[1:]
else:
basename = name
basename = name[1:] if name.startswith(('+', '-')) else name

url = _codelist_url(basename, extension_version, lang)

Expand All @@ -261,10 +256,7 @@ def get_removed_fields(extension_version, lang):

original_field = _add_link_to_original_field(field, schema, sources)

if original_field.get('deprecated'):
group = 'deprecated'
else:
group = 'active'
group = 'deprecated' if original_field.get('deprecated') else 'active'

d = field.asdict(exclude=('definition_pointer', 'pointer', 'schema', 'required', 'deprecated', 'multilingual'))
tables[group].append(d)
Expand Down Expand Up @@ -303,10 +295,8 @@ def get_schema_tables(extension_version, lang):
if field.definition_path in sources:
tables[key]['source'] = sources[field.definition_path]

try:
with contextlib.suppress(jsonpointer.JsonPointerException):
_add_link_to_original_field(field, schema, sources)
except jsonpointer.JsonPointerException:
pass

d = field.asdict(sep='.', exclude=('definition_pointer', 'pointer', 'required', 'deprecated'))
d['title'] = field.schema.get('title', '')
Expand All @@ -324,10 +314,7 @@ def _get_types(value, sources, extension_version, lang, n=1, field=None):

if '$ref' in value:
name = value['$ref'][14:] # remove '#/definitions/'
if name in sources:
url = sources[name]['url']
else:
url = f'#{name.lower()}' # local definition
url = sources[name]['url'] if name in sources else f'#{name.lower()}' # local definition
noun = ngettext('object', 'objects', n)
return [f'<a href="{url}">{name}</a> {noun}']

Expand Down Expand Up @@ -423,7 +410,8 @@ def _codelist_url(basename, extension_version, lang):
elif basename in codelist_names:
anchor = re.sub(r'[A-Z]', lambda s: '-' + s[0].lower(), os.path.splitext(basename)[0])
url = f'{codelist_reference_url}#{anchor}'
# XXX: Hardcoding.
# TODO(james): Hardcoding for ocds_statistics_extension.
# https://github.com/open-contracting/extension-explorer/issues/58
elif basename == 'statistic.csv':
url = url_for('extension_codelists', lang=lang, identifier='bids', version='master', _anchor=basename)
else:
Expand Down Expand Up @@ -499,13 +487,13 @@ def _get_sources(schema, lang):
return sources


def _patch_schema(version, lang, include_test_dependencies=False):
def _patch_schema(version, lang, *, include_test_dependencies=False):
schema = deepcopy(_ocds_release_schema(lang))
_patch_schema_recursive(schema, version, lang, include_test_dependencies=include_test_dependencies)
return schema


def _patch_schema_recursive(schema, version, lang, include_test_dependencies=False):
def _patch_schema_recursive(schema, version, lang, *, include_test_dependencies=False):
dependencies = version['metadata'].get('dependencies', [])
if include_test_dependencies:
dependencies += version['metadata'].get('testDependencies', [])
Expand Down Expand Up @@ -538,7 +526,9 @@ def _extension_versions_by_base_url():

@lru_cache
def _ocds_release_schema(lang):
return requests.get(_ocds_release_schema_url(lang)).json()
response = requests.get(_ocds_release_schema_url(lang), timeout=10)
response.raise_for_status()
return response.json()


def _ocds_release_schema_url(lang):
Expand Down
12 changes: 6 additions & 6 deletions tests/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"version": "master",
"metadata": {
"testDependencies": [
"https://raw.githubusercontent.com/open-contracting-extensions/ocds_location_extension/v1.1.3/extension.json" # noqa: E501
"https://raw.githubusercontent.com/open-contracting-extensions/ocds_location_extension/v1.1.3/extension.json"
]
},
"schemas": {
Expand Down Expand Up @@ -348,7 +348,7 @@ def test_get_removed_fields(client):
{'definition_path': '', 'path': 'buyer',
'url': 'https://standard.open-contracting.org/1.1/en/schema/reference/#release-schema.json,,buyer'},
{'definition_path': 'Tender', 'path': 'description',
'url': 'https://standard.open-contracting.org/1.1/en/schema/reference/#release-schema.json,/definitions/Tender,description'}, # noqa: E501
'url': 'https://standard.open-contracting.org/1.1/en/schema/reference/#release-schema.json,/definitions/Tender,description'},
{'definition_path': 'Location', 'path': 'description',
'url': '/en/extensions/location/v1.1.3/schema/#Location.description'},
],
Expand Down Expand Up @@ -537,7 +537,7 @@ def test_get_schema_tables(client):
'source': {
'type': 'core',
'url': 'https://standard.open-contracting.org/1.1/en/schema/reference/#release',
'field_url_prefix': 'https://standard.open-contracting.org/1.1/en/schema/reference/#release-schema.json,,', # noqa: E501
'field_url_prefix': 'https://standard.open-contracting.org/1.1/en/schema/reference/#release-schema.json,,',
},
},
'Tender': {
Expand All @@ -559,13 +559,13 @@ def test_get_schema_tables(client):
'title': 'Replacement',
'description': '',
'types': '',
'url': 'https://standard.open-contracting.org/1.1/en/schema/reference/#release-schema.json,/definitions/Tender,title', # noqa: E501
'url': 'https://standard.open-contracting.org/1.1/en/schema/reference/#release-schema.json,/definitions/Tender,title',
},
],
'source': {
'type': 'core',
'url': 'https://standard.open-contracting.org/1.1/en/schema/reference/#tender',
'field_url_prefix': 'https://standard.open-contracting.org/1.1/en/schema/reference/#release-schema.json,/definitions/Tender,', # noqa: E501
'field_url_prefix': 'https://standard.open-contracting.org/1.1/en/schema/reference/#release-schema.json,/definitions/Tender,',
},
},
'Location': {
Expand Down Expand Up @@ -636,7 +636,7 @@ def test_get_schema_tables_mixed_array_success(client):
},
],
'source': {
'field_url_prefix': 'https://standard.open-contracting.org/1.1/en/schema/reference/#release-schema.json,,', # noqa: E501
'field_url_prefix': 'https://standard.open-contracting.org/1.1/en/schema/reference/#release-schema.json,,',
'type': 'core',
'url': 'https://standard.open-contracting.org/1.1/en/schema/reference/#release',
},
Expand Down

0 comments on commit 6d435a4

Please sign in to comment.