From 878bf34d525d34d8cdf5e3d73dbd84c0f1150e83 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rn-Michael=20Miehe?= Date: Wed, 22 Nov 2023 14:43:43 +0100 Subject: [PATCH] TransformedString into own file --- api/advent22_api/core/config.py | 86 +----------------- api/advent22_api/core/transformed_string.py | 96 +++++++++++++++++++++ 2 files changed, 97 insertions(+), 85 deletions(-) create mode 100644 api/advent22_api/core/transformed_string.py diff --git a/api/advent22_api/core/config.py b/api/advent22_api/core/config.py index 97b5461..39cebb0 100644 --- a/api/advent22_api/core/config.py +++ b/api/advent22_api/core/config.py @@ -1,13 +1,11 @@ -import re import tomllib -from enum import Enum -from random import Random from markdown import markdown from pydantic import BaseModel, ConfigDict, field_validator from .dav.webdav import WebDAV from .settings import SETTINGS +from .transformed_string import TransformedString class User(BaseModel): @@ -15,88 +13,6 @@ class User(BaseModel): password: str -class TransformedString(BaseModel): - class __Whitespace(str, Enum): - # unverändert - KEEP = "KEEP" - - # Leerzeichen an Anfang und Ende entfernen - STRIP = "STRIP" - - # whitespace durch Leerzeichen ersetzen - SPACE = "SPACE" - - # whitespace entfernen - REMOVE = "REMOVE" - - class __SpecialChars(str, Enum): - # unverändert - KEEP = "KEEP" - - # Sonderzeichen entfernen - REMOVE = "REMOVE" - - class __Case(str, Enum): - # unverändert - KEEP = "KEEP" - - # GROSSBUCHSTABEN - UPPER = "UPPER" - - # kleinbuchstaben - LOWER = "LOWER" - - # ZuFÄllIg - RANDOM = "RANDOM" - - value: str - - whitespace: __Whitespace = __Whitespace.REMOVE - special_chars: __SpecialChars = __SpecialChars.REMOVE - case: __Case = __Case.UPPER - - @field_validator("whitespace", "case", mode="before") - def transform_from_str(cls, v) -> str: - return str(v).upper() - - @property - def clean(self) -> str: - result = self.value - - # Whitespace verarbeiten - if self.whitespace is TransformedString.__Whitespace.STRIP: - result = result.strip() - - elif self.whitespace is TransformedString.__Whitespace.SPACE: - result = re.sub(string=result, pattern=r"\s+", repl=" ") - - elif self.whitespace is TransformedString.__Whitespace.REMOVE: - result = re.sub(string=result, pattern=r"\s+", repl="") - - # Sonderzeichen verarbeiten - if self.special_chars is TransformedString.__SpecialChars.REMOVE: - result = re.sub(string=result, pattern=r"[^a-zA-Z0-9\s]+", repl="") - - # Groß-/Kleinschreibung verarbeiten - if self.case is TransformedString.__Case.UPPER: - result = result.upper() - - elif self.case is TransformedString.__Case.LOWER: - result = result.lower() - - elif self.case is TransformedString.__Case.RANDOM: - rnd = Random(self.value) - - def randomcase(c: str) -> str: - if rnd.choice((True, False)): - return c.upper() - return c.lower() - - result = "".join(randomcase(c) for c in result) - - return result - - class Site(BaseModel): model_config = ConfigDict(validate_default=True) diff --git a/api/advent22_api/core/transformed_string.py b/api/advent22_api/core/transformed_string.py new file mode 100644 index 0000000..3099abb --- /dev/null +++ b/api/advent22_api/core/transformed_string.py @@ -0,0 +1,96 @@ +import re +from enum import Enum +from random import Random + +from pydantic import BaseModel, field_validator + +_RE_WHITESPACE = re.compile( + pattern=r"\s+", + flags=re.UNICODE | re.IGNORECASE, +) +_RE_SPECIAL_CHARS = re.compile( + pattern=r"[^a-zA-Z0-9\s]+", + flags=re.UNICODE | re.IGNORECASE, +) + + +class TransformedString(BaseModel): + class __Whitespace(str, Enum): + # unverändert + KEEP = "KEEP" + + # Leerzeichen an Anfang und Ende entfernen + STRIP = "STRIP" + + # whitespace durch Leerzeichen ersetzen + SPACE = "SPACE" + + # whitespace entfernen + REMOVE = "REMOVE" + + class __SpecialChars(str, Enum): + # unverändert + KEEP = "KEEP" + + # Sonderzeichen entfernen + REMOVE = "REMOVE" + + class __Case(str, Enum): + # unverändert + KEEP = "KEEP" + + # GROSSBUCHSTABEN + UPPER = "UPPER" + + # kleinbuchstaben + LOWER = "LOWER" + + # ZuFÄllIg + RANDOM = "RANDOM" + + value: str + + whitespace: __Whitespace = __Whitespace.REMOVE + special_chars: __SpecialChars = __SpecialChars.REMOVE + case: __Case = __Case.UPPER + + @field_validator("whitespace", "case", mode="before") + def transform_from_str(cls, v) -> str: + return str(v).upper() + + @property + def clean(self) -> str: + result = self.value + + # Whitespace verarbeiten + if self.whitespace is TransformedString.__Whitespace.STRIP: + result = result.strip() + + elif self.whitespace is TransformedString.__Whitespace.SPACE: + result = _RE_WHITESPACE.sub(string=result, repl=" ") + + elif self.whitespace is TransformedString.__Whitespace.REMOVE: + result = _RE_WHITESPACE.sub(string=result, repl="") + + # Sonderzeichen verarbeiten + if self.special_chars is TransformedString.__SpecialChars.REMOVE: + result = _RE_SPECIAL_CHARS.sub(string=result, repl="") + + # Groß-/Kleinschreibung verarbeiten + if self.case is TransformedString.__Case.UPPER: + result = result.upper() + + elif self.case is TransformedString.__Case.LOWER: + result = result.lower() + + elif self.case is TransformedString.__Case.RANDOM: + rnd = Random(self.value) + + def randomcase(c: str) -> str: + if rnd.choice((True, False)): + return c.upper() + return c.lower() + + result = "".join(randomcase(c) for c in result) + + return result