-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnomenclature.py
179 lines (132 loc) · 4.51 KB
/
nomenclature.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
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
"""
Nomenclature
============
This is the nomenclature about manifest references names (rules and properties).
* A rule name starts with a prefix from ``RULE_BASE_PREFIX``;
* Rule prefix is followed from its type that can be either:
* The ``RULE_META`` value;
* ``reference`` string;
* Then rule name ends with its component name;
* Rule name parts are separated with ``-``.
There is a limited set of allowed characters:
* For rule component name they are registred in ``RULE_ALLOWED_CHARS``;
* For rule property name they are registred in ``PROPERTY_ALLOWED_CHARS``;
There is some reserved name:
* For rule component name they are registred in ``RESERVED_RULE_NAMES``;
* For rule property name they are registred in ``RESERVED_PROPERTY_NAMES``;
"""
from string import ascii_letters, digits
from .exceptions import StyleguideValidationError
RULE_BASE_PREFIX = "styleguide"
RULE_META = "metas"
RULE_META_REFERENCES = "-".join((RULE_BASE_PREFIX, RULE_META, "references"))
RULE_META_COMPILER = "-".join((RULE_BASE_PREFIX, RULE_META, "compiler"))
RULE_REFERENCE = "-".join((RULE_BASE_PREFIX, "reference"))
RULE_ALLOWED_START = ascii_letters
RULE_ALLOWED_CHARS = ascii_letters + digits + "_"
PROPERTY_ALLOWED_START = ascii_letters
PROPERTY_ALLOWED_CHARS = ascii_letters + digits + "_"
FORBIDDEN_PREFIXES = ("_", "-")
"""
Rule and property names can not start with following strings
"""
RESERVED_RULE_NAMES = (
"styleguide",
"load",
"to_dict",
"to_json",
"from_dict",
"metas",
)
"""
Rule name can not be one of the following string
"""
RESERVED_PROPERTY_NAMES = ("structure",)
"""
Property (variable) name can not be one of the following string
"""
REFERENCE_STRUCTURES = (
"flat",
"list",
"string",
"number",
"json",
"object-complex",
"nested",
)
"""
Tuple of available reference structures. Structure name ``json`` is deprecated and
supersed by name ``object-complex``.
"""
def is_reserved_rule(name):
"""
Validate name against ``RESERVED_RULE_NAMES``.
Arguments:
name (string): Rule name.
Returns:
bool: ``True`` if name match a reserved name.
"""
return name in RESERVED_RULE_NAMES
def is_reserved_property(name):
"""
Validate name against ``RESERVED_PROPERTY_NAMES``.
Arguments:
name (string): Property name.
Returns:
bool: ``True`` if name match a reserved name.
"""
return name in RESERVED_PROPERTY_NAMES
def is_valid_rule(name):
"""
Validate rule name.
Arguments:
name (string): Rule name.
Returns:
bool: ``True`` if rule name is valid.
"""
if not name:
raise StyleguideValidationError("Rule name is empty")
if is_reserved_rule(name):
msg = "Rule name '{}' is reserved, you can not use it for a rule"
raise StyleguideValidationError(msg.format(name))
if name.startswith(FORBIDDEN_PREFIXES):
msg = "Rule name '{}' cannot starts with special characters"
raise StyleguideValidationError(msg.format(name))
if name[0] not in RULE_ALLOWED_START:
msg = "Rule name '{}' must starts with a letter"
raise StyleguideValidationError(msg.format(name))
for item in name:
if item not in RULE_ALLOWED_CHARS:
msg = (
"Invalid rule name '{}': it must only contains "
"letters, numbers and '_' character"
)
raise StyleguideValidationError(msg.format(name))
return True
def is_valid_property(name):
"""
Validate property name.
Arguments:
name (string): Property name.
Returns:
bool: ``True`` if variable name is valid.
"""
if not name:
raise StyleguideValidationError("Variable name is empty")
if is_reserved_property(name):
msg = "Variable name '{}' is reserved, you can not use it for a variable"
raise StyleguideValidationError(msg.format(name))
if name.startswith(FORBIDDEN_PREFIXES):
msg = "Variable name '{}' cannot starts with special characters"
raise StyleguideValidationError(msg.format(name))
if name[0] not in PROPERTY_ALLOWED_START:
msg = "Variable name '{}' must starts with a letter"
raise StyleguideValidationError(msg.format(name))
for item in name:
if item not in PROPERTY_ALLOWED_CHARS:
msg = (
"Invalid variable name '{}': it must only contains "
"letters, numbers and '_' character"
)
raise StyleguideValidationError(msg.format(name))
return True