|
3 | 3 | import importlib |
4 | 4 | import os |
5 | 5 | import pathlib |
| 6 | +import string |
6 | 7 | import sys |
7 | 8 | import typing as t |
8 | 9 |
|
@@ -33,24 +34,48 @@ def module_exists(module_name: str) -> bool: |
33 | 34 | return True |
34 | 35 |
|
35 | 36 |
|
| 37 | +APP_NAME_ALLOWED_CHARACTERS = [*string.ascii_lowercase, *string.digits, "_"] |
| 38 | + |
| 39 | + |
| 40 | +def validate_app_name(app_name: str): |
| 41 | + """ |
| 42 | + Make sure the app name is something which is a valid Python package name. |
| 43 | +
|
| 44 | + :raises ValueError: |
| 45 | + If ``app_name`` isn't valid. |
| 46 | +
|
| 47 | + """ |
| 48 | + for char in app_name: |
| 49 | + if not char.lower() in APP_NAME_ALLOWED_CHARACTERS: |
| 50 | + raise ValueError( |
| 51 | + f"The app name contains a disallowed character: `{char}`. " |
| 52 | + "It must only include a-z, 0-9, and _ characters." |
| 53 | + ) |
| 54 | + |
| 55 | + |
36 | 56 | def get_app_module(app_name: str, root: str) -> str: |
37 | 57 | return ".".join([*pathlib.Path(root).parts, app_name, "piccolo_app"]) |
38 | 58 |
|
39 | 59 |
|
40 | 60 | def new_app(app_name: str, root: str = ".", register: bool = False): |
41 | 61 | print(f"Creating {app_name} app ...") |
42 | 62 |
|
43 | | - app_root = os.path.join(root, app_name) |
44 | | - |
45 | | - if os.path.exists(app_root): |
46 | | - sys.exit("Folder already exists - exiting.") |
| 63 | + try: |
| 64 | + validate_app_name(app_name=app_name) |
| 65 | + except ValueError as exception: |
| 66 | + sys.exit(str(exception)) |
47 | 67 |
|
48 | 68 | if module_exists(app_name): |
49 | 69 | sys.exit( |
50 | 70 | f"A module called {app_name} already exists - possibly a builtin " |
51 | 71 | "Python module. Please choose a different name for your app." |
52 | 72 | ) |
53 | 73 |
|
| 74 | + app_root = os.path.join(root, app_name) |
| 75 | + |
| 76 | + if os.path.exists(app_root): |
| 77 | + sys.exit("Folder already exists - exiting.") |
| 78 | + |
54 | 79 | os.makedirs(app_root) |
55 | 80 |
|
56 | 81 | with open(os.path.join(app_root, "__init__.py"), "w"): |
|
0 commit comments