OpenGeodeWeb-Microservice 1.3.1rc1__tar.gz → 1.3.2rc1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {opengeodeweb_microservice-1.3.1rc1/src/OpenGeodeWeb_Microservice.egg-info → opengeodeweb_microservice-1.3.2rc1}/PKG-INFO +1 -1
- {opengeodeweb_microservice-1.3.1rc1 → opengeodeweb_microservice-1.3.2rc1}/pyproject.toml +1 -1
- {opengeodeweb_microservice-1.3.1rc1 → opengeodeweb_microservice-1.3.2rc1/src/OpenGeodeWeb_Microservice.egg-info}/PKG-INFO +1 -1
- opengeodeweb_microservice-1.3.2rc1/src/opengeodeweb_microservice/database/connection.py +62 -0
- {opengeodeweb_microservice-1.3.1rc1 → opengeodeweb_microservice-1.3.2rc1}/src/opengeodeweb_microservice/database/data.py +7 -7
- {opengeodeweb_microservice-1.3.1rc1 → opengeodeweb_microservice-1.3.2rc1}/src/opengeodeweb_microservice/database/data_types.py +10 -5
- {opengeodeweb_microservice-1.3.1rc1 → opengeodeweb_microservice-1.3.2rc1}/src/opengeodeweb_microservice/schemas.py +30 -25
- {opengeodeweb_microservice-1.3.1rc1 → opengeodeweb_microservice-1.3.2rc1}/tests/test_database.py +8 -5
- {opengeodeweb_microservice-1.3.1rc1 → opengeodeweb_microservice-1.3.2rc1}/tests/test_schemas.py +5 -12
- opengeodeweb_microservice-1.3.1rc1/src/opengeodeweb_microservice/database/connection.py +0 -37
- {opengeodeweb_microservice-1.3.1rc1 → opengeodeweb_microservice-1.3.2rc1}/LICENSE +0 -0
- {opengeodeweb_microservice-1.3.1rc1 → opengeodeweb_microservice-1.3.2rc1}/README.md +0 -0
- {opengeodeweb_microservice-1.3.1rc1 → opengeodeweb_microservice-1.3.2rc1}/requirements.txt +0 -0
- {opengeodeweb_microservice-1.3.1rc1 → opengeodeweb_microservice-1.3.2rc1}/setup.cfg +0 -0
- {opengeodeweb_microservice-1.3.1rc1 → opengeodeweb_microservice-1.3.2rc1}/src/OpenGeodeWeb_Microservice.egg-info/SOURCES.txt +0 -0
- {opengeodeweb_microservice-1.3.1rc1 → opengeodeweb_microservice-1.3.2rc1}/src/OpenGeodeWeb_Microservice.egg-info/dependency_links.txt +0 -0
- {opengeodeweb_microservice-1.3.1rc1 → opengeodeweb_microservice-1.3.2rc1}/src/OpenGeodeWeb_Microservice.egg-info/requires.txt +0 -0
- {opengeodeweb_microservice-1.3.1rc1 → opengeodeweb_microservice-1.3.2rc1}/src/OpenGeodeWeb_Microservice.egg-info/top_level.txt +0 -0
- {opengeodeweb_microservice-1.3.1rc1 → opengeodeweb_microservice-1.3.2rc1}/src/opengeodeweb_microservice/__init__.py +0 -0
- {opengeodeweb_microservice-1.3.1rc1 → opengeodeweb_microservice-1.3.2rc1}/src/opengeodeweb_microservice/database/__init__.py +0 -0
- {opengeodeweb_microservice-1.3.1rc1 → opengeodeweb_microservice-1.3.2rc1}/src/opengeodeweb_microservice/database/base.py +0 -0
- {opengeodeweb_microservice-1.3.1rc1 → opengeodeweb_microservice-1.3.2rc1}/src/opengeodeweb_microservice/error.json +0 -0
- {opengeodeweb_microservice-1.3.1rc1 → opengeodeweb_microservice-1.3.2rc1}/src/opengeodeweb_microservice/py.typed +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: OpenGeodeWeb-Microservice
|
|
3
|
-
Version: 1.3.
|
|
3
|
+
Version: 1.3.2rc1
|
|
4
4
|
Summary: Database model and ORM layer for OpenGeodeWeb ecosystem
|
|
5
5
|
Author-email: Geode-solutions <team-web@geode-solutions.com>
|
|
6
6
|
Project-URL: Homepage, https://github.com/Geode-solutions/OpenGeodeWeb-Microservice
|
|
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
|
|
|
4
4
|
|
|
5
5
|
[project]
|
|
6
6
|
name = "OpenGeodeWeb-Microservice"
|
|
7
|
-
version = "1.3.
|
|
7
|
+
version = "1.3.2-rc.1"
|
|
8
8
|
dynamic = ["dependencies"]
|
|
9
9
|
authors = [{ name = "Geode-solutions", email = "team-web@geode-solutions.com" }]
|
|
10
10
|
description = "Database model and ORM layer for OpenGeodeWeb ecosystem"
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: OpenGeodeWeb-Microservice
|
|
3
|
-
Version: 1.3.
|
|
3
|
+
Version: 1.3.2rc1
|
|
4
4
|
Summary: Database model and ORM layer for OpenGeodeWeb ecosystem
|
|
5
5
|
Author-email: Geode-solutions <team-web@geode-solutions.com>
|
|
6
6
|
Project-URL: Homepage, https://github.com/Geode-solutions/OpenGeodeWeb-Microservice
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
"""Database connection management"""
|
|
2
|
+
|
|
3
|
+
import logging
|
|
4
|
+
from dataclasses import dataclass
|
|
5
|
+
from pathlib import Path
|
|
6
|
+
|
|
7
|
+
from sqlalchemy import Engine, create_engine
|
|
8
|
+
from sqlalchemy.orm import Session, scoped_session, sessionmaker
|
|
9
|
+
|
|
10
|
+
from .base import Base
|
|
11
|
+
|
|
12
|
+
DATABASE_FILENAME = Path("project.db")
|
|
13
|
+
|
|
14
|
+
logger = logging.getLogger(__name__)
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
class DatabaseNotInitializedError(RuntimeError):
|
|
18
|
+
def __init__(self) -> None:
|
|
19
|
+
super().__init__("Database not initialized. Call init_database() first.")
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
@dataclass
|
|
23
|
+
class _DatabaseState:
|
|
24
|
+
engine: Engine | None = None
|
|
25
|
+
scoped_session_registry: scoped_session[Session] | None = None
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
_state = _DatabaseState()
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def init_database(db_path: Path = DATABASE_FILENAME, *, create_tables: bool = True) -> None:
|
|
32
|
+
if _state.engine is not None:
|
|
33
|
+
logger.info("Database engine already exists for %s, reusing", db_path)
|
|
34
|
+
return
|
|
35
|
+
|
|
36
|
+
_state.engine = create_engine(
|
|
37
|
+
f"sqlite:///{db_path}",
|
|
38
|
+
connect_args={"check_same_thread": False},
|
|
39
|
+
)
|
|
40
|
+
logger.info("Database engine created for %s", db_path)
|
|
41
|
+
_state.scoped_session_registry = scoped_session(sessionmaker(bind=_state.engine))
|
|
42
|
+
if create_tables:
|
|
43
|
+
Base.metadata.create_all(_state.engine)
|
|
44
|
+
logger.info("Database tables created for %s", db_path)
|
|
45
|
+
else:
|
|
46
|
+
logger.info("Database connected (tables not created) for %s", db_path)
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
def close_database() -> None:
|
|
50
|
+
"""Release every session and connection so the database file can be replaced."""
|
|
51
|
+
if _state.scoped_session_registry is not None:
|
|
52
|
+
_state.scoped_session_registry.remove()
|
|
53
|
+
if _state.engine is not None:
|
|
54
|
+
_state.engine.dispose()
|
|
55
|
+
_state.engine = None
|
|
56
|
+
_state.scoped_session_registry = None
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
def get_session() -> Session:
|
|
60
|
+
if _state.scoped_session_registry is None:
|
|
61
|
+
raise DatabaseNotInitializedError
|
|
62
|
+
return _state.scoped_session_registry()
|
|
@@ -1,9 +1,11 @@
|
|
|
1
|
-
|
|
1
|
+
import uuid
|
|
2
|
+
|
|
3
|
+
from sqlalchemy import String, select
|
|
2
4
|
from sqlalchemy.orm import Mapped, mapped_column
|
|
3
|
-
|
|
5
|
+
|
|
4
6
|
from .base import Base
|
|
5
|
-
from .
|
|
6
|
-
import
|
|
7
|
+
from .connection import get_session
|
|
8
|
+
from .data_types import GeodeObjectType, ViewerElementsType, ViewerType
|
|
7
9
|
|
|
8
10
|
|
|
9
11
|
class Data(Base):
|
|
@@ -15,9 +17,7 @@ class Data(Base):
|
|
|
15
17
|
geode_id: Mapped[str] = mapped_column(String, nullable=False)
|
|
16
18
|
geode_object: Mapped[GeodeObjectType] = mapped_column(String, nullable=False)
|
|
17
19
|
viewer_object: Mapped[ViewerType] = mapped_column(String, nullable=False)
|
|
18
|
-
viewer_elements_type: Mapped[ViewerElementsType] = mapped_column(
|
|
19
|
-
String, nullable=False
|
|
20
|
-
)
|
|
20
|
+
viewer_elements_type: Mapped[ViewerElementsType] = mapped_column(String, nullable=False)
|
|
21
21
|
native_file: Mapped[str | None] = mapped_column(String, nullable=True)
|
|
22
22
|
viewable_file: Mapped[str | None] = mapped_column(String, nullable=True)
|
|
23
23
|
light_viewable_file: Mapped[str | None] = mapped_column(String, nullable=True)
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
from typing import Literal,
|
|
1
|
+
from typing import Literal, cast, get_args
|
|
2
2
|
|
|
3
3
|
GeodePointMeshType = Literal[
|
|
4
4
|
"PointSet2D",
|
|
@@ -60,12 +60,17 @@ def _flatten_literal_args(literal: object) -> tuple[str, ...]:
|
|
|
60
60
|
GeodeObjectType_values = _flatten_literal_args(GeodeObjectType)
|
|
61
61
|
|
|
62
62
|
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
63
|
+
class InvalidGeodeObjectTypeError(ValueError):
|
|
64
|
+
def __init__(self, value: str) -> None:
|
|
65
|
+
super().__init__(
|
|
66
66
|
f"Invalid GeodeObjectType: {value!r}. Must be one of {GeodeObjectType_values}"
|
|
67
67
|
)
|
|
68
|
-
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
def geode_object_type(value: str) -> GeodeObjectType:
|
|
71
|
+
if value not in GeodeObjectType_values:
|
|
72
|
+
raise InvalidGeodeObjectTypeError(value)
|
|
73
|
+
return cast("GeodeObjectType", value)
|
|
69
74
|
|
|
70
75
|
|
|
71
76
|
ViewerType = Literal["mesh", "model"]
|
|
@@ -1,37 +1,41 @@
|
|
|
1
|
-
import os
|
|
2
|
-
import glob
|
|
3
|
-
import json
|
|
4
1
|
import dataclasses
|
|
2
|
+
import json
|
|
3
|
+
import sys
|
|
4
|
+
from collections.abc import Sized
|
|
5
5
|
from dataclasses import dataclass
|
|
6
|
-
from
|
|
6
|
+
from pathlib import Path
|
|
7
|
+
from typing import TYPE_CHECKING, Any
|
|
7
8
|
|
|
8
9
|
from dataclasses_json import DataClassJsonMixin
|
|
9
10
|
|
|
10
|
-
|
|
11
|
+
if TYPE_CHECKING:
|
|
12
|
+
from _typeshed import DataclassInstance
|
|
11
13
|
|
|
12
|
-
|
|
14
|
+
type SchemaDict = dict[str, str]
|
|
13
15
|
|
|
14
|
-
|
|
15
|
-
ResponseT = TypeVar("ResponseT", bound=DataClassJsonMixin)
|
|
16
|
+
ERROR_SCHEMA_PATH = Path(__file__).parent / "error.json"
|
|
16
17
|
|
|
17
18
|
|
|
18
19
|
MAX_VALUE_LENGTH = 80
|
|
19
20
|
|
|
20
21
|
|
|
21
|
-
def _format_value(value:
|
|
22
|
+
def _format_value(value: object, max_length: int) -> str:
|
|
22
23
|
if dataclasses.is_dataclass(value) and not isinstance(value, type):
|
|
23
24
|
return format_dataclass(value, max_length)
|
|
24
25
|
text = repr(value)
|
|
25
26
|
if len(text) <= max_length:
|
|
26
27
|
return text
|
|
27
28
|
details = type(value).__name__
|
|
28
|
-
if
|
|
29
|
+
if isinstance(value, Sized):
|
|
29
30
|
details += f", len={len(value)}"
|
|
30
31
|
return f"{text[:max_length]}... ({details})"
|
|
31
32
|
|
|
32
33
|
|
|
33
|
-
def format_dataclass(instance:
|
|
34
|
-
"""Like repr(), but every field value longer than max_length
|
|
34
|
+
def format_dataclass(instance: "DataclassInstance", max_length: int = MAX_VALUE_LENGTH) -> str:
|
|
35
|
+
"""Like repr(), but truncate every field value longer than max_length.
|
|
36
|
+
|
|
37
|
+
Each truncated value is followed by its type and length.
|
|
38
|
+
"""
|
|
35
39
|
fields = ", ".join(
|
|
36
40
|
f"{field.name}={_format_value(getattr(instance, field.name), max_length)}"
|
|
37
41
|
for field in dataclasses.fields(instance)
|
|
@@ -39,30 +43,28 @@ def format_dataclass(instance: Any, max_length: int = MAX_VALUE_LENGTH) -> str:
|
|
|
39
43
|
return f"{type(instance).__name__}({fields})"
|
|
40
44
|
|
|
41
45
|
|
|
42
|
-
def print_dataclass(instance:
|
|
43
|
-
|
|
46
|
+
def print_dataclass(instance: "DataclassInstance") -> None:
|
|
47
|
+
sys.stdout.write(f"{format_dataclass(instance)}\n")
|
|
48
|
+
sys.stdout.flush()
|
|
44
49
|
|
|
45
50
|
|
|
46
|
-
def get_schemas_dict(path:
|
|
51
|
+
def get_schemas_dict(path: Path) -> dict[str, SchemaDict]:
|
|
47
52
|
schemas_dict: dict[str, SchemaDict] = {}
|
|
48
|
-
for json_file in
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
file_content = json.load(file)
|
|
52
|
-
schemas_dict[os.path.splitext(filename)[0]] = file_content
|
|
53
|
+
for json_file in path.glob("*.json"):
|
|
54
|
+
with json_file.open() as file:
|
|
55
|
+
schemas_dict[json_file.stem] = json.load(file)
|
|
53
56
|
return schemas_dict
|
|
54
57
|
|
|
55
58
|
|
|
56
|
-
def load_schema(python_file:
|
|
59
|
+
def load_schema(python_file: Path) -> dict[str, Any]:
|
|
57
60
|
"""Load the JSON route schema sitting next to a generated schema module."""
|
|
58
|
-
|
|
59
|
-
with open(json_file, "r") as file:
|
|
61
|
+
with python_file.with_suffix(".json").open() as file:
|
|
60
62
|
schema: dict[str, Any] = json.load(file)
|
|
61
63
|
return schema
|
|
62
64
|
|
|
63
65
|
|
|
64
66
|
@dataclass(frozen=True)
|
|
65
|
-
class Route
|
|
67
|
+
class Route[ParamsT: DataClassJsonMixin, ResponseT: DataClassJsonMixin]:
|
|
66
68
|
"""Generated binding between a route schema, its request params and its success response."""
|
|
67
69
|
|
|
68
70
|
schema: dict[str, Any]
|
|
@@ -72,7 +74,10 @@ class Route(Generic[ParamsT, ResponseT]):
|
|
|
72
74
|
|
|
73
75
|
@dataclass
|
|
74
76
|
class BinaryResponse(DataClassJsonMixin):
|
|
75
|
-
"""Response of a route streaming a file
|
|
77
|
+
"""Response of a route streaming a file instead of JSON.
|
|
78
|
+
|
|
79
|
+
Matches a schema with `"response": {"format": "binary"}`.
|
|
80
|
+
"""
|
|
76
81
|
|
|
77
82
|
|
|
78
83
|
@dataclass
|
{opengeodeweb_microservice-1.3.1rc1 → opengeodeweb_microservice-1.3.2rc1}/tests/test_database.py
RENAMED
|
@@ -1,19 +1,20 @@
|
|
|
1
|
+
import pytest
|
|
1
2
|
from sqlalchemy import select
|
|
2
3
|
|
|
3
|
-
from opengeodeweb_microservice.database.data import Data
|
|
4
4
|
from opengeodeweb_microservice.database.connection import get_session
|
|
5
|
+
from opengeodeweb_microservice.database.data import Data
|
|
5
6
|
|
|
6
7
|
GEODE_ID = "01a08187-2c4c-7e64-85c5-52c3439f0626"
|
|
7
8
|
|
|
8
9
|
|
|
9
|
-
|
|
10
|
+
@pytest.mark.usefixtures("clean_database")
|
|
11
|
+
def test_data_crud_operations() -> None:
|
|
10
12
|
data = Data.create(
|
|
11
13
|
geode_id=GEODE_ID,
|
|
12
14
|
geode_object="test_object",
|
|
13
15
|
viewer_object="test_viewer",
|
|
14
16
|
viewer_elements_type="test_type",
|
|
15
17
|
)
|
|
16
|
-
print("id", data.id, flush=True)
|
|
17
18
|
assert data.id is not None
|
|
18
19
|
assert isinstance(data.id, str)
|
|
19
20
|
assert len(data.id) == 32
|
|
@@ -28,7 +29,8 @@ def test_data_crud_operations(clean_database: None) -> None:
|
|
|
28
29
|
assert non_existent is None
|
|
29
30
|
|
|
30
31
|
|
|
31
|
-
|
|
32
|
+
@pytest.mark.usefixtures("clean_database")
|
|
33
|
+
def test_data_with_file_assignments() -> None:
|
|
32
34
|
data = Data.create(
|
|
33
35
|
geode_id=GEODE_ID,
|
|
34
36
|
geode_object="geode_object",
|
|
@@ -52,7 +54,8 @@ def test_data_with_file_assignments(clean_database: None) -> None:
|
|
|
52
54
|
assert retrieved.geode_object == "geode_object"
|
|
53
55
|
|
|
54
56
|
|
|
55
|
-
|
|
57
|
+
@pytest.mark.usefixtures("clean_database")
|
|
58
|
+
def test_data_geode_id_is_not_unique() -> None:
|
|
56
59
|
first = Data.create(
|
|
57
60
|
geode_id=GEODE_ID,
|
|
58
61
|
geode_object="geode_object",
|
{opengeodeweb_microservice-1.3.1rc1 → opengeodeweb_microservice-1.3.2rc1}/tests/test_schemas.py
RENAMED
|
@@ -1,9 +1,8 @@
|
|
|
1
1
|
import json
|
|
2
|
-
|
|
3
|
-
import fastjsonschema # type: ignore
|
|
4
|
-
|
|
5
2
|
from dataclasses import dataclass
|
|
6
3
|
|
|
4
|
+
import fastjsonschema
|
|
5
|
+
|
|
7
6
|
from opengeodeweb_microservice.schemas import (
|
|
8
7
|
ERROR_SCHEMA_PATH,
|
|
9
8
|
ErrorResponse,
|
|
@@ -12,13 +11,9 @@ from opengeodeweb_microservice.schemas import (
|
|
|
12
11
|
|
|
13
12
|
|
|
14
13
|
def test_error_response_matches_error_schema() -> None:
|
|
15
|
-
with open(
|
|
14
|
+
with ERROR_SCHEMA_PATH.open() as file:
|
|
16
15
|
validate = fastjsonschema.compile(json.load(file))
|
|
17
|
-
validate(
|
|
18
|
-
ErrorResponse(
|
|
19
|
-
code=500, name="Internal Server Error", description="boom"
|
|
20
|
-
).to_dict()
|
|
21
|
-
)
|
|
16
|
+
validate(ErrorResponse(code=500, name="Internal Server Error", description="boom").to_dict())
|
|
22
17
|
|
|
23
18
|
|
|
24
19
|
@dataclass
|
|
@@ -38,7 +33,5 @@ def test_format_dataclass_truncates_long_values() -> None:
|
|
|
38
33
|
_Outer(name="short", content="x" * 1000, inner=_Inner(values=list(range(500)))),
|
|
39
34
|
max_length=20,
|
|
40
35
|
)
|
|
41
|
-
assert text.startswith(
|
|
42
|
-
"_Outer(name='short', content='xxxxxxxxxxxxxxxxxxx... (str, len=1000)"
|
|
43
|
-
)
|
|
36
|
+
assert text.startswith("_Outer(name='short', content='xxxxxxxxxxxxxxxxxxx... (str, len=1000)")
|
|
44
37
|
assert "inner=_Inner(values=[0, 1, 2, 3, 4, 5, 6... (list, len=500))" in text
|
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
"""Database connection management"""
|
|
2
|
-
|
|
3
|
-
from sqlalchemy import create_engine
|
|
4
|
-
from sqlalchemy.orm import sessionmaker, scoped_session, Session
|
|
5
|
-
from .base import Base
|
|
6
|
-
|
|
7
|
-
DATABASE_FILENAME = "project.db"
|
|
8
|
-
|
|
9
|
-
engine = None
|
|
10
|
-
session_factory = None
|
|
11
|
-
scoped_session_registry = None
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
def init_database(db_path: str = DATABASE_FILENAME, create_tables: bool = True) -> None:
|
|
15
|
-
global engine, session_factory, scoped_session_registry
|
|
16
|
-
|
|
17
|
-
if engine is None:
|
|
18
|
-
engine = create_engine(
|
|
19
|
-
f"sqlite:///{db_path}",
|
|
20
|
-
connect_args={"check_same_thread": False},
|
|
21
|
-
)
|
|
22
|
-
print(f"Database engine created for {db_path}", flush=True)
|
|
23
|
-
session_factory = sessionmaker(bind=engine)
|
|
24
|
-
scoped_session_registry = scoped_session(session_factory)
|
|
25
|
-
if create_tables:
|
|
26
|
-
Base.metadata.create_all(engine)
|
|
27
|
-
print(f"Database tables created for {db_path}", flush=True)
|
|
28
|
-
else:
|
|
29
|
-
print(f"Database connected (tables not created) for {db_path}", flush=True)
|
|
30
|
-
else:
|
|
31
|
-
print(f"Database engine already exists for {db_path}, reusing", flush=True)
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
def get_session() -> Session:
|
|
35
|
-
if scoped_session_registry is None:
|
|
36
|
-
raise RuntimeError("Database not initialized. Call init_database() first.")
|
|
37
|
-
return scoped_session_registry()
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|