Skip to content

Commit

Permalink
Use patched version of jsonschema
Browse files Browse the repository at this point in the history
  • Loading branch information
dragon-dxw committed Sep 26, 2024
1 parent 20c80d8 commit 10cdce9
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 8 deletions.
18 changes: 15 additions & 3 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,31 @@ repos:
- id: check-xml
- id: check-yaml
- id: end-of-file-fixer
exclude: courts_schema_types_autogenerated\.py
- id: forbid-submodules
- id: mixed-line-ending
- id: no-commit-to-branch
- id: trailing-whitespace

# There is a bug with upstream which means the imports are
# not sorted -- this should do for now
# - repo: https://github.com/camptocamp/jsonschema-gentypes
- repo: https://github.com/dxw/jsonschema-gentypes
rev: patch-1
hooks:
- id: jsonschema-gentypes
files: src/ds_caselaw_utils/data/schema/courts.schema.json

- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.6.7
rev: v0.6.8
hooks:
- id: ruff
args:
- --fix
- --exit-non-zero-on-fix
exclude: courts_schema_types_autogenerated\.py
- id: ruff-format
exclude: courts_schema_types_autogenerated\.py

- repo: https://github.com/pre-commit/mirrors-mypy
rev: v1.11.2
Expand All @@ -33,7 +45,7 @@ repos:
args:
- --strict
files: ^(src/ds_caselaw_utils|scripts)
exclude: test_
exclude: (?:test_|courts_schema_types_autogenerated\.py)

- repo: https://github.com/pre-commit/mirrors-mypy
rev: v1.11.2
Expand Down Expand Up @@ -71,7 +83,7 @@ repos:
exclude: courts\.md

- repo: https://github.com/commitizen-tools/commitizen
rev: v3.29.0
rev: v3.29.1
hooks:
- id: commitizen
- id: commitizen-branch
Expand Down
22 changes: 22 additions & 0 deletions jsonschema-gentypes.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
headers: >
# Automatically generated file from a JSON schema
# Used to correctly format the generated file
# callbacks:
# - - black
# - - isort
generate:
- # JSON schema file path
source: src/ds_caselaw_utils/data/schema/courts.schema.json
# Python file path
destination: src/ds_caselaw_utils/courts_schema_types_autogenerated.py
# The name of the root element
root_name: RawCourtRepository
# Argument passed to the API
api_arguments:
additional_properties: Only explicit
# Rename an element
name_mapping: {}
# The minimum Python version that the code should support. By default the
# currently executing Python version is chosen. Note that the output
# may require typing_extensions to be installed.
python_version: "3.9"
9 changes: 5 additions & 4 deletions src/ds_caselaw_utils/courts.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from datetime import date
from typing import NewType, Optional

from courts_schema_types_autogenerated import RawCourt, RawCourtRepositoryData, RawJurisdiction
from ruamel.yaml import YAML

CourtCode = NewType("CourtCode", str)
Expand All @@ -16,14 +17,14 @@


class Jurisdiction:
def __init__(self, data):
def __init__(self, data: RawJurisdiction):
self.code: JurisdictionCode = data["code"]
self.name = data["name"]
self.prefix: str = data.get("prefix")


class Court:
def __init__(self, data) -> None:
def __init__(self, data: RawCourt) -> None:
self.code: CourtCode = CourtCode(data["code"])
self.name: str = data["name"]
self.grouped_name: str = data.get("grouped_name") or data["name"]
Expand Down Expand Up @@ -112,7 +113,7 @@ class CourtNotFoundException(Exception):


class CourtsRepository:
def __init__(self, data):
def __init__(self, data: RawCourtRepositoryData):
self._data = data
self._byParam = {}
self._byCode = {}
Expand Down Expand Up @@ -223,6 +224,6 @@ def get_listable_tribunals(self) -> list[Court]:
yaml = YAML()
datafile = pathlib.Path(__file__).parent / "data/court_names.yaml"
with open(datafile) as f:
court_data = yaml.load(f)
court_data: RawCourtRepositoryData = yaml.load(f)

courts = CourtsRepository(court_data)
86 changes: 86 additions & 0 deletions src/ds_caselaw_utils/courts_schema_types_autogenerated.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
# Automatically generated file from a JSON schema


from typing import List, Required, TypedDict, Union


class RawCourt(TypedDict, total=False):
""" Raw Court. """

code: Required[str]
"""
pattern: ^[A-Za-z]{2,}(-[A-Za-z0-9]+)*$
Required property
"""

name: Required[str]
""" Required property """

grouped_name: str
param: str
""" pattern: ^[a-z]{2,}(?:/[a-z0-9]+)?$ """

extra_params: List["_RawCourtExtraParamsItem"]
ncn: str
link: Required[str]
"""
format: uri
Required property
"""

start_year: int
""" minimum: 1066 """

end_year: int
listable: Required[bool]
""" Required property """

selectable: Required[bool]
""" Required property """

jurisdictions: List["RawJurisdiction"]


RawCourtRepository = List["_RawCourtListItem"]
"""
Raw Court List.
A list of courts
"""



class RawJurisdiction(TypedDict, total=False):
""" Raw Jurisdiction. """

prefix: Required[str]
""" Required property """

name: Required[str]
""" Required property """

code: Required[str]
""" Required property """



_RawCourtExtraParamsItem = str
""" pattern: ^[a-z]{2,}(/[a-z]+)?$ """



class _RawCourtListItem(TypedDict, total=False):
name: Required[str]
""" Required property """

display_name: Required[Union[str, None]]
""" Required property """

is_tribunal: Required[bool]
""" Required property """

courts: Required[List["RawCourt"]]
""" Required property """

4 changes: 3 additions & 1 deletion src/ds_caselaw_utils/data/schema/courts.schema.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "https://schema.caselaw.nationalarchives.gov.uk/courts.schema.json",
"title": "Courts",
"title": "Raw Court List",
"description": "A list of courts",
"type": "array",
"items": {
Expand All @@ -20,6 +20,7 @@
"type": "array",
"items": {
"type": "object",
"title": "Raw Court",
"properties": {
"code": {
"type": "string",
Expand Down Expand Up @@ -65,6 +66,7 @@
"jurisdictions": {
"type": "array",
"items": {
"title": "Raw Jurisdiction",
"type": "object",
"properties": {
"prefix": {
Expand Down

0 comments on commit 10cdce9

Please sign in to comment.