Skip to content
Snippets Groups Projects
Commit e1a8818e authored by DAVID Axel's avatar DAVID Axel
Browse files

refactor: :truck: Move some errors in helpers/errors

parent 8c5211da
No related branches found
No related tags found
No related merge requests found
...@@ -23,19 +23,21 @@ from typing import final, Union ...@@ -23,19 +23,21 @@ from typing import final, Union
from pydantic import StrictInt, StrictStr, validator from pydantic import StrictInt, StrictStr, validator
from vigenere_api.helpers import Model from vigenere_api.helpers import Model
from .errors import ( from .errors import (
AlgorithmExpectedKeyType,
AlgorithmKeyTypeError, AlgorithmKeyTypeError,
AlgorithmTextTypeError, AlgorithmTextTypeError,
BadKeyError,
ContentTypeError, ContentTypeError,
EmptyContentError, EmptyContentError,
)
from .helpers import convert_key, move_char
from .helpers.errors import (
BadKeyError,
EmptyKeyError, EmptyKeyError,
ExpectedKeyType,
KeyTypeError, KeyTypeError,
TooLongKeyError, TooLongKeyError,
) )
from .helpers import move_char
Key = Union[StrictInt, StrictStr] Key = Union[StrictInt, StrictStr]
...@@ -123,13 +125,14 @@ class CaesarData(Model): ...@@ -123,13 +125,14 @@ class CaesarData(Model):
raise AlgorithmTextTypeError(text) raise AlgorithmTextTypeError(text)
if not isinstance(key, int): if not isinstance(key, int):
raise AlgorithmKeyTypeError(key) raise AlgorithmKeyTypeError(key, AlgorithmExpectedKeyType.INTEGER)
result = "" result = ""
for char in text: for char in text:
if char.isalpha():
if char.isupper(): if char.isupper():
result += move_char(char, key, "A") result += move_char(char, key, "A")
elif char.islower(): else:
result += move_char(char, key, "a") result += move_char(char, key, "a")
else: else:
result += char result += char
...@@ -148,8 +151,7 @@ class CaesarData(Model): ...@@ -148,8 +151,7 @@ class CaesarData(Model):
if isinstance(self.key, int): if isinstance(self.key, int):
return self.key return self.key
key = self.key return convert_key(self.key)
return ord(key) - ord("A") if key.isupper() else ord(key) - ord("a")
@validator("content", pre=True) @validator("content", pre=True)
def validate_content(cls, content: str) -> str: def validate_content(cls, content: str) -> str:
...@@ -208,7 +210,7 @@ class CaesarData(Model): ...@@ -208,7 +210,7 @@ class CaesarData(Model):
Key Key
""" """
if not isinstance(key, (str, int)): if not isinstance(key, (str, int)):
raise KeyTypeError(key) raise KeyTypeError(key, ExpectedKeyType.STRING_OR_INTEGER)
if isinstance(key, str): if isinstance(key, str):
if len(key) == 0: if len(key) == 0:
...@@ -218,6 +220,6 @@ class CaesarData(Model): ...@@ -218,6 +220,6 @@ class CaesarData(Model):
raise TooLongKeyError raise TooLongKeyError
if not key.isalpha(): if not key.isalpha():
raise BadKeyError(key) raise BadKeyError(key, ExpectedKeyType.STRING_OR_INTEGER)
return key return key
...@@ -14,10 +14,12 @@ ...@@ -14,10 +14,12 @@
# this program. If not, see <https://www.gnu.org/licenses/>. + # 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 typing import Any, final
from strenum import LowercaseStrEnum
from vigenere_api.helpers import VigenereAPITypeError from vigenere_api.helpers import VigenereAPITypeError
...@@ -37,22 +39,6 @@ class ContentTypeError(VigenereAPITypeError): ...@@ -37,22 +39,6 @@ class ContentTypeError(VigenereAPITypeError):
super().__init__(content, "content", "a string") 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 @final
class EmptyContentError(ValueError): class EmptyContentError(ValueError):
"""Thrown if the content is empty.""" """Thrown if the content is empty."""
...@@ -64,47 +50,6 @@ class EmptyContentError(ValueError): ...@@ -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 @final
class AlgorithmTextTypeError(VigenereAPITypeError): class AlgorithmTextTypeError(VigenereAPITypeError):
"""Thrown if the algorithm receives a bad type for the text variable.""" """Thrown if the algorithm receives a bad type for the text variable."""
...@@ -121,11 +66,21 @@ class AlgorithmTextTypeError(VigenereAPITypeError): ...@@ -121,11 +66,21 @@ class AlgorithmTextTypeError(VigenereAPITypeError):
super().__init__(text, "text variable", "a string") 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 @final
class AlgorithmKeyTypeError(VigenereAPITypeError): class AlgorithmKeyTypeError(VigenereAPITypeError):
"""Thrown if the algorithm receives a bad type for the key variable.""" """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. Create an AlgorithmKeyTypeError with the key.
...@@ -133,5 +88,21 @@ class AlgorithmKeyTypeError(VigenereAPITypeError): ...@@ -133,5 +88,21 @@ class AlgorithmKeyTypeError(VigenereAPITypeError):
---------- ----------
key : Any key : Any
The given key. 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")
...@@ -15,11 +15,14 @@ ...@@ -15,11 +15,14 @@
# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ # ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
"""All errors thrown by the helper.""" """All errors thrown by the helper."""
from enum import unique
from typing import Any, final from typing import Any, final
from strenum import StrEnum
from vigenere_api.helpers import VigenereAPITypeError from vigenere_api.helpers import VigenereAPITypeError
A_STRING = "a string"
@final @final
class HelperCharTypeError(VigenereAPITypeError): class HelperCharTypeError(VigenereAPITypeError):
...@@ -34,7 +37,7 @@ class HelperCharTypeError(VigenereAPITypeError): ...@@ -34,7 +37,7 @@ class HelperCharTypeError(VigenereAPITypeError):
char : Any char : Any
The received char. The received char.
""" """
super().__init__(char, "char variable", "a string") super().__init__(char, "char variable", A_STRING)
@final @final
...@@ -102,7 +105,7 @@ class HelperFirstLetterTypeError(VigenereAPITypeError): ...@@ -102,7 +105,7 @@ class HelperFirstLetterTypeError(VigenereAPITypeError):
first_letter : Any first_letter : Any
The first letter. The first letter.
""" """
super().__init__(first_letter, "first letter variable", "a string") super().__init__(first_letter, "first letter variable", A_STRING)
@final @final
...@@ -122,3 +125,81 @@ class HelperBadFirstLetterValueError(ValueError): ...@@ -122,3 +125,81 @@ class HelperBadFirstLetterValueError(ValueError):
f"The helper function receives a first letter equals to '{first_letter}'." f"The helper function receives a first letter equals to '{first_letter}'."
+ " Please give a string in {'a', 'A'}.", + " 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.",
)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment