Skip to content

Commit f12a996

Browse files
committed
Linting and easier version handling
1 parent d5b57db commit f12a996

File tree

10 files changed

+32
-19
lines changed

10 files changed

+32
-19
lines changed

tests/test_create_geoparquet.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
from vecorel_cli.create_geoparquet import CreateGeoParquet
66
from vecorel_cli.encoding.geoparquet import GeoParquet
7+
from vecorel_cli.vecorel.schemas import Schemas
78

89

910
def test_create_geoparquet(tmp_parquet_file: Path):
@@ -22,6 +23,7 @@ def test_create_geoparquet(tmp_parquet_file: Path):
2223
schema_ids = collection["schemas"].keys()
2324
assert len(schema_ids) == 1
2425
assert "inspire" in schema_ids
26+
assert all(Schemas.get_core_uri() in s for s in collection["schemas"].values())
2527

2628
data = gp.read()
2729
assert len(data) == 2

tests/test_jsonschema.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,10 @@
22
from vecorel_cli.encoding.geojson import GeoJSON
33
from vecorel_cli.validate_schema import ValidateSchema
44
from vecorel_cli.vecorel.util import load_file
5-
from vecorel_cli.vecorel.version import vecorel_version
65

76
vecorel_schema_uri = "tests/data-files/jsonschema/schema.yaml"
87
vecorel_schema = load_file(vecorel_schema_uri)
9-
datatypes_uri = GeoJSON.get_datatypes_uri(vecorel_version)
8+
datatypes_uri = GeoJSON.get_datatypes_uri()
109
datatypes = GeoJSON.load_datatypes(datatypes_uri)
1110
schema_id = "https://example.com/schema.json"
1211
point = {"type": "Point", "coordinates": [0, 0]}

vecorel_cli/cli/logger.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import re
22
import sys
33
from logging import Logger
4-
from typing import Any, Optional
4+
from typing import Any, Optional, Union
55

66
from loguru import logger
77

@@ -36,7 +36,7 @@ def info(self, message: str, **kwargs):
3636
def warning(self, message: str, **kwargs):
3737
self.log(message, "warning", **kwargs)
3838

39-
def error(self, message: str, **kwargs):
39+
def error(self, message: Union[Exception, str], **kwargs):
4040
self.log(message, "error", **kwargs)
4141

