Skip to content

Commit

Permalink
style: automatic reformat
Browse files Browse the repository at this point in the history
auto reformat with ruff/docformatter/prettier after config changes.

This is a huge commit, but shouldn't have any functional changes. Reasons so many files have changed:
- prettier hasn't been running for a while, so a lot of js has been reformatted
- apparently we still had a lot of double-quoted strings
- a lot of imports have been sorted
- we were using ":return:" instead of ":returns:" in a lot of places
- there were quite a lot of empty or mostly empty (with just a param list) docstrings
  • Loading branch information
alycejenni committed Oct 30, 2024
1 parent 4e453ad commit af4a908
Show file tree
Hide file tree
Showing 82 changed files with 867 additions and 687 deletions.
10 changes: 6 additions & 4 deletions ckanext/nhm/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def get_commands():
"""
Returns the list of commands the nhm plugin exposes.
:return: a list of click commands
:returns: a list of click commands
"""
return [nhm]

Expand All @@ -39,7 +39,7 @@ def create_context():
"""
Creates a new context dict with the site user set as the user and returns it.
:return: a new context dict
:returns: a new context dict
"""
user = toolkit.get_action('get_site_user')({'ignore_auth': True}, {})
return {'user': user['name']}
Expand All @@ -51,8 +51,10 @@ def get_vocabulary(context, create_if_missing=True):
doesn't exist, optionally creates it.
:param context: the context dict to use when calling actions
:param create_if_missing: whether to create the vocabulary if it doesn't exists, default: True.
:return: the vocabulary dict or None if it doesn't exist and create_if_missing is False
:param create_if_missing: whether to create the vocabulary if it doesn't exists,
default: True.
:returns: the vocabulary dict or None if it doesn't exist and create_if_missing is
False
"""
try:
data = {'id': DATASET_TYPE_VOCABULARY}
Expand Down
16 changes: 8 additions & 8 deletions ckanext/nhm/dcat/profiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@

import rdflib
from ckan.plugins import toolkit
from rdflib import OWL, BNode, Literal, URIRef
from rdflib.namespace import FOAF, RDF, SKOS, XSD, Namespace

from ckanext.dcat.profiles import RDFProfile
from ckanext.dcat.utils import catalog_uri, resource_uri
from rdflib import BNode, Literal, OWL, URIRef
from rdflib.namespace import FOAF, Namespace, RDF, SKOS, XSD

ADMS = Namespace('http://www.w3.org/ns/adms#')
AIISO = Namespace('http://purl.org/vocab/aiiso/schema#')
Expand Down Expand Up @@ -59,10 +60,11 @@ def user_uri(user_id):
return f'{catalog_uri().rstrip("/")}/user/{user_id}'

def get_context(self):
'''
Set up context
:return: context dict
'''
"""
Set up context.
:returns: context dict
"""
user = toolkit.get_action('get_site_user')({'ignore_auth': True}, {})
context = {'user': user['name']}
return context
Expand Down Expand Up @@ -201,7 +203,6 @@ def graph_from_dataset(self, dataset_dict, dataset_ref):

author = dataset_dict.get('author', None)
if author:

if author == 'Natural History Museum':
g.add((dataset_uri, DC.creator, nhm_uri))
else:
Expand Down Expand Up @@ -265,7 +266,6 @@ def graph_add_resources(self, dataset_uri, dataset_dict):
g = self.g

for resource_dict in dataset_dict.get('resources', []):

distribution = URIRef(resource_uri(resource_dict))

g.add((dataset_uri, DCAT.distribution, distribution))
Expand Down
130 changes: 82 additions & 48 deletions ckanext/nhm/dcat/specimen_records.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,25 @@
#
# This file is part of ckanext-nhm
# Created by the Natural History Museum in London, UK
from typing import Optional

import itertools
import json
from datetime import datetime
from typing import Optional

from ckan.plugins import toolkit
from rdflib import Literal, URIRef

from ckanext.dcat.processors import RDFSerializer
from ckanext.dcat.utils import url_to_rdflib_format, dataset_uri
from ckanext.dcat.utils import dataset_uri, url_to_rdflib_format
from ckanext.nhm.dcat.utils import (
Namespaces,
object_uri,
as_dwc_list,
epoch_to_datetime,
object_uri,
)
from ckanext.nhm.lib.dwc import dwc_terms
from ckanext.nhm.lib.helpers import get_department
from ckanext.nhm.lib.record import Record
from datetime import datetime
from rdflib import URIRef, Literal


class ObjectSerializer(RDFSerializer):
Expand Down Expand Up @@ -82,13 +83,13 @@ def __init__(
output_format: str,
version: Optional[int],
):
'''
"""
:param record: a Record object
:param namespaces: the namespaces object to use
:param output_format: the format the generated graph will be output in. This will be used to
create the metadata URI (i.e. object_url + .{output_format})
:param version: the version of the record in question, or None if there is no version
'''
"""
self.record = record
self.namespaces = namespaces
self.output_format = output_format
Expand Down Expand Up @@ -120,8 +121,8 @@ def _get_gbif_record(self):
"""
Retrieve the GBIF representation of the record if we can.
:return: the GBIF record dict or None if we couldn't get it or didn't have a GBIF ID
associated with the record
:returns: the GBIF record dict or None if we couldn't get it or didn't have a
GBIF ID associated with the record
"""
gbif_id = self.record.data.get('gbifID', None)

Expand All @@ -138,7 +139,7 @@ def __iter__(self):
"""
Iterating over this object will yield the triples that represent the record.
:return: yields 3-tuples
:returns: yields 3-tuples
"""
triple_generators = [
self._metadata(),
Expand Down Expand Up @@ -166,7 +167,8 @@ def _get_value(self, field, source=None):
:param field: the field to get the value of
:param source: the source dict to retrieve the field's value from
:return: the value wrapped in a Literal or None if the field doesn't exist on the source
:returns: the value wrapped in a Literal or None if the field doesn't exist on
the source
"""
if source is None:
source = self.record.data
Expand All @@ -180,13 +182,15 @@ def _metadata(self):
"""
Yields triples which describe this RDF output, i.e. a meta-metadata description.
:return: yields triples
:returns: yields triples
"""
metadata_uri = '{}.{}'.format(self.base_object_uri, self.output_format)
meta_ref = URIRef(metadata_uri)
yield meta_ref, self.namespaces.dc.subject, self.record_ref
yield meta_ref, self.namespaces.dc.creator, Literal(
'Natural History Museum, London'
yield (
meta_ref,
self.namespaces.dc.creator,
Literal('Natural History Museum, London'),
)
yield meta_ref, self.namespaces.dc.created, Literal(datetime.now())

Expand All @@ -196,10 +200,12 @@ def _cetaf_cspp(self):
CETAF CSPP recommendations, for more info see here:
https://cetafidentifiers.biowikifarm.net/wiki/CSPP.
:return: yields triples
:returns: yields triples
"""
yield self.record_ref, self.namespaces.dc.title, self._get_value(
'scientificName'
yield (
self.record_ref,
self.namespaces.dc.title,
self._get_value('scientificName'),
)
yield self.record_ref, self.namespaces.dc.type, Literal('Specimen')
yield (
Expand All @@ -225,11 +231,15 @@ def _cetaf_cspp(self):
Literal(as_dwc_list(names)),
)

yield self.record_ref, self.namespaces.dwc.fieldNumber, self._get_value(
'fieldNumber'
yield (
self.record_ref,
self.namespaces.dwc.fieldNumber,
self._get_value('fieldNumber'),
)
yield self.record_ref, self.namespaces.dwc.recordedBy, self._get_value(
'recordedBy'
yield (
self.record_ref,
self.namespaces.dwc.recordedBy,
self._get_value('recordedBy'),
)

# if there is associated media, yield it as a list
Expand Down Expand Up @@ -272,7 +282,7 @@ def _images(self):
connection and then the image is described in its own set of triples where the
image URI is used as the subject.
:return: yields triples
:returns: yields triples
"""
for image in self.record.images:
image_uri = URIRef(image.url)
Expand All @@ -289,21 +299,29 @@ def _images(self):
yield self.record_ref, self.namespaces.foaf.depiction, image_uri
# add a thumbnail link
if image.is_mss_image:
yield image_uri, self.namespaces.foaf.thumbnail, URIRef(
image.thumbnail_url
yield (
image_uri,
self.namespaces.foaf.thumbnail,
URIRef(image.thumbnail_url),
)

def _gbif(self):
"""
Yields triples describing the record using the GBIF record data associated with
it.
:return: yields triples
:returns: yields triples
"""
if self.gbif_record is not None:
# assert equivalence with the GBIF record
yield self.record_ref, self.namespaces.owl.sameAs, URIRef(
'https://www.gbif.org/occurrence/{}'.format(self.gbif_record['gbifID'])
yield (
self.record_ref,
self.namespaces.owl.sameAs,
URIRef(
'https://www.gbif.org/occurrence/{}'.format(
self.gbif_record['gbifID']
)
),
)
# if we have a GBIF country code, add it
yield (
Expand All @@ -316,10 +334,12 @@ def _dwc(self):
"""
Yields triples describing the record using DWC (DarWin Core) terms.
:return: yields triples
:returns: yields triples
"""
yield self.record_ref, self.namespaces.dc.identifier, Literal(
self.record.data['occurrenceID']
yield (
self.record_ref,
self.namespaces.dc.identifier,
Literal(self.record.data['occurrenceID']),
)

dwc_terms_dict = dwc_terms(self.record.data.keys())
Expand All @@ -345,9 +365,11 @@ def _dwc(self):
Literal(self.record.data.get(term)),
)
# and associated our specimen object's DWC term with the GBIF URI
yield self.record_ref, getattr(
self.namespaces.dwc, term
), gbif_uri
yield (
self.record_ref,
getattr(self.namespaces.dwc, term),
gbif_uri,
)
else:
yield (
self.record_ref,
Expand All @@ -365,15 +387,19 @@ def _dwc(self):
dynamic_property
)
if dynamic_properties_dict:
yield self.record_ref, self.namespaces.dwc.dynamicProperties, Literal(
json.dumps(dynamic_properties_dict)
yield (
self.record_ref,
self.namespaces.dwc.dynamicProperties,
Literal(json.dumps(dynamic_properties_dict)),
)

# yield the associatedMedia term as a pipe-separated list of image URIs
images = self.record.images
if images:
yield self.record_ref, self.namespaces.dwc.associatedMedia, Literal(
as_dwc_list(image.url for image in images)
yield (
self.record_ref,
self.namespaces.dwc.associatedMedia,
Literal(as_dwc_list(image.url for image in images)),
)

if self.record.data.get('created', None) is not None:
Expand All @@ -396,36 +422,44 @@ def _version_info(self):
"""
Yield simple version information about the record.
:return: yields triples
:returns: yields triples
"""
yield self.record_ref, self.namespaces.owl.versionInfo, Literal(
self.rounded_version
yield (
self.record_ref,
self.namespaces.owl.versionInfo,
Literal(self.rounded_version),
)

if self.version is None or self.version > self.rounded_version:
# if there is no version given or the version requested is beyond the latest version the
# data we're using is the same as the latest version's data, yield a same as to show
# this
yield self.record_ref, self.namespaces.owl.sameAs, URIRef(
object_uri(
self.record.data['occurrenceID'], version=self.rounded_version
)
yield (
self.record_ref,
self.namespaces.owl.sameAs,
URIRef(
object_uri(
self.record.data['occurrenceID'], version=self.rounded_version
)
),
)

def _extras(self):
"""
Yields some additional triples that don't really fit under any of the other
existing method groupings.
:return: yields triples
:returns: yields triples
"""
yield (
self.record_ref,
self.namespaces.aiiso.Department,
Literal(get_department(self.record.data['collectionCode'])),
)
yield self.record_ref, self.namespaces.aiiso.Division, self._get_value(
'subDepartment'
yield (
self.record_ref,
self.namespaces.aiiso.Division,
self._get_value('subDepartment'),
)

yield (
Expand Down
Loading

0 comments on commit af4a908

Please sign in to comment.