AnPv2/Python/Utils/Options.py
2026-05-01 10:12:58 +02:00

74 lines
2.0 KiB
Python

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from typing import Self, Optional
class Options:
OVERWRITING:int = 0
OVERWRITE:int = 1 * 3 ** OVERWRITING
NO_OVERWRITE:int = 2 * 3 ** OVERWRITING
NULLISH:int = 1
ALLOW_NULLS:int = 1 * 3 ** NULLISH
NOT_NULLS:int = 2 * 3 ** NULLISH
CHECKS:int = 2
ALLOW_CHECKS:int = 1 * 3 ** CHECKS
NO_CHECKS:int = 2 * 3 ** CHECKS
SHOWING_ERRORS:int = 3
SHOW_ERRORS:int = 1 * 3 ** SHOWING_ERRORS
HIDE_ERRORS:int = 2 * 3 ** SHOWING_ERRORS
DEFAULT:Self = Self(NO_OVERWRITE + ALLOW_NULLS)
@classmethod
def get_value(cls:type[Self], value:int, option:int) -> bool|None:
ternary:int = value // 3 ** option % 3
return (
True if ternary == 1 else
False if ternary == 2 else
None)
def __init__(self:Self, options:int = 0, default:Optional[Self] = None) -> None:
self.overwrite:bool|None
self.allow_null:bool|None
self.check_errors:bool|None
self.show_errors:bool|None
if default is None:
default = self.DEFAULT
for key, option in (
("overwrite", self.OVERWRITE),
("allow_null", self.NULLISH),
("check_errors", self.CHECKS),
("show_errors", self.SHOWING_ERRORS)
):
value:bool|None = self.get_value(options, option)
setattr(self, key, getattr(default, key) if value is None and default is not None else value)
def get(self:Self) -> int:
code:int = 0
option:int
value:bool|None
for option, value in (
(self.OVERWRITING, self.overwrite),
(self.NULLISH, self.allow_null),
(self.CHECKS, self.allow_checks),
(self.SHOWING_ERRORS, self.show_errors)
):
code += (
1 if value is True else
2 if value is False else
0) * 3 ** option
return code