4242
def success(self, message: str, **kwargs):
@@ -58,7 +58,9 @@ def log(
5858
# Escape XML/HTML tags etc. that look like loguru color directives
5959
# The regexp is coming directly from the loguru source code, see
6060
# https://github.com/Delgan/loguru/blob/master/loguru/_colorizer.py
61-
message = re.sub(r"(</?(?:[fb]g\s)?[^<>\s]*>)", r"\\\1", message, count=0, flags=re.IGNORECASE)
61+
message = re.sub(
62+
r"(</?(?:[fb]g\s)?[^<>\s]*>)", r"\\\1", message, count=0, flags=re.IGNORECASE
63+
)
6264

6365
# Handle indentation (including multiple lines)
6466
message = self._indent_text(message, indent)

vecorel_cli/conversion/base.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
from ..vecorel.schemas import Schemas
3131
from ..vecorel.typing import Sources
3232
from ..vecorel.util import get_fs, name_from_uri, stream_file
33-
from ..vecorel.version import vecorel_version
3433

3534

3635
class BaseConverter(LoggerMixin):
@@ -274,7 +273,7 @@ def get_title(self):
274273
return f"{title} ({self.variant})" if self.variant else title
275274

276275
def create_collection(self, cid) -> Collection:
277-
schema_uris = [Schemas.get_core_uri(vecorel_version)]
276+
schema_uris = [Schemas.get_core_uri()]
278277
schema_uris.extend(self.extensions)
279278
collection = Collection(
280279
{

vecorel_cli/create_jsonschema.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def get_cli_args():
3131
type=PathOrURL(extensions=[".json"]),
3232
help=f"Vecorel schema to create the JSON Schema for. Can be a local file or a URL. If not provided, loads the schema for Vecorel version {vecorel_version}.",
3333
show_default=True,
34-
default=Schemas.get_core_uri(vecorel_version),
34+
default=Schemas.get_core_uri(),
3535
),
3636
"datatypes": click.option(
3737
"--datatypes",
@@ -40,7 +40,7 @@ def get_cli_args():
4040
type=PathOrURL(extensions=[".json"]),
4141
help=f"Schema for the Vecorel GeoJSON datatypes. Can be a local file or a URL. If not provided, loads the GeoJSON datatypes for Vecorel version {vecorel_version}.",
4242
show_default=True,
43-
default=GeoJSON.get_datatypes_uri(vecorel_version),
43+
default=GeoJSON.get_datatypes_uri(),
4444
),
4545
"target": VECOREL_TARGET_CONSOLE,
4646
"id": click.option(

vecorel_cli/encoding/base.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,16 @@ def get_summary(self) -> dict:
3737
"Compression": self.get_compression() or "None",
3838
}
3939

40-
def _load_collection(self) -> dict:
40+
def _load_collection(self) -> Union[Collection, dict]:
4141
return {}
4242

4343
def get_collection(self) -> Collection:
4444
"""
4545
Get the collection metadata.
4646
"""
4747
if self.collection is None:
48-
self.collection = Collection(self._load_collection())
48+
c = self._load_collection()
49+
self.collection = c if isinstance(c, Collection) else Collection(c)
4950
return self.collection
5051

5152
def set_collection(self, collection: Union[Collection, dict]):

vecorel_cli/encoding/geojson.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from ..vecorel.collection import Collection
1515
from ..vecorel.typing import Feature, FeatureCollection, SchemaMapping
1616
from ..vecorel.util import load_file, to_iso8601
17+
from ..vecorel.version import vecorel_version
1718
from .base import BaseEncoding
1819

1920

@@ -34,16 +35,18 @@ def load_datatypes(uri: Union[Path, URL, str]) -> dict:
3435
return response.get("$defs", {})
3536

3637
@staticmethod
37-
def get_datatypes_uri(version: str) -> str:
38+
def get_datatypes_uri(version: Optional[str] = None) -> str:
39+
if version is None:
40+
version = vecorel_version
3841
return GeoJSON.datatypes_schema_uri.format(version=version)
3942

4043
def get_format(self) -> str:
4144
return "GeoJSON"
4245

43-
def _load_collection(self) -> Collection:
46+
def _load_collection(self) -> Union[Collection, dict]:
4447
if self.fs.exists(self.uri):
4548
self.read(num=0)
46-
return self.collection or Collection()
49+
return self.collection or {}
4750

4851
def get_validator(self) -> Optional[Validator]:
4952
from ..validation.geojson import GeoJSONValidator

vecorel_cli/registry.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,12 @@ class VecorelRegistry:
3333
"collection",
3434
]
3535

36-
# Any schemas that are mandatory to have, except for the vecorel specification schema
36+
# Any schemas patterns that are mandatory to have, except for the vecorel specification schema
3737
required_extensions: list[Union[str, re.Pattern]] = []
3838

39+
# Any specific schemas URIs that are added by default when creating a new dataset
40+
default_extensions: list[str] = []
41+
3942
# The filenames for datasets (converters) that should be ignored by the CLI.
4043
# Always ignores files with a starting "." or "__"
4144
ignored_datasets: list[str] = [

vecorel_cli/validate.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,14 @@ def get_cli_args():
3535
def validate_cli(
3636
self,
3737
source: list[Union[str, Path]],
38-
num: int = 100,
38+
num: Optional[int] = 100,
3939
schema_map: SchemaMapping = {},
4040
):
4141
if not isinstance(source, list):
4242
raise ValueError("Source must be a list.")
4343
if len(source) == 0:
4444
raise ValueError("No source files provided")
45-
if num < 0:
45+
if num is not None and num < 0:
4646
num = None
4747

4848
invalid = 0
@@ -102,6 +102,8 @@ def validate(
102102
) -> Validator:
103103
encoding = create_encoding(file)
104104
validator = encoding.get_validator()
105+
if validator is None:
106+
raise ValueError(f"No validator available for files of type {encoding.get_format()}")
105107
validator.set_required_schemas(Registry.required_extensions)
106108
validator.validate(num=num, schema_map=schema_map)
107109
return validator

vecorel_cli/vecorel/schemas.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
from ..vecorel.typing import RawSchemas, SchemaMapping
88
from ..vecorel.util import load_file
9-
from .version import is_sdl_supported, sdl_uri
9+
from .version import is_sdl_supported, sdl_uri, vecorel_version
1010

1111
if TYPE_CHECKING:
1212
from ..validation.base import Validator
@@ -314,7 +314,9 @@ class Schemas(dict):
314314
spec_schema = "https://vecorel.org/specification/v{version}/schema.yaml"
315315

316316
@staticmethod
317-
def get_core_uri(version: str) -> str:
317+
def get_core_uri(version: Optional[str] = None) -> str:
318+
if version is None:
319+
version = vecorel_version
318320
return Schemas.spec_schema.format(version=version)
319321

320322
def __init__(self, schemas: Union[RawSchemas, "Schemas"] = {}):

0 commit comments

Comments
 (0)