import os import re from flask import current_app from click import command, argument from click.exceptions import ClickException from flask.cli import with_appcontext @command("startapp") @argument("name") @with_appcontext def startapp(name: str) -> None: """Create the application structure""" if not re.match("^[a-zA-Z].*$", name): raise ClickException(f"The name argument must be type of string!") app_dir = current_app.config["SRC_DIR"] / name if os.path.exists(app_dir): raise ClickException("Directory {name} has already exists!") os.mkdir(app_dir) schema_content = """\'\'\' Below you write a schema of model Example: class ExampleSchema(ma.SQLAlchemyAutoSchema): class Meta: model = MODEL_NAME \'\'\' from ..dependencies import ma """ model_content = """\'\'\' Below you write a model. Example: class Example(db.Model): __tablename__ = "examples" id = db.Column(db.Integer, primary_key=True, autoincrement=True) title = db.Column(db.String(255), unique=True) \'\'\' from ..dependencies import db """ route_content = f"""from flask import Blueprint, make_response, jsonify, Response bp = Blueprint("{name}", __name__, url_prefix="/{name}") @bp.route("/", methods=["GET"]) def index() -> Response: return make_response(jsonify({{"hello": "{name}"}}), 200) """ filenames = ["__init__.py", "schemas.py", "models.py", "routes.py"] contents = ["", schema_content, model_content, route_content] for filename, content in zip(filenames, contents): with open(app_dir / filename, "w") as f: f.write(content)