Skip to content

Commit

Permalink
Python 3.10 Compatibility (#8)
Browse files Browse the repository at this point in the history
* lint

* use InquirerPy
  • Loading branch information
petabite authored Dec 10, 2022
1 parent 6a4c23b commit 2be1ea1
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 59 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.DS_Store
.vscode/
test/
# Byte-compiled / optimized / DLL files
Expand Down
2 changes: 1 addition & 1 deletion create_flask_app/app/tests/test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@

def test_hello(client):
resp = client.get("/hello")
assert resp.data == b'hello'
assert resp.data == b"hello"
123 changes: 74 additions & 49 deletions create_flask_app/create_flask_app.py
Original file line number Diff line number Diff line change
@@ -1,75 +1,81 @@
from typing import List
from PyInquirer import prompt, Separator
from InquirerPy import prompt
from InquirerPy.separator import Separator
from shutil import copy, copytree
import os, errno, subprocess
from jinja2 import Template

scaffold_path = 'app/'
scaffold_path = "app/"


def resource_path(path: str) -> str:
"""
return the absolute path for a file
"""
return os.path.join(os.path.abspath(os.path.dirname(__file__)), path)


def extras_includes(extra: str) -> bool:
"""
check if extra is included in extras selected by user
"""
return any(extra in s.lower() for s in extras)


def render_and_copy(src: str, dest: str) -> None:
"""
read in src as a jinja template and save the rendered output to dest
"""
"""
with open(resource_path(src)) as f:
template = Template(f.read())
template.globals['extras_includes'] = extras_includes
template.globals["extras_includes"] = extras_includes
parsed = template.render(name=name, extras=extras)
if not os.path.exists(os.path.dirname(dest)):
try:
os.makedirs(os.path.dirname(dest))
except OSError as exc: # Guard against race condition
except OSError as exc: # Guard against race condition
if exc.errno != errno.EEXIST:
raise
with open(dest, "w") as f:
f.write(parsed)



def render_and_copy_files(list_of_files: List[str]) -> None:
"""
run render_and_copy for a list of files
"""
for file in list_of_files:
render_and_copy(scaffold_path + file, name + '/' + file)
render_and_copy(scaffold_path + file, name + "/" + file)


def prompt_user() -> None:
"""
prompt user for selections, copy files, and run make install
"""
questions = [
{"type": "input", "name": "project_name", "message": "Name your project:"},
{
"type": "checkbox",
"message": "What else do you want to generate?",
"name": "extras",
"choices": [
{"name": "Test Suite(tox, pytest)"},
{"name": "Docker"},
{"name": "Heroku"},
{"name": "Job Scheduler"},
Separator("Flask Libraries"),
{"name": "Flask-Login"},
{"name": "Flask-Admin"},
{"name": "Flask-WTF"},
Separator("JS Libraries"),
{"name": "Vue.js(CDN Version)"},
{"name": "jQuery"},
Separator("CSS"),
{"name": "Sass"},
{"name": "Bootstrap"},
Separator("Database"),
{"name": "SQLite(Flask-SQLAlchemy)"},
{"name": "MongoDB(Flask-PyMongo)"},
{"type": "input", "name": "project_name", "message": "Name your project:"},
{
"type": "checkbox",
"message": "What else do you want to generate?",
"name": "extras",
"choices": [
"Test Suite(tox, pytest)",
"Docker",
"Heroku",
"Job Scheduler",
Separator("Flask Libraries"),
"Flask-Login",
"Flask-Admin",
"Flask-WTF",
Separator("JS Libraries"),
"Vue.js(CDN Version)",
"jQuery",
Separator("CSS"),
"Sass",
"Bootstrap",
Separator("Database"),
"SQLite(Flask-SQLAlchemy)",
"MongoDB(Flask-PyMongo)",
],
},
]
Expand Down Expand Up @@ -101,45 +107,64 @@ def prompt_user() -> None:
print("Ok. Bye.")
exit()

print('Copying files...')
print("Copying files...")
os.makedirs(name, exist_ok=True)

base_folders = ['static/css', 'static/js', 'templates']
base_files = ['app.py', 'config.py', 'Makefile', 'README.md', 'requirements.txt', 'setup.py', '.gitignore']
base_folders = ["static/css", "static/js", "templates"]
base_files = [
"app.py",
"config.py",
"Makefile",
"README.md",
"requirements.txt",
"setup.py",
".gitignore",
]

# render and copy flask static and template dirs
for folder in base_folders:
copytree(resource_path(scaffold_path + folder), name + '/' + folder, dirs_exist_ok=True, copy_function=render_and_copy)
copytree(
resource_path(scaffold_path + folder),
name + "/" + folder,
dirs_exist_ok=True,
copy_function=render_and_copy,
)

# render and copy root files: app.py, README, Makefile, etc
render_and_copy_files(base_files)

test_suite_files = ['tests/client.py', 'tests/test_app.py', 'tox.ini']
if extras_includes('test'):
test_suite_files = ["tests/client.py", "tests/test_app.py", "tox.ini"]
if extras_includes("test"):
render_and_copy_files(test_suite_files)

docker_files = ['.dockerignore', 'docker-compose.yml', 'Dockerfile']
if extras_includes('docker'):
docker_files = [".dockerignore", "docker-compose.yml", "Dockerfile"]
if extras_includes("docker"):
render_and_copy_files(docker_files)

heroku_files = ['Procfile', 'runtime.txt']
if extras_includes('heroku'):
heroku_files = ["Procfile", "runtime.txt"]
if extras_includes("heroku"):
render_and_copy_files(heroku_files)

if extras_includes('sass'):
copytree(resource_path(scaffold_path + 'static/scss'), name + '/static/scss', dirs_exist_ok=True, copy_function=render_and_copy)
if extras_includes("sass"):
copytree(
resource_path(scaffold_path + "static/scss"),
name + "/static/scss",
dirs_exist_ok=True,
copy_function=render_and_copy,
)

sqlite_files = ['models.py']
if extras_includes('sqlite'):
sqlite_files = ["models.py"]
if extras_includes("sqlite"):
render_and_copy_files(sqlite_files)

print('Running make install...')
subprocess.check_output(['make', 'install'], cwd=name)
print("Running make install...")
subprocess.check_output(["make", "install"], cwd=name)

print("Successfully created %s!" % (name,))
print('To get started, do:')
print('\t1) cd', name)
print('\t2) make start')
print("To get started, do:")
print("\t1) cd", name)
print("\t2) make start")


if __name__ == "__main__":
prompt_user()
12 changes: 5 additions & 7 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
Jinja2==2.11.2
MarkupSafe==1.1.1
prompt-toolkit==1.0.14
Pygments==2.6.1
PyInquirer==1.0.3
regex==2020.7.14
six==1.15.0
inquirerpy==0.3.4
Jinja2==3.1.2
MarkupSafe==2.1.1
pfzy==0.3.4
prompt-toolkit==3.0.36
wcwidth==0.2.5
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setuptools.setup(
name="new-flask-app",
version="1.0.2",
version="1.0.3",
author="Philip Zhang",
author_email="[email protected]",
description="Autogenerate boilerplate code for a Flask app",
Expand All @@ -22,7 +22,7 @@
],
python_requires=">=3.8",
license="MIT",
install_requires=["PyInquirer", "jinja2"],
install_requires=["InquirerPy", "jinja2"],
entry_points={
"console_scripts": [
"create-flask-app=create_flask_app.create_flask_app:prompt_user",
Expand Down

0 comments on commit 2be1ea1

Please sign in to comment.