Skip to content

Commit

Permalink
Path for config (#296)
Browse files Browse the repository at this point in the history
[DOG-2636]
  • Loading branch information
cel055 authored Feb 15, 2024
1 parent 9810600 commit b2f9392
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ Changes are grouped as follows
- The queues now require to be set up with a max size. The max upload latencey is removed.
As long as you use the queue in as a context (ie, using `with FileUploadQueue(...) as queue:`) you should not have to change anything in your code. The behaviour of the queue will change, it will most likely be much faster, but it will not require any changes from you as a user of the queue.

### Added

* You can now use `Path` as a type in your config files

## [6.4.1]

### Changed
Expand Down
5 changes: 4 additions & 1 deletion cognite/extractorutils/configtools/loaders.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import sys
from enum import Enum
from hashlib import sha256
from pathlib import Path
from typing import Any, Callable, Dict, Generic, Iterable, Optional, TextIO, Type, TypeVar, Union

import dacite
Expand Down Expand Up @@ -188,7 +189,9 @@ def _load_yaml(

try:
config = dacite.from_dict(
data=config_dict, data_class=config_type, config=dacite.Config(strict=True, cast=[Enum, TimeIntervalConfig])
data=config_dict,
data_class=config_type,
config=dacite.Config(strict=True, cast=[Enum, TimeIntervalConfig, Path]),
)
except dacite.UnexpectedDataError as e:
unknowns = [f'"{k.replace("_", "-") if case_style == "hyphen" else k}"' for k in e.keys]
Expand Down
30 changes: 30 additions & 0 deletions tests/tests_unit/test_configtools.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import os
import unittest
from dataclasses import dataclass
from pathlib import Path

import yaml

Expand Down Expand Up @@ -44,6 +45,7 @@ class CastingClass:
string_field: str
another_string_field: str
yet_another_string_field: str
path_field: Path


@dataclass
Expand Down Expand Up @@ -233,6 +235,7 @@ def test_read_boolean_casting(self):
string-field: "true"
another-string-field: "test"
yet-another-string-field: ${STR_VAL}
path-field: "./file"
"""
config: CastingClass = load_yaml(config_raw, CastingClass)
self.assertTrue(config.boolean_field)
Expand All @@ -253,10 +256,37 @@ def test_read_invalid_boolean_casting(self):
string-field: "true"
another-string-field: "test"
yet-another-string-field: "test"
path-field: "./file"
"""
with self.assertRaises(InvalidConfigError):
load_yaml(config, CastingClass)

def test_read_relative_path(self):
config = """
boolean-field: true
another-boolean-field: false
yet-another-boolean-field: false
string-field: "true"
another-string-field: "test"
yet-another-string-field: "test"
path-field: "./folder/file.txt"
"""
config: CastingClass = load_yaml(config, CastingClass)
self.assertEqual(config.path_field.name, "file.txt")

def test_read_absolute_path(self):
config = """
boolean-field: true
another-boolean-field: false
yet-another-boolean-field: false
string-field: "true"
another-string-field: "test"
yet-another-string-field: "test"
path-field: "c:/folder/file.txt"
"""
config: CastingClass = load_yaml(config, CastingClass)
self.assertEqual(config.path_field.name, "file.txt")

def test_parse_time_interval(self):
self.assertEqual(TimeIntervalConfig("54").seconds, 54)
self.assertEqual(TimeIntervalConfig("54s").seconds, 54)
Expand Down

0 comments on commit b2f9392

Please sign in to comment.