diff --git a/src/vigenere_api/models/caesar.py b/src/vigenere_api/models/caesar.py index 330b04a81dd95a078919856f789bcf6d9d9be79b..82cb12ab78f258c88be49352c48dbd583bb7f684 100644 --- a/src/vigenere_api/models/caesar.py +++ b/src/vigenere_api/models/caesar.py @@ -23,19 +23,21 @@ from typing import final, Union from pydantic import StrictInt, StrictStr, validator from vigenere_api.helpers import Model - from .errors import ( + AlgorithmExpectedKeyType, AlgorithmKeyTypeError, AlgorithmTextTypeError, - BadKeyError, ContentTypeError, EmptyContentError, +) +from .helpers import convert_key, move_char +from .helpers.errors import ( + BadKeyError, EmptyKeyError, + ExpectedKeyType, KeyTypeError, TooLongKeyError, ) -from .helpers import move_char - Key = Union[StrictInt, StrictStr] @@ -123,14 +125,15 @@ class CaesarData(Model): raise AlgorithmTextTypeError(text) if not isinstance(key, int): - raise AlgorithmKeyTypeError(key) + raise AlgorithmKeyTypeError(key, AlgorithmExpectedKeyType.INTEGER) result = "" for char in text: - if char.isupper(): - result += move_char(char, key, "A") - elif char.islower(): - result += move_char(char, key, "a") + if char.isalpha(): + if char.isupper(): + result += move_char(char, key, "A") + else: + result += move_char(char, key, "a") else: result += char @@ -148,8 +151,7 @@ class CaesarData(Model): if isinstance(self.key, int): return self.key - key = self.key - return ord(key) - ord("A") if key.isupper() else ord(key) - ord("a") + return convert_key(self.key) @validator("content", pre=True) def validate_content(cls, content: str) -> str: @@ -208,7 +210,7 @@ class CaesarData(Model): Key """ if not isinstance(key, (str, int)): - raise KeyTypeError(key) + raise KeyTypeError(key, ExpectedKeyType.STRING_OR_INTEGER) if isinstance(key, str): if len(key) == 0: @@ -218,6 +220,6 @@ class CaesarData(Model): raise TooLongKeyError if not key.isalpha(): - raise BadKeyError(key) + raise BadKeyError(key, ExpectedKeyType.STRING_OR_INTEGER) return key diff --git a/src/vigenere_api/models/errors.py b/src/vigenere_api/models/errors.py index 5c211d5302bb3f0d69cf11d5d7a51d6b43e9edbb..76cf57bc84748840a71e770145fc73b130bb42af 100644 --- a/src/vigenere_api/models/errors.py +++ b/src/vigenere_api/models/errors.py @@ -14,10 +14,12 @@ # this program. If not, see <https://www.gnu.org/licenses/>. + # ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -"""All errors thrown by the caesar model.""" +"""All errors thrown by the caesar and vigenere model.""" +from enum import auto, unique from typing import Any, final +from strenum import LowercaseStrEnum from vigenere_api.helpers import VigenereAPITypeError @@ -37,22 +39,6 @@ class ContentTypeError(VigenereAPITypeError): super().__init__(content, "content", "a string") -@final -class KeyTypeError(VigenereAPITypeError): - """Thrown if the key is not a string and not an integer.""" - - def __init__(self, key: Any) -> None: - """ - Create a KeyTypeError with the key. - - Parameters - ---------- - key : Any - The received key. - """ - super().__init__(key, "key", "a string or an integer") - - @final class EmptyContentError(ValueError): """Thrown if the content is empty.""" @@ -64,47 +50,6 @@ class EmptyContentError(ValueError): ) -@final -class EmptyKeyError(ValueError): - """Thrown if the key is empty.""" - - def __init__(self) -> None: - """Create an EmptyKeyError.""" - super().__init__( - "The key is empty. Please give a one character string or an integer.", - ) - - -@final -class TooLongKeyError(ValueError): - """Thrown if the key is not an integer and not a one character string.""" - - def __init__(self) -> None: - """Create a TooLongKeyError.""" - super().__init__( - "The key is too long. Please give a one character string or an integer.", - ) - - -@final -class BadKeyError(ValueError): - """Thrown if the key is not an integer and not a one character string.""" - - def __init__(self, key: str) -> None: - """ - Create a BadKeyError with the key. - - Parameters - ---------- - key : str - The received key. - """ - super().__init__( - f"The key '{key}' is invalid." - + " Please give an alphabetic one character string or an integer.", - ) - - @final class AlgorithmTextTypeError(VigenereAPITypeError): """Thrown if the algorithm receives a bad type for the text variable.""" @@ -121,11 +66,21 @@ class AlgorithmTextTypeError(VigenereAPITypeError): super().__init__(text, "text variable", "a string") +@final +@unique +class AlgorithmExpectedKeyType(LowercaseStrEnum): + """The type of key, the algorithm needs it.""" + + STRING = auto() + INTEGER = auto() + VIGENERE_KEY = "VigenereKey" + + @final class AlgorithmKeyTypeError(VigenereAPITypeError): """Thrown if the algorithm receives a bad type for the key variable.""" - def __init__(self, key: Any) -> None: + def __init__(self, key: Any, expected_type: AlgorithmExpectedKeyType) -> None: """ Create an AlgorithmKeyTypeError with the key. @@ -133,5 +88,21 @@ class AlgorithmKeyTypeError(VigenereAPITypeError): ---------- key : Any The given key. + expected_type : AlgorithmExpectedKeyType + The expected type of the key. """ - super().__init__(key, "key variable", "an integer") + expected = f"a {expected_type}" + if expected_type == AlgorithmExpectedKeyType.INTEGER: + expected = f"an {expected_type}" + elif expected_type == AlgorithmExpectedKeyType.VIGENERE_KEY: + expected += " object" + + super().__init__(key, "key variable", expected) + + +@final +class AlgorithmOperationTypeError(VigenereAPITypeError): + """Thrown if the operation is not a VigenereOperation object.""" + + def __init__(self, operation: Any) -> None: + super().__init__(operation, "operation", "a VigenereOperation object") diff --git a/src/vigenere_api/models/helpers/errors.py b/src/vigenere_api/models/helpers/errors.py index 2c39a403fb22a69785f54889050a1f3b638cda10..569950e38b8ef5941b02e426116cba1c9b908f6f 100644 --- a/src/vigenere_api/models/helpers/errors.py +++ b/src/vigenere_api/models/helpers/errors.py @@ -15,11 +15,14 @@ # ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ """All errors thrown by the helper.""" - +from enum import unique from typing import Any, final +from strenum import StrEnum from vigenere_api.helpers import VigenereAPITypeError +A_STRING = "a string" + @final class HelperCharTypeError(VigenereAPITypeError): @@ -34,7 +37,7 @@ class HelperCharTypeError(VigenereAPITypeError): char : Any The received char. """ - super().__init__(char, "char variable", "a string") + super().__init__(char, "char variable", A_STRING) @final @@ -102,7 +105,7 @@ class HelperFirstLetterTypeError(VigenereAPITypeError): first_letter : Any The first letter. """ - super().__init__(first_letter, "first letter variable", "a string") + super().__init__(first_letter, "first letter variable", A_STRING) @final @@ -122,3 +125,81 @@ class HelperBadFirstLetterValueError(ValueError): f"The helper function receives a first letter equals to '{first_letter}'." + " Please give a string in {'a', 'A'}.", ) + + +@final +class EmptyKeyError(ValueError): + """Thrown if the key is empty.""" + + def __init__(self) -> None: + """Create an EmptyKeyError.""" + super().__init__( + "The key is empty. Please give a one character string or an integer.", + ) + + +@final +class TooShortKeyError(ValueError): + """Thrown if the key is a string of one character.""" + + def __init__(self) -> None: + """Create a TooLongKeyError.""" + super().__init__( + "The key is too short. Please give a string with more than one character.", + ) + + +@final +@unique +class ExpectedKeyType(StrEnum): + """The type of key, the function needs it.""" + + STRING = A_STRING + STRING_OR_INTEGER = "a string or an integer" + + +@final +class BadKeyError(ValueError): + """Thrown if the key is not an integer and not a one character string.""" + + def __init__(self, key: str, excepted_type: ExpectedKeyType) -> None: + """ + Create a BadKeyError with the key. + + Parameters + ---------- + key : str + The received key. + """ + super().__init__( + f"The key '{key}' is invalid." + f" Please give {excepted_type}.", + ) + + +@final +class KeyTypeError(VigenereAPITypeError): + """Thrown if the key is not a string and not an integer.""" + + def __init__(self, key: Any, expected_type: ExpectedKeyType) -> None: + """ + Create a KeyTypeError with the key. + + Parameters + ---------- + key : Any + The received key. + expected_type : ExpectedKeyType + The expected type of the key. + """ + super().__init__(key, "key", expected_type) + + +@final +class TooLongKeyError(ValueError): + """Thrown if the key is not an integer and not a one character string.""" + + def __init__(self) -> None: + """Create a TooLongKeyError.""" + super().__init__( + "The key is too long. Please give a one character string or an integer.", + )