Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 54 additions & 48 deletions piccolo/apps/asgi/commands/new.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,58 @@ def get_server() -> str:
return SERVERS[int(server)]


def _process_directory(output_dir_path: str, sub_dir_names: list[str]):
for sub_dir_name in sub_dir_names:
if sub_dir_name.startswith("_"):
continue
sub_dir_path = os.path.join(output_dir_path, sub_dir_name)
if not os.path.exists(sub_dir_path):
os.mkdir(sub_dir_path)


def _process_file(
dir_path: str,
output_dir_path: str,
file_name: str,
template_context: dict,
):
if file_name.startswith("_") and file_name != "__init__.py.jinja":
return
extension = file_name.rsplit(".")[0]
if extension in ("pyc",):
return
if file_name.endswith(".jinja"):
output_file_name = file_name.replace(".jinja", "")
template = Environment(
loader=FileSystemLoader(searchpath=dir_path)
).get_template(file_name)
output_contents = template.render(**template_context)
if output_file_name.endswith(".py"):
try:
output_contents = black.format_str(
output_contents,
mode=black.FileMode(line_length=80),
)
except Exception as exception:
print(f"Problem processing {output_file_name}")
raise exception from exception
with open(
os.path.join(output_dir_path, output_file_name), "w"
) as f:
f.write(output_contents)
else:
if file_name.endswith(".jinja_raw"):
output_file_name = file_name.replace(
".jinja_raw", ".jinja"
)
else:
output_file_name = file_name
shutil.copy(
os.path.join(dir_path, file_name),
os.path.join(output_dir_path, output_file_name),
)


def new(root: str = ".", name: str = "piccolo_project"):
"""
Create a basic ASGI app, including Piccolo, routing, and an admin.
Expand Down Expand Up @@ -77,56 +129,10 @@ def new(root: str = ".", name: str = "piccolo_project"):
continue
os.mkdir(dir_path)

for sub_dir_name in sub_dir_names:
if sub_dir_name.startswith("_"):
continue

sub_dir_path = os.path.join(output_dir_path, sub_dir_name)
if not os.path.exists(sub_dir_path):
os.mkdir(sub_dir_path)
_process_directory(output_dir_path, sub_dir_names)

for file_name in file_names:
if file_name.startswith("_") and file_name != "__init__.py.jinja":
continue

extension = file_name.rsplit(".")[0]
if extension in ("pyc",):
continue

if file_name.endswith(".jinja"):
output_file_name = file_name.replace(".jinja", "")
template = Environment(
loader=FileSystemLoader(searchpath=dir_path)
).get_template(file_name)

output_contents = template.render(**template_context)

if output_file_name.endswith(".py"):
try:
output_contents = black.format_str(
output_contents,
mode=black.FileMode(line_length=80),
)
except Exception as exception:
print(f"Problem processing {output_file_name}")
raise exception from exception

with open(
os.path.join(output_dir_path, output_file_name), "w"
) as f:
f.write(output_contents)
else:
if file_name.endswith(".jinja_raw"):
output_file_name = file_name.replace(
".jinja_raw", ".jinja"
)
else:
output_file_name = file_name

shutil.copy(
os.path.join(dir_path, file_name),
os.path.join(output_dir_path, output_file_name),
)
_process_file(dir_path, output_dir_path, file_name, template_context)

print(
"Run `pip install -r requirements.txt` and `python main.py` to get "
Expand Down
Loading
Loading