cognite-neat 0.105.0__py3-none-any.whl → 0.105.2__py3-none-any.whl

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.

Potentially problematic release.


This version of cognite-neat might be problematic. Click here for more details.

@@ -103,6 +103,7 @@ DEFAULT_DOCS_URL = "https://cognite-neat.readthedocs-hosted.com/en/latest/"
103
103
 
104
104
  DMS_CONTAINER_PROPERTY_SIZE_LIMIT = 100
105
105
  DMS_VIEW_CONTAINER_SIZE_LIMIT = 10
106
+ DMS_DIRECT_RELATION_LIST_LIMIT = 100
106
107
 
107
108
  _ASSET_ROOT_PROPERTY = {
108
109
  "connection": "direct",
@@ -1,10 +1,11 @@
1
1
  import itertools
2
2
  import json
3
+ import warnings
3
4
  from collections import defaultdict
4
5
  from collections.abc import Iterable, Sequence
5
6
  from graphlib import TopologicalSorter
6
7
  from pathlib import Path
7
- from typing import Any, get_args
8
+ from typing import Any, cast, get_args
8
9
 
9
10
  import yaml
10
11
  from cognite.client import CogniteClient
@@ -19,7 +20,7 @@ from pydantic import BaseModel, ValidationInfo, create_model, field_validator
19
20
  from rdflib import RDF, URIRef
20
21
 
21
22
  from cognite.neat._client import NeatClient
22
- from cognite.neat._constants import is_readonly_property
23
+ from cognite.neat._constants import DMS_DIRECT_RELATION_LIST_LIMIT, is_readonly_property
23
24
  from cognite.neat._graph._tracking import LogTracker, Tracker
24
25
  from cognite.neat._issues import IssueList, NeatIssue, NeatIssueList
25
26
  from cognite.neat._issues.errors import (
@@ -28,7 +29,7 @@ from cognite.neat._issues.errors import (
28
29
  ResourceDuplicatedError,
29
30
  ResourceRetrievalError,
30
31
  )
31
- from cognite.neat._issues.warnings import PropertyTypeNotSupportedWarning
32
+ from cognite.neat._issues.warnings import PropertyDirectRelationLimitWarning, PropertyTypeNotSupportedWarning
32
33
  from cognite.neat._rules.analysis._dms import DMSAnalysis
33
34
  from cognite.neat._rules.models import DMSRules
34
35
  from cognite.neat._rules.models.data_types import _DATA_TYPE_BY_DMS_TYPE, Json
@@ -373,7 +374,21 @@ class DMSLoader(CDFLoader[dm.InstanceApply]):
373
374
  def parse_direct_relation(cls, value: list, info: ValidationInfo) -> dict | list[dict]:
374
375
  # We validate above that we only get one value for single direct relations.
375
376
  if list.__name__ in _get_field_value_types(cls, info):
376
- return [{"space": self.instance_space, "externalId": remove_namespace_from_uri(v)} for v in value]
377
+ result = [{"space": self.instance_space, "externalId": remove_namespace_from_uri(v)} for v in value]
378
+ if len(result) <= DMS_DIRECT_RELATION_LIST_LIMIT:
379
+ return result
380
+ warnings.warn(
381
+ PropertyDirectRelationLimitWarning(
382
+ identifier="unknown",
383
+ resource_type="view property",
384
+ property_name=cast(str, cls.model_fields[info.field_name].alias or info.field_name),
385
+ limit=DMS_DIRECT_RELATION_LIST_LIMIT,
386
+ ),
387
+ stacklevel=2,
388
+ )
389
+ # To get deterministic results, we sort by space and externalId
390
+ result.sort(key=lambda x: (x["space"], x["externalId"]))
391
+ return result[:DMS_DIRECT_RELATION_LIST_LIMIT]
377
392
  elif value:
378
393
  return {"space": self.instance_space, "externalId": remove_namespace_from_uri(value[0])}
379
394
  return {}
@@ -1,4 +1,3 @@
1
- import textwrap
2
1
  import warnings
3
2
  from abc import ABC
4
3
  from collections.abc import Callable, Iterable
@@ -6,6 +5,7 @@ from functools import lru_cache
6
5
  from typing import cast
7
6
 
8
7
  from rdflib import RDF, Graph, Literal, Namespace, URIRef
8
+ from rdflib.query import ResultRow
9
9
 
10
10
  from cognite.neat._constants import CLASSIC_CDF_NAMESPACE, DEFAULT_NAMESPACE
11
11
  from cognite.neat._graph import extractors
@@ -14,158 +14,219 @@ from cognite.neat._utils.collection_ import iterate_progress_bar
14
14
  from cognite.neat._utils.rdf_ import (
15
15
  Triple,
16
16
  add_triples_in_batch,
17
+ get_namespace,
17
18
  remove_instance_ids_in_batch,
18
19
  remove_namespace_from_uri,
19
20
  )
20
21
 
21
- from ._base import BaseTransformer
22
+ from ._base import BaseTransformer, BaseTransformerStandardised, RowTransformationOutput
22
23
 
23
24
 
24
- # TODO: standardise
25
- class AddAssetDepth(BaseTransformer):
26
- description: str = "Adds depth of asset in the asset hierarchy to the graph"
25
+ class AddAssetDepth(BaseTransformerStandardised):
26
+ description: str = "Adds depth of asset in the asset hierarchy and optionally types asset based on depth"
27
27
  _use_only_once: bool = True
28
28
  _need_changes = frozenset({str(extractors.AssetsExtractor.__name__)})
29
29
 
30
- _parent_template: str = """SELECT ?child ?parent WHERE {{
31
- <{asset_id}> <{parent_prop}> ?child .
32
- OPTIONAL{{?child <{parent_prop}>+ ?parent .}}}}"""
33
-
34
- _root_template: str = """SELECT ?root WHERE {{
35
- <{asset_id}> <{root_prop}> ?root .}}"""
36
-
37
30
  def __init__(
38
31
  self,
39
32
  asset_type: URIRef | None = None,
40
- root_prop: URIRef | None = None,
41
33
  parent_prop: URIRef | None = None,
42
34
  depth_typing: dict[int, str] | None = None,
43
35
  ):
44
36
  self.asset_type = asset_type or DEFAULT_NAMESPACE.Asset
45
- self.root_prop = root_prop or DEFAULT_NAMESPACE.rootId
46
37
  self.parent_prop = parent_prop or DEFAULT_NAMESPACE.parentId
47
38
  self.depth_typing = depth_typing
48
39
 
49
- def transform(self, graph: Graph) -> None:
50
- """Adds depth of asset in the asset hierarchy to the graph."""
51
- for result in graph.query(f"SELECT DISTINCT ?asset_id WHERE {{?asset_id a <{self.asset_type}>}}"):
52
- asset_id = cast(tuple, result)[0]
53
- if depth := self.get_depth(graph, asset_id, self.root_prop, self.parent_prop):
54
- graph.add((asset_id, DEFAULT_NAMESPACE.depth, Literal(depth)))
55
-
56
- if self.depth_typing and (type_ := self.depth_typing.get(depth, None)):
57
- # remove existing type
58
- graph.remove((asset_id, RDF.type, None))
59
-
60
- # add new type
61
- graph.add((asset_id, RDF.type, DEFAULT_NAMESPACE[type_]))
62
-
63
- @classmethod
64
- def get_depth(
65
- cls,
66
- graph: Graph,
67
- asset_id: URIRef,
68
- root_prop: URIRef,
69
- parent_prop: URIRef,
70
- ) -> int | None:
71
- """Get asset depth in the asset hierarchy."""
72
-
73
- # Handles non-root assets
74
- if result := list(graph.query(cls._parent_template.format(asset_id=asset_id, parent_prop=parent_prop))):
75
- return len(cast(list[tuple], result)) + 2 if cast(list[tuple], result)[0][1] else 2
76
-
77
- # Handles root assets
78
- elif (
79
- (result := list(graph.query(cls._root_template.format(asset_id=asset_id, root_prop=root_prop))))
80
- and len(cast(list[tuple], result)) == 1
81
- and cast(list[tuple], result)[0][0] == asset_id
82
- ):
83
- return 1
84
- else:
85
- return None
40
+ def _iterate_query(self) -> str:
41
+ query = """SELECT ?asset (IF(?isRoot, 0, COUNT(?parent)) AS ?parentCount)
42
+ WHERE {{
43
+ ?asset a <{asset_type}> .
44
+ OPTIONAL {{ ?asset <{parent_prop}>+ ?parent . }}
45
+ BIND(IF(BOUND(?parent), false, true) AS ?isRoot)}}
46
+ GROUP BY ?asset ?isRoot
47
+ ORDER BY DESC(?parentCount)"""
48
+
49
+ return query.format(
50
+ asset_type=self.asset_type,
51
+ parent_prop=self.parent_prop,
52
+ )
86
53
 
54
+ def _count_query(self) -> str:
55
+ query = """SELECT (COUNT(?asset) as ?count)
56
+ WHERE {{ ?asset a <{asset_type}> . }}"""
87
57
 
88
- # TODO: standardise
89
- class BaseAssetConnector(BaseTransformer, ABC):
90
- _asset_type: URIRef = DEFAULT_NAMESPACE.Asset
91
- _item_type: URIRef
92
- _default_attribute: URIRef
93
- _connection_type: URIRef
58
+ return query.format(asset_type=self.asset_type)
94
59
 
95
- _select_item_ids = "SELECT DISTINCT ?item_id WHERE {{?item_id a <{item_type}>}}"
96
- _select_connected_assets: str = textwrap.dedent("""SELECT ?asset_id WHERE {{
97
- <{item_id}> <{attribute}> ?asset_id .
98
- ?asset_id a <{asset_type}>}}""")
60
+ def operation(self, query_result_row: ResultRow) -> RowTransformationOutput:
61
+ row_output = RowTransformationOutput()
62
+ subject, object = query_result_row
99
63
 
100
- def __init__(self, attribute: URIRef | None = None) -> None:
101
- self._attribute = attribute or self._default_attribute
64
+ row_output.add_triples.append(cast(Triple, (subject, DEFAULT_NAMESPACE.depth, object)))
102
65
 
103
- def transform(self, graph: Graph) -> None:
104
- for item_id, *_ in graph.query(self._select_item_ids.format(item_type=self._item_type)): # type: ignore[misc]
105
- triples: list[Triple] = []
106
- for asset_id, *_ in graph.query( # type: ignore[misc]
107
- self._select_connected_assets.format(
108
- item_id=item_id, attribute=self._attribute, asset_type=self._asset_type
109
- )
110
- ):
111
- triples.append((asset_id, self._connection_type, item_id)) # type: ignore[arg-type]
112
- add_triples_in_batch(graph, triples)
66
+ if self.depth_typing and (type_ := self.depth_typing.get(int(object), None)):
67
+ row_output.remove_triples.append(cast(Triple, (subject, RDF.type, self.asset_type)))
68
+ row_output.add_triples.append(cast(Triple, (subject, RDF.type, DEFAULT_NAMESPACE[type_])))
69
+
70
+ row_output.instances_modified_count += 1
71
+
72
+ return row_output
73
+
74
+
75
+ class BaseAssetConnector(BaseTransformerStandardised, ABC):
76
+ description: str = "Connects assets to other cognite resources, thus forming bi-directional connection"
77
+ _use_only_once: bool = True
78
+
79
+ def _count_query(self) -> str:
80
+ query = """SELECT (COUNT(?asset) as ?count)
81
+ WHERE {{
82
+ ?resource a <{resource_type}> .
83
+ ?resource <{connection}> ?asset .
84
+ ?asset a <{asset_type}> .
85
+ }}"""
86
+
87
+ return query.format(
88
+ asset_type=self.asset_type,
89
+ resource_type=self.resource_type,
90
+ connection=self.resource_to_asset_connection,
91
+ )
92
+
93
+ def _iterate_query(self) -> str:
94
+ query = """SELECT ?asset ?resource
95
+ WHERE {{
96
+ ?resource a <{resource_type}> .
97
+ ?resource <{connection}> ?asset .
98
+ ?asset a <{asset_type}> .
99
+ }}"""
100
+
101
+ return query.format(
102
+ asset_type=self.asset_type,
103
+ resource_type=self.resource_type,
104
+ connection=self.resource_to_asset_connection,
105
+ )
106
+
107
+ def __init__(
108
+ self,
109
+ resource_to_asset_connection: URIRef,
110
+ resource_type: URIRef,
111
+ asset_to_resource_connection: URIRef | None = None,
112
+ asset_type: URIRef | None = None,
113
+ ) -> None:
114
+ self.asset_type = asset_type or DEFAULT_NAMESPACE.Asset
115
+ self.resource_to_asset_connection = resource_to_asset_connection
116
+ self.resource_type = resource_type
117
+
118
+ if asset_to_resource_connection:
119
+ self.asset_to_resource_connection = asset_to_resource_connection
120
+ else:
121
+ namespace = Namespace(get_namespace(resource_type))
122
+ type_ = remove_namespace_from_uri(resource_type)
123
+ self.asset_to_resource_connection = namespace[type_[0].lower() + type_[1:]]
124
+
125
+ def operation(self, query_result_row: ResultRow) -> RowTransformationOutput:
126
+ row_output = RowTransformationOutput()
127
+ subject, object = query_result_row
128
+
129
+ row_output.add_triples.append(cast(Triple, (subject, self.asset_to_resource_connection, object)))
130
+
131
+ row_output.instances_modified_count += 1
132
+
133
+ return row_output
113
134
 
114
135
 
115
136
  class AssetTimeSeriesConnector(BaseAssetConnector):
116
137
  description: str = "Connects assets to timeseries, thus forming bi-directional connection"
117
- _use_only_once: bool = True
118
138
  _need_changes = frozenset(
119
139
  {
120
140
  str(extractors.AssetsExtractor.__name__),
121
141
  str(extractors.TimeSeriesExtractor.__name__),
122
142
  }
123
143
  )
124
- _item_type = DEFAULT_NAMESPACE.TimeSeries
125
- _default_attribute = DEFAULT_NAMESPACE.assetId
126
- _connection_type = DEFAULT_NAMESPACE.timeSeries
144
+
145
+ def __init__(
146
+ self,
147
+ resource_to_asset_connection: URIRef | None = None,
148
+ resource_type: URIRef | None = None,
149
+ asset_to_resource_connection: URIRef | None = None,
150
+ asset_type: URIRef | None = None,
151
+ ):
152
+ super().__init__(
153
+ resource_to_asset_connection=resource_to_asset_connection or DEFAULT_NAMESPACE.assetId,
154
+ resource_type=resource_type or DEFAULT_NAMESPACE.TimeSeries,
155
+ asset_to_resource_connection=asset_to_resource_connection or DEFAULT_NAMESPACE.timeSeries,
156
+ asset_type=asset_type or DEFAULT_NAMESPACE.Asset,
157
+ )
127
158
 
128
159
 
129
160
  class AssetSequenceConnector(BaseAssetConnector):
130
161
  description: str = "Connects assets to sequences, thus forming bi-directional connection"
131
- _use_only_once: bool = True
132
162
  _need_changes = frozenset(
133
163
  {
134
164
  str(extractors.AssetsExtractor.__name__),
135
165
  str(extractors.SequencesExtractor.__name__),
136
166
  }
137
167
  )
138
- _item_type = DEFAULT_NAMESPACE.Sequence
139
- _default_attribute = DEFAULT_NAMESPACE.assetId
140
- _connection_type = DEFAULT_NAMESPACE.sequence
168
+
169
+ def __init__(
170
+ self,
171
+ resource_to_asset_connection: URIRef | None = None,
172
+ resource_type: URIRef | None = None,
173
+ asset_to_resource_connection: URIRef | None = None,
174
+ asset_type: URIRef | None = None,
175
+ ):
176
+ super().__init__(
177
+ resource_to_asset_connection=resource_to_asset_connection or DEFAULT_NAMESPACE.assetId,
178
+ resource_type=resource_type or DEFAULT_NAMESPACE.Sequence,
179
+ asset_to_resource_connection=asset_to_resource_connection or DEFAULT_NAMESPACE.sequence,
180
+ asset_type=asset_type or DEFAULT_NAMESPACE.Asset,
181
+ )
141
182
 
142
183
 
143
184
  class AssetFileConnector(BaseAssetConnector):
144
185
  description: str = "Connects assets to files, thus forming bi-directional connection"
145
- _use_only_once: bool = True
146
186
  _need_changes = frozenset(
147
187
  {
148
188
  str(extractors.AssetsExtractor.__name__),
149
189
  str(extractors.FilesExtractor.__name__),
150
190
  }
151
191
  )
152
- _item_type = DEFAULT_NAMESPACE.File
153
- _default_attribute = DEFAULT_NAMESPACE.assetIds
154
- _connection_type = DEFAULT_NAMESPACE.file
192
+
193
+ def __init__(
194
+ self,
195
+ resource_to_asset_connection: URIRef | None = None,
196
+ resource_type: URIRef | None = None,
197
+ asset_to_resource_connection: URIRef | None = None,
198
+ asset_type: URIRef | None = None,
199
+ ):
200
+ super().__init__(
201
+ resource_to_asset_connection=resource_to_asset_connection or DEFAULT_NAMESPACE.assetIds,
202
+ resource_type=resource_type or DEFAULT_NAMESPACE.File,
203
+ asset_to_resource_connection=asset_to_resource_connection or DEFAULT_NAMESPACE.file,
204
+ asset_type=asset_type or DEFAULT_NAMESPACE.Asset,
205
+ )
155
206
 
156
207
 
157
208
  class AssetEventConnector(BaseAssetConnector):
158
209
  description: str = "Connects assets to events, thus forming bi-directional connection"
159
- _use_only_once: bool = True
160
210
  _need_changes = frozenset(
161
211
  {
162
212
  str(extractors.AssetsExtractor.__name__),
163
213
  str(extractors.EventsExtractor.__name__),
164
214
  }
165
215
  )
166
- _item_type = DEFAULT_NAMESPACE.Event
167
- _default_attribute = DEFAULT_NAMESPACE.assetIds
168
- _connection_type = DEFAULT_NAMESPACE.event
216
+
217
+ def __init__(
218
+ self,
219
+ resource_to_asset_connection: URIRef | None = None,
220
+ resource_type: URIRef | None = None,
221
+ asset_to_resource_connection: URIRef | None = None,
222
+ asset_type: URIRef | None = None,
223
+ ):
224
+ super().__init__(
225
+ resource_to_asset_connection=resource_to_asset_connection or DEFAULT_NAMESPACE.assetIds,
226
+ resource_type=resource_type or DEFAULT_NAMESPACE.Event,
227
+ asset_to_resource_connection=asset_to_resource_connection or DEFAULT_NAMESPACE.event,
228
+ asset_type=asset_type or DEFAULT_NAMESPACE.Asset,
229
+ )
169
230
 
170
231
 
171
232
  # TODO: standardise
@@ -1,15 +1,14 @@
1
1
  from typing import cast
2
2
  from urllib.parse import quote
3
3
 
4
- from rdflib import Graph, URIRef
4
+ from rdflib import Graph, Namespace, URIRef
5
5
  from rdflib.query import ResultRow
6
6
 
7
- from cognite.neat._constants import DEFAULT_NAMESPACE
8
7
  from cognite.neat._rules.analysis import InformationAnalysis
9
8
  from cognite.neat._rules.models._rdfpath import RDFPath, SingleProperty
10
9
  from cognite.neat._rules.models.information import InformationRules
11
10
  from cognite.neat._shared import Triple
12
- from cognite.neat._utils.rdf_ import remove_namespace_from_uri
11
+ from cognite.neat._utils.rdf_ import get_namespace, remove_namespace_from_uri
13
12
 
14
13
  from ._base import BaseTransformer, BaseTransformerStandardised, RowTransformationOutput
15
14
 
@@ -76,11 +75,11 @@ class MakeConnectionOnExactMatch(BaseTransformerStandardised):
76
75
  self.subject_predicate = subject_predicate
77
76
  self.object_type = object_type
78
77
  self.object_predicate = object_predicate
79
-
78
+ subject_namespace = Namespace(get_namespace(subject_type))
80
79
  self.connection = (
81
- DEFAULT_NAMESPACE[quote(connection.strip())]
80
+ subject_namespace[quote(connection.strip())]
82
81
  if isinstance(connection, str)
83
- else connection or DEFAULT_NAMESPACE[remove_namespace_from_uri(self.object_type).lower()]
82
+ else connection or subject_namespace[remove_namespace_from_uri(self.object_type).lower()]
84
83
  )
85
84
 
86
85
  self.limit = limit
@@ -88,10 +87,10 @@ class MakeConnectionOnExactMatch(BaseTransformerStandardised):
88
87
  def _iterate_query(self) -> str:
89
88
  query = """SELECT DISTINCT ?subject ?object
90
89
  WHERE {{
91
- ?subject a <{subject_type}> .
92
- ?subject <{subject_predicate}> ?value .
93
- ?object <{object_predicate}> ?value .
94
- ?object a <{object_type}> .
90
+ ?subject a <{subject_type}> ;
91
+ <{subject_predicate}> ?value .
92
+ ?object a <{object_type}> ;
93
+ <{object_predicate}> ?value .
95
94
  }}"""
96
95
 
97
96
  if self.limit and isinstance(self.limit, int) and self.limit > 0:
@@ -105,12 +104,12 @@ class MakeConnectionOnExactMatch(BaseTransformerStandardised):
105
104
  )
106
105
 
107
106
  def _count_query(self) -> str:
108
- query = """SELECT (COUNT(DISTINCT (?subject ?object)) as ?count)
107
+ query = """SELECT (COUNT(DISTINCT ?subject) as ?count)
109
108
  WHERE {{
110
- ?subject a <{subject_type}> .
111
- ?subject <{subject_predicate}> ?value .
112
- ?object <{object_predicate}> ?value .
113
- ?object a <{object_type}> .
109
+ ?subject a <{subject_type}> ;
110
+ <{subject_predicate}> ?value .
111
+ ?object a <{object_type}> ;
112
+ <{object_predicate}> ?value .
114
113
  }}"""
115
114
 
116
115
  if self.limit and isinstance(self.limit, int) and self.limit > 0:
@@ -28,6 +28,7 @@ from ._models import (
28
28
  from ._properties import (
29
29
  PropertyDataTypeConversionWarning,
30
30
  PropertyDefinitionDuplicatedWarning,
31
+ PropertyDirectRelationLimitWarning,
31
32
  PropertyNotFoundWarning,
32
33
  PropertyOverwritingWarning,
33
34
  PropertySkippedWarning,
@@ -64,6 +65,7 @@ __all__ = [
64
65
  "PrincipleSolutionBuildsOnEnterpriseWarning",
65
66
  "PropertyDataTypeConversionWarning",
66
67
  "PropertyDefinitionDuplicatedWarning",
68
+ "PropertyDirectRelationLimitWarning",
67
69
  "PropertyNotFoundWarning",
68
70
  "PropertyOverwritingWarning",
69
71
  "PropertySkippedWarning",
@@ -1,6 +1,7 @@
1
1
  from dataclasses import dataclass
2
2
  from typing import Generic
3
3
 
4
+ from cognite.neat._constants import DMS_DIRECT_RELATION_LIST_LIMIT
4
5
  from cognite.neat._issues._base import ResourceType
5
6
 
6
7
  from ._resources import ResourceNeatWarning, T_Identifier, T_ReferenceIdentifier
@@ -77,3 +78,13 @@ class PropertyDataTypeConversionWarning(PropertyWarning[T_Identifier]):
77
78
  """The {resource_type} with identifier {identifier} failed to convert the property {property_name}: {error}"""
78
79
 
79
80
  error: str
81
+
82
+
83
+ @dataclass(unsafe_hash=True)
84
+ class PropertyDirectRelationLimitWarning(PropertyWarning[T_Identifier]):
85
+ """The listable direct relation property {property_name} in the {resource_type} with identifier {identifier}
86
+ has more than {limit} relations. The relations will be sorted by (space, externalId) and truncated."""
87
+
88
+ resource_type = "view"
89
+
90
+ limit: int = DMS_DIRECT_RELATION_LIST_LIMIT
@@ -23,8 +23,9 @@ def get_cognite_client(env_file_name: str) -> CogniteClient:
23
23
  be prompted to enter them.
24
24
 
25
25
  Args:
26
- env_file_name: The name of the .env file to look for in the repository root. If the file is found, the variables
27
- will be loaded from the file. If the file is not found, the user will be prompted to enter the variables.
26
+ env_file_name: The name of the .env file to look for in the repository root / current working directory. If
27
+ the file is found, the variables will be loaded from the file. If the file is not found, the user will
28
+ be prompted to enter the variables and the file will be created.
28
29
 
29
30
  Returns:
30
31
  CogniteClient: A CogniteClient instance.
@@ -40,26 +41,44 @@ def get_cognite_client(env_file_name: str) -> CogniteClient:
40
41
  client = variables.get_client()
41
42
  print(f"Found {env_file_name} file in repository root. Loaded variables from {env_file_name} file.")
42
43
  return client
44
+ elif (Path.cwd() / env_file_name).exists():
45
+ with suppress(KeyError, FileNotFoundError, TypeError):
46
+ variables = _from_dotenv(Path.cwd() / env_file_name)
47
+ client = variables.get_client()
48
+ print(
49
+ f"Found {env_file_name} file in current working directory. Loaded variables from {env_file_name} file."
50
+ )
51
+ return client
43
52
  # Then try to load from environment variables
44
53
  with suppress(KeyError):
45
54
  variables = EnvironmentVariables.create_from_environ()
55
+ print("Loaded variables from environment variables.")
46
56
  return variables.get_client()
47
57
  # If not found, prompt the user
48
58
  variables = _prompt_user()
49
59
  if repo_root and _env_in_gitignore(repo_root, env_file_name):
50
- local_import("rich", "jupyter")
51
- from rich.prompt import Prompt
52
-
53
60
  env_file = repo_root / env_file_name
54
- answer = Prompt.ask(
55
- f"Do you store the variables in an {env_file_name} file in the repository root for easy reuse?",
56
- choices=["y", "n"],
57
- )
58
- if env_file.exists():
59
- answer = Prompt.ask(f"{env_file} already exists. Overwrite?", choices=["y", "n"])
60
- if answer == "y":
61
- env_file.write_text(variables.create_env_file())
62
- print(f"Created {env_file_name} file in repository root.")
61
+ location = "repository root"
62
+ elif repo_root:
63
+ # We do not offer to create the file in the repository root if it is in .gitignore
64
+ # as an inexperienced user might accidentally commit it.
65
+ print("Cannot create .env file in repository root as there is no .env entry in the .gitignore.")
66
+ return variables.get_client()
67
+ else:
68
+ env_file = Path.cwd() / env_file_name
69
+ location = "current working directory"
70
+ local_import("rich", "jupyter")
71
+ from rich.prompt import Prompt
72
+
73
+ answer = Prompt.ask(
74
+ f"Do you store the variables in an {env_file_name} file in the {location} for easy reuse?",
75
+ choices=["y", "n"],
76
+ )
77
+ if env_file.exists():
78
+ answer = Prompt.ask(f"{env_file} already exists. Overwrite?", choices=["y", "n"])
79
+ if answer == "y":
80
+ env_file.write_text(variables.create_env_file())
81
+ print(f"Created {env_file_name} file in {location}.")
63
82
 
64
83
  return variables.get_client()
65
84
 
@@ -320,7 +339,8 @@ def _prompt_cluster_and_project() -> EnvironmentVariables:
320
339
  def _repo_root() -> Path | None:
321
340
  with suppress(Exception):
322
341
  result = subprocess.run("git rev-parse --show-toplevel".split(), stdout=subprocess.PIPE)
323
- return Path(result.stdout.decode().strip())
342
+ if (output := result.stdout.decode().strip()) != "":
343
+ return Path(output)
324
344
  return None
325
345
 
326
346
 
cognite/neat/_version.py CHANGED
@@ -1,2 +1,2 @@
1
- __version__ = "0.105.0"
1
+ __version__ = "0.105.2"
2
2
  __engine__ = "^2.0.3"
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: cognite-neat
3
- Version: 0.105.0
3
+ Version: 0.105.2
4
4
  Summary: Knowledge graph transformation
5
5
  Home-page: https://cognite-neat.readthedocs-hosted.com/
6
6
  License: Apache-2.0
@@ -20,7 +20,7 @@ Provides-Extra: oxi
20
20
  Provides-Extra: service
21
21
  Requires-Dist: PyYAML
22
22
  Requires-Dist: backports.strenum (>=1.2,<2.0) ; python_version < "3.11"
23
- Requires-Dist: cognite-sdk (>=7.54.6,<8.0.0)
23
+ Requires-Dist: cognite-sdk (>=7.71.2,<8.0.0)
24
24
  Requires-Dist: elementpath (>=4.0.0,<5.0.0)
25
25
  Requires-Dist: exceptiongroup (>=1.1.3,<2.0.0) ; python_version < "3.11"
26
26
  Requires-Dist: fastapi (>=0,<1) ; extra == "service" or extra == "all"
@@ -10,7 +10,7 @@ cognite/neat/_client/data_classes/neat_sequence.py,sha256=QZWSfWnwk6KlYJvsInco4W
10
10
  cognite/neat/_client/data_classes/schema.py,sha256=uD8ExxEiIP3zhK4b--Q5fND-vmcC05a9WU5ItLsqG88,23179
11
11
  cognite/neat/_client/testing.py,sha256=c5ADJkRJFYGlJVQz-uPqxKExKXT297pxKh_ka4oGBjs,1082
12
12
  cognite/neat/_config.py,sha256=f9Py4SEHwYYquIg-k1rC7MbXBLENXQauoZtLyUbWvJQ,10118
13
- cognite/neat/_constants.py,sha256=XO5-wL6wjY722MQAmWfIW-sC0WT_PTWSFL_GpYSRFjQ,4962
13
+ cognite/neat/_constants.py,sha256=ei0ODx_P1Z4O-8tVK_K_7oRvEMzovVJ-0kw96fW8a_0,4999
14
14
  cognite/neat/_graph/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
15
15
  cognite/neat/_graph/_shared.py,sha256=g7XFITbVxdDyGZ6mlgFUv5cBycrU7QbPktRikdUVkks,863
16
16
  cognite/neat/_graph/_tracking/__init__.py,sha256=WOwsYieZtCW-iW15YkxUFrfKVVdLWdXHOGGStTwvE8A,91
@@ -40,17 +40,17 @@ cognite/neat/_graph/extractors/_mock_graph_generator.py,sha256=7WqyFu2Qj03pJD4au
40
40
  cognite/neat/_graph/extractors/_rdf_file.py,sha256=YgPZN4Ayk6UlbwFFjdWn4Yo3P74D8KeNUb3slXg6Ox8,1604
41
41
  cognite/neat/_graph/loaders/__init__.py,sha256=1eam_rG1BXTUJ8iDm8_IYZldEe177vn2GmHihDBi8qk,718
42
42
  cognite/neat/_graph/loaders/_base.py,sha256=Fp6uUkNfAM-SVgsLz7tyNJxJ1eeEw3h2d4Q0YyppR-Y,3991
43
- cognite/neat/_graph/loaders/_rdf2dms.py,sha256=tTEGh_d0WZ_sTbqSkKUkb8IEtamiNGY46B5lnq0pCwc,24349
43
+ cognite/neat/_graph/loaders/_rdf2dms.py,sha256=BskCCo_2P4JjPOyAc4His6tv_nEPH8u0qoQZyaSjVGQ,25247
44
44
  cognite/neat/_graph/queries/__init__.py,sha256=BgDd-037kvtWwAoGAy8eORVNMiZ5-E9sIV0txIpeaN4,50
45
45
  cognite/neat/_graph/queries/_base.py,sha256=Y3Amuave6xdQsDE5ZXCrYLUgOMIH8BXa4n5Pc9czAF0,14926
46
46
  cognite/neat/_graph/queries/_construct.py,sha256=CW8uHtXXACUXDj1AcEjROXtvoiuyx0CTgZ0bURY5Neo,7213
47
47
  cognite/neat/_graph/queries/_shared.py,sha256=uhw-nY4jJvivgtj1msdCRrfTWgauU7ybSHUqqUaFOUU,5390
48
48
  cognite/neat/_graph/transformers/__init__.py,sha256=sSrjK3_UDeesN8RzOjfNAYqyOEwjZJPUGO4uDuMLZ3s,1620
49
49
  cognite/neat/_graph/transformers/_base.py,sha256=omFmfRGaudojjq3GlW6b8PA2TNRcN3jXkw6Xvnx5r4M,4345
50
- cognite/neat/_graph/transformers/_classic_cdf.py,sha256=WoxjPNmuiLwIvbdVqeUX2HIjPoaBI4zWriR5Po1iXA0,19406
50
+ cognite/neat/_graph/transformers/_classic_cdf.py,sha256=rKgrhYpChzO4pfg08Nl-Yin4793N00DA6rSXdC3Tlh4,21742
51
51
  cognite/neat/_graph/transformers/_iodd.py,sha256=KNz1fPdKK40UaHgPMECzNZgSeU5PdPRyLdaRYdq1iug,866
52
52
  cognite/neat/_graph/transformers/_prune_graph.py,sha256=LFiAMYFteND5LGEv9KqYJr5C9-n7S5fR6IrEdtJyRnk,12447
53
- cognite/neat/_graph/transformers/_rdfpath.py,sha256=jaJwtvfY65X2aRTJ__MSWcv_i_CyvOrlYYLRMCx2Ji4,4993
53
+ cognite/neat/_graph/transformers/_rdfpath.py,sha256=WzydQvBzsnZ4BSv3HJWSH73e-sf1p-R4M2dm2H-DtEk,5019
54
54
  cognite/neat/_graph/transformers/_value_type.py,sha256=ZtH1KZmOGBGpUcqj4fBRlPOMq6kADt-HMShvYPS5Org,12841
55
55
  cognite/neat/_issues/__init__.py,sha256=OVgWivp_Br31p8zPeHjOEXs-Wj7lJU1pU1L3pfhfuqE,518
56
56
  cognite/neat/_issues/_base.py,sha256=vV0E8cfXKlOnRXFIDZKg5QPMruELEbCaUfQ01l5dR9A,20073
@@ -61,11 +61,11 @@ cognite/neat/_issues/errors/_properties.py,sha256=T_nquQeEQS3DQY--DQ13acxhGX_-gp
61
61
  cognite/neat/_issues/errors/_resources.py,sha256=YoajFF4Nxq_mhhVSZ7r3J6V-sH8cMMn576dSTsbcQZk,3964
62
62
  cognite/neat/_issues/errors/_workflow.py,sha256=m_Hlsvl5A1Oy7P3IROnz-4_do8_orZ1Pr1IHqsMyEys,971
63
63
  cognite/neat/_issues/formatters.py,sha256=ziNWT_YXwovTfU8Av5iYuSLgszzJYKTawM_z67VBdlU,3331
64
- cognite/neat/_issues/warnings/__init__.py,sha256=Qq5z9Bi_4Htn0bKeHQyxssfwHRSMv9nuQbJX1psWThI,2885
64
+ cognite/neat/_issues/warnings/__init__.py,sha256=6iIa8IFb7i1TtCS6Bp2ZiwPggQPWziKv9_jsb9v1m0U,2967
65
65
  cognite/neat/_issues/warnings/_external.py,sha256=3tE98nLzOx9pb-VMi0MmQskHj-IuEqwjjoqhKMJ-xIM,1325
66
66
  cognite/neat/_issues/warnings/_general.py,sha256=idZZZDbeSrDJJ1PomdmIO40QsZQNn_lWztWvMA-9q50,782
67
67
  cognite/neat/_issues/warnings/_models.py,sha256=i4ZXr1IINKbFiVhUd8-qAt9_cXB8D3W-ng1Ime_lQTA,4376
68
- cognite/neat/_issues/warnings/_properties.py,sha256=xo_kaXaMOozmhxBRHhVTPaYobFpHWvfiO9q0UYqnKS8,2678
68
+ cognite/neat/_issues/warnings/_properties.py,sha256=cC1mWcHm7NM2avheTh3eUZmNrnI-s5rqjGvg0-YR6NI,3146
69
69
  cognite/neat/_issues/warnings/_resources.py,sha256=jRhV7ROxuqcwah4rB3vTjBf_cZufRgnkDKZAflhlV3c,3556
70
70
  cognite/neat/_issues/warnings/user_modeling.py,sha256=_cN1wbaQUStZ13aG0VbN5UwKM9YdtyPfuSNJ1AAS6o8,3668
71
71
  cognite/neat/_rules/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
@@ -157,7 +157,7 @@ cognite/neat/_store/_provenance.py,sha256=g_u6O7jo3ZekQVtc-FfJR1fTGqD9L3ipwfSEjd
157
157
  cognite/neat/_store/_rules_store.py,sha256=mpx63LSceedrDiRDG66RW5zlsaCYzyYspDiAV_WwW5U,15335
158
158
  cognite/neat/_store/exceptions.py,sha256=1xLtWqX-TiGcXdgayBgeNx1cipoXkye7LmTMFdpMg1s,506
159
159
  cognite/neat/_utils/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
160
- cognite/neat/_utils/auth.py,sha256=Y35foeQHHbnAWhfDtgNqXGOoPqBk5gtrfhPWgoSa5XQ,13444
160
+ cognite/neat/_utils/auth.py,sha256=jOoPN3hKyVDalsc-AotzuuyZP9DwtYmKhYxGzbwAC_w,14412
161
161
  cognite/neat/_utils/auxiliary.py,sha256=WFOycFgoYipvDmtGvn6ZNH3H8iNZmHamrfe2kXRb8lM,6667
162
162
  cognite/neat/_utils/collection_.py,sha256=Q_LN1qypr0SeAV3dAR5KLD1szHNohSdYxyA8hr3n4T8,1433
163
163
  cognite/neat/_utils/graph_transformations_report.py,sha256=rjEy4XMvOygFL4UgnYOmFW6AHxaU9IXep-dmYc5654c,1230
@@ -170,10 +170,10 @@ cognite/neat/_utils/text.py,sha256=0IffvBIAmeGh92F4T6xiEdd-vv3B7FOGEMbfuTktO5Y,4
170
170
  cognite/neat/_utils/time_.py,sha256=O30LUiDH9TdOYz8_a9pFqTtJdg8vEjC3qHCk8xZblG8,345
171
171
  cognite/neat/_utils/upload.py,sha256=iWKmsQgw4EHLv-11NjYu7zAj5LtqTAfNa87a1kWeuaU,5727
172
172
  cognite/neat/_utils/xml_.py,sha256=FQkq84u35MUsnKcL6nTMJ9ajtG9D5i1u4VBnhGqP2DQ,1710
173
- cognite/neat/_version.py,sha256=k6808j5bC3aQEf0yqHcziCekV2_7uiJmkn-B2vMfJGs,46
173
+ cognite/neat/_version.py,sha256=ZdP8OHYaYjC3n9FoYFk-2lJvFDOhdAi2zKtn9SR_Jlg,46
174
174
  cognite/neat/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
175
- cognite_neat-0.105.0.dist-info/LICENSE,sha256=W8VmvFia4WHa3Gqxq1Ygrq85McUNqIGDVgtdvzT-XqA,11351
176
- cognite_neat-0.105.0.dist-info/METADATA,sha256=VocFhvXyWxYiP10dCC4xlD7SUyNq37jSH5BX6FqIPUA,5759
177
- cognite_neat-0.105.0.dist-info/WHEEL,sha256=RaoafKOydTQ7I_I3JTrPCg6kUmTgtm4BornzOqyEfJ8,88
178
- cognite_neat-0.105.0.dist-info/entry_points.txt,sha256=SsQlnl8SNMSSjE3acBI835JYFtsIinLSbVmHmMEXv6E,51
179
- cognite_neat-0.105.0.dist-info/RECORD,,
175
+ cognite_neat-0.105.2.dist-info/LICENSE,sha256=W8VmvFia4WHa3Gqxq1Ygrq85McUNqIGDVgtdvzT-XqA,11351
176
+ cognite_neat-0.105.2.dist-info/METADATA,sha256=R9RyL5gu5s7rtLz7uNaNREc0tzMCu0DyGIBL5AHjk5w,5759
177
+ cognite_neat-0.105.2.dist-info/WHEEL,sha256=RaoafKOydTQ7I_I3JTrPCg6kUmTgtm4BornzOqyEfJ8,88
178
+ cognite_neat-0.105.2.dist-info/entry_points.txt,sha256=SsQlnl8SNMSSjE3acBI835JYFtsIinLSbVmHmMEXv6E,51
179
+ cognite_neat-0.105.2.dist-info/RECORD,,