-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidators.py
More file actions
201 lines (139 loc) · 5.59 KB
/
Copy pathvalidators.py
File metadata and controls
201 lines (139 loc) · 5.59 KB
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
"""
Validation classes guaranteeing the launch of the program only with acceptable values and types of parameters
Authors: Oleksandr Perederii, Anatolii Kashchuk
2022
"""
import os
from exceptions import BaseParticleException, FilenameError, ThrtypeError, ThrError, RError, RoiError, \
PosAngleError, DzError, ZError, MidRndError, EpsilonError, MinptsError, DcError, DfrError, NfrMinError
class BaseValidator:
@staticmethod
def validate():
raise NotImplemented
class FilenameValidator(BaseValidator):
@staticmethod
def validate(value):
if not isinstance(value, str):
raise FilenameError(f"filename must be string got {type(value)}")
if not os.path.exists(value):
raise FilenameError(f"file {value} does not exist.")
class ThrtypeValidator(BaseValidator):
@staticmethod
def validate(value):
if not isinstance(value, str):
raise ThrtypeError(f"thrtype must be string got {type(value)}")
if value not in ('topfraction', 'topvalue'):
raise ThrtypeError(f"thrtype must be either 'topfraction' nor 'topvalue'")
class ThrValidator(BaseValidator):
@staticmethod
def validate(value):
if (not isinstance(value, float)) and (not isinstance(value, int)):
raise ThrError(f"thr must be float or int. Got {str(type(value))}")
if value <= 0.5:
raise ThrError(f"thr must be more that 0.5, got {value}")
class RValidator(BaseValidator):
@staticmethod
def validate(value):
if (not isinstance(value, float)) and (not isinstance(value, int)):
raise RError(f"R must be float or int. Got {str(type(value))}")
if value <= 0:
raise RError(f"R must be positive, got {value}")
class RoiValidator(BaseValidator):
@staticmethod
def validate(value):
if (not isinstance(value, list)) and (not isinstance(value, tuple)):
raise RoiError(f"roi must be list or tuple of length 4, got {str(type(value))}")
if (len(value) != 4):
raise RoiError(f"roi must be list or tuple of length 4, got {str(type(value))}")
class PosAngleValidator(BaseValidator):
@staticmethod
def validate(value):
if (not isinstance(value, int)):
raise PosAngleError(f"positiveAngle must be int, got {str(type(value))}")
if value < 0:
raise PosAngleError(f"positiveAngle must be positive, got {value}")
class DzValidator(BaseValidator):
@staticmethod
def validate(value):
if not isinstance(value, float):
raise DzError(f"dz must be float, got {str(type(value))}")
if value <= 0:
raise DzError(f"dz must be positive, got {value}")
class ZValidator(BaseValidator):
#int,
@staticmethod
def validate(value):
if (not isinstance(value, int)):
raise ZError(f"z must be int, got {str(type(value))}")
class MidRndValidator(BaseValidator):
@staticmethod
def validate(value):
if (not isinstance(value, int)):
raise MidRndError(f"mid_rng must be int, got {str(type(value))}")
if (value > 180) or (value < -180):
raise MidRndError(f"mid_rng must be in range [-180..180]")
class EpsilonValidator(BaseValidator):
@staticmethod
def validate(value):
if (not isinstance(value, float)) and (not isinstance(value, int)):
raise EpsilonError(f"epsilon must be float or int. Got {str(type(value))}")
if value <= 0:
raise EpsilonError(f"epsilon must be positive, got {value}")
class MinptsValidator(BaseValidator):
@staticmethod
def validate(value):
if not isinstance(value, int):
raise MinptsError(f"minpts must be int. Got {str(type(value))}")
if value <= 0:
raise MinptsError(f"minpts must be positive, got {value}")
class DcValidator(BaseValidator):
@staticmethod
def validate(value):
if not isinstance(value, int):
raise DcError(f"dc must be int. Got {str(type(value))}")
if value <= 0:
raise DcError(f"dc must be positive, got {value}")
class DfrValidator(BaseValidator):
@staticmethod
def validate(value):
if not isinstance(value, int):
raise DfrError(f"dfr must be int. Got {str(type(value))}")
if value <= 0:
raise DfrError(f"dfr must be positive, got {value}")
class NfrMinValidator(BaseValidator):
@staticmethod
def validate(value):
if not isinstance(value, int):
raise NfrMinError(f"Nfr_min must be int. Got {str(type(value))}")
if value <= 0:
raise NfrMinError(f"Nfr_min must be positive, got {value}")
validators = {
"filename": FilenameValidator,
"thrtype": ThrtypeValidator,
"thr": ThrValidator,
"R": RValidator,
"roi": RoiValidator,
"positiveAngle": PosAngleValidator,
"dz": DzValidator,
"z0": ZValidator,
"mid_rng": MidRndValidator,
"epsilon": EpsilonValidator,
"minpts": MinptsValidator,
"dc": DcValidator,
"dfr": DfrValidator,
"Nfr_min": NfrMinValidator
}
def validate(f):
def wrapper(*args, **kwargs):
errors = []
for key, value in kwargs.items():
if key in validators:
validator = validators[key]
try:
validator.validate(value)
except BaseParticleException as exc:
errors.append(str(exc))
if errors:
raise SystemExit("Some input data is incorrect: \n - " +"\n\r - ".join(errors))
return f(*args, **kwargs)
return wrapper