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/_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/_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/__init__.py b/cognite/neat/_session/engine/__init__.py new file mode 100644 index 000000000..40219cef7 --- /dev/null +++ b/cognite/neat/_session/engine/__init__.py @@ -0,0 +1,4 @@ +from ._import import import_engine +from ._load import 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 new file mode 100644 index 000000000..226f0b65c --- /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() diff --git a/cognite/neat/_session/engine/_interface.py b/cognite/neat/_session/engine/_interface.py new file mode 100644 index 000000000..ac413d96b --- /dev/null +++ b/cognite/neat/_session/engine/_interface.py @@ -0,0 +1,25 @@ +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): + version: ClassVar[str] = "0.1.0" + + @property + def set(self) -> SetterAPI: ... + + def create_extractor(self) -> Extractor: ... 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