Skip to content

Commit

Permalink
chore: lint and format
Browse files Browse the repository at this point in the history
  • Loading branch information
lalewis1 committed Nov 6, 2024
1 parent 8b77ad0 commit 883940b
Show file tree
Hide file tree
Showing 45 changed files with 393 additions and 398 deletions.
35 changes: 18 additions & 17 deletions prez/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,61 +3,62 @@
from functools import partial
from pathlib import Path
from textwrap import dedent
from typing import Optional, Dict, Union, Any
from typing import Any, Dict, Optional, Union

from fastapi import FastAPI
from fastapi.openapi.utils import get_openapi
from rdflib import Graph
from starlette.middleware.cors import CORSMiddleware
from starlette.staticfiles import StaticFiles

from prez.config import settings, Settings
from prez.config import Settings, settings
from prez.dependencies import (
get_annotations_store,
get_async_http_client,
get_pyoxi_store,
load_local_data_to_oxigraph,
get_oxrdflib_store,
get_pyoxi_store,
get_queryable_props,
get_system_store,
load_system_data_to_oxigraph,
load_annotations_data_to_oxigraph,
get_annotations_store,
get_queryable_props,
load_local_data_to_oxigraph,
load_system_data_to_oxigraph,
)
from prez.exceptions.model_exceptions import (
ClassNotFoundException,
URINotFoundException,
NoProfilesException,
InvalidSPARQLQueryException,
PrefixNotFoundException,
NoEndpointNodeshapeException,
NoProfilesException,
PrefixNotFoundException,
URINotFoundException,
)
from prez.middleware import create_validate_header_middleware
from prez.repositories import RemoteSparqlRepo, PyoxigraphRepo, OxrdflibRepo
from prez.repositories import OxrdflibRepo, PyoxigraphRepo, RemoteSparqlRepo
from prez.routers.base_router import router as base_prez_router
from prez.routers.custom_endpoints import create_dynamic_router
from prez.routers.identifier import router as identifier_router
from prez.routers.management import router as management_router, config_router
from prez.routers.management import config_router
from prez.routers.management import router as management_router
from prez.routers.ogc_features_router import features_subapi
from prez.routers.sparql import router as sparql_router
from prez.services.app_service import (
healthcheck_sparql_endpoints,
count_objects,
create_endpoints_graph,
healthcheck_sparql_endpoints,
populate_api_info,
prefix_initialisation,
retrieve_remote_template_queries,
retrieve_remote_queryable_definitions,
retrieve_remote_template_queries,
)
from prez.services.exception_catchers import (
catch_400,
catch_404,
catch_500,
catch_class_not_found_exception,
catch_uri_not_found_exception,
catch_no_profiles_exception,
catch_invalid_sparql_query,
catch_prefix_not_found_exception,
catch_no_endpoint_nodeshape_exception,
catch_no_profiles_exception,
catch_prefix_not_found_exception,
catch_uri_not_found_exception,
)
from prez.services.generate_profiles import create_profiles_graph
from prez.services.prez_logging import setup_logger
Expand Down
2 changes: 1 addition & 1 deletion prez/bnode.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from rdflib import Graph, URIRef, BNode
from rdflib import BNode, Graph, URIRef
from rdflib.term import Node


Expand Down
2 changes: 1 addition & 1 deletion prez/cache.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from aiocache import caches
from pyoxigraph.pyoxigraph import Store
from rdflib import Graph, ConjunctiveGraph, Dataset
from rdflib import ConjunctiveGraph, Dataset, Graph

profiles_graph_cache = Dataset()
profiles_graph_cache.bind("prez", "https://prez.dev/")
Expand Down
2 changes: 1 addition & 1 deletion prez/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from typing import Any, Dict, List, Optional, Tuple, Union

import toml
from pydantic import field_validator, Field
from pydantic import field_validator
from pydantic_settings import BaseSettings
from rdflib import DCTERMS, RDFS, SDO, URIRef
from rdflib.namespace import SKOS
Expand Down
33 changes: 18 additions & 15 deletions prez/dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,35 @@
from pathlib import Path

import httpx
from fastapi import Depends, Request, HTTPException
from fastapi import Depends, HTTPException, Request
from pyoxigraph import Store
from rdflib import Dataset, URIRef, Graph, SKOS, RDF
from rdflib import RDF, SKOS, Dataset, URIRef
from sparql_grammar_pydantic import IRI, Var

