settingsclass 0.1.4__tar.gz → 0.1.5__tar.gz
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.
- {settingsclass-0.1.4 → settingsclass-0.1.5}/PKG-INFO +3 -2
- {settingsclass-0.1.4 → settingsclass-0.1.5}/pyproject.toml +1 -1
- {settingsclass-0.1.4 → settingsclass-0.1.5}/src/settingsclass/loc/settings.csv +7 -3
- {settingsclass-0.1.4 → settingsclass-0.1.5}/src/settingsclass/settingsclass.py +206 -28
- {settingsclass-0.1.4 → settingsclass-0.1.5}/src/settingsclass.egg-info/PKG-INFO +3 -2
- {settingsclass-0.1.4 → settingsclass-0.1.5}/tests/input/settingsclass/config.ini/config.ini +3 -1
- {settingsclass-0.1.4 → settingsclass-0.1.5}/tests/input/settingsclass/config_bad_type.ini +3 -1
- {settingsclass-0.1.4 → settingsclass-0.1.5}/tests/input/settingsclass/config_confusing_types.ini +1 -1
- {settingsclass-0.1.4 → settingsclass-0.1.5}/tests/input/settingsclass/config_duplicate_param.ini +1 -1
- {settingsclass-0.1.4 → settingsclass-0.1.5}/tests/input/settingsclass/config_duplicate_section.ini +2 -2
- {settingsclass-0.1.4 → settingsclass-0.1.5}/tests/input/settingsclass/config_extra_parts.ini +1 -1
- {settingsclass-0.1.4 → settingsclass-0.1.5}/tests/input/settingsclass/config_generated_good.ini +3 -1
- {settingsclass-0.1.4 → settingsclass-0.1.5}/tests/input/settingsclass/config_generated_good_half_enc.ini +1 -1
- {settingsclass-0.1.4 → settingsclass-0.1.5}/tests/input/settingsclass/config_good_case_var.ini +4 -2
- {settingsclass-0.1.4 → settingsclass-0.1.5}/tests/input/settingsclass/config_modified.ini +3 -1
- settingsclass-0.1.5/tests/reports/coverage-badge.svg +1 -0
- {settingsclass-0.1.4 → settingsclass-0.1.5}/tests/test_settings.py +103 -29
- settingsclass-0.1.4/tests/reports/coverage-badge.svg +0 -1
- {settingsclass-0.1.4 → settingsclass-0.1.5}/.github/workflows/python-publish.yml +0 -0
- {settingsclass-0.1.4 → settingsclass-0.1.5}/.github/workflows/tests.yml +0 -0
- {settingsclass-0.1.4 → settingsclass-0.1.5}/.gitignore +0 -0
- {settingsclass-0.1.4 → settingsclass-0.1.5}/LICENSE +0 -0
- {settingsclass-0.1.4 → settingsclass-0.1.5}/README.md +0 -0
- {settingsclass-0.1.4 → settingsclass-0.1.5}/README_JA.md +0 -0
- {settingsclass-0.1.4 → settingsclass-0.1.5}/demo.py +0 -0
- {settingsclass-0.1.4 → settingsclass-0.1.5}/demo_ja.py +0 -0
- {settingsclass-0.1.4 → settingsclass-0.1.5}/requirements-dev.txt +0 -0
- {settingsclass-0.1.4 → settingsclass-0.1.5}/requirements.txt +0 -0
- {settingsclass-0.1.4 → settingsclass-0.1.5}/setup.cfg +0 -0
- {settingsclass-0.1.4 → settingsclass-0.1.5}/src/settingsclass/__init__.py +0 -0
- {settingsclass-0.1.4 → settingsclass-0.1.5}/src/settingsclass/localizer.py +0 -0
- {settingsclass-0.1.4 → settingsclass-0.1.5}/src/settingsclass.egg-info/SOURCES.txt +0 -0
- {settingsclass-0.1.4 → settingsclass-0.1.5}/src/settingsclass.egg-info/dependency_links.txt +0 -0
- {settingsclass-0.1.4 → settingsclass-0.1.5}/src/settingsclass.egg-info/requires.txt +0 -0
- {settingsclass-0.1.4 → settingsclass-0.1.5}/src/settingsclass.egg-info/top_level.txt +0 -0
- {settingsclass-0.1.4 → settingsclass-0.1.5}/tests/__init__.py +0 -0
- {settingsclass-0.1.4 → settingsclass-0.1.5}/tests/input/loc/test_strings_correct.csv +0 -0
- {settingsclass-0.1.4 → settingsclass-0.1.5}/tests/input/loc/test_strings_duplicate_key.csv +0 -0
- {settingsclass-0.1.4 → settingsclass-0.1.5}/tests/input/loc/test_strings_error_few.csv +0 -0
- {settingsclass-0.1.4 → settingsclass-0.1.5}/tests/input/loc/test_strings_error_many.csv +0 -0
- {settingsclass-0.1.4 → settingsclass-0.1.5}/tests/input/loc/test_strings_error_nostr.csv +0 -0
- {settingsclass-0.1.4 → settingsclass-0.1.5}/tests/input/loc/test_strings_half_specified.csv +0 -0
- {settingsclass-0.1.4 → settingsclass-0.1.5}/tests/input/loc/test_strings_warning.csv +0 -0
- {settingsclass-0.1.4 → settingsclass-0.1.5}/tests/input/settingsclass/config_missing_section.ini +0 -0
- {settingsclass-0.1.4 → settingsclass-0.1.5}/tests/test_localization.py +0 -0
- {settingsclass-0.1.4 → settingsclass-0.1.5}/tests/test_localization_fail_cases.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
Metadata-Version: 2.
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
2
|
Name: settingsclass
|
|
3
|
-
Version: 0.1.
|
|
3
|
+
Version: 0.1.5
|
|
4
4
|
Summary: A class decorator for easily storing setting values, with automatic sync to disk
|
|
5
5
|
Author: Robert Kajnak
|
|
6
6
|
License: MIT Licence
|
|
@@ -12,6 +12,7 @@ Description-Content-Type: text/markdown
|
|
|
12
12
|
License-File: LICENSE
|
|
13
13
|
Requires-Dist: loguru
|
|
14
14
|
Requires-Dist: pycryptodome>=3.20.0
|
|
15
|
+
Dynamic: license-file
|
|
15
16
|
|
|
16
17
|
[](https://github.com/id-ai-labo/settingsclass/actions/workflows/tests.yml)
|
|
17
18
|
[](./tests/reports/www/index.html)
|
|
@@ -9,7 +9,7 @@ authors = [
|
|
|
9
9
|
]
|
|
10
10
|
description = "A class decorator for easily storing setting values, with automatic sync to disk"
|
|
11
11
|
keywords = ["settings", "config", "configuration", "ini", "configparser", "dataclass"]
|
|
12
|
-
version = "0.1.
|
|
12
|
+
version = "0.1.5"
|
|
13
13
|
license = {text = "MIT Licence"}
|
|
14
14
|
readme = "README.md"
|
|
15
15
|
requires-python = ">=3.11"
|
|
@@ -9,20 +9,24 @@ unencrypted_data_found_1,"暗号化されていないデータ({})が見つ
|
|
|
9
9
|
#エラーメッセージ/error messages
|
|
10
10
|
|
|
11
11
|
file_is_folder_1,"指定されたパスはファイルを指すことが期待されていましたが、フォルダを指しています。:{}。","The path was expected to point to a file, but points to a folder. Path: {}"
|
|
12
|
-
|
|
12
|
+
invalid_type_5,"【{0}】セクションのパラメータ【{1}】に対して、【{3}】をタイプ{2}に変換できませんでした。デフォルト値の【{4}】に設定します。","Could not convert '{3}' to type {2} for parameter <{1}> in section <{0}>. Setting to the default value of {4}"
|
|
13
13
|
missing_config_section_1,"セクション【{}】が見つかりませんでした。","The config file is missing section <{}>"
|
|
14
14
|
config_param_missing_2,"セクションのパラメータ【{0}】に【{1}】が見つかりませんでした。","Config file is missing parameter <{1}> in section <{0}>"
|
|
15
15
|
extra_config_sections_1,"設定ファイルに予期しないセクションは使用できません。:{}","The following sections are not in the definition and will be ignored: {}"
|
|
16
16
|
extra_config_parameter_2,"設定ファイルの【{0}】セクションに予期されていない【{1}】のパラメーターが見つかりました。","Unexpected parameter {1} found in section {0} of the settings file"
|
|
17
17
|
param_type_is_string_but_looks_x_4,"設定ファイルの【{}】セクション【{}】の【{}】値を設定されているパラメーターは文字列ですが、{}に見えます。","The parameter <{}> in section <{}> with value {} of the settings file is a string but looks like a {}"
|
|
18
18
|
cannot_convert_to_bool_1,"【{}】の値をbooleanに変換できませんでした。","Could not convert the value of <{}> to a boolean."
|
|
19
|
-
|
|
19
|
+
dictionary_not_supported_2,"辞書型はサポート対象外ため、初期化できませんでした【{}】({})","Dictionaries are currently not supported. Please use a different type for <{}> ({})"
|
|
20
|
+
unexpected_class_found_2,"予期されない型が見つかりました。:{}{}。GenericAliasを継承するクラスは_RandomTypeまたは_TypeWrapperも継承する必要があります。ほかの可変タイプ(例:set)は対象外です","Unexpected type found in: {}/{}. Classes inheriting GenericAlias should also inherit _RandomType or _TypeWrapper. Other parametrized types such as set are not supported"
|
|
20
21
|
invalid_encryption_key_type_1,"暗号化キーの型が不正です:{}。使用可能なタイプは【None】・【str】・【tuple[Callable]】。Tupleの場合は【暗号化関数、複合化関数】の順番が必用","{} has been provided but only types str, None and tuple[Callable] can be used as encryption keys. In case of tuple, the first callable should handle encryption, with the second handling decrpytion"
|
|
21
22
|
could_not_decode_string_1,"ファイルのデコードに失敗しました。ファイルが壊れている可能性があります:{}","Failed to decode the file. The file may be corrupted: {}"
|
|
22
23
|
function_can_only_be_called_on_randomtype_1,"この関数は_RandomTypeのみで呼び出すことができます:{}","This function can only be called on _RandomType: {}"
|
|
23
24
|
keyfile_location_unaccessible_1,"キーファイルの場所にアクセスできません。代わりに文字列または呼び出し可能なキーで指定してください:{}","The keyfile location is unaccessable. Please provide a string or callable key instead: {}"
|
|
24
25
|
no_initial_value_3,"クラス{}.{}の<{}>の値は見つかりませんでした。ランダムの変数の場合でもプレースホルダーの値は必用です。","Missing value for variable <{2}> inside {0}.{1}. All keys need to be initialized, even random properties require a placeholder."
|
|
25
|
-
parameter_type_missing_1,"{}
|
|
26
|
+
parameter_type_missing_1,"{} のパラメータの型ヒントが見つかりませんでした。明示的な型が指定されていないパラメータは、その値から型が推測されますが、これは部分的に失敗しました。値は引き続き推測されますが、文字列の変換ではこのメンバーは表示されません。","Parameter type hint for {} was not found. Parameters without explicit type are inferred from their values, but this has partially failed. The value will still be inferred, but string conversion will not show this member"
|
|
27
|
+
json_decode_failed_3,"パラメータ【{}】の【{}】型へのでコードは失敗しました。デフォルト値の【{}】に設定します。","Failed to decode parameter <{}> into type <{}> defaulting to <{}>"
|
|
28
|
+
iterable_class_annotation_too_many_1,"【{}】のアノテーションの数が足りません","Annotation <{}> has too many type annotations, should be exactly 1"
|
|
29
|
+
iterable_class_annotation_too_few_1,【{}】のアノテーションの数が多すぎます,"Annotation <{}> has too few type annotations, should be exactly 1"
|
|
26
30
|
|
|
27
31
|
#UT
|
|
28
32
|
##設定モジュール/Settings module
|
|
@@ -8,7 +8,7 @@ Created on Thu May 11 16:54:34 2023
|
|
|
8
8
|
# %% Imports
|
|
9
9
|
# from __future__ import annotations # do NOT use! Makes strings become un-callable
|
|
10
10
|
|
|
11
|
-
from dataclasses import dataclass
|
|
11
|
+
from dataclasses import dataclass, field, Field
|
|
12
12
|
from Crypto.Cipher import AES
|
|
13
13
|
from Crypto.Util.Padding import pad, unpad
|
|
14
14
|
import hashlib
|
|
@@ -20,6 +20,7 @@ import secrets
|
|
|
20
20
|
from types import GenericAlias, MethodType, FunctionType
|
|
21
21
|
from typing import Any, Callable, TypeVar
|
|
22
22
|
import os
|
|
23
|
+
import json
|
|
23
24
|
from threading import Lock
|
|
24
25
|
|
|
25
26
|
from loguru import logger
|
|
@@ -153,6 +154,12 @@ def _is_encoded(message: str):
|
|
|
153
154
|
return len(message) > len(ENC_PREFIX) and message[: len(ENC_PREFIX)] == ENC_PREFIX
|
|
154
155
|
|
|
155
156
|
|
|
157
|
+
def _is_list_type(typ: type):
|
|
158
|
+
return (
|
|
159
|
+
isinstance(typ, GenericAlias) and typ.__origin__ in (list, tuple)
|
|
160
|
+
) or typ in (list, tuple)
|
|
161
|
+
|
|
162
|
+
|
|
156
163
|
class _TypeWrapper:
|
|
157
164
|
def __class_getitem__(cls, item):
|
|
158
165
|
# class Secret[Generic]: # req. python>=3.12
|
|
@@ -238,13 +245,51 @@ class RandomFloat(float, _RandomType):
|
|
|
238
245
|
# note: use lowercase for subclasses, they will be shadowed by instance values
|
|
239
246
|
|
|
240
247
|
|
|
248
|
+
def _ensure_correct_list_type(
|
|
249
|
+
config_param_value: Any,
|
|
250
|
+
expected_type: type[list] | type[tuple],
|
|
251
|
+
cast_function: Callable[[Any], ClassType] = lambda x: x,
|
|
252
|
+
) -> list[ClassType] | tuple[ClassType]:
|
|
253
|
+
"""リスト等型の全ての要素を正しい型に変換する
|
|
254
|
+
|
|
255
|
+
Args:
|
|
256
|
+
config_param_value (Any): リスト等
|
|
257
|
+
expected_type (ClassType): 要素の型
|
|
258
|
+
cast_function (Callable[[Any], ClassType], optional): 変換関数. Defaults to lambda x:x.
|
|
259
|
+
|
|
260
|
+
Returns:
|
|
261
|
+
_type_: _description_
|
|
262
|
+
"""
|
|
263
|
+
if isinstance(config_param_value, (list, tuple)):
|
|
264
|
+
# Checking the types separately, then checking if each element in the iterable
|
|
265
|
+
# is the correct type may be faster, but more error prone. Considering this is a
|
|
266
|
+
# settings file, not a database with thousands of entries, the overhead should be
|
|
267
|
+
# negligible
|
|
268
|
+
if expected_type is tuple:
|
|
269
|
+
param_value_after_cast = tuple(cast_function(x) for x in config_param_value)
|
|
270
|
+
else:
|
|
271
|
+
param_value_after_cast = [cast_function(x) for x in config_param_value]
|
|
272
|
+
else:
|
|
273
|
+
param_value_after_cast = (
|
|
274
|
+
[cast_function(config_param_value)]
|
|
275
|
+
if expected_type is list
|
|
276
|
+
else (cast_function(config_param_value),)
|
|
277
|
+
)
|
|
278
|
+
|
|
279
|
+
return param_value_after_cast
|
|
280
|
+
|
|
281
|
+
|
|
241
282
|
def _auto_cast_type(
|
|
242
|
-
expected_type: type, config_param_value:
|
|
283
|
+
expected_type: type, config_param_value: Any, *, force_random=False
|
|
243
284
|
):
|
|
244
285
|
"""設定クラスの型ヒントにに合わせる"""
|
|
245
286
|
|
|
246
287
|
# ここのラッパーは全てGenericAliasも継承している
|
|
247
|
-
if
|
|
288
|
+
if expected_type in (list, tuple):
|
|
289
|
+
param_value_after_cast = _ensure_correct_list_type(
|
|
290
|
+
config_param_value, expected_type
|
|
291
|
+
)
|
|
292
|
+
elif isinstance(expected_type, GenericAlias):
|
|
248
293
|
# Randomの場合は、そのタイプのクラスにコンストラクタをあげる
|
|
249
294
|
if issubclass(expected_type.__origin__, _RandomType):
|
|
250
295
|
if config_param_value == "" or force_random:
|
|
@@ -258,7 +303,30 @@ def _auto_cast_type(
|
|
|
258
303
|
param_value_after_cast = _auto_cast_type(
|
|
259
304
|
expected_type.__args__[0], config_param_value, force_random=force_random
|
|
260
305
|
)
|
|
261
|
-
|
|
306
|
+
elif issubclass(expected_type.__origin__, (list, tuple)):
|
|
307
|
+
element_types = expected_type.__args__
|
|
308
|
+
if len(element_types) == 1:
|
|
309
|
+
param_value_after_cast = _ensure_correct_list_type(
|
|
310
|
+
config_param_value, expected_type.__origin__, element_types[0]
|
|
311
|
+
)
|
|
312
|
+
elif len(element_types) == 0:
|
|
313
|
+
raise ValueError(
|
|
314
|
+
tr(
|
|
315
|
+
"iterable_class_annotation_too_few_1",
|
|
316
|
+
expected_type,
|
|
317
|
+
)
|
|
318
|
+
)
|
|
319
|
+
else:
|
|
320
|
+
raise ValueError(
|
|
321
|
+
tr(
|
|
322
|
+
"iterable_class_annotation_too_many_1",
|
|
323
|
+
expected_type,
|
|
324
|
+
)
|
|
325
|
+
)
|
|
326
|
+
elif issubclass(expected_type.__origin__, dict):
|
|
327
|
+
raise ValueError(
|
|
328
|
+
tr("dictionary_not_supported_2", config_param_value, expected_type)
|
|
329
|
+
)
|
|
262
330
|
else:
|
|
263
331
|
raise ValueError(
|
|
264
332
|
tr("unexpected_class_found_2", expected_type, config_param_value)
|
|
@@ -267,7 +335,9 @@ def _auto_cast_type(
|
|
|
267
335
|
# コンフィグファイルに値が入っていない場合は型のヒントで初期化する
|
|
268
336
|
elif config_param_value == "":
|
|
269
337
|
param_value_after_cast = expected_type()
|
|
270
|
-
|
|
338
|
+
elif expected_type is Field:
|
|
339
|
+
# This only happens if the type is inferred, therefore casting here would be circular
|
|
340
|
+
param_value_after_cast = config_param_value
|
|
271
341
|
else:
|
|
272
342
|
# boolのサブクラスは定期不可能
|
|
273
343
|
if expected_type is bool and not isinstance(config_param_value, bool):
|
|
@@ -290,7 +360,7 @@ def user_friendly_type_name(typ):
|
|
|
290
360
|
friendly_type = typ
|
|
291
361
|
if issubclass(typ, _TypeWrapper):
|
|
292
362
|
friendly_type = typ.__args__[0]
|
|
293
|
-
elif issubclass(typ.__origin__, _RandomType):
|
|
363
|
+
elif hasattr(typ, "__origin__") and issubclass(typ.__origin__, _RandomType):
|
|
294
364
|
friendly_type = typ.__base__
|
|
295
365
|
|
|
296
366
|
return friendly_type
|
|
@@ -298,7 +368,7 @@ def user_friendly_type_name(typ):
|
|
|
298
368
|
|
|
299
369
|
def _default_value_for_type(typ, default_value_for_primitive):
|
|
300
370
|
"""Returns a default value for simple types, but generates one for random types"""
|
|
301
|
-
if issubclass(typ.__origin__, _RandomType):
|
|
371
|
+
if hasattr(typ, "__origin__") and issubclass(typ.__origin__, _RandomType):
|
|
302
372
|
return _init_randomclass(typ)
|
|
303
373
|
return default_value_for_primitive
|
|
304
374
|
|
|
@@ -371,7 +441,7 @@ def __post_init__(self):
|
|
|
371
441
|
def _class_name_without_path(typ) -> str:
|
|
372
442
|
if issubclass(typ, _RandomType):
|
|
373
443
|
return f"{typ.__name__}[{','.join([str(a) for a in typ.__args__])}]"
|
|
374
|
-
elif issubclass(typ, _TypeWrapper):
|
|
444
|
+
elif issubclass(typ, _TypeWrapper) or isinstance(typ, GenericAlias):
|
|
375
445
|
return f"{typ.__name__}[{_class_name_without_path(typ.__args__[0])}]"
|
|
376
446
|
|
|
377
447
|
return f"{typ.__name__}"
|
|
@@ -397,7 +467,7 @@ def __repr__(self) -> str:
|
|
|
397
467
|
subclass_contents = __subrepr__(subclass_instance)
|
|
398
468
|
concated_str += f"{subclass_name}: {subclass_contents}\n "
|
|
399
469
|
# TODO finish after shadowing with instances
|
|
400
|
-
return concated_str
|
|
470
|
+
return concated_str.rstrip(" ")
|
|
401
471
|
|
|
402
472
|
|
|
403
473
|
def _encrypt_field(
|
|
@@ -448,6 +518,59 @@ def _safe_decrypt_field(
|
|
|
448
518
|
return message
|
|
449
519
|
|
|
450
520
|
|
|
521
|
+
def _unpack_json(config_param_value, expected_type, default_parameter_value):
|
|
522
|
+
try:
|
|
523
|
+
return json.loads(config_param_value)
|
|
524
|
+
except json.JSONDecodeError:
|
|
525
|
+
new_default_value = (
|
|
526
|
+
default_parameter_value.default_factory()
|
|
527
|
+
if isinstance(default_parameter_value, Field)
|
|
528
|
+
else default_parameter_value
|
|
529
|
+
)
|
|
530
|
+
logger.warning(
|
|
531
|
+
tr(
|
|
532
|
+
"json_decode_failed_3",
|
|
533
|
+
config_param_value,
|
|
534
|
+
expected_type,
|
|
535
|
+
new_default_value,
|
|
536
|
+
)
|
|
537
|
+
)
|
|
538
|
+
return new_default_value
|
|
539
|
+
|
|
540
|
+
|
|
541
|
+
def _insert_in_order(
|
|
542
|
+
dictionary: dict[Any, Any], new_key: Any, new_value: Any, after_key: Any
|
|
543
|
+
):
|
|
544
|
+
"""Inserts a key into a dictionary after the selected key"""
|
|
545
|
+
|
|
546
|
+
entries = []
|
|
547
|
+
|
|
548
|
+
while dictionary:
|
|
549
|
+
key, value = dictionary.popitem()
|
|
550
|
+
entries.append((key, value))
|
|
551
|
+
|
|
552
|
+
if key == after_key:
|
|
553
|
+
dictionary[key] = value
|
|
554
|
+
break
|
|
555
|
+
|
|
556
|
+
dictionary[new_key] = new_value
|
|
557
|
+
for key, value in entries[::-1]:
|
|
558
|
+
dictionary[key] = value
|
|
559
|
+
|
|
560
|
+
|
|
561
|
+
def _infer_annotations(instance):
|
|
562
|
+
"""Infers the annotations of non-specified members and adds them to <__annotations__>"""
|
|
563
|
+
prev_name = None
|
|
564
|
+
for var_name, var_value in instance.__dict__.copy().items():
|
|
565
|
+
if not _is_hidden_variable(instance, var_name):
|
|
566
|
+
if var_name not in instance.__annotations__:
|
|
567
|
+
_insert_in_order(
|
|
568
|
+
instance.__annotations__, var_name, type(var_value), prev_name
|
|
569
|
+
)
|
|
570
|
+
|
|
571
|
+
prev_name = var_name
|
|
572
|
+
|
|
573
|
+
|
|
451
574
|
def update_config(self, config: configparser.ConfigParser) -> None:
|
|
452
575
|
"""Updates the provided config file with the values inside current instance
|
|
453
576
|
The Hidden type variables are not added.
|
|
@@ -463,12 +586,13 @@ def update_config(self, config: configparser.ConfigParser) -> None:
|
|
|
463
586
|
if section_name not in config:
|
|
464
587
|
config[section_name] = {}
|
|
465
588
|
for var_name in section_instance.__dir__():
|
|
466
|
-
if
|
|
589
|
+
if not _is_hidden_variable(section_instance, var_name):
|
|
467
590
|
var_value = getattr(section_instance, var_name)
|
|
468
591
|
var_type = section_instance.__annotations__[var_name]
|
|
469
592
|
is_wrapper_type = isinstance(var_type, GenericAlias)
|
|
470
593
|
# 乱数等がGenericAliasを使っています
|
|
471
594
|
# __origin__ はGenericAliasのみ
|
|
595
|
+
|
|
472
596
|
if not is_wrapper_type or (
|
|
473
597
|
is_wrapper_type and not issubclass(var_type.__origin__, Hidden)
|
|
474
598
|
):
|
|
@@ -495,7 +619,11 @@ def update_config(self, config: configparser.ConfigParser) -> None:
|
|
|
495
619
|
var_value = f"{ENC_PREFIX}{var_value.hex()}"
|
|
496
620
|
else:
|
|
497
621
|
var_value = config_val
|
|
498
|
-
|
|
622
|
+
elif (
|
|
623
|
+
is_wrapper_type and var_type.__origin__ in (list, tuple)
|
|
624
|
+
) or var_type in (list, tuple):
|
|
625
|
+
# e.g. str(<tuple>)!=dumps(<tuple>)
|
|
626
|
+
var_value = json.dumps(var_value)
|
|
499
627
|
if (
|
|
500
628
|
var_value
|
|
501
629
|
and issubclass(var_type, _RandomType)
|
|
@@ -702,6 +830,9 @@ def _set_param_from_env(
|
|
|
702
830
|
|
|
703
831
|
if environmental_key in os.environ:
|
|
704
832
|
env_value = os.environ[environmental_key]
|
|
833
|
+
if _is_list_type(parameter_type):
|
|
834
|
+
default_parameter_value = section_class.__dict__[parameter_name]
|
|
835
|
+
env_value = _unpack_json(env_value, parameter_type, default_parameter_value)
|
|
705
836
|
env_value = _auto_cast_type(parameter_type, env_value)
|
|
706
837
|
setattr(section_class, parameter_name, env_value)
|
|
707
838
|
|
|
@@ -765,6 +896,7 @@ def _set_members(
|
|
|
765
896
|
section_class.__annotations__[parameter_name] = type(
|
|
766
897
|
default_parameter_value
|
|
767
898
|
)
|
|
899
|
+
# These are added at the moment of wrapping, therefore should not happen
|
|
768
900
|
logger.warning(tr("parameter_type_missing_1", parameter_name))
|
|
769
901
|
|
|
770
902
|
expected_type: type = (
|
|
@@ -792,7 +924,12 @@ def _set_members(
|
|
|
792
924
|
|
|
793
925
|
elif config_param_value:
|
|
794
926
|
need_encryption.append(parameter_name)
|
|
795
|
-
|
|
927
|
+
elif _is_list_type(expected_type):
|
|
928
|
+
# ここは、<config_param_value>が_auto_cast_typeでキャストするため、
|
|
929
|
+
# param_value_after_castではなく、config_param_valueを設定する
|
|
930
|
+
config_param_value = _unpack_json(
|
|
931
|
+
config_param_value, expected_type, default_parameter_value
|
|
932
|
+
)
|
|
796
933
|
try:
|
|
797
934
|
param_value_after_cast = _auto_cast_type(
|
|
798
935
|
expected_type, config_param_value
|
|
@@ -800,20 +937,20 @@ def _set_members(
|
|
|
800
937
|
|
|
801
938
|
# キャストが失敗したらValueErrorになる
|
|
802
939
|
except ValueError:
|
|
940
|
+
# 失敗したらデフォルト値にする
|
|
941
|
+
param_value_after_cast = _default_value_for_type(
|
|
942
|
+
expected_type, default_parameter_value
|
|
943
|
+
)
|
|
803
944
|
logger.warning(
|
|
804
945
|
tr(
|
|
805
|
-
"
|
|
946
|
+
"invalid_type_5",
|
|
806
947
|
config_section.name,
|
|
807
948
|
parameter_name,
|
|
808
949
|
user_friendly_type_name(expected_type),
|
|
809
950
|
config_param_value,
|
|
951
|
+
param_value_after_cast,
|
|
810
952
|
)
|
|
811
953
|
)
|
|
812
|
-
# 失敗したらデフォルト値にする
|
|
813
|
-
|
|
814
|
-
param_value_after_cast = _default_value_for_type(
|
|
815
|
-
expected_type, default_parameter_value
|
|
816
|
-
)
|
|
817
954
|
|
|
818
955
|
else:
|
|
819
956
|
# ヒントがない場合はそのままに残る(str)
|
|
@@ -843,12 +980,47 @@ def _set_members(
|
|
|
843
980
|
|
|
844
981
|
|
|
845
982
|
# %%
|
|
983
|
+
def _replace_dataclass_forbidden_types(cls):
|
|
984
|
+
"""
|
|
985
|
+
The inner classes are wrapped with @dataclass dynamically.
|
|
986
|
+
These require having field(default_factory=list) for lists, dicts etc.
|
|
987
|
+
To provide a more natural type definitions, these will be replaced automatically
|
|
988
|
+
"""
|
|
989
|
+
|
|
990
|
+
fields_set = []
|
|
991
|
+
for param_name, param_value in cls.__dict__.items():
|
|
992
|
+
# logger.debug(f"Checking {param_name}")
|
|
993
|
+
supported_types = (list, dict, set)
|
|
994
|
+
for supported_type in supported_types:
|
|
995
|
+
if not _is_hidden_variable(cls, param_name) and isinstance(
|
|
996
|
+
param_value, supported_type
|
|
997
|
+
):
|
|
998
|
+
|
|
999
|
+
def field_default_generator(value: list = param_value):
|
|
1000
|
+
return value.copy()
|
|
1001
|
+
|
|
1002
|
+
setattr(
|
|
1003
|
+
cls,
|
|
1004
|
+
param_name,
|
|
1005
|
+
field_generator := field(default_factory=field_default_generator),
|
|
1006
|
+
)
|
|
1007
|
+
fields_set.append((param_name, field_generator))
|
|
1008
|
+
# logger.error(f"Setting attribute: {[x for x in cls.__dict__.keys() if x[0] != '_']} ")
|
|
1009
|
+
return fields_set
|
|
1010
|
+
|
|
1011
|
+
|
|
1012
|
+
# TODO It should be possible to handle this more elegantly
|
|
1013
|
+
def _reset_removed_vals(cls, replaced_values):
|
|
1014
|
+
for name, value in replaced_values:
|
|
1015
|
+
setattr(cls, name, value)
|
|
1016
|
+
|
|
1017
|
+
|
|
846
1018
|
def _add_settings_layer(
|
|
847
|
-
cls:
|
|
1019
|
+
cls: type,
|
|
848
1020
|
env_prefix: str = "",
|
|
849
|
-
common_encryption_key:
|
|
1021
|
+
common_encryption_key: str | tuple[Callable[[Any], str]] | None = None,
|
|
850
1022
|
salt: bytes = None,
|
|
851
|
-
) ->
|
|
1023
|
+
) -> type:
|
|
852
1024
|
"""Dynamically binds the necessary functions after applying the @dataclass decorator.
|
|
853
1025
|
|
|
854
1026
|
Args:
|
|
@@ -867,12 +1039,16 @@ def _add_settings_layer(
|
|
|
867
1039
|
Defaults to None.
|
|
868
1040
|
|
|
869
1041
|
Returns:
|
|
870
|
-
|
|
1042
|
+
type: Returns the decorated class
|
|
871
1043
|
"""
|
|
872
1044
|
|
|
873
1045
|
for subclass_name, subclass_proper in cls.__dict__.items():
|
|
874
1046
|
if not _is_hidden_variable(cls, subclass_name):
|
|
875
|
-
|
|
1047
|
+
fb_vals = _replace_dataclass_forbidden_types(subclass_proper)
|
|
1048
|
+
_infer_annotations(getattr(cls, subclass_name))
|
|
1049
|
+
dataclass(subclass_proper) # In place ?!
|
|
1050
|
+
_reset_removed_vals(subclass_proper, fb_vals)
|
|
1051
|
+
setattr(cls, subclass_name, subclass_proper)
|
|
876
1052
|
|
|
877
1053
|
def repr_wrapper(_self, subclass_name=subclass_name):
|
|
878
1054
|
return f"{cls.__name__} section: [{subclass_name}]{__subrepr__(_self)}"
|
|
@@ -905,7 +1081,7 @@ def _add_settings_layer(
|
|
|
905
1081
|
self,
|
|
906
1082
|
*args,
|
|
907
1083
|
env_prefix: str = env_prefix,
|
|
908
|
-
encryption_key:
|
|
1084
|
+
encryption_key: str | Callable[[Any], str] | None = None,
|
|
909
1085
|
**kwargs,
|
|
910
1086
|
):
|
|
911
1087
|
if common_encryption_key and not encryption_key: # local has priority
|
|
@@ -929,7 +1105,7 @@ def settingsclass(
|
|
|
929
1105
|
/,
|
|
930
1106
|
*,
|
|
931
1107
|
env_prefix="",
|
|
932
|
-
encryption_key:
|
|
1108
|
+
encryption_key: str | Callable[[Any], str] | None = None,
|
|
933
1109
|
_salt: bytes = None,
|
|
934
1110
|
):
|
|
935
1111
|
"""Dynamically binds the necessary functions after applying the @dataclass decorator.
|
|
@@ -950,7 +1126,7 @@ def settingsclass(
|
|
|
950
1126
|
Defaults to None.
|
|
951
1127
|
|
|
952
1128
|
Returns:
|
|
953
|
-
|
|
1129
|
+
type: Returns the decorated class
|
|
954
1130
|
"""
|
|
955
1131
|
|
|
956
1132
|
# ↓ copied from dataclass
|
|
@@ -998,10 +1174,12 @@ if __name__ == "__main__": # pragma: no cover
|
|
|
998
1174
|
seed: Encrypted[RandomFloat[0, 2**16]] = 1.2
|
|
999
1175
|
api_id: RandomInt[1000, 9999] = 0
|
|
1000
1176
|
|
|
1001
|
-
class
|
|
1177
|
+
class llm:
|
|
1002
1178
|
api_key: Encrypted[str] = ""
|
|
1003
1179
|
backup_pin: Encrypted[int] = -1
|
|
1004
|
-
model: str = "
|
|
1180
|
+
model: str = "35t"
|
|
1181
|
+
fallback_models = ["4.1", "4.0", 5.0]
|
|
1182
|
+
other_settings: tuple = (1, 2, "x")
|
|
1005
1183
|
temperature: Hidden[float] = 5
|
|
1006
1184
|
timeout = 300
|
|
1007
1185
|
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
Metadata-Version: 2.
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
2
|
Name: settingsclass
|
|
3
|
-
Version: 0.1.
|
|
3
|
+
Version: 0.1.5
|
|
4
4
|
Summary: A class decorator for easily storing setting values, with automatic sync to disk
|
|
5
5
|
Author: Robert Kajnak
|
|
6
6
|
License: MIT Licence
|
|
@@ -12,6 +12,7 @@ Description-Content-Type: text/markdown
|
|
|
12
12
|
License-File: LICENSE
|
|
13
13
|
Requires-Dist: loguru
|
|
14
14
|
Requires-Dist: pycryptodome>=3.20.0
|
|
15
|
+
Dynamic: license-file
|
|
15
16
|
|
|
16
17
|
[](https://github.com/id-ai-labo/settingsclass/actions/workflows/tests.yml)
|
|
17
18
|
[](./tests/reports/www/index.html)
|
|
@@ -8,9 +8,11 @@ rfph = 34260.804
|
|
|
8
8
|
seed = ?ENCfdceb9f617840d6c64459b377962252b2dcc4b1f0d9da088365784a35f4d871a
|
|
9
9
|
api_id = 9955
|
|
10
10
|
|
|
11
|
-
[
|
|
11
|
+
[llm]
|
|
12
12
|
api_key = ?ENC22e6de0f80d81f54fbae752d27cd5663e693758554d3520466e7c90423fd3997
|
|
13
13
|
backup_pin = ?ENC78153b2c9a0e28664913023834be3e8186db3f6489983e2502b197cbf1c5fd31
|
|
14
14
|
timeout = 281
|
|
15
15
|
temperature = 0.15
|
|
16
|
+
versions = [10, 20, 30]
|
|
17
|
+
extra = ["abc"]
|
|
16
18
|
|
|
@@ -8,9 +8,11 @@ rfph = alma
|
|
|
8
8
|
seed = ?ENC94f5e4bec577703f96ef03519a34b91e2c6bbea68c1b9956cc2927c3cfd15ee6
|
|
9
9
|
api_id = bar
|
|
10
10
|
|
|
11
|
-
[
|
|
11
|
+
[llm]
|
|
12
12
|
api_key = ?ENC7b2192455b42ef024f7d98ac69e0f670917cf658136a3e52a9eeb07448a3d1ac
|
|
13
13
|
backup_pin = ?ENC1addbca1fa969bb455c9c55bb544f3193e93546ad21af91e7ea933722ae2878d
|
|
14
14
|
timeout = 281
|
|
15
15
|
temperature = 0.15
|
|
16
|
+
versions = [35, 40.0, "50"]
|
|
17
|
+
extra = ("abc","a")
|
|
16
18
|
|
{settingsclass-0.1.4 → settingsclass-0.1.5}/tests/input/settingsclass/config_confusing_types.ini
RENAMED
|
@@ -8,7 +8,7 @@ rfph = 34260.804
|
|
|
8
8
|
seed = ?ENCf208cee5b3bf7b2037ef8be25caa800ae3904aab05c170475df5230fbab0e03f
|
|
9
9
|
api_id = 9955
|
|
10
10
|
|
|
11
|
-
[
|
|
11
|
+
[llm]
|
|
12
12
|
api_key = ?ENCb1298c8987909d41e2d5e7d4026aec35eccea75b074b297ad4aa461e82cbd9cd
|
|
13
13
|
backup_pin = ?ENCb255786a2faa5e6dcaf64fb9c17574f56d2b4cc5aac113b65bc75818364a8497
|
|
14
14
|
timeout = 281
|
{settingsclass-0.1.4 → settingsclass-0.1.5}/tests/input/settingsclass/config_extra_parts.ini
RENAMED
|
@@ -8,7 +8,7 @@ rfph = 34260.804
|
|
|
8
8
|
seed = ?ENCd7d21f6c54087d7b668c4e0d2c9717270d0d8b04114db64dea6262553975b537
|
|
9
9
|
api_id = 9955
|
|
10
10
|
|
|
11
|
-
[
|
|
11
|
+
[llm]
|
|
12
12
|
api_key = ?ENC4d37f2f70e6a8202e04f29580b4523146bb811bbf8a1ab28d3fc76380f2b3130
|
|
13
13
|
backup_pin = ?ENCabb9bdd35c2feb3c91ada2f8d4a7ab9394715b10ebfac5d90b6e3474bb579899
|
|
14
14
|
timeout = 281
|
{settingsclass-0.1.4 → settingsclass-0.1.5}/tests/input/settingsclass/config_generated_good.ini
RENAMED
|
@@ -7,8 +7,10 @@ rfph = 34260.804
|
|
|
7
7
|
seed = ?ENCa6ae7caffd3723def41c789b52a07cef02d2a476a7280451bac778cba4c43695
|
|
8
8
|
api_id = 1234
|
|
9
9
|
|
|
10
|
-
[
|
|
10
|
+
[llm]
|
|
11
11
|
api_key =
|
|
12
12
|
backup_pin = ?ENCef3106b5b827128acf69551ce6d4603fbad14bce053b7105efad047ff13b3cc5
|
|
13
13
|
timeout = 300
|
|
14
|
+
versions = [35, 40, 50]
|
|
15
|
+
extra = [5, "x"]
|
|
14
16
|
|
{settingsclass-0.1.4 → settingsclass-0.1.5}/tests/input/settingsclass/config_good_case_var.ini
RENAMED
|
@@ -7,10 +7,12 @@ colored_console_output = False
|
|
|
7
7
|
rfph = 34260.804
|
|
8
8
|
seed = ?ENC278aacf223d8db0edb8b05afed3926d8760ecc90e0008c1c15eb2080159b7343
|
|
9
9
|
api_id = 9955
|
|
10
|
-
|
|
11
|
-
[gpt]
|
|
10
|
+
[llm]
|
|
12
11
|
api_key = ?ENCbe414664a866012ed0caa2dbf2326548a6b3bc25238d9d7d3eb46ed8a885350e
|
|
13
12
|
Backup_pin = ?ENC78446e26ca290aa2c691bbff2fef700025c8359cbb264643603553d348ddf2be
|
|
14
13
|
timeout = 281
|
|
15
14
|
temperature = 0.15
|
|
15
|
+
versions = [10, 20, 30]
|
|
16
|
+
extra = "abc"
|
|
17
|
+
|
|
16
18
|
|
|
@@ -8,9 +8,11 @@ rfph = 34260.804
|
|
|
8
8
|
seed = ?ENCe52b40adb850a8825daaa54840be2ce267d138e8a4c36751faea3c71acdab2e3
|
|
9
9
|
api_id = 9955
|
|
10
10
|
|
|
11
|
-
[
|
|
11
|
+
[llm]
|
|
12
12
|
api_key = ?ENCf4ec38c47403c758c4d024f94f20add06e2ee5d53cc010d6847038b6ef7976a8
|
|
13
13
|
backup_pin = ?ENCd513c6bd25e6d9c03d9946948c88edb74bfdac8db6f6e33477d8e083ddcce3a7
|
|
14
14
|
timeout = 281
|
|
15
15
|
temperature = 0.15
|
|
16
|
+
versions = [10, 20, 30]
|
|
17
|
+
extra = "abc"
|
|
16
18
|
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
<svg xmlns="http://www.w3.org/2000/svg" width="114" height="20" role="img" aria-label="coverage: 98.51%"><title>coverage: 98.51%</title><linearGradient id="s" x2="0" y2="100%"><stop offset="0" stop-color="#bbb" stop-opacity=".1"/><stop offset="1" stop-opacity=".1"/></linearGradient><clipPath id="r"><rect width="114" height="20" rx="3" fill="#fff"/></clipPath><g clip-path="url(#r)"><rect width="61" height="20" fill="#555"/><rect x="61" width="53" height="20" fill="#4c1"/><rect width="114" height="20" fill="url(#s)"/></g><g fill="#fff" text-anchor="middle" font-family="Verdana,Geneva,DejaVu Sans,sans-serif" text-rendering="geometricPrecision" font-size="110"><text aria-hidden="true" x="315" y="150" fill="#010101" fill-opacity=".3" transform="scale(.1)" textLength="510">coverage</text><text x="315" y="140" transform="scale(.1)" fill="#fff" textLength="510">coverage</text><text aria-hidden="true" x="865" y="150" fill="#010101" fill-opacity=".3" transform="scale(.1)" textLength="430">98.51%</text><text x="865" y="140" transform="scale(.1)" fill="#fff" textLength="430">98.51%</text></g></svg>
|
|
@@ -95,11 +95,13 @@ class Settings:
|
|
|
95
95
|
seed: Encrypted[RandomFloat[0, 2**16]] = 1.2
|
|
96
96
|
api_id: RandomInt[1000, 9999] = 0
|
|
97
97
|
|
|
98
|
-
class
|
|
98
|
+
class llm:
|
|
99
99
|
api_key: Encrypted[str] = ""
|
|
100
100
|
backup_pin: Encrypted[int] = -1
|
|
101
|
+
timeout = 300
|
|
101
102
|
temperature: Hidden[float] = 5
|
|
102
|
-
|
|
103
|
+
versions: list[int] = [35, 40, 50]
|
|
104
|
+
extra: tuple = (5, "x")
|
|
103
105
|
|
|
104
106
|
|
|
105
107
|
@settingsclass(
|
|
@@ -414,10 +416,17 @@ def validate_good_contents(config: Settings):
|
|
|
414
416
|
assert config.program.seed == 99.8 # mod
|
|
415
417
|
assert config.program.api_id == 9955 # mod
|
|
416
418
|
|
|
417
|
-
assert config.
|
|
418
|
-
assert config.
|
|
419
|
-
|
|
420
|
-
assert config.
|
|
419
|
+
assert config.llm.api_key == "sk-123kld-12141" # dmod
|
|
420
|
+
assert config.llm.backup_pin == 852 # mod
|
|
421
|
+
|
|
422
|
+
assert config.llm.temperature == 0.15 # mod
|
|
423
|
+
assert config.llm.timeout == 281
|
|
424
|
+
assert config.llm.versions == [10, 20, 30] # mod
|
|
425
|
+
assert config.llm.extra == ("abc",) # mod
|
|
426
|
+
|
|
427
|
+
# Validate, that the implied parameter is also printed,
|
|
428
|
+
# and at the correct place
|
|
429
|
+
assert "timeout: <int> = 281" in str(config).split("\n")[-5]
|
|
421
430
|
|
|
422
431
|
|
|
423
432
|
def validate_init_contents(config: Settings):
|
|
@@ -437,10 +446,16 @@ def validate_init_contents(config: Settings):
|
|
|
437
446
|
assert isinstance(api_id := config.program.api_id, int)
|
|
438
447
|
assert api_id >= 1000 and api_id <= 9999
|
|
439
448
|
|
|
440
|
-
assert config.
|
|
441
|
-
assert config.
|
|
442
|
-
assert config.
|
|
443
|
-
assert config.
|
|
449
|
+
assert config.llm.api_key == ""
|
|
450
|
+
assert config.llm.backup_pin == -1
|
|
451
|
+
assert config.llm.temperature == 5
|
|
452
|
+
assert config.llm.timeout == 300
|
|
453
|
+
assert config.llm.versions == [35, 40, 50]
|
|
454
|
+
assert config.llm.extra == (5, "x")
|
|
455
|
+
|
|
456
|
+
# Validate, that the implied parameter is also printed,
|
|
457
|
+
# and at the correct place (2nd from the back + 1x newline)
|
|
458
|
+
assert "timeout: <int> = 300" in str(config).split("\n")[-5]
|
|
444
459
|
|
|
445
460
|
|
|
446
461
|
def test_ram_only_init():
|
|
@@ -494,7 +509,7 @@ def test_object_not_static():
|
|
|
494
509
|
validate_good_contents(config1)
|
|
495
510
|
validate_good_contents(config2)
|
|
496
511
|
|
|
497
|
-
config1.
|
|
512
|
+
config1.llm.api_key = "aqsd"
|
|
498
513
|
config1.program.seed = 21
|
|
499
514
|
|
|
500
515
|
validate_good_contents(config2)
|
|
@@ -542,6 +557,52 @@ def test_missing_init_value_msg():
|
|
|
542
557
|
_ = LocalSettingsFirst(join(PARENT_OUT, "plpl.ini"))
|
|
543
558
|
|
|
544
559
|
|
|
560
|
+
def test_invalid_list_hint_msg():
|
|
561
|
+
@settingsclass
|
|
562
|
+
class LocalSettingsTooMany:
|
|
563
|
+
class subc:
|
|
564
|
+
too_many: tuple[int, str] = [1, "a"]
|
|
565
|
+
|
|
566
|
+
too_many_error_pattern = re.escape(
|
|
567
|
+
tr("iterable_class_annotation_too_many_1", "tuple[int, str]")
|
|
568
|
+
)
|
|
569
|
+
with pytest.raises(ValueError, match=too_many_error_pattern):
|
|
570
|
+
_ = LocalSettingsTooMany(join(PARENT_OUT, "list_hint_many.ini"))
|
|
571
|
+
|
|
572
|
+
@settingsclass
|
|
573
|
+
class LocalSettingsTooMany:
|
|
574
|
+
class subc:
|
|
575
|
+
too_many: tuple[()] = [1, "a"]
|
|
576
|
+
|
|
577
|
+
too_few_error_pattern = re.escape(
|
|
578
|
+
tr("iterable_class_annotation_too_few_1", "tuple[()]")
|
|
579
|
+
)
|
|
580
|
+
with pytest.raises(ValueError, match=too_few_error_pattern):
|
|
581
|
+
_ = LocalSettingsTooMany(join(PARENT_OUT, "list_hint_few.ini"))
|
|
582
|
+
|
|
583
|
+
|
|
584
|
+
def test_unsupported_types():
|
|
585
|
+
@settingsclass
|
|
586
|
+
class LocalSettingsDictType:
|
|
587
|
+
class subc:
|
|
588
|
+
too_many: dict[int, str] = {1: "a"}
|
|
589
|
+
|
|
590
|
+
dictionary_type = re.escape(
|
|
591
|
+
tr("dictionary_not_supported_2", "{1: 'a'}", "dict[int, str]")
|
|
592
|
+
)
|
|
593
|
+
with pytest.raises(ValueError, match=dictionary_type):
|
|
594
|
+
_ = LocalSettingsDictType(join(PARENT_OUT, "dict_type.ini"))
|
|
595
|
+
|
|
596
|
+
@settingsclass
|
|
597
|
+
class LocalSettingsUnknownType:
|
|
598
|
+
class subc:
|
|
599
|
+
too_many: set[int] = {1, 2}
|
|
600
|
+
|
|
601
|
+
dictionary_type = re.escape(tr("unexpected_class_found_2", "set[int]", "{1, 2}"))
|
|
602
|
+
with pytest.raises(ValueError, match=dictionary_type):
|
|
603
|
+
_ = LocalSettingsUnknownType(join(PARENT_OUT, "unknown_type.ini"))
|
|
604
|
+
|
|
605
|
+
|
|
545
606
|
def test_case_sensitivity():
|
|
546
607
|
"""「case_sensitive」パラメータ効果の有無確認"""
|
|
547
608
|
# UTF-8とキャストのテストも含む
|
|
@@ -562,9 +623,9 @@ def test_case_sensitivity():
|
|
|
562
623
|
def test_settings_is_folder():
|
|
563
624
|
"""設定ファイルのパスはフォルダーになった場合のエラーの確認"""
|
|
564
625
|
ini_dir = join(PARENT_IN, "config.ini")
|
|
565
|
-
assert exists(ini_dir) and isdir(
|
|
566
|
-
ini_dir
|
|
567
|
-
)
|
|
626
|
+
assert exists(ini_dir) and isdir(ini_dir), (
|
|
627
|
+
f"Test setup is incorrect, {ini_dir} should be a directory"
|
|
628
|
+
)
|
|
568
629
|
|
|
569
630
|
# path <- config.ini/config.ini、最初はフォルダー
|
|
570
631
|
|
|
@@ -682,7 +743,7 @@ def test_missing_section_and_variable(caplog): # noqa: F811
|
|
|
682
743
|
_ = Settings(join(PARENT_IN, "config_missing_section.ini"))
|
|
683
744
|
|
|
684
745
|
# エラーがある場合、まずはエラーメッセージ内容、引数などが変わっていないことを確認して下さい
|
|
685
|
-
assert tr("missing_config_section_1", "
|
|
746
|
+
assert tr("missing_config_section_1", "llm") in caplog.text
|
|
686
747
|
assert tr("config_param_missing_2", "program", "rfph") in caplog.text
|
|
687
748
|
assert tr("config_param_missing_2", "program", "seed") in caplog.text
|
|
688
749
|
|
|
@@ -698,7 +759,7 @@ def test_extra_section_and_variables(caplog): # noqa: F811
|
|
|
698
759
|
assert (
|
|
699
760
|
tr(
|
|
700
761
|
"extra_config_parameter_2",
|
|
701
|
-
"
|
|
762
|
+
"llm",
|
|
702
763
|
["imaginary_variable", "also_doesnt_exist"],
|
|
703
764
|
)
|
|
704
765
|
in caplog.text
|
|
@@ -724,11 +785,18 @@ def test_invalid_param_type(caplog): # noqa: F811
|
|
|
724
785
|
config = Settings(join(PARENT_IN, "config_bad_type.ini"))
|
|
725
786
|
|
|
726
787
|
assert (
|
|
727
|
-
tr("
|
|
788
|
+
tr("invalid_type_5", "program", "colored_console_output", bool, "Igen", True)
|
|
789
|
+
in caplog.text
|
|
790
|
+
)
|
|
791
|
+
assert (
|
|
792
|
+
tr("invalid_type_5", "program", "rfph", float, "alma", config.program.rfph)
|
|
728
793
|
in caplog.text
|
|
729
794
|
)
|
|
730
|
-
assert
|
|
731
|
-
|
|
795
|
+
assert (
|
|
796
|
+
tr("invalid_type_5", "program", "api_id", int, "bar", config.program.api_id)
|
|
797
|
+
in caplog.text
|
|
798
|
+
)
|
|
799
|
+
assert tr("json_decode_failed_3", '("abc","a")', tuple, "(5, 'x')") in caplog.text
|
|
732
800
|
|
|
733
801
|
api_id = config.program.api_id
|
|
734
802
|
assert api_id >= 1000 and api_id <= 9999
|
|
@@ -738,6 +806,9 @@ def test_invalid_param_type(caplog): # noqa: F811
|
|
|
738
806
|
|
|
739
807
|
assert config.program.colored_console_output
|
|
740
808
|
|
|
809
|
+
assert config.llm.versions == [35, 40, 50]
|
|
810
|
+
assert config.llm.extra == (5, "x")
|
|
811
|
+
|
|
741
812
|
|
|
742
813
|
def test_type_confusion(caplog): # noqa: F811
|
|
743
814
|
with caplog.at_level(logging.DEBUG):
|
|
@@ -754,20 +825,21 @@ def test_type_confusion(caplog): # noqa: F811
|
|
|
754
825
|
in caplog.text
|
|
755
826
|
)
|
|
756
827
|
assert (
|
|
757
|
-
tr("param_type_is_string_but_looks_x_4", "
|
|
828
|
+
tr("param_type_is_string_but_looks_x_4", "llm", "api_key", 3.0, "float")
|
|
758
829
|
in caplog.text
|
|
759
830
|
)
|
|
760
831
|
|
|
761
832
|
|
|
762
833
|
def test_need_encryption(caplog):
|
|
763
|
-
with
|
|
764
|
-
|
|
765
|
-
|
|
834
|
+
with (
|
|
835
|
+
open(join(PARENT_IN, "config_generated_good_half_enc.ini"), "rb") as f,
|
|
836
|
+
open(copied_file := join(PARENT_OUT, "cgghe.ini"), "wb") as g,
|
|
837
|
+
):
|
|
766
838
|
g.write(f.read())
|
|
767
839
|
caplog.clear()
|
|
768
840
|
with caplog.at_level(logging.INFO):
|
|
769
841
|
_ = Settings(copied_file)
|
|
770
|
-
assert tr("unencrypted_data_found_1", "['
|
|
842
|
+
assert tr("unencrypted_data_found_1", "['llm/backup_pin']") in caplog.text
|
|
771
843
|
|
|
772
844
|
|
|
773
845
|
# %% ENV check
|
|
@@ -807,7 +879,7 @@ def test_environmental_variables_instance():
|
|
|
807
879
|
|
|
808
880
|
validate_init_contents(config)
|
|
809
881
|
validate_init_contents(config_disabled)
|
|
810
|
-
env_prefix = f
|
|
882
|
+
env_prefix = f"{prefix}{prefix and '_'}"
|
|
811
883
|
|
|
812
884
|
old_values, temp_values = _set_env_values(
|
|
813
885
|
{
|
|
@@ -819,10 +891,12 @@ def test_environmental_variables_instance():
|
|
|
819
891
|
f"{env_prefix}PROGRAM_RFPH": 34260.804,
|
|
820
892
|
f"{env_prefix}PROGRAM_SEED": 99.8,
|
|
821
893
|
f"{env_prefix}PROGRAM_API_ID": 9955,
|
|
822
|
-
f"{env_prefix}
|
|
823
|
-
f"{env_prefix}
|
|
824
|
-
f"{env_prefix}
|
|
825
|
-
f"{env_prefix}
|
|
894
|
+
f"{env_prefix}LLM_API_KEY": "sk-123kld-12141",
|
|
895
|
+
f"{env_prefix}LLM_BACKUP_PIN": 852,
|
|
896
|
+
f"{env_prefix}LLM_TEMPERATURE": 0.15,
|
|
897
|
+
f"{env_prefix}LLM_TIMEOUT": 281,
|
|
898
|
+
f"{env_prefix}LLM_VERSIONS": [10, 20, 30],
|
|
899
|
+
f"{env_prefix}LLM_EXTRA": '["abc"]',
|
|
826
900
|
}
|
|
827
901
|
)
|
|
828
902
|
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="114" height="20" role="img" aria-label="coverage: 98.54%"><title>coverage: 98.54%</title><linearGradient id="s" x2="0" y2="100%"><stop offset="0" stop-color="#bbb" stop-opacity=".1"/><stop offset="1" stop-opacity=".1"/></linearGradient><clipPath id="r"><rect width="114" height="20" rx="3" fill="#fff"/></clipPath><g clip-path="url(#r)"><rect width="61" height="20" fill="#555"/><rect x="61" width="53" height="20" fill="#4c1"/><rect width="114" height="20" fill="url(#s)"/></g><g fill="#fff" text-anchor="middle" font-family="Verdana,Geneva,DejaVu Sans,sans-serif" text-rendering="geometricPrecision" font-size="110"><text aria-hidden="true" x="315" y="150" fill="#010101" fill-opacity=".3" transform="scale(.1)" textLength="510">coverage</text><text x="315" y="140" transform="scale(.1)" fill="#fff" textLength="510">coverage</text><text aria-hidden="true" x="865" y="150" fill="#010101" fill-opacity=".3" transform="scale(.1)" textLength="430">98.54%</text><text x="865" y="140" transform="scale(.1)" fill="#fff" textLength="430">98.54%</text></g></svg>
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{settingsclass-0.1.4 → settingsclass-0.1.5}/tests/input/settingsclass/config_missing_section.ini
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|