cognite-extractor-utils 7.1.0__py3-none-any.whl → 7.1.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-extractor-utils might be problematic. Click here for more details.

@@ -16,5 +16,5 @@
16
16
  Cognite extractor utils is a Python package that simplifies the development of new extractors.
17
17
  """
18
18
 
19
- __version__ = "7.1.0"
19
+ __version__ = "7.1.2"
20
20
  from .base import Extractor
@@ -152,17 +152,17 @@ class Extractor(Generic[CustomConfigClass]):
152
152
  def _reload_config(self) -> None:
153
153
  self.logger.info("Config file has changed")
154
154
 
155
- if self.reload_config_action == ReloadConfigAction.REPLACE_ATTRIBUTE:
155
+ if self.reload_config_action is ReloadConfigAction.REPLACE_ATTRIBUTE:
156
156
  self.logger.info("Loading in new config file")
157
157
  self.config_resolver.accept_new_config()
158
158
  self.config = self.config_resolver.config
159
159
  Extractor._config_singleton = self.config # type: ignore
160
160
 
161
- elif self.reload_config_action == ReloadConfigAction.SHUTDOWN:
161
+ elif self.reload_config_action is ReloadConfigAction.SHUTDOWN:
162
162
  self.logger.info("Shutting down, expecting to be restarted")
163
163
  self.cancellation_token.cancel()
164
164
 
165
- elif self.reload_config_action == ReloadConfigAction.CALLBACK:
165
+ elif self.reload_config_action is ReloadConfigAction.CALLBACK:
166
166
  self.logger.info("Loading in new config file")
167
167
  self.config_resolver.accept_new_config()
168
168
  self.config = self.config_resolver.config
@@ -372,7 +372,7 @@ class CogniteConfig:
372
372
 
373
373
  return cdf_client.data_sets.retrieve(
374
374
  id=self.data_set.either_id.internal_id,
375
- external_id=self.data_set.either_id.external_id, # type: ignore
375
+ external_id=self.data_set.either_id.external_id,
376
376
  )
377
377
 
378
378
  def get_extraction_pipeline(self, cdf_client: CogniteClient) -> Optional[ExtractionPipeline]:
@@ -381,8 +381,8 @@ class CogniteConfig:
381
381
 
382
382
  either_id = self.extraction_pipeline.either_id
383
383
  extraction_pipeline = cdf_client.extraction_pipelines.retrieve(
384
- id=either_id.internal_id, # type: ignore
385
- external_id=either_id.external_id, # type: ignore
384
+ id=either_id.internal_id,
385
+ external_id=either_id.external_id,
386
386
  )
387
387
  if extraction_pipeline is None:
388
388
  raise ValueError(f"Extraction pipeline with {either_id.type()} {either_id.content()} not found")
@@ -13,6 +13,7 @@
13
13
  # limitations under the License.
14
14
 
15
15
  import argparse
16
+ import dataclasses
16
17
  import json
17
18
  import logging
18
19
  import os
@@ -21,7 +22,7 @@ import sys
21
22
  from enum import Enum
22
23
  from hashlib import sha256
23
24
  from pathlib import Path
24
- from typing import Any, Callable, Dict, Generic, Iterable, Optional, TextIO, Type, TypeVar, Union
25
+ from typing import Any, Callable, Dict, Generic, Iterable, Optional, TextIO, Type, TypeVar, Union, cast
25
26
 
26
27
  import dacite
27
28
  import yaml
@@ -349,45 +350,43 @@ class ConfigResolver(Generic[CustomConfigClass]):
349
350
  return cls(args.config[0], config_type)
350
351
 
351
352
  def _inject_cognite(self, local_part: _BaseConfig, remote_part: Dict[str, Any]) -> Dict[str, Any]:
352
- if "cognite" not in remote_part:
353
- remote_part["cognite"] = {}
354
-
355
- remote_part["cognite"]["idp-authentication"] = {
356
- "client_id": local_part.cognite.idp_authentication.client_id,
357
- "scopes": local_part.cognite.idp_authentication.scopes,
358
- "secret": local_part.cognite.idp_authentication.secret,
359
- "tenant": local_part.cognite.idp_authentication.tenant,
360
- "token_url": local_part.cognite.idp_authentication.token_url,
361
- "resource": local_part.cognite.idp_authentication.resource,
362
- "authority": local_part.cognite.idp_authentication.authority,
363
- }
353
+ # We can not dump 'local_part.cognite' directly because e.g. 'data_set' may be set remote only...
354
+ remote_part.setdefault("cognite", {})
355
+ remote_part["cognite"]["idp_authentication"] = dataclasses.asdict(local_part.cognite.idp_authentication)
356
+ remote_part["cognite"]["extraction-pipeline"] = dataclasses.asdict(
357
+ local_part.cognite.extraction_pipeline # type: ignore [arg-type]
358
+ )
359
+
364
360
  if local_part.cognite.host is not None:
365
361
  remote_part["cognite"]["host"] = local_part.cognite.host
366
362
  remote_part["cognite"]["project"] = local_part.cognite.project
367
363
 
368
- # Ignoring None type, extraction pipelines is required at this point
369
- remote_part["cognite"]["extraction-pipeline"] = {}
370
- remote_part["cognite"]["extraction-pipeline"]["id"] = local_part.cognite.extraction_pipeline.id # type: ignore
371
- remote_part["cognite"]["extraction-pipeline"][
372
- "external_id"
373
- ] = local_part.cognite.extraction_pipeline.external_id # type: ignore
374
-
375
364
  return remote_part
376
365
 
366
+ def _use_cached_cognite_client(self, tmp_config: _BaseConfig) -> bool:
367
+ # Ideally we'd check tmp_config == self._config, but due to 'is_remote & _inject_...', this is not
368
+ # reliable to avoid new unneeded instantiations of CogniteClient:
369
+ return (
370
+ self.cognite_client is not None
371
+ and self._config is not None
372
+ and tmp_config.cognite.host == self._config.cognite.host
373
+ and tmp_config.cognite.project == self._config.cognite.project
374
+ and tmp_config.cognite.idp_authentication == self._config.cognite.idp_authentication
375
+ )
376
+
377
377
  def _resolve_config(self) -> None:
378
378
  self._reload_file()
379
379
 
380
380
  if self.is_remote:
381
381
  _logger.debug("Loading remote config file")
382
382
  tmp_config: _BaseConfig = load_yaml(self._config_text, _BaseConfig) # type: ignore
383
- if self.cognite_client is None or self._config is None or tmp_config.cognite != self._config.cognite:
384
- # Credentials towards CDF may have changed, instantiate (and store) a new client:
385
- client = tmp_config.cognite.get_cognite_client("config_resolver")
386
- self.cognite_client = client
387
- else:
383
+ if self._use_cached_cognite_client(tmp_config):
388
384
  # Use existing client to avoid invoking a token refresh, if possible. Reason: this is run every 5 min
389
385
  # by default ('ConfigReloader' thread) which for certain OAuth providers like Auth0, incurs a cost:
390
- client = self.cognite_client
386
+ client = cast(CogniteClient, self.cognite_client)
387
+ else:
388
+ # Credentials towards CDF may have changed, instantiate (and store) a new client:
389
+ client = self.cognite_client = tmp_config.cognite.get_cognite_client("config_resolver")
391
390
 
392
391
  response = client.extraction_pipelines.config.retrieve(
393
392
  tmp_config.cognite.get_extraction_pipeline(client).external_id # type: ignore # ignoring extpipe None
@@ -384,7 +384,7 @@ class CognitePusher(AbstractMetricsPusher):
384
384
  data_set_id = dataset.id
385
385
 
386
386
  for metric in REGISTRY.collect():
387
- if type(metric) == Metric and metric.type in ["gauge", "counter"]:
387
+ if type(metric) is Metric and metric.type in ["gauge", "counter"]:
388
388
  external_id = self.external_id_prefix + metric.name
389
389
 
390
390
  time_series.append(
@@ -393,8 +393,8 @@ class CognitePusher(AbstractMetricsPusher):
393
393
  name=metric.name,
394
394
  legacy_name=external_id,
395
395
  description=metric.documentation,
396
- asset_id=asset_id, # type: ignore # this is optional. Type hint in SDK is wrong
397
- data_set_id=data_set_id, # type: ignore # this is optional. Type hint in SDK is wrong
396
+ asset_id=asset_id,
397
+ data_set_id=data_set_id,
398
398
  )
399
399
  )
400
400
 
@@ -389,8 +389,7 @@ class RawStateStore(AbstractStateStore):
389
389
  if self._initialized and not force:
390
390
  return
391
391
 
392
- # ignore type since list _is_ optional, sdk types are wrong
393
- rows = self._cdf_client.raw.rows.list(db_name=self.database, table_name=self.table, limit=None) # type: ignore
392
+ rows = self._cdf_client.raw.rows.list(db_name=self.database, table_name=self.table, limit=None)
394
393
 
395
394
  with self.lock:
396
395
  self._local_state.clear()
@@ -522,8 +522,8 @@ class SequenceUploadQueue(AbstractUploadQueue):
522
522
 
523
523
  try:
524
524
  self.cdf_client.sequences.data.insert(
525
- id=either_id.internal_id, # type: ignore
526
- external_id=either_id.external_id, # type: ignore
525
+ id=either_id.internal_id,
526
+ external_id=either_id.external_id,
527
527
  rows=upload_this,
528
528
  column_external_ids=None,
529
529
  )
@@ -534,8 +534,8 @@ class SequenceUploadQueue(AbstractUploadQueue):
534
534
 
535
535
  # Retry
536
536
  self.cdf_client.sequences.data.insert(
537
- id=either_id.internal_id, # type: ignore
538
- external_id=either_id.external_id, # type: ignore
537
+ id=either_id.internal_id,
538
+ external_id=either_id.external_id,
539
539
  rows=upload_this,
540
540
  column_external_ids=None,
541
541
  )
@@ -553,7 +553,6 @@ class SequenceUploadQueue(AbstractUploadQueue):
553
553
  self._resolve_dataset_ids()
554
554
 
555
555
  for either_id, upload_this in self.upload_queue.items():
556
- _labels = str(either_id.content())
557
556
  _upload_single(either_id, upload_this)
558
557
  self.points_written.inc()
559
558
 
@@ -582,11 +581,11 @@ class SequenceUploadQueue(AbstractUploadQueue):
582
581
  try:
583
582
  seq = self.cdf_client.sequences.create(
584
583
  Sequence(
585
- id=either_id.internal_id, # type: ignore # these are optional, the SDK types are wrong
586
- external_id=either_id.external_id, # type: ignore
587
- name=self.sequence_names.get(either_id, None), # type: ignore
588
- description=self.sequence_descriptions.get(either_id, None), # type: ignore
589
- metadata=self.sequence_metadata.get(either_id, None), # type: ignore
584
+ id=either_id.internal_id,
585
+ external_id=either_id.external_id,
586
+ name=self.sequence_names.get(either_id, None),
587
+ description=self.sequence_descriptions.get(either_id, None),
588
+ metadata=self.sequence_metadata.get(either_id, None),
590
589
  asset_id=self.asset_ids.get(self.sequence_asset_external_ids.get(either_id, None), None), # type: ignore
591
590
  data_set_id=self.dataset_ids.get(self.sequence_dataset_external_ids.get(either_id, None), None), # type: ignore
592
591
  columns=column_def, # type: ignore # We already checked for None, mypy is wrong
@@ -595,9 +594,9 @@ class SequenceUploadQueue(AbstractUploadQueue):
595
594
 
596
595
  except CogniteDuplicatedError:
597
596
  self.logger.info("Sequnce already exist: {}".format(either_id))
598
- seq = self.cdf_client.sequences.retrieve(
599
- id=either_id.internal_id, # type: ignore # these are optional, the SDK types are wrong
600
- external_id=either_id.external_id, # type: ignore
597
+ seq = self.cdf_client.sequences.retrieve( # type: ignore [assignment]
598
+ id=either_id.internal_id,
599
+ external_id=either_id.external_id,
601
600
  )
602
601
 
603
602
  # Update definition of cached sequence
@@ -1,12 +1,12 @@
1
+ import sys
1
2
  from typing import Iterable, List, Optional, Union
2
3
 
3
4
  from cognite.client.data_classes import Event as _Event
4
5
  from cognite.client.data_classes import Row as _Row
5
6
 
6
- try:
7
- from typing import TypeAlias # type: ignore
8
- except ImportError:
9
- # Backport for python < 3.10
7
+ if sys.version_info >= (3, 10):
8
+ from typing import TypeAlias
9
+ else:
10
10
  from typing_extensions import TypeAlias
11
11
 
12
12
 
@@ -16,6 +16,7 @@
16
16
  The ``util`` package contains miscellaneous functions and classes that can some times be useful while developing
17
17
  extractors.
18
18
  """
