Skip to content

Commit

Permalink
Merge pull request #244 from RDFLib/lawson/lint
Browse files Browse the repository at this point in the history
lint and format
  • Loading branch information
lalewis1 authored Aug 1, 2024
2 parents e8b4049 + e54253a commit 8062531
Show file tree
Hide file tree
Showing 35 changed files with 215 additions and 180 deletions.
23 changes: 15 additions & 8 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,26 @@
# See https://pre-commit.com/hooks.html for more hooks
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.0.1
rev: v4.6.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
- id: check-yaml
- id: check-json
- id: check-added-large-files
- repo: https://github.com/psf/black
rev: 22.3.0
hooks:
- id: black
- repo: https://github.com/codespell-project/codespell
rev: v2.1.0
rev: v2.3.0
hooks:
- id: codespell
args: [--skip, "*.ttl"]
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.5.5
hooks:
- id: ruff
args: [ --fix ]
- repo: https://github.com/psf/black
rev: 24.4.2
hooks:
- id: black
- repo: https://github.com/pycqa/isort
rev: 5.13.2
hooks:
- id: isort
55 changes: 48 additions & 7 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions prez/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
from prez.models.model_exceptions import (
ClassNotFoundException,
URINotFoundException,
NoProfilesException, InvalidSPARQLQueryException,
NoProfilesException,
InvalidSPARQLQueryException,
)
from prez.routers.catprez import router as catprez_router
from prez.routers.cql import router as cql_router
Expand Down Expand Up @@ -168,7 +169,7 @@ def assemble_app(
ClassNotFoundException: catch_class_not_found_exception,
URINotFoundException: catch_uri_not_found_exception,
NoProfilesException: catch_no_profiles_exception,
InvalidSPARQLQueryException: catch_invalid_sparql_query
InvalidSPARQLQueryException: catch_invalid_sparql_query,
},
**kwargs
)
Expand Down
2 changes: 1 addition & 1 deletion prez/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
from prez.models.search_method import SearchMethod
from prez.models.search_method import SearchMethod as SearchMethod
2 changes: 1 addition & 1 deletion prez/models/model_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,4 @@ class InvalidSPARQLQueryException(Exception):

def __init__(self, error: str):
self.message = f"Invalid SPARQL query: {error}"
super().__init__(self.message)
super().__init__(self.message)
11 changes: 2 additions & 9 deletions prez/models/object_item.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,8 @@
from typing import Optional, FrozenSet, Tuple
from typing import Set
from typing import Optional, FrozenSet

from pydantic import BaseModel, root_validator
from pydantic import BaseModel
from rdflib import URIRef, PROF

from prez.cache import endpoints_graph_cache
from prez.models.model_exceptions import ClassNotFoundException
from prez.reference_data.prez_ns import PREZ, ONT
from prez.services.curie_functions import get_uri_for_curie_id
from prez.services.model_methods import get_classes


class ObjectItem(BaseModel):
uri: Optional[URIRef] = None
Expand Down
1 change: 0 additions & 1 deletion prez/models/profiles_item.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
from prez.cache import profiles_graph_cache
from prez.config import settings
from prez.services.curie_functions import get_uri_for_curie_id, get_curie_id_for_uri
from prez.services.model_methods import get_classes

PREZ = Namespace("https://prez.dev/")

