-
Notifications
You must be signed in to change notification settings - Fork 244
Expand file tree
/
Copy pathformat_check.py
More file actions
67 lines (57 loc) · 2.86 KB
/
format_check.py
File metadata and controls
67 lines (57 loc) · 2.86 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
# Copyright (c) 2021, NVIDIA CORPORATION. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import inspect
import re
from functools import wraps
type_pattern_mapping = {
"server": r"^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])$",
"host_name": r"^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])$",
"overseer": r"^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])$",
"sp_end_point": r"^((([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9]):[0-9]*:[0-9]*)$",
"client": r"^[A-Za-z0-9-_]+$",
"relay": r"^[A-Za-z0-9-_]+$",
"admin": r"^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}$",
"email": r"^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}$",
"org": r"^[A-Za-z0-9_]+$",
"simple_name": r"^[A-Za-z0-9_]+$",
"project": r"^[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?$",
}
def name_check(name: str, entity_type: str):
regex_pattern = type_pattern_mapping.get(entity_type)
if regex_pattern is None:
return True, "entity_type={} not defined, unable to check name={}.".format(entity_type, name)
if re.match(regex_pattern, name):
return False, "name={} passed on regex_pattern={} check".format(name, regex_pattern)
else:
return True, "name={} is ill-formatted for entity_type={} based on regex_pattern={}".format(
name, entity_type, regex_pattern
)
def validate_class_methods_args(cls):
for name, method in inspect.getmembers(cls, inspect.isfunction):
if name != "__init_subclass__":
setattr(cls, name, validate_args(method))
return cls
def validate_args(method):
signature = inspect.signature(method)
@wraps(method)
def wrapper(*args, **kwargs):
bound_arguments = signature.bind(*args, **kwargs)
for name, value in bound_arguments.arguments.items():
annotation = signature.parameters[name].annotation
if not (annotation is inspect.Signature.empty or isinstance(value, annotation)):
raise TypeError(
"argument '{}' of {} must be {} but got {}".format(name, method, annotation, type(value))
)
return method(*args, **kwargs)
return wrapper