19
+
19
20
  import logging
20
21
  import random
21
22
  from functools import partial, wraps
@@ -27,7 +28,7 @@ from decorator import decorator
27
28
 
28
29
  from cognite.client import CogniteClient
29
30
  from cognite.client.data_classes import Asset, ExtractionPipelineRun, TimeSeries
30
- from cognite.client.exceptions import CogniteAPIError, CogniteException, CogniteNotFoundError
31
+ from cognite.client.exceptions import CogniteAPIError, CogniteException, CogniteFileUploadError, CogniteNotFoundError
31
32
  from cognite.extractorutils.threading import CancellationToken
32
33
 
33
34
 
@@ -424,7 +425,7 @@ def requests_exceptions(
424
425
  """
425
426
  status_codes = status_codes or [408, 425, 429, 500, 502, 503, 504]
426
427
  # types ignored, since they are not installed as we don't depend on the package
427
- from requests.exceptions import HTTPError, RequestException # type: ignore
428
+ from requests.exceptions import HTTPError, RequestException
428
429
 
429
430
  def handle_http_errors(exception: RequestException) -> bool:
430
431
  if isinstance(exception, HTTPError):
@@ -458,7 +459,7 @@ def httpx_exceptions(
458
459
  """
459
460
  status_codes = status_codes or [408, 425, 429, 500, 502, 503, 504]
460
461
  # types ignored, since they are not installed as we don't depend on the package
461
- from httpx import HTTPError, HTTPStatusError # type: ignore
462
+ from httpx import HTTPError, HTTPStatusError
462
463
 
463
464
  def handle_http_errors(exception: HTTPError) -> bool:
464
465
  if isinstance(exception, HTTPStatusError):
@@ -492,7 +493,7 @@ def cognite_exceptions(
492
493
  status_codes = status_codes or [408, 425, 429, 500, 502, 503, 504]
493
494
 
494
495
  def handle_cognite_errors(exception: CogniteException) -> bool:
495
- if isinstance(exception, CogniteAPIError):
496
+ if isinstance(exception, (CogniteAPIError, CogniteFileUploadError)):
496
497
  return exception.code in status_codes
497
498
  return True
498
499
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: cognite-extractor-utils
3
- Version: 7.1.0
3
+ Version: 7.1.2
4
4
  Summary: Utilities for easier development of extractors for CDF
5
5
  Home-page: https://github.com/cognitedata/python-extractor-utils
6
6
  License: Apache-2.0
@@ -18,7 +18,7 @@ Provides-Extra: experimental
18
18
  Requires-Dist: arrow (>=1.0.0,<2.0.0)
19
19
  Requires-Dist: azure-identity (>=1.14.0,<2.0.0)
20
20
  Requires-Dist: azure-keyvault-secrets (>=4.7.0,<5.0.0)
21
- Requires-Dist: cognite-sdk (>=7.28.1,<8.0.0)
21
+ Requires-Dist: cognite-sdk (>=7.28.1,<7.35.0)
22
22
  Requires-Dist: dacite (>=1.6.0,<2.0.0)
23
23
  Requires-Dist: decorator (>=5.1.1,<6.0.0)
24
24
  Requires-Dist: more-itertools (>=10.0.0,<11.0.0)
@@ -1,14 +1,14 @@
1
- cognite/extractorutils/__init__.py,sha256=tzmeAmZqXbBOYIeP3jFuPUAC_ZDhZ2EYpsRFUawHyVA,739
1
+ cognite/extractorutils/__init__.py,sha256=Jr9_i-43717ZejlWzgVBNpwiLv8OXGDt6xZDnG1Svzo,739
2
2
  cognite/extractorutils/_inner_util.py,sha256=gmz6aqS7jDNsg8z4RHgJjMFohDLOMiaU4gMWBhg3xcE,1558
3
- cognite/extractorutils/base.py,sha256=o5oFzGBaszv7-HWMVG2ATmZwZfzi8enyrYIKwUagyCQ,16148
3
+ cognite/extractorutils/base.py,sha256=0t9HUANPLKSbAyfDtb0X7stwuarW1NkamRF3vyXyQzc,16148
4
4
  cognite/extractorutils/configtools/__init__.py,sha256=L-daaqInIsmHcjb2forJeY0fW8tz1mlteOUo7IsWnrU,3059
5
5
  cognite/extractorutils/configtools/_util.py,sha256=SZycZm_py9v9WZbDiDQbgS6_PiLtu-TtwuuH7tG2YCI,4739
6
- cognite/extractorutils/configtools/elements.py,sha256=G9-2J-tzao4z5PfOpWifJCMgP7X7ABHK257NtrWsweI,21382
7
- cognite/extractorutils/configtools/loaders.py,sha256=MvgFxgjExqLIS7z5DVziXq-O51wfcc13B53H0ptfvps,16372
6
+ cognite/extractorutils/configtools/elements.py,sha256=P9oXw07my3Q6UNwef9Ln6-LupChwaSrsF69BHuee9sY,21334
7
+ cognite/extractorutils/configtools/loaders.py,sha256=VmKNfGqwdHycwZB91i-BHarjW-2Mw_Shv31R6uozm88,16317
8
8
  cognite/extractorutils/exceptions.py,sha256=XiwyNPSN0YxFYaPw7tfA63B94PL48xDK3EfdGdhgQgc,1084
9
- cognite/extractorutils/metrics.py,sha256=IzYevOH19N_Nhi2XfhcuGgsGfmF7SexfcHcTQyZmH1o,15635
9
+ cognite/extractorutils/metrics.py,sha256=01ZMRbDisXPxrfCSyTSEkXMsslzmZwEqw18fuu9okdc,15509
10
10
  cognite/extractorutils/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
11
- cognite/extractorutils/statestore.py,sha256=98_UchuEw6k69aqVqFljjUtc10-MZ8-RLTHc-9Jdyf8,18733
11
+ cognite/extractorutils/statestore.py,sha256=CiE2E6zOCWCLXmEzxaoM9V_-laH3T7U4-ll_qE6rgRE,18645
12
12
  cognite/extractorutils/threading.py,sha256=2Hke5cFvP-wA45Crvh58JahoKXB64P3tr7R4y_BhBqM,3605
13
13
  cognite/extractorutils/uploader/__init__.py,sha256=W22u6QHA4cR0j78LN5LTL5YGbfC-uTApagTyP5ab7uQ,3110
14
14
  cognite/extractorutils/uploader/_base.py,sha256=-aFfoMSBGd9YUUMHL3ZQpLIuNMA7TNklWCEjPA18ER8,5282
@@ -17,11 +17,11 @@ cognite/extractorutils/uploader/assets.py,sha256=2E90N1kxsaA6Ah4h0_r_dTVhDYY_68I
17
17
  cognite/extractorutils/uploader/events.py,sha256=NZP2tMoU_rh_rb-EZiUBsOT5KdNABHN4c9Oddk0OsdE,5680
18
18
  cognite/extractorutils/uploader/files.py,sha256=-yskmzcS9FcAsT2wmu3G4pd9cHJeiNqxmrERoRC72Dg,18417
19
19
  cognite/extractorutils/uploader/raw.py,sha256=wFjF90PFTjmByOWx_Y4_YfDJ2w2jl0EQJ2Tjx2MP2PM,6738
20
- cognite/extractorutils/uploader/time_series.py,sha256=VptUq129MY0t8yw4rxeL0kOhz2dMibz4XdyvfhfYGj8,26840
20
+ cognite/extractorutils/uploader/time_series.py,sha256=7lLiIcNo1FS6e9zHI3WXbFuqyJFzICHdqIDFYSBVlRM,26548
21
21
  cognite/extractorutils/uploader_extractor.py,sha256=E-mpVvbPg_Tk90U4S9JybV0duptJ2SXE88HB6npE3zI,7732
22
- cognite/extractorutils/uploader_types.py,sha256=5MKT14DUnTFVD5Nx4Zvnfp2SfaICuTKLWAFAYaZishk,1045
23
- cognite/extractorutils/util.py,sha256=1Atq4-N2Pyi6GrxMa1PEfMCTqhPiLb2YoUXTzd60Roc,17179
24
- cognite_extractor_utils-7.1.0.dist-info/LICENSE,sha256=psuoW8kuDP96RQsdhzwOqi6fyWv0ct8CR6Jr7He_P_k,10173
25
- cognite_extractor_utils-7.1.0.dist-info/METADATA,sha256=uBij3gu7rMwr28ENpJ0TRoOshUDRujnfhj22DC-1us4,5446
26
- cognite_extractor_utils-7.1.0.dist-info/WHEEL,sha256=sP946D7jFCHeNz5Iq4fL4Lu-PrWrFsgfLXbbkciIZwg,88
27
- cognite_extractor_utils-7.1.0.dist-info/RECORD,,
22
+ cognite/extractorutils/uploader_types.py,sha256=wxfrsiKPTzG5lmoYtQsxt8Xyj-s5HnaLl8WDzJNrazg,1020
23
+ cognite/extractorutils/util.py,sha256=p7AGEgeIU0bNjuFJcFR3V5ZYr6QDj_ZC3zGxRJTf4yk,17198
24
+ cognite_extractor_utils-7.1.2.dist-info/LICENSE,sha256=psuoW8kuDP96RQsdhzwOqi6fyWv0ct8CR6Jr7He_P_k,10173
25
+ cognite_extractor_utils-7.1.2.dist-info/METADATA,sha256=qMnjci_XOaGrKVmI6cqMdWN2nwZjPvmR2qPljGE2pP0,5447
26
+ cognite_extractor_utils-7.1.2.dist-info/WHEEL,sha256=sP946D7jFCHeNz5Iq4fL4Lu-PrWrFsgfLXbbkciIZwg,88
27
+ cognite_extractor_utils-7.1.2.dist-info/RECORD,,