Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[NEAT-525] 🤗Engine interface #725

Merged
merged 4 commits into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions cognite/neat/_graph/extractors/__init__.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -48,6 +50,7 @@
| DMSExtractor
| ClassicGraphExtractor
| DataSetExtractor
| EngineExtractor
)


Expand Down
2 changes: 1 addition & 1 deletion cognite/neat/_session/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@
Change,
)

from ._engine import load_neat_engine
from ._inspect import InspectAPI
from ._prepare import PrepareAPI
from ._read import ReadAPI
from ._set import SetAPI
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


Expand Down
13 changes: 13 additions & 0 deletions cognite/neat/_session/_read.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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:
Expand Down
4 changes: 4 additions & 0 deletions cognite/neat/_session/engine/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from ._import import import_engine
from ._load import load_neat_engine

__all__ = ["load_neat_engine", "import_engine"]
7 changes: 7 additions & 0 deletions cognite/neat/_session/engine/_import.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from ._interface import NeatEngine


def import_engine() -> NeatEngine:
from neatengine import NeatEngine # type: ignore[import-not-found]

return NeatEngine()
25 changes: 25 additions & 0 deletions cognite/neat/_session/engine/_interface.py
Original file line number Diff line number Diff line change
@@ -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: ...
Loading