-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathutils.py
125 lines (103 loc) · 2.64 KB
/
utils.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
118
119
120
121
122
123
124
125
import re
import string
from rpdk.core.exceptions import WizardValidationError
# https://docs.oracle.com/javase/tutorial/java/nutsandbolts/_keywords.html
LANGUAGE_KEYWORDS = {
"abstract",
"continue",
"for",
"new",
"switch",
"assert",
"default",
"goto",
"package",
"synchronized",
"boolean",
"do",
"if",
"private",
"this",
"break",
"double",
"implements",
"protected",
"throw",
"byte",
"else",
"import",
"public",
"throws",
"case",
"enum",
"instanceof",
"return",
"transient",
"catch",
"extends",
"int",
"short",
"try",
"char",
"final",
"interface",
"static",
"void",
"class",
"finally",
"long",
"strictfp",
"volatile",
"const",
"float",
"native",
"super",
"while",
}
def safe_reserved(token):
if token in LANGUAGE_KEYWORDS:
return token + "_"
return token
def get_default_namespace(project):
if project.type_info[0] == "AWS":
namespace = ("software", "amazon") + project.type_info[1:]
else:
namespace = ("com",) + project.type_info
namespace = tuple(safe_reserved(s.lower()) for s in namespace)
return namespace
def validate_namespace(default):
pattern = r"^[_a-z][_a-z0-9]+$"
def _validate_namespace(value):
if not value:
return default
if value.lower() != value:
raise WizardValidationError("Package names must be all lower case")
namespace = value.split(".")
for name in namespace:
if not name:
raise WizardValidationError(f"Empty segment in '{value}'")
if name in LANGUAGE_KEYWORDS:
raise WizardValidationError(f"'{name}' is a reserved keyword")
startswith = name[0]
if startswith not in string.ascii_lowercase + "_":
raise WizardValidationError(
f"Segment '{name}' must begin with a lower case letter or "
"an underscore"
)
match = re.match(pattern, name)
if not match:
raise WizardValidationError(
f"Segment '{name}' should match '{pattern}'"
)
return tuple(namespace)
return _validate_namespace
def validate_codegen_model(default):
pattern = r"^[1-2]$"
def _validate_codegen_model(value):
if not value:
return default
match = re.match(pattern, value)
if not match:
raise WizardValidationError("Invalid selection.")
return value
return _validate_codegen_model