-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpre_gen_project.py
62 lines (43 loc) · 1.77 KB
/
pre_gen_project.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
"""This module contains hooks which are executed before the template is rendered."""
from __future__ import annotations
import re
MODULE_REGEX = r"^[_a-zA-Z][_a-zA-Z0-9]*$"
ENVIRON_REGEX = r"^[-_a-zA-Z0-9]*$"
PYTHONVERSION_REGEX = r"^(3\.(1[0-9]|[7-9])(\.[0-9]{1,2})?)$"
PYTHONVERSION_MIN = "3.7"
EXCEPTION_MSG_MODULE_NAME = """
ERROR: The project slug ({module_name}) is not a valid Python module name.
Please do not use anything other than letters, numbers, and underscores '_'.
The first character must not be a number.
"""
EXCEPTION_MSG_ENVIRON_NAME = """
ERROR: The project slug ({environment_name}) is not a valid conda environment name.
Please do not use anything other than letters, numbers, underscores '_',
and minus signs '-'.
"""
EXCEPTION_MSG_PYTHONVERSION = """
ERROR: The python version must be >= {min_python_version}, got {python_version}.
"""
_ = """{{ cookiecutter.update(
{
"__package_name":
cookiecutter.package_name|lower|replace(' ', '_')|replace('-', '_'),
}
)}}"""
def main() -> None:
"""Apply pre-generation hooks."""
module_name = "{{ cookiecutter.__package_name }}"
if not re.match(MODULE_REGEX, module_name):
raise ValueError(EXCEPTION_MSG_MODULE_NAME.format(module_name=module_name))
environment_name = "{{ cookiecutter.conda_environment_name }}"
if not re.match(ENVIRON_REGEX, environment_name):
raise ValueError(EXCEPTION_MSG_ENVIRON_NAME.format(environment_name))
python_version = "{{ cookiecutter.python_version }}"
if not re.match(PYTHONVERSION_REGEX, python_version):
raise ValueError(
EXCEPTION_MSG_PYTHONVERSION.format(
min_python_version=PYTHONVERSION_MIN, python_version=python_version
)
)
if __name__ == "__main__":
main()