from prez.cache import (
store,
oxrdflib_store,
system_store,
profiles_graph_cache,
endpoints_graph_cache,
annotations_store,
endpoints_graph_cache,
oxrdflib_store,
prez_system_graph,
profiles_graph_cache,
queryable_props,
store,
system_store,
)
from prez.config import settings
from prez.enums import (
GeoJSONMediaType,
JSONMediaType,
NonAnnotatedRDFMediaType,
SPARQLQueryMediaType,
JSONMediaType,
GeoJSONMediaType,
)
from prez.exceptions.model_exceptions import NoEndpointNodeshapeException, URINotFoundException
from prez.exceptions.model_exceptions import (
NoEndpointNodeshapeException,
URINotFoundException,
)
from prez.models.query_params import QueryParams
from prez.reference_data.prez_ns import ALTREXT, ONT, EP, OGCE, OGCFEAT
from prez.repositories import PyoxigraphRepo, RemoteSparqlRepo, OxrdflibRepo, Repo
from prez.reference_data.prez_ns import ALTREXT, EP, OGCE, OGCFEAT, ONT
from prez.repositories import OxrdflibRepo, PyoxigraphRepo, RemoteSparqlRepo, Repo
from prez.services.classes import get_classes_single
from prez.services.connegp_service import NegotiatedPMTs
from prez.services.curie_functions import get_uri_for_curie_id
Expand Down Expand Up @@ -180,7 +183,7 @@ async def cql_get_parser_dependency(
return cql_parser
except json.JSONDecodeError:
raise HTTPException(status_code=400, detail="Invalid JSON format.")
except Exception as e:
except Exception:
raise HTTPException(
status_code=400, detail="Invalid CQL format: Parsing failed."
)
Expand Down Expand Up @@ -257,7 +260,7 @@ async def get_unprefixed_url_path(
request: Request,
) -> str:
root_path = request.scope.get("app_root_path", request.scope.get("root_path", ""))
return request.url.path[len(root_path):]
return request.url.path[len(root_path) :]


async def get_focus_node(
Expand Down Expand Up @@ -357,7 +360,7 @@ async def get_endpoint_nodeshapes(
"""
node_selection_shape_uri = None
relevant_ns_query = f"""SELECT ?ns ?tc
WHERE {{
WHERE {{
{ep_uri.n3()} <https://prez.dev/ont/relevantShapes> ?ns .
?ns <http://www.w3.org/ns/shacl#targetClass> ?tc ;
<https://prez.dev/ont/hierarchyLevel> {hierarchy_level} .
Expand Down
7 changes: 4 additions & 3 deletions prez/exceptions/model_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@ class URINotFoundException(Exception):

def __init__(self, uri: URIRef = None, curie: str = None):
if uri:
self.message = f"URI \"{uri}\" not found at endpoint {settings.sparql_endpoint}."
self.message = (
f'URI "{uri}" not found at endpoint {settings.sparql_endpoint}.'
)
if curie:
self.message = f"URI for curie \"{curie}\" not found at endpoint {settings.sparql_endpoint}."
self.message = f'URI for curie "{curie}" not found at endpoint {settings.sparql_endpoint}.'
super().__init__(self.message)


Expand Down Expand Up @@ -73,4 +75,3 @@ def __init__(self, ep_uri: str, hierarchy_level: int):
f"{hierarchy_level}, and parent URI"
)
super().__init__(self.message)

11 changes: 3 additions & 8 deletions prez/models/ogc_features.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
from typing import List
from typing import Optional
from enum import Enum
from typing import List, Literal, Optional, Union

from pydantic import BaseModel, Field
from pydantic import AnyUrl, BaseModel, Field

from prez.config import settings


########################################################################################################################
# Landing Page

Expand Down Expand Up @@ -173,10 +172,6 @@ class Config:
########################################################################################################################
# Queryables

from pydantic import BaseModel, Field, AnyUrl
from typing import Optional, List, Union, Literal
from enum import Enum


class GeometryType(str, Enum):
POINT = "Point"
Expand Down
6 changes: 3 additions & 3 deletions prez/models/query_params.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import json
from datetime import datetime
from typing import Optional, List, Tuple, Union
from typing import List, Optional, Tuple, Union

from fastapi import HTTPException, Query, Depends
from fastapi import Depends, HTTPException, Query

from prez.enums import FilterLangEnum, OrderByDirectionEnum

Expand Down Expand Up @@ -66,7 +66,7 @@ def validate_datetime(
None,
description=""" Either a date-time or an interval. Date and time expressions adhere to RFC 3339.
Intervals may be bounded or half-bounded (double-dots at start or end).
Temporal geometries are either a date-time value or a time interval. The parameter value SHALL conform to the following syntax (using ABNF):
interval-bounded = date-time "/" date-time
Expand Down
23 changes: 15 additions & 8 deletions prez/renderers/json_renderer.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from rdflib import Graph, URIRef, RDF, SH
from rdflib import RDF, SH, Graph, URIRef
from rdflib.term import Node

from prez.cache import profiles_graph_cache
Expand Down Expand Up @@ -77,13 +77,20 @@ async def render_json_dropdown(
"iri": "@id",
}

(
child_to_focus_predicates,
parent_to_focus,
focus_to_child_predicates,
focus_to_parent_predicates,
relative_predicates,
) = get_listing_predicates(profile, selected_class)
# BUG: get_listing_predicates is undefined
# (
# child_to_focus_predicates,
# parent_to_focus,
# focus_to_child_predicates,
# focus_to_parent_predicates,
# relative_predicates,
# ) = get_listing_predicates(profile, selected_class)

# see above ^
child_to_focus_predicates = None
focus_to_parent_predicates = None
focus_to_child_predicates = None
relative_predicates = None

if (
not child_to_focus_predicates
Expand Down
4 changes: 2 additions & 2 deletions prez/renderers/renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
from fastapi.exceptions import HTTPException
from fastapi.responses import StreamingResponse
from rdf2geojson import convert
from rdflib import Graph, URIRef, RDF
from rdflib import RDF, Graph, URIRef

from prez.cache import prefix_graph
from prez.renderers.csv_renderer import render_csv_dropdown
from prez.renderers.json_renderer import render_json_dropdown, NotFoundError
from prez.renderers.json_renderer import NotFoundError, render_json_dropdown
from prez.repositories import Repo
from prez.services.annotations import get_annotation_properties
from prez.services.connegp_service import RDF_MEDIATYPES, RDF_SERIALIZER_TYPES_MAP
Expand Down
5 changes: 2 additions & 3 deletions prez/repositories/base.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import asyncio
import logging
from abc import ABC, abstractmethod
from typing import List
from typing import Tuple
from typing import List, Tuple

from rdflib import Namespace, Graph, URIRef
from rdflib import Graph, Namespace, URIRef

from prez.cache import prefix_graph

Expand Down
2 changes: 1 addition & 1 deletion prez/repositories/oxrdflib.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import logging

from fastapi.concurrency import run_in_threadpool
from rdflib import Namespace, Graph, URIRef, Literal, BNode
from rdflib import BNode, Graph, Literal, Namespace, URIRef

from prez.repositories.base import Repo

Expand Down
2 changes: 1 addition & 1 deletion prez/repositories/pyoxigraph.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import pyoxigraph
from fastapi.concurrency import run_in_threadpool
from rdflib import Namespace, Graph, URIRef
from rdflib import Graph, Namespace, URIRef

from prez.exceptions.model_exceptions import InvalidSPARQLQueryException
from prez.repositories.base import Repo
Expand Down
25 changes: 13 additions & 12 deletions prez/repositories/remote_sparql.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from urllib.parse import quote_plus

import httpx
from rdflib import Namespace, Graph, URIRef
from rdflib import Graph, Namespace, URIRef

from prez.config import settings
from prez.repositories.base import Repo
Expand Down Expand Up @@ -58,13 +58,17 @@ async def tabular_query_to_table(self, query: str, context: URIRef = None):
return context, response.json()["results"]["bindings"]

async def sparql(
self, query: str, raw_headers: list[tuple[bytes, bytes]], method: str = "GET"
self, query: str, raw_headers: list[tuple[bytes, bytes]], method: str = "GET"
):
"""Sends a request (containing a SPARQL query in the URL parameters) to a proxied SPARQL endpoint."""
# Convert raw_headers to a dict, excluding the 'host' header
headers = {k.decode('utf-8'): v.decode('utf-8') for k, v in raw_headers if k.lower() != b'host'}
headers = {
k.decode("utf-8"): v.decode("utf-8")
for k, v in raw_headers
if k.lower() != b"host"
}

if method == 'GET':
if method == "GET":
query_escaped = quote_plus(query)
url = f"{settings.sparql_endpoint}?query={query_escaped}"
request = httpx.Request(method, url, headers=headers)
Expand All @@ -74,18 +78,15 @@ async def sparql(
form_data = f"query={quote_plus(query)}"

# Set correct headers for form data
headers['content-type'] = 'application/x-www-form-urlencoded'
headers['content-length'] = str(len(form_data))
headers["content-type"] = "application/x-www-form-urlencoded"
headers["content-length"] = str(len(form_data))

request = httpx.Request(
method,
url,
headers=headers,
content=form_data.encode('utf-8')
method, url, headers=headers, content=form_data.encode("utf-8")
)

# Add the correct 'host' header
request.headers['host'] = httpx.URL(url).host
request.headers["host"] = httpx.URL(url).host

response = await self.async_client.send(request, stream=True)
try:
Expand All @@ -96,7 +97,7 @@ async def sparql(
raise httpx.HTTPStatusError(
f"HTTP Error {response.status_code}: {response.text}",
request=request,
response=response
response=response,
) from e

return response
Loading

0 comments on commit 883940b

Please sign in to comment.