Expand Down
42 changes: 21 additions & 21 deletions prez/queries/vocprez.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,37 +8,37 @@ def get_concept_scheme_query(iri: str, bnode_depth: int) -> str:
"""
PREFIX prez: <https://prez.dev/>
PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
CONSTRUCT {
CONSTRUCT {
?iri ?p ?o .
{% if bnode_depth > 0 +%}
?iri ?p0 ?o0 .
{% endif %}
{% for i in range(bnode_depth) %}
?o{{ i }} ?p{{ i + 1 }} ?o{{ i + 1 }} .
{% endfor %}
?iri prez:childrenCount ?childrenCount .
}
WHERE {
BIND(<{{ iri }}> as ?iri)
?iri ?p ?o .
FILTER (?p != skos:hasTopConcept)
{
SELECT (COUNT(?topConcept) AS ?childrenCount)
WHERE {
BIND(<{{ iri }}> as ?iri)
?iri skos:hasTopConcept ?topConcept .
}
}
{% if bnode_depth > 0 %}
?iri ?p0 ?o0 .
{% endif %}
{% for i in range(bnode_depth) %}
?o{{ i }} ?p{{ i + 1 }} ?o{{ i + 1 }} .
FILTER (isBlank(?o0))
Expand All @@ -58,7 +58,7 @@ def get_concept_scheme_top_concepts_query(iri: str, page: int, per_page: int) ->
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
CONSTRUCT {
?concept skos:prefLabel ?label .
?concept prez:childrenCount ?narrowerChildrenCount .
Expand All @@ -79,27 +79,27 @@ def get_concept_scheme_top_concepts_query(iri: str, page: int, per_page: int) ->
}
?iri rdf:type ?type .
?concept rdf:type ?conceptType .
{
SELECT (COUNT(?childConcept) AS ?childrenCount)
WHERE {
BIND(<{{ iri }}> as ?iri)
?iri skos:hasTopConcept ?childConcept .
}
}
{
# Using two OPTIONAL clauses with a UNION causes ?narrowConcept to be duplicated.
# Use DISTINCT to get an accurate count.
SELECT ?concept ?label (COUNT(DISTINCT ?narrowerConcept) AS ?narrowerChildrenCount)
WHERE {
BIND(<{{ iri }}> as ?iri)
{
OPTIONAL {
?iri skos:hasTopConcept ?concept .
?concept skos:prefLabel ?label .
OPTIONAL {
?narrowerConcept skos:broader ?concept .
}
Expand All @@ -112,7 +112,7 @@ def get_concept_scheme_top_concepts_query(iri: str, page: int, per_page: int) ->
OPTIONAL {
?concept skos:topConceptOf ?iri .
?concept skos:prefLabel ?label .
OPTIONAL {
?narrowerConcept skos:broader ?concept .
}
Expand Down Expand Up @@ -141,7 +141,7 @@ def get_concept_narrowers_query(iri: str, page: int, per_page: int) -> str:
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
CONSTRUCT {
?concept skos:prefLabel ?label .
?concept prez:childrenCount ?narrowerChildrenCount .
Expand All @@ -161,25 +161,25 @@ def get_concept_narrowers_query(iri: str, page: int, per_page: int) -> str:
?concept skos:prefLabel ?label .
}
?iri rdf:type ?type .
{
SELECT (COUNT(?childConcept) AS ?childrenCount)
WHERE {
BIND(<{{ iri }}> as ?iri)
?childConcept skos:broader ?iri .
}
}
{
SELECT ?concept ?label (skos:Concept AS ?conceptType) (COUNT(?narrowerConcept) AS ?narrowerChildrenCount)
WHERE {
BIND(<{{ iri }}> as ?iri)
{
OPTIONAL {
?concept skos:broader ?iri .
?concept skos:prefLabel ?label .
OPTIONAL {
?narrowerConcept skos:broader ?concept .
}
Expand All @@ -192,7 +192,7 @@ def get_concept_narrowers_query(iri: str, page: int, per_page: int) -> str:
OPTIONAL {
?iri skos:narrower ?concept .
?concept skos:prefLabel ?label .
OPTIONAL {
?narrowerConcept skos:broader ?concept .
}
Expand All @@ -201,7 +201,7 @@ def get_concept_narrowers_query(iri: str, page: int, per_page: int) -> str:
}
}
}
# Filter out any unbound ?concept rows which are invalid and may contain
# a count of 0. This is possible because both paths within the select
# query are using the OPTIONAL clause.
Expand Down
4 changes: 1 addition & 3 deletions prez/renderers/json_renderer.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
from itertools import chain

from rdflib import Graph, URIRef, RDF, SH, Literal
from rdflib import Graph, URIRef, RDF, SH
from rdflib.term import Node

from prez.cache import profiles_graph_cache
Expand Down
2 changes: 1 addition & 1 deletion prez/routers/management.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ async def index():
g.add((URIRef(settings.system_uri), PREZ.version, Literal(settings.prez_version)))
g += endpoints_graph_cache
g += await return_annotation_predicates()
log.info(f"Populated API info")
log.info("Populated API info")
return await return_rdf(g, "text/turtle", profile_headers={})


Expand Down
28 changes: 3 additions & 25 deletions prez/routers/object.py
Original file line number Diff line number Diff line change
@@ -1,35 +1,11 @@
from string import Template
from typing import FrozenSet, Optional

from fastapi import APIRouter, Request, HTTPException, status, Query
from fastapi import Depends
from rdflib import Graph, Literal, URIRef, PROF, DCTERMS
from starlette.responses import PlainTextResponse

from prez.cache import (
endpoints_graph_cache,
profiles_graph_cache,
links_ids_graph_cache,
)
from prez.dependencies import get_repo, get_system_repo
from prez.models.listing import ListingModel
from prez.models.object_item import ObjectItem
from prez.models.profiles_and_mediatypes import ProfilesMediatypesInfo
from prez.queries.object import object_inbound_query, object_outbound_query
from prez.reference_data.prez_ns import PREZ
from prez.renderers.renderer import return_from_graph, return_profiles
from prez.routers.identifier import get_iri_route
from prez.services.curie_functions import get_curie_id_for_uri, get_uri_for_curie_id
from prez.services.model_methods import get_classes
from prez.services.objects import object_function
from prez.sparql.methods import Repo
from prez.sparql.objects_listings import (
get_endpoint_template_queries,
generate_relationship_query,
generate_item_construct,
generate_listing_construct,
generate_listing_count_construct,
)

router = APIRouter(tags=["Object"])

Expand Down Expand Up @@ -93,5 +69,7 @@ async def count_route(


@router.get("/object", summary="Object", name="https://prez.dev/endpoint/object")
async def object_route(request: Request, repo=Depends(get_repo), system_repo=Depends(get_system_repo)):
async def object_route(
request: Request, repo=Depends(get_repo), system_repo=Depends(get_system_repo)
):
return await object_function(request, repo=repo, system_repo=system_repo)
Loading

0 comments on commit 8062531

Please sign in to comment.