settingsclass 0.1.4__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.
@@ -0,0 +1,1012 @@
1
+ # -*- coding: utf-8 -*-
2
+ """
3
+ Created on Thu May 11 16:54:34 2023
4
+
5
+ @author: uid7067
6
+ """
7
+
8
+ # %% Imports
9
+ # from __future__ import annotations # do NOT use! Makes strings become un-callable
10
+
11
+ from dataclasses import dataclass
12
+ from Crypto.Cipher import AES
13
+ from Crypto.Util.Padding import pad, unpad
14
+ import hashlib
15
+ from secrets import token_bytes
16
+ import configparser
17
+ import random
18
+ import re
19
+ import secrets
20
+ from types import GenericAlias, MethodType, FunctionType
21
+ from typing import Any, Callable, TypeVar
22
+ import os
23
+ from threading import Lock
24
+
25
+ from loguru import logger
26
+
27
+ from .localizer import tr
28
+ from .localizer import set_language as _set_language
29
+
30
+
31
+ # %%
32
+ ENC_PREFIX = "?ENC"
33
+ DEFAULT_FLOAT_PRECISION = 3
34
+
35
+ _key_lock = Lock()
36
+
37
+ ClassType = TypeVar("ClassType")
38
+
39
+
40
+ def available_languages():
41
+ """Returns the available logging and error message languages"""
42
+ return ("ja", "en")
43
+
44
+
45
+ def set_language(lang: str):
46
+ """Changes the language and error messages
47
+
48
+ Args:
49
+ lang (str): Two character code of the language see <available_languages>
50
+ """
51
+ _set_language(lang)
52
+
53
+
54
+ class KeyfileLocationError(RuntimeError):
55
+ """The location of the key cannot be accessed"""
56
+
57
+
58
+ class MissingSettingsError(RuntimeError):
59
+ """The settings has not been given an initialization value"""
60
+
61
+
62
+ def hash_value(value: str):
63
+ hash = hashlib.sha256()
64
+ hash.update(value.encode("utf-8"))
65
+ return hash.hexdigest()
66
+
67
+
68
+ def _ensure_parent_dirs(parent_dir: str = None) -> str:
69
+ """Attempts to create parent directory. If None is specified, the current lib directory is used"""
70
+
71
+ if not parent_dir:
72
+ parent_dir = os.path.dirname(__file__)
73
+ elif not os.path.exists(parent_dir):
74
+ os.makedirs(parent_dir)
75
+
76
+ return parent_dir
77
+
78
+
79
+ def _load_key_unchecked(full_path: str) -> bytes:
80
+ """Loads a key from the specified path. If not parent_dir, the library directory is used.
81
+ The full path should be hashed"""
82
+
83
+ if os.path.exists(full_path) and os.path.isfile(full_path):
84
+ with open(full_path, "rb") as f:
85
+ key_content = f.read()
86
+ else:
87
+ key_content = token_bytes(16)
88
+ with open(full_path, "wb") as f:
89
+ f.write(key_content)
90
+
91
+ return key_content
92
+
93
+
94
+ def _load_key(plain_filename: str, parent_dir: str = None) -> bytes:
95
+ """Loads a key from the specified path. If not parent_dir, the library directory is used"""
96
+
97
+ true_filename = hash_value(plain_filename)
98
+
99
+ full_path = parent_dir # for the exception
100
+ try:
101
+ parent_dir = _ensure_parent_dirs(parent_dir)
102
+ full_path = os.path.join(parent_dir, true_filename)
103
+ return _load_key_unchecked(full_path)
104
+ except (PermissionError, OSError) as ex:
105
+ logger.error(error_string := tr("keyfile_location_unaccessible_1", full_path))
106
+ raise PermissionError(error_string) from ex
107
+
108
+
109
+ def _load_guard(key, iv, salt: bytes = None):
110
+ if key:
111
+ hash = hashlib.sha256()
112
+ hash.update(key.encode("utf-8"))
113
+ key = hash.digest()
114
+ with _key_lock:
115
+ if not salt:
116
+ try:
117
+ _load_guard._salt
118
+ except AttributeError:
119
+ _load_guard._salt = _load_key("salty_boy")
120
+ salt = _load_guard._salt
121
+
122
+ if not key:
123
+ try:
124
+ _load_key._key
125
+ except AttributeError:
126
+ _load_key._key = _load_key("keygirl")
127
+ key = _load_key._key
128
+
129
+ hash = hashlib.sha256()
130
+ hash.update(key)
131
+ hash.update(salt)
132
+ key = hash.digest()[: AES.block_size]
133
+ return AES.new(key, AES.MODE_CFB, iv)
134
+
135
+
136
+ # TODO optimize this
137
+ def encrypt_message(message: str, key=None, salt: bytes = None):
138
+ iv = secrets.token_bytes(AES.block_size)
139
+ guard = _load_guard(key, iv, salt)
140
+ message = pad(str(message).encode("utf-8"), AES.block_size)
141
+ return iv + guard.encrypt(message)
142
+
143
+
144
+ def decrypt_message(message: str, key=None, salt: bytes = None):
145
+ iv = message[: AES.block_size]
146
+ guard = _load_guard(key, iv, salt)
147
+ return unpad(guard.decrypt(message[AES.block_size :]), AES.block_size).decode(
148
+ "utf-8"
149
+ )
150
+
151
+
152
+ def _is_encoded(message: str):
153
+ return len(message) > len(ENC_PREFIX) and message[: len(ENC_PREFIX)] == ENC_PREFIX
154
+
155
+
156
+ class _TypeWrapper:
157
+ def __class_getitem__(cls, item):
158
+ # class Secret[Generic]: # req. python>=3.12
159
+ return GenericAlias(cls, item)
160
+
161
+
162
+ class _Encrypted: # TODO change to expected_type.__origin__
163
+ pass
164
+
165
+
166
+ class _Hidden: # TODO change to expected_type.__origin__
167
+ pass
168
+
169
+
170
+ class Encrypted(_TypeWrapper, _Encrypted):
171
+ pass
172
+
173
+
174
+ class Hidden(_TypeWrapper, _Hidden):
175
+ pass
176
+
177
+
178
+ class _RandomType:
179
+ def __class_getitem__(cls, item):
180
+ """see __new__ for parameters"""
181
+ return GenericAlias(cls, item)
182
+
183
+
184
+ class RandomString(str, _RandomType):
185
+ def __new__(
186
+ cls,
187
+ min_length,
188
+ max_length=-1,
189
+ *,
190
+ random_function: Callable = secrets.token_urlsafe,
191
+ ):
192
+ """the random function should take one parameter, and return a string at least the length of the string"""
193
+
194
+ if max_length < min_length:
195
+ min_length, max_length = max_length, min_length
196
+
197
+ if min_length <= 0:
198
+ min_length = max_length
199
+
200
+ if min_length == max_length:
201
+ true_length = max_length
202
+ else:
203
+ true_length = random.randint(min_length, max_length)
204
+
205
+ value = random_function(max_length)[:true_length]
206
+
207
+ return str(value)
208
+
209
+
210
+ class RandomInt(int, _RandomType):
211
+ def __new__(
212
+ cls,
213
+ min_value,
214
+ max_value,
215
+ *,
216
+ random_function: Callable = random.randint,
217
+ ):
218
+ return random_function(min_value, max_value)
219
+
220
+
221
+ class RandomFloat(float, _RandomType):
222
+ def __new__(
223
+ cls,
224
+ min_value: float,
225
+ max_value: float,
226
+ precision: int = DEFAULT_FLOAT_PRECISION,
227
+ *,
228
+ random_function: Callable = random.random,
229
+ ):
230
+ if min_value == max_value:
231
+ uncapped_val = super().__new__(cls, min_value)
232
+ uncapped_val = float(random_function() * (max_value - min_value) + min_value)
233
+ if precision >= 0:
234
+ return round(uncapped_val, precision)
235
+ return uncapped_val
236
+
237
+
238
+ # note: use lowercase for subclasses, they will be shadowed by instance values
239
+
240
+
241
+ def _auto_cast_type(
242
+ expected_type: type, config_param_value: str, *, force_random=False
243
+ ):
244
+ """設定クラスの型ヒントにに合わせる"""
245
+
246
+ # ここのラッパーは全てGenericAliasも継承している
247
+ if isinstance(expected_type, GenericAlias):
248
+ # Randomの場合は、そのタイプのクラスにコンストラクタをあげる
249
+ if issubclass(expected_type.__origin__, _RandomType):
250
+ if config_param_value == "" or force_random:
251
+ param_value_after_cast = _init_randomclass(expected_type)
252
+ else:
253
+ param_value_after_cast = expected_type.__base__(config_param_value)
254
+
255
+ # HiddenやEncryptedの場合はクラスで定義している値をとる
256
+ elif issubclass(expected_type, _TypeWrapper):
257
+ # 値確認のためキャストする
258
+ param_value_after_cast = _auto_cast_type(
259
+ expected_type.__args__[0], config_param_value, force_random=force_random
260
+ )
261
+
262
+ else:
263
+ raise ValueError(
264
+ tr("unexpected_class_found_2", expected_type, config_param_value)
265
+ )
266
+
267
+ # コンフィグファイルに値が入っていない場合は型のヒントで初期化する
268
+ elif config_param_value == "":
269
+ param_value_after_cast = expected_type()
270
+
271
+ else:
272
+ # boolのサブクラスは定期不可能
273
+ if expected_type is bool and not isinstance(config_param_value, bool):
274
+ # 'FALSE', 'False', 'false'のすべてがFalseにする
275
+ if config_param_value.upper() == "FALSE" or config_param_value == "0":
276
+ param_value_after_cast = False
277
+ elif config_param_value.upper() == "TRUE" or config_param_value == "1":
278
+ param_value_after_cast = True
279
+ else:
280
+ raise ValueError(tr("cannot_convert_to_bool_1", config_param_value))
281
+ else:
282
+ # キャストが失敗したら関数外で対応する
283
+ param_value_after_cast = expected_type(config_param_value)
284
+
285
+ return param_value_after_cast
286
+
287
+
288
+ def user_friendly_type_name(typ):
289
+ """Returns <float> instead of __main__.RandomFloat[5] etc."""
290
+ friendly_type = typ
291
+ if issubclass(typ, _TypeWrapper):
292
+ friendly_type = typ.__args__[0]
293
+ elif issubclass(typ.__origin__, _RandomType):
294
+ friendly_type = typ.__base__
295
+
296
+ return friendly_type
297
+
298
+
299
+ def _default_value_for_type(typ, default_value_for_primitive):
300
+ """Returns a default value for simple types, but generates one for random types"""
301
+ if issubclass(typ.__origin__, _RandomType):
302
+ return _init_randomclass(typ)
303
+ return default_value_for_primitive
304
+
305
+
306
+ def _init_randomclass(_cls):
307
+ """Returns a possible value for the random value of the appropriate type"""
308
+ return _cls.__origin__(*_cls.__args__)
309
+
310
+
311
+ def _within_random_limits(typ, value):
312
+ """Returns true if the provided value is within the type hint limits"""
313
+ if not issubclass(typ, _RandomType) or isinstance(typ, _RandomType):
314
+ raise TypeError(tr("function_can_only_be_called_on_randomtype_1", typ))
315
+ if not hasattr(typ, "__args__"):
316
+ return True
317
+ if issubclass(typ, RandomString) or issubclass(typ.__origin__, RandomString):
318
+ if len(typ.__args__) == 1:
319
+ return len(value) == typ.__args__[0]
320
+ else:
321
+ return typ.__args__[0] <= len(value) and len(value) <= typ.__args__[1]
322
+ else:
323
+ return value >= typ.__args__[0] and value <= typ.__args__[1]
324
+ # the below matches it to python's random function, but settings-wise including both ends seems more reasonable
325
+ # return value >= typ.__args__[0] and (
326
+ # value <= typ.__args__[1] if issubclass(typ.__origin__, RandomInt) else value < typ.__args__[1]
327
+ # )
328
+
329
+
330
+ def _is_hidden_variable(obj, member_name) -> bool:
331
+ """Verifies that the member is not a function, method or a hidden variable"""
332
+ return member_name[:1] == "_" or isinstance(
333
+ getattr(obj, member_name), (MethodType, FunctionType)
334
+ )
335
+
336
+
337
+ def is_settingsclass_instance(obj):
338
+ raise NotImplementedError("まだです")
339
+
340
+
341
+ def __post_init__(self):
342
+ for member_name in self.__dir__():
343
+ if not _is_hidden_variable(self, member_name):
344
+ subclass = getattr(self, member_name)
345
+ try:
346
+ subclass_instance = subclass()
347
+ except TypeError as ex:
348
+ msg = str(ex)
349
+ try:
350
+ cls_name, subclass_name, var_name = re.findall(
351
+ r"(\w+)\.(\w+)\.__init__\(\) missing \d+ required positional argument: \'(\w+)\'",
352
+ msg,
353
+ )[0]
354
+ except (ValueError, IndexError): # pragma: no cover
355
+ # Just in case there is a different error than can occur that I have overlooked
356
+ raise ex
357
+ else:
358
+ raise MissingSettingsError(
359
+ tr("no_initial_value_3", cls_name, subclass_name, var_name)
360
+ ) from ex
361
+
362
+ setattr(self, member_name, subclass_instance)
363
+ for var_name, var_type in subclass_instance.__annotations__.items():
364
+ var_value = getattr(subclass_instance, var_name)
365
+ dynamically_set_val = _auto_cast_type(
366
+ var_type, var_value, force_random=True
367
+ )
368
+ setattr(subclass_instance, var_name, dynamically_set_val)
369
+
370
+
371
+ def _class_name_without_path(typ) -> str:
372
+ if issubclass(typ, _RandomType):
373
+ return f"{typ.__name__}[{','.join([str(a) for a in typ.__args__])}]"
374
+ elif issubclass(typ, _TypeWrapper):
375
+ return f"{typ.__name__}[{_class_name_without_path(typ.__args__[0])}]"
376
+
377
+ return f"{typ.__name__}"
378
+
379
+
380
+ def __subrepr__(self) -> str:
381
+ subclass_instance = self
382
+ subclass_contents = ""
383
+ for variable_name, variable_type in subclass_instance.__annotations__.items():
384
+ variable_value = getattr(subclass_instance, variable_name)
385
+ vartype_str = _class_name_without_path(variable_type)
386
+ subclass_contents += f"\n\t{variable_name}: <{vartype_str}> = {variable_value}" # ({type(variable_value)})"
387
+
388
+ return subclass_contents
389
+
390
+
391
+ def __repr__(self) -> str:
392
+ concated_str = f"SettingsClass [{self.__class__.__name__}]:\n"
393
+ for subclass_name in self.__dir__():
394
+ if not _is_hidden_variable(self, subclass_name):
395
+ subclass_instance = getattr(self, subclass_name)
396
+
397
+ subclass_contents = __subrepr__(subclass_instance)
398
+ concated_str += f"{subclass_name}: {subclass_contents}\n "
399
+ # TODO finish after shadowing with instances
400
+ return concated_str
401
+
402
+
403
+ def _encrypt_field(
404
+ message, encryption_key: tuple[Callable, Callable] | str, salt: bytes = None
405
+ ):
406
+ """Encrypts the field choosing a method depending on the encryption key"""
407
+ if isinstance(encryption_key, tuple):
408
+ message = encryption_key[0](message)
409
+ elif encryption_key is None or isinstance(encryption_key, str):
410
+ message = encrypt_message(message, encryption_key, salt)
411
+ else:
412
+ raise NotImplementedError(
413
+ tr("invalid_encryption_key_type_1", type(encryption_key))
414
+ )
415
+ return message
416
+
417
+
418
+ def _decrypt_field(
419
+ message, encryption_key: tuple[Callable, Callable] | str, salt: bytes = None
420
+ ):
421
+ """Decrypts the field choosing a method depending on the encryption key"""
422
+ if isinstance(encryption_key, tuple):
423
+ message = encryption_key[1](message)
424
+ elif encryption_key is None or isinstance(encryption_key, str):
425
+ message = decrypt_message(message, encryption_key, salt)
426
+ else:
427
+ raise NotImplementedError(
428
+ tr("invalid_encryption_key_type_1", type(encryption_key))
429
+ )
430
+ return message
431
+
432
+
433
+ def _safe_decrypt_field(
434
+ message: str,
435
+ parameter_name_debug: str,
436
+ encryption_key: tuple[Callable, Callable] | str,
437
+ salt: bytes,
438
+ ) -> str:
439
+ """Decrypts the target fields while only logging ValueError, UnicodeDecodeError exceptions"""
440
+ true_val = message[len(ENC_PREFIX) :]
441
+ try:
442
+ # Both parts can throws\w ValuError, the user-facing message is the same
443
+ true_val = bytes.fromhex(true_val)
444
+ message = _decrypt_field(true_val, encryption_key, salt)
445
+ except (ValueError, UnicodeDecodeError):
446
+ logger.error(tr("could_not_decode_string_1", parameter_name_debug))
447
+ message = ""
448
+ return message
449
+
450
+
451
+ def update_config(self, config: configparser.ConfigParser) -> None:
452
+ """Updates the provided config file with the values inside current instance
453
+ The Hidden type variables are not added.
454
+ Encryption is performed for members of type Encrypted
455
+
456
+ Args:
457
+ config (configparser.ConfigParser): target configuration object
458
+ """
459
+
460
+ for section_name in self.__dir__():
461
+ if not _is_hidden_variable(self, section_name):
462
+ section_instance = getattr(self, section_name)
463
+ if section_name not in config:
464
+ config[section_name] = {}
465
+ for var_name in section_instance.__dir__():
466
+ if var_name[:1] != "_":
467
+ var_value = getattr(section_instance, var_name)
468
+ var_type = section_instance.__annotations__[var_name]
469
+ is_wrapper_type = isinstance(var_type, GenericAlias)
470
+ # 乱数等がGenericAliasを使っています
471
+ # __origin__ はGenericAliasのみ
472
+ if not is_wrapper_type or (
473
+ is_wrapper_type and not issubclass(var_type.__origin__, Hidden)
474
+ ):
475
+ if var_name in config[section_name]:
476
+ config_val = config[section_name][var_name]
477
+ else:
478
+ config_val = ""
479
+
480
+ if var_value and issubclass(var_type, _Encrypted):
481
+ current_unencrypted_value = None
482
+ if _is_encoded(config_val):
483
+ current_unencrypted_value = _safe_decrypt_field(
484
+ config_val,
485
+ var_name,
486
+ self._encryption_key,
487
+ self._salt,
488
+ )
489
+ if (not current_unencrypted_value) or str(
490
+ current_unencrypted_value
491
+ ) != str(var_value):
492
+ var_value = _encrypt_field(
493
+ var_value, self._encryption_key, self._salt
494
+ )
495
+ var_value = f"{ENC_PREFIX}{var_value.hex()}"
496
+ else:
497
+ var_value = config_val
498
+
499
+ if (
500
+ var_value
501
+ and issubclass(var_type, _RandomType)
502
+ and var_type.__base__ is float
503
+ ):
504
+ precision = (
505
+ DEFAULT_FLOAT_PRECISION
506
+ if len(var_type.__args__) < 3
507
+ else var_type.__args__[2]
508
+ )
509
+ var_value = f"{var_value:{precision}}"
510
+ config[section_name][var_name] = str(
511
+ var_value
512
+ ) # windowsのconfigクラスは文字列のみ
513
+
514
+
515
+ def update_from(
516
+ self, config: configparser.ConfigParser, encrypted_only: False
517
+ ) -> list[str]:
518
+ """Overwrites current values with values contained in the specified config object
519
+
520
+ Args:
521
+ config (configparser.ConfigParser): ConfigParser object.
522
+ User is responsible for population, case-sensitivity settings etc.
523
+ encrypted_only (False): Only copies values that are of type Encrypted
524
+
525
+ Returns:
526
+ list[str]: list of values that should have been encrypted in the ConfigParser object
527
+ but were not
528
+ """
529
+
530
+ config_file_sections_remaining = [
531
+ section for section in config if section != "DEFAULT"
532
+ ]
533
+
534
+ if encrypted_only:
535
+ raise NotImplementedError() # TODO
536
+
537
+ # 期待のクラスを反復処理する
538
+ # 例:[('Program', <class ...Settings.Program'), ('GPT', <class ...), ...]
539
+ need_encryption = []
540
+ for section_name, section_class in self.__class__.__dict__.copy().items():
541
+ # dunder(例:'__doc__')等を除く
542
+ if section_name[:1] != "_" and isinstance(section_class, type):
543
+ # まずはサブクラスを設定する(「Program」等)
544
+ if section_name in config:
545
+ need_enc_subset = _set_members(
546
+ self,
547
+ config[section_name],
548
+ section_class,
549
+ getattr(self, section_name),
550
+ )
551
+ need_encryption += [
552
+ f"{section_name}/{varname}" for varname in need_enc_subset
553
+ ]
554
+ config_file_sections_remaining.remove(section_name)
555
+ else:
556
+ logger.warning(tr("missing_config_section_1", section_name))
557
+
558
+ if config_file_sections_remaining:
559
+ logger.warning(tr("extra_config_sections_1", config_file_sections_remaining))
560
+
561
+ return need_encryption
562
+
563
+
564
+ def save_to_file(self, path=None):
565
+ """Generates a config file to the specified path.
566
+ If the file does not exist, both parent folders and file will be generated
567
+ """
568
+ if path and path != self._file_path:
569
+ config = configparser.ConfigParser(allow_no_value=True)
570
+ else:
571
+ config = self._config
572
+ return self._save_to_file(path or self._file_path, config)
573
+
574
+
575
+ def _save_to_file(self, path, config):
576
+ self.update_config(config)
577
+ parent_dir = os.path.dirname(path)
578
+ if parent_dir and not os.path.exists(parent_dir):
579
+ os.makedirs(parent_dir)
580
+ with open(path, "w", encoding="utf-8") as config_file:
581
+ config.write(config_file)
582
+
583
+
584
+ def warn_confusing_types(value: Any, section_name: str, parameter_name: str) -> None:
585
+ """Checks for types that could be a user input mistake.
586
+ Currently checked types:
587
+ bool:
588
+ if the value is "true", "False", "FALSE" etc, but is marked as :str
589
+ int, float:
590
+ the value can be cast to int of float, but is marked as :str
591
+
592
+ Args:
593
+ value (Any): The value of the option
594
+ section_name (str): name of the section in the config file
595
+ parameter_name (str): name of the option inside the config section
596
+ """
597
+ if isinstance(value, str):
598
+ if value.upper() in ("TRUE", "FALSE"):
599
+ logger.warning(
600
+ tr(
601
+ "param_type_is_string_but_looks_x_4",
602
+ section_name,
603
+ parameter_name,
604
+ value,
605
+ "bool",
606
+ )
607
+ )
608
+ else:
609
+ for typ in (int, float):
610
+ try:
611
+ typ(value)
612
+ except ValueError:
613
+ pass
614
+ else:
615
+ logger.warning(
616
+ tr(
617
+ "param_type_is_string_but_looks_x_4",
618
+ section_name,
619
+ parameter_name,
620
+ value,
621
+ typ.__name__,
622
+ )
623
+ )
624
+ return
625
+
626
+
627
+ def _load_settings_init(
628
+ self,
629
+ path: str = "config.ini",
630
+ secret_config_path="/run/secrets/flask_settings",
631
+ case_sensitive: bool = False,
632
+ ) -> None:
633
+ """Loads the settings values after the @dataclass initialization has already taken place
634
+
635
+ Args:
636
+ path (str, optional): The file path to read from and write to. Defaults to "config.ini".
637
+ secret_config_path (str, optional): If this file exists, it will be used instead of path.
638
+ See https://docs.docker.com/engine/swarm/secrets/
639
+ Defaults to "/run/secrets/flask_settings".
640
+ case_sensitive (bool, optional): If true section and variable names will be case sensitive
641
+ Defaults to False.
642
+
643
+ Raises:
644
+ IsADirectoryError: If the target is a folder AND there is a [config.ini] folder inside
645
+
646
+ """
647
+
648
+ # flow: <file exists?> YES -> [read file (Config)] -> [Enforce Types (w/ warnings)] -> [Convert to Settings]
649
+ # NO -> [init values (Config)] -> [Enforce Types] (w/out warnings)]-> [Convert to Settings]
650
+
651
+ # メモリーのみの設定
652
+ if not path:
653
+ return
654
+
655
+ if secret_config_path and os.path.exists(secret_config_path):
656
+ path = secret_config_path
657
+
658
+ config = configparser.ConfigParser(allow_no_value=True)
659
+
660
+ # 大文字小文字区別
661
+ if case_sensitive:
662
+ config.optionxform = str
663
+
664
+ # 指定されたパスがフォルダーの場合は、ユーザーの意思はおそらくその中に生成することだろう
665
+ if os.path.exists(path) and os.path.isdir(path):
666
+ path = os.path.join(path, "config.ini")
667
+
668
+ # 通常、returnやraise後のelseは紛らしいが、この場合ある方が意思を伝える
669
+ # pylint: disable=R1705
670
+ need_encryption = []
671
+ if path_exists := os.path.exists(path):
672
+ if os.path.isfile(path):
673
+ # 通常に存在する場合
674
+ logger.debug(tr("loading_settings_from_1", path))
675
+ config.read(path, encoding="utf-8")
676
+ need_encryption = self.update_from(config, encrypted_only=False)
677
+ else:
678
+ # config.iniはフォルダーである可能性はある
679
+ raise IsADirectoryError(tr("file_is_folder_1", path))
680
+
681
+ if need_encryption:
682
+ logger.info(tr("unencrypted_data_found_1", need_encryption))
683
+ if need_encryption or not path_exists:
684
+ # 存在しない場合は初期化し、初期化された値を返す
685
+ logger.debug(tr("initing_settings_1", path))
686
+ self._save_to_file(path, config)
687
+ self.update_from_env()
688
+
689
+ self._file_path = path
690
+ self._config = config
691
+
692
+
693
+ # %%
694
+
695
+
696
+ def _set_param_from_env(
697
+ section_class, section_name, parameter_name, parameter_type, env_prefix
698
+ ):
699
+ env_prefix = env_prefix.upper()
700
+ environmental_key = f"{env_prefix}{env_prefix and '_'}{section_name.upper()}_{parameter_name.upper()}"
701
+ used_keys = []
702
+
703
+ if environmental_key in os.environ:
704
+ env_value = os.environ[environmental_key]
705
+ env_value = _auto_cast_type(parameter_type, env_value)
706
+ setattr(section_class, parameter_name, env_value)
707
+
708
+ return used_keys
709
+
710
+
711
+ def update_from_env(self) -> None:
712
+ """環境変数から設定変更を取得する。存在しない場合は設定ファイルから抽出する。
713
+
714
+ Args:
715
+ config_section (configparser.SectionProxy): configparserの項目
716
+ parameter_name (str): パラメーター名
717
+ Defaults to None.
718
+
719
+ Returns:
720
+ str: 変数の値
721
+ """
722
+ if self._env_prefix is None:
723
+ return
724
+
725
+ used_keys = []
726
+ for section_name, section_instance in self.__dict__.items():
727
+ if section_name[:1] != "_":
728
+ # for parameter_name in section_class.__dict__.copy().keys():
729
+ for parameter_name, var_type in section_instance.__annotations__.items():
730
+ if parameter_name[:1] != "_":
731
+ used_keys += _set_param_from_env(
732
+ section_instance,
733
+ section_name,
734
+ parameter_name,
735
+ var_type,
736
+ self._env_prefix,
737
+ )
738
+
739
+ if used_keys:
740
+ logger.info(tr("using_key_var_from_env_1", used_keys))
741
+
742
+
743
+ # pylint: disable=C2801
744
+ def _set_members(
745
+ self,
746
+ config_section: configparser.SectionProxy,
747
+ section_class: type,
748
+ section_inst: Any,
749
+ ):
750
+ # section(項目)をインスタンスにする。クラスだと静的となる
751
+ # 使用されていないパラメーターを見つかるため
752
+ config_section_parameter_names = list(config_section)
753
+
754
+ need_encryption = []
755
+ # 例:[('lang','ja'),'verbosity','DEBUG')...]
756
+ for (
757
+ parameter_name,
758
+ default_parameter_value,
759
+ ) in section_class.__dict__.copy().items():
760
+ # __dict__, __eq__等をのぞく
761
+ if parameter_name[:1] != "_":
762
+ # コンフィグファイルから読み込んだ値
763
+
764
+ if parameter_name not in section_class.__annotations__:
765
+ section_class.__annotations__[parameter_name] = type(
766
+ default_parameter_value
767
+ )
768
+ logger.warning(tr("parameter_type_missing_1", parameter_name))
769
+
770
+ expected_type: type = (
771
+ section_class.__annotations__[parameter_name]
772
+ if parameter_name in section_class.__annotations__
773
+ else type(default_parameter_value)
774
+ )
775
+ if parameter_name in config_section:
776
+ # 使用されていないパラメーターから削除
777
+ config_section_parameter_names.remove(parameter_name)
778
+ config_param_value = config_section[parameter_name]
779
+
780
+ # 例:{'mock_model': bool}
781
+ if parameter_name in section_class.__annotations__:
782
+ # 設定クラスの型ヒントにに合わせてみる
783
+
784
+ if issubclass(expected_type, _Encrypted):
785
+ if _is_encoded(config_param_value):
786
+ config_param_value = _safe_decrypt_field(
787
+ config_param_value,
788
+ parameter_name,
789
+ self._encryption_key,
790
+ self._salt,
791
+ )
792
+
793
+ elif config_param_value:
794
+ need_encryption.append(parameter_name)
795
+
796
+ try:
797
+ param_value_after_cast = _auto_cast_type(
798
+ expected_type, config_param_value
799
+ )
800
+
801
+ # キャストが失敗したらValueErrorになる
802
+ except ValueError:
803
+ logger.warning(
804
+ tr(
805
+ "invalid_type_4",
806
+ config_section.name,
807
+ parameter_name,
808
+ user_friendly_type_name(expected_type),
809
+ config_param_value,
810
+ )
811
+ )
812
+ # 失敗したらデフォルト値にする
813
+
814
+ param_value_after_cast = _default_value_for_type(
815
+ expected_type, default_parameter_value
816
+ )
817
+
818
+ else:
819
+ #  ヒントがない場合はそのままに残る(str)
820
+ param_value_after_cast = config_param_value
821
+
822
+ section_inst.__setattr__(parameter_name, param_value_after_cast)
823
+ warn_confusing_types(
824
+ param_value_after_cast, config_section.name, parameter_name
825
+ )
826
+
827
+ elif not issubclass(expected_type, _Hidden):
828
+ config_param_value = section_class.__dict__[parameter_name]
829
+ logger.warning(
830
+ tr("config_param_missing_2", config_section.name, parameter_name)
831
+ )
832
+
833
+ # 使用されていないパラメーター確認
834
+ if config_section_parameter_names:
835
+ logger.warning(
836
+ tr(
837
+ "extra_config_parameter_2",
838
+ config_section.name,
839
+ config_section_parameter_names,
840
+ )
841
+ )
842
+ return need_encryption
843
+
844
+
845
+ # %%
846
+ def _add_settings_layer(
847
+ cls: ClassType,
848
+ env_prefix: str = "",
849
+ common_encryption_key: type[str | tuple[Callable[[Any], str]] | None] = None,
850
+ salt: bytes = None,
851
+ ) -> ClassType:
852
+ """Dynamically binds the necessary functions after applying the @dataclass decorator.
853
+
854
+ Args:
855
+ env_prefix (str, optional): prefix to be attached when checking env vars.
856
+ e.g. if set to foo, in case of config.ui.color the checked name is FOO_UI_COLOR
857
+ No check is performed if set to None.
858
+ Defaults to "", in which case only the section and var names are used,
859
+ e.g config.ui.color -> UI_COLOR
860
+ common_encryption_key (type[str | tuple[Callable[[Any], str]] | None], optional):
861
+ Key used to encrypt values. If no key is set when using the class constructor,
862
+ this value is used. If you do not wish to use the default encryption method,
863
+ you can also defy a tuple of encryption and decryption functions.
864
+ Defaults to None.
865
+ salt (bytes, optional): The salt that is used in combination with the key.
866
+ By default, a random file is generated on the machine, but can be set manually.
867
+ Defaults to None.
868
+
869
+ Returns:
870
+ ClassType: Returns the decorated class
871
+ """
872
+
873
+ for subclass_name, subclass_proper in cls.__dict__.items():
874
+ if not _is_hidden_variable(cls, subclass_name):
875
+ setattr(cls, subclass_name, dataclass(getattr(cls, subclass_name)))
876
+
877
+ def repr_wrapper(_self, subclass_name=subclass_name):
878
+ return f"{cls.__name__} section: [{subclass_name}]{__subrepr__(_self)}"
879
+
880
+ subclass_proper.__repr__ = repr_wrapper
881
+
882
+ for extra_func in [
883
+ __post_init__,
884
+ __repr__,
885
+ update_config,
886
+ update_from,
887
+ update_from_env,
888
+ save_to_file,
889
+ _save_to_file,
890
+ ]:
891
+ # _set_qualname(cls, extra_func.__name__)
892
+ extra_func.__qualname__ = f"{cls.__qualname__}.{extra_func.__name__}" # TODO is this necessary? Interferes with inheritance?
893
+ setattr(cls, extra_func.__name__, extra_func)
894
+
895
+ # add wrapper for easier access
896
+ for static_func in (available_languages, set_language):
897
+ setattr(cls, static_func.__name__, staticmethod(static_func))
898
+
899
+ cls = dataclass(cls)
900
+
901
+ # obj = cls()
902
+ # cls.__new__ = lambda *args, **kwargs: load_settings_self(*args, **kwargs)
903
+
904
+ def double_init(
905
+ self,
906
+ *args,
907
+ env_prefix: str = env_prefix,
908
+ encryption_key: type[str | Callable[[Any], str] | None] = None,
909
+ **kwargs,
910
+ ):
911
+ if common_encryption_key and not encryption_key: # local has priority
912
+ encryption_key = common_encryption_key
913
+ self._encryption_key = encryption_key
914
+ self._salt = salt
915
+ self._env_prefix = env_prefix
916
+ self.original_init()
917
+ _load_settings_init(self, *args, **kwargs)
918
+
919
+ cls.original_init = cls.__init__
920
+ # cls.additional_init = load_settings
921
+ cls.__init__ = double_init
922
+
923
+ return cls
924
+
925
+
926
+ # placeholder for the wrapper
927
+ def settingsclass(
928
+ cls=None,
929
+ /,
930
+ *,
931
+ env_prefix="",
932
+ encryption_key: type[str | Callable[[Any], str] | None] = None,
933
+ _salt: bytes = None,
934
+ ):
935
+ """Dynamically binds the necessary functions after applying the @dataclass decorator.
936
+
937
+ Args:
938
+ env_prefix (str, optional): prefix to be attached when checking env vars.
939
+ e.g. if set to foo, in case of config.ui.color the checked name is FOO_UI_COLOR
940
+ No check is performed if set to None.
941
+ Defaults to "", in which case only the section and var names are used,
942
+ e.g config.ui.color -> UI_COLOR
943
+ encryption_key (type[str | Callable[[Any], str] | None], optional):
944
+ Key used to encrypt values. Takes priority over decorator setting
945
+ this value is used.If you do not wish to use the default encryption method,
946
+ you can also defy a tuple of encryption and decryption functions.
947
+ Defaults to None.
948
+ _salt (bytes, optional): The salt that is used in combination with the key.
949
+ By default, a random file is generated on the machine, but can be set manually.
950
+ Defaults to None.
951
+
952
+ Returns:
953
+ ClassType: Returns the decorated class
954
+ """
955
+
956
+ # ↓ copied from dataclass
957
+ def wrap(cls):
958
+ return _add_settings_layer(
959
+ cls,
960
+ env_prefix,
961
+ common_encryption_key=encryption_key,
962
+ salt=_salt,
963
+ )
964
+
965
+ # See if we're being called as @dataclass or @dataclass().
966
+ if cls is None:
967
+ # We're called with parens.
968
+ return wrap
969
+
970
+ # We're called as @dataclass without parens.
971
+ return wrap(cls)
972
+
973
+
974
+ # %%
975
+
976
+ # TODO add option to enforce limits for Random Types
977
+ # TODO add option to be silent - How?
978
+ # TODO add option to silence type confusion warnings
979
+ # TODO Having the options for JSON etc. would be nice
980
+
981
+
982
+ if __name__ == "__main__": # pragma: no cover
983
+ # Only for quick testing during development
984
+ @settingsclass # (encryption_key="123456789")
985
+ class _Settings:
986
+ """設定ファイルを初期化するときに使われる値。ロードするときにタイプヒントを確認する。
987
+ RandomString・RandomInt・RandomFloatの初期値値は無視される"""
988
+
989
+ # file_path = '<NOT_SET>'
990
+
991
+ class program:
992
+ lang = "ja"
993
+ log_level: str = "debug"
994
+ colored_console_output: Hidden[bool] = True # ログをカラー表示するか
995
+ machine_id: RandomString[5] = ""
996
+ auto_update_model: bool = False
997
+ rfph: RandomFloat[0, 2**16] = 1.2
998
+ seed: Encrypted[RandomFloat[0, 2**16]] = 1.2
999
+ api_id: RandomInt[1000, 9999] = 0
1000
+
1001
+ class gpt:
1002
+ api_key: Encrypted[str] = ""
1003
+ backup_pin: Encrypted[int] = -1
1004
+ model: str = "gpt-3.5-turbo" # GPTモデルの識別文字列
1005
+ temperature: Hidden[float] = 5
1006
+ timeout = 300
1007
+
1008
+ conf = _Settings("cx.ini")
1009
+ # conf = _Settings(f"conf/{RandomString(5)}.ini")
1010
+ print(conf)
1011
+
1012
+ # %%