-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathvalidators.py
109 lines (73 loc) · 2.66 KB
/
validators.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
from prompt_toolkit.validation import Validator, ValidationError
from prompt_toolkit.formatted_text import HTML
from utils import bcolors, Input
from pathlib import Path
from prompt_toolkit import prompt
from resources_paths import DATA_PATH, GAMES_PATH, YAML_TEMPLATES_PATH, INIT_WORLDS_PATH
def _is_number(text: str):
return text.isdigit()
def _is_tick_type(text: str):
return text in ("day", "hour", "minute", "second")
def _is_float(text: str):
try:
float(text)
return True
except ValueError:
return False
def _is_era(text: str):
return text in ("BC", "AD")
def _is_month(text: str):
return text in [str(i) for i in range(1, 13)]
def _is_day(text: str):
return text in [str(i) for i in range(1, 32)]
def _is_hour(text: str):
return text in [str(i) for i in range(0, 24)]
def _is_minute(text: str):
return text in [str(i) for i in range(0, 60)]
def _is_second(text: str):
return text in [str(i) for i in range(0, 60)]
is_number = Validator.from_callable(
_is_number, error_message="This input contains non-numeric characters"
)
is_tick_type = Validator.from_callable(
_is_tick_type, error_message="This input contains incorrect tick type"
)
is_float = Validator.from_callable(
_is_float, error_message="This input contains non-float value"
)
is_era = Validator.from_callable(
_is_era, error_message="This input contains incorrect era"
)
is_month = Validator.from_callable(
_is_month, error_message="This input contains incorrect month, use numbers 1-12"
)
is_day = Validator.from_callable(
_is_day, error_message="This input contains incorrect day"
)
is_hour = Validator.from_callable(
_is_hour, error_message="This input contains incorrect hour"
)
is_minute = Validator.from_callable(
_is_minute, error_message="This input contains incorrect minute"
)
is_second = Validator.from_callable(
_is_second, error_message="This input contains incorrect second"
)
class NotInListValidator(Validator):
def __init__(self, list_to_check: list):
self.list_to_check = list_to_check
def validate(self, document):
text = document.text
if text not in self.list_to_check:
raise ValidationError(
message=f"{text} does not exist in {self.list_to_check}. Try again"
)
class IsInListValidator(Validator):
def __init__(self, list_to_check: list):
self.list_to_check = list_to_check
def validate(self, document):
text = document.text
if text in self.list_to_check:
raise ValidationError(
message=f"{text} already exists in {self.list_to_check}. Try again"
)