From cffdd99291ee2ca105225a9c08c65af3986dc051 Mon Sep 17 00:00:00 2001 From: anders-albert Date: Mon, 11 Nov 2024 15:35:59 +0100 Subject: [PATCH 1/4] refactor: moved out engine --- cognite/neat/_session/_base.py | 2 +- cognite/neat/_session/engine/__init__.py | 3 +++ cognite/neat/_session/{_engine.py => engine/_load.py} | 0 3 files changed, 4 insertions(+), 1 deletion(-) create mode 100644 cognite/neat/_session/engine/__init__.py rename cognite/neat/_session/{_engine.py => engine/_load.py} (100%) diff --git a/cognite/neat/_session/_base.py b/cognite/neat/_session/_base.py index 0863adf33..44c2ee79e 100644 --- a/cognite/neat/_session/_base.py +++ b/cognite/neat/_session/_base.py @@ -20,7 +20,6 @@ Change, ) -from ._engine import load_neat_engine from ._inspect import InspectAPI from ._prepare import PrepareAPI from ._read import ReadAPI @@ -28,6 +27,7 @@ from ._show import ShowAPI from ._state import SessionState from ._to import ToAPI +from .engine import load_neat_engine from .exceptions import NeatSessionError, intercept_session_exceptions diff --git a/cognite/neat/_session/engine/__init__.py b/cognite/neat/_session/engine/__init__.py new file mode 100644 index 000000000..239dde1a0 --- /dev/null +++ b/cognite/neat/_session/engine/__init__.py @@ -0,0 +1,3 @@ +from ._load import load_neat_engine + +__all__ = ["load_neat_engine"] diff --git a/cognite/neat/_session/_engine.py b/cognite/neat/_session/engine/_load.py similarity index 100% rename from cognite/neat/_session/_engine.py rename to cognite/neat/_session/engine/_load.py From ee401ea81a35cfe23f3f823243a2ca7c03133af8 Mon Sep 17 00:00:00 2001 From: anders-albert Date: Mon, 11 Nov 2024 15:41:29 +0100 Subject: [PATCH 2/4] refactor: setup shell --- cognite/neat/_session/engine/_interface.py | 7 +++++++ cognite/neat/_session/engine/import_.py | 7 +++++++ 2 files changed, 14 insertions(+) create mode 100644 cognite/neat/_session/engine/_interface.py create mode 100644 cognite/neat/_session/engine/import_.py diff --git a/cognite/neat/_session/engine/_interface.py b/cognite/neat/_session/engine/_interface.py new file mode 100644 index 000000000..c078267be --- /dev/null +++ b/cognite/neat/_session/engine/_interface.py @@ -0,0 +1,7 @@ +from typing import Any, Protocol + + +class NeatEngine(Protocol): + interface_version: str = "0.1.0" + + def read(self, source_file: Any) -> str: ... diff --git a/cognite/neat/_session/engine/import_.py b/cognite/neat/_session/engine/import_.py new file mode 100644 index 000000000..4528191d7 --- /dev/null +++ b/cognite/neat/_session/engine/import_.py @@ -0,0 +1,7 @@ +from ._interface import NeatEngine + + +def import_engine() -> NeatEngine: + from neatengine import NeatEngine # type: ignore[import-not-found] + + return NeatEngine From 27c583ae62b76033059f6190f4c6b8bafbbf55bd Mon Sep 17 00:00:00 2001 From: anders-albert Date: Mon, 11 Nov 2024 15:42:01 +0100 Subject: [PATCH 3/4] build: changelog --- cognite/neat/_session/engine/__init__.py | 3 ++- cognite/neat/_session/engine/{import_.py => _import.py} | 0 2 files changed, 2 insertions(+), 1 deletion(-) rename cognite/neat/_session/engine/{import_.py => _import.py} (100%) diff --git a/cognite/neat/_session/engine/__init__.py b/cognite/neat/_session/engine/__init__.py index 239dde1a0..40219cef7 100644 --- a/cognite/neat/_session/engine/__init__.py +++ b/cognite/neat/_session/engine/__init__.py @@ -1,3 +1,4 @@ +from ._import import import_engine from ._load import load_neat_engine -__all__ = ["load_neat_engine"] +__all__ = ["load_neat_engine", "import_engine"] diff --git a/cognite/neat/_session/engine/import_.py b/cognite/neat/_session/engine/_import.py similarity index 100% rename from cognite/neat/_session/engine/import_.py rename to cognite/neat/_session/engine/_import.py From 6179de02e3cb3e08579702db5db19fc02500462b Mon Sep 17 00:00:00 2001 From: anders-albert Date: Mon, 11 Nov 2024 16:30:11 +0100 Subject: [PATCH 4/4] refactor: added interface engine --- cognite/neat/_graph/extractors/__init__.py | 3 +++ cognite/neat/_session/_read.py | 13 ++++++++++++ cognite/neat/_session/engine/_import.py | 2 +- cognite/neat/_session/engine/_interface.py | 24 +++++++++++++++++++--- 4 files changed, 38 insertions(+), 4 deletions(-) diff --git a/cognite/neat/_graph/extractors/__init__.py b/cognite/neat/_graph/extractors/__init__.py index 06824cd7d..04ff37a54 100644 --- a/cognite/neat/_graph/extractors/__init__.py +++ b/cognite/neat/_graph/extractors/__init__.py @@ -1,3 +1,5 @@ +from cognite.neat._session.engine._interface import Extractor as EngineExtractor + from ._base import BaseExtractor from ._classic_cdf._assets import AssetsExtractor from ._classic_cdf._classic import ClassicGraphExtractor @@ -48,6 +50,7 @@ | DMSExtractor | ClassicGraphExtractor | DataSetExtractor + | EngineExtractor ) diff --git a/cognite/neat/_session/_read.py b/cognite/neat/_session/_read.py index 8c1acd0e6..ab40f30bf 100644 --- a/cognite/neat/_session/_read.py +++ b/cognite/neat/_session/_read.py @@ -17,6 +17,7 @@ from ._state import SessionState from ._wizard import NeatObjectType, RDFFileType, object_wizard, rdf_dm_wizard +from .engine import import_engine from .exceptions import NeatSessionError, intercept_session_exceptions @@ -140,6 +141,18 @@ def __call__(self, io: Any) -> IssueList: return input_rules.issues +@intercept_session_exceptions +class CSVReadAPI(BaseReadAPI): + def __call__(self, io: Any, type: str, primary_key: str) -> None: + engine = import_engine() + engine.set.file(io) + engine.set.type(type) + engine.set.primary_key(primary_key) + extractor = engine.create_extractor() + + self._state.instances.store.write(extractor) + + @intercept_session_exceptions class RDFReadAPI(BaseReadAPI): def __init__(self, state: SessionState, client: CogniteClient | None, verbose: bool) -> None: diff --git a/cognite/neat/_session/engine/_import.py b/cognite/neat/_session/engine/_import.py index 4528191d7..226f0b65c 100644 --- a/cognite/neat/_session/engine/_import.py +++ b/cognite/neat/_session/engine/_import.py @@ -4,4 +4,4 @@ def import_engine() -> NeatEngine: from neatengine import NeatEngine # type: ignore[import-not-found] - return NeatEngine + return NeatEngine() diff --git a/cognite/neat/_session/engine/_interface.py b/cognite/neat/_session/engine/_interface.py index c078267be..ac413d96b 100644 --- a/cognite/neat/_session/engine/_interface.py +++ b/cognite/neat/_session/engine/_interface.py @@ -1,7 +1,25 @@ -from typing import Any, Protocol +from collections.abc import Iterable +from typing import Any, ClassVar, Protocol + +from rdflib import Literal, URIRef + + +class Extractor(Protocol): + def extract(self) -> Iterable[tuple[URIRef, URIRef, Literal | URIRef]]: ... + + +class SetterAPI(Protocol): + def file(self, io: Any) -> None: ... + + def type(self, type: str) -> None: ... + + def primary_key(self, key: str) -> None: ... class NeatEngine(Protocol): - interface_version: str = "0.1.0" + version: ClassVar[str] = "0.1.0" + + @property + def set(self) -> SetterAPI: ... - def read(self, source_file: Any) -> str: ... + def create_extractor(self) -> Extractor: ...