-
Notifications
You must be signed in to change notification settings - Fork 6
/
validators.py
117 lines (79 loc) · 2.41 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
110
111
112
113
114
115
116
117
import os
from qt_wrapper import WITH_PYMOL
class Validator:
def __init__(self):
self.value = ''
def __call__(self, value):
self.value = value
return self.validate(value)
@staticmethod
def validate(value):
raise NotImplementedError
def tooltip(self):
return ''
class NotEmptyValidator(Validator):
def __init__(self, name='Field'):
self.name = name
@staticmethod
def validate(value):
return len(value) > 0
def tooltip(self):
return "{} must not be empty".format(self.name)
class IntegerValidator(Validator):
def __init__(self, name='Field'):
self.name = name
@staticmethod
def validate(value):
try:
int(value)
return True
except ValueError:
return False
def tooltip(self):
return "{} '{}' is not an integer".format(self.name, self.value)
class PositiveIntegerValidator(Validator):
def __init__(self, name='Field'):
self.name = name
@staticmethod
def validate(value):
try:
return int(value) > 0
except ValueError:
return False
def tooltip(self):
return "{} '{}' is not a positive integer".format(self.name, self.value)
class FileValidator(Validator):
@staticmethod
def validate(value):
return os.path.isfile(value)
def tooltip(self):
return "{} is not a valid file".format(self.value)
class DirectoryValidator(Validator):
@staticmethod
def validate(value):
return os.path.isdir(value)
def tooltip(self):
return "Not a valid directory"
class PdbValidator(Validator):
@staticmethod
def validate(value):
ext = os.path.splitext(value)[-1]
return os.path.isfile(value) and ext.lower() == '.pdb'
def tooltip(self):
return "'{}' is not a valid PDB file".format(self.value)
class XYZValidator(Validator):
@classmethod
def validate(cls, value):
if not value:
return True
try:
return len(cls._split_value(value)) == 3
except AttributeError:
return False
@staticmethod
def _split_value(value):
import re
number_query = '([+-]?\d+(?:\.\d+)?)'
return re.match(' '.join([number_query]*3) + '$', value).groups()
def tooltip(self):
return "Not valid XYZ coordinates".format(self.value)