From 5154fc5075941f4f0e767bf0608684e0a9d51a90 Mon Sep 17 00:00:00 2001 From: Marcus Pereira Date: Sat, 26 Nov 2022 13:46:05 -0300 Subject: [PATCH 1/3] Create cli commands for generate templates --- mvc_flask/__init__.py | 4 +- mvc_flask/cli.py | 113 ++++++++++++++++++++++++++++++ mvc_flask/templates/controller.md | 3 + 3 files changed, 119 insertions(+), 1 deletion(-) create mode 100644 mvc_flask/cli.py create mode 100644 mvc_flask/templates/controller.md diff --git a/mvc_flask/__init__.py b/mvc_flask/__init__.py index 23abee8..a367c03 100644 --- a/mvc_flask/__init__.py +++ b/mvc_flask/__init__.py @@ -1,10 +1,11 @@ from importlib import import_module -from flask import Flask, render_template, request +from flask import Flask from flask.blueprints import Blueprint from mvc_flask import plugins from .router import Router +from . import cli class FlaskMVC: @@ -20,6 +21,7 @@ def init_app(self, app: Flask = None, path="app"): self.register_blueprint(app) plugins.register(app) + cli.init_app(app) def register_blueprint(self, app: Flask): # load routes defined from users diff --git a/mvc_flask/cli.py b/mvc_flask/cli.py new file mode 100644 index 0000000..596331b --- /dev/null +++ b/mvc_flask/cli.py @@ -0,0 +1,113 @@ +import os +import platform +import shutil +import subprocess +from distutils.dir_util import copy_tree +from black import format_str, FileMode + + +import click +from flask.cli import with_appcontext + + +BASEDIR = os.path.dirname(os.path.dirname(__file__)) +MVC_DIR = os.path.join(BASEDIR, "mvc_flask") + + +@click.group() +def generate(): + """ Use this command for generate controller, models, auth and etc. """ + pass + +@generate.command() +@click.argument('name') +@with_appcontext +def controller(name): + """ Generate controller, e.g: flask generate controller home """ + controller_template = os.path.join(MVC_DIR, "templates", "controller.md") + file_content = None + + with open(controller_template, 'r') as controller: + file_content = controller.read() + + path = os.path.join(os.getcwd(), "app", "controllers", f"{name}_controller.py".lower()) + with open(path, 'w+') as f: + string = file_content.format(name=f"{name}".title()) + content = format_str(string, mode=FileMode()) + f.write(content) + + +# @mvc.command() +# @click.option("--init", default=False) +# @with_appcontext +# def init(init): +# """ Configure your project. """ +# front_original = os.path.join(BASEDIR, "flask_vuejs") + +# if ( +# os.path.isdir("frontend") +# or os.path.isfile("package.json") +# or os.path.isfile("webpack.config.js") +# ): +# return click.echo( +# f"You already configured your application. Try running {Fore.GREEN}\ +# flask vue restore {Fore.WHITE}command before running this command again." +# ) + +# # copy directories locals to application directory +# copy_tree(os.path.join(front_original, "bootstrap"), ".") + +# return click.echo( +# f"Everything went well, now you need to execute the following commands:\ +# \n\n{Fore.GREEN}$ flask vue install\n{Fore.GREEN}$ flask vue compile\n" +# ) + + +# @vue.command() +# @with_appcontext +# def install(): +# """ Install the vue packages """ +# subprocess.check_call("npm i --silent", shell=True) + + +# @vue.command() +# @with_appcontext +# def compile(): +# """ Compile assets only once """ +# subprocess.check_call("npm run build", shell=True) +# return click.echo("\nCompiled!\n") + + +# @vue.command() +# @with_appcontext +# def watch(): +# """ Watch the vue files """ +# text = "Before execute this command, you should be set FLASK_ENV as development mode. \n\n$ {} FLASK_ENV=development\n" + +# if os.getenv("FLASK_ENV") == "development": +# subprocess.check_call("npm run watch", shell=True) + +# if platform.system() == "Windows": +# return click.echo(text.format("set")) +# return click.echo(text.format("export")) + + +# @vue.command() +# def restore(): +# """ Restore factory application """ +# files = ["webpack.config.js", "package.json", "package-lock.json"] +# dirs = ["frontend", "node_modules"] + +# for f in files: +# if os.path.isfile(f): +# os.remove(f) + +# for d in dirs: +# if os.path.isdir(d): +# shutil.rmtree(d) + +# return click.echo(f"Restored successfully!") + + +def init_app(app): + app.cli.add_command(generate) \ No newline at end of file diff --git a/mvc_flask/templates/controller.md b/mvc_flask/templates/controller.md new file mode 100644 index 0000000..70e96e4 --- /dev/null +++ b/mvc_flask/templates/controller.md @@ -0,0 +1,3 @@ +class {name}Controller: + def index(): + ... \ No newline at end of file From 53cbcb5fc2c16f7efeb6e7b34d68804fe1e3301b Mon Sep 17 00:00:00 2001 From: Marcus Pereira Date: Sat, 26 Nov 2022 23:35:28 -0300 Subject: [PATCH 2/3] Implement generate controller cmd --- mvc_flask/__init__.py | 2 +- mvc_flask/cli.py | 103 ++++--------------- tests/app/controllers/telegram_controller.py | 3 + 3 files changed, 24 insertions(+), 84 deletions(-) create mode 100644 tests/app/controllers/telegram_controller.py diff --git a/mvc_flask/__init__.py b/mvc_flask/__init__.py index a367c03..be085ce 100644 --- a/mvc_flask/__init__.py +++ b/mvc_flask/__init__.py @@ -5,7 +5,7 @@ from mvc_flask import plugins from .router import Router -from . import cli +from . import cli class FlaskMVC: diff --git a/mvc_flask/cli.py b/mvc_flask/cli.py index 596331b..105e9b0 100644 --- a/mvc_flask/cli.py +++ b/mvc_flask/cli.py @@ -1,11 +1,6 @@ import os -import platform -import shutil -import subprocess -from distutils.dir_util import copy_tree from black import format_str, FileMode - import click from flask.cli import with_appcontext @@ -16,98 +11,40 @@ @click.group() def generate(): - """ Use this command for generate controller, models, auth and etc. """ + """Use this command for generate controller, models, auth and etc.""" pass + @generate.command() -@click.argument('name') +@click.argument("name") @with_appcontext def controller(name): - """ Generate controller, e.g: flask generate controller home """ + """Generate controller, e.g: flask generate controller home""" controller_template = os.path.join(MVC_DIR, "templates", "controller.md") + controller_filename = f"{name}_controller.py".lower() file_content = None - with open(controller_template, 'r') as controller: + with open(controller_template, "r") as controller: file_content = controller.read() - path = os.path.join(os.getcwd(), "app", "controllers", f"{name}_controller.py".lower()) - with open(path, 'w+') as f: + file = os.path.join(os.getcwd(), "app", "controllers", controller_filename) + + if os.path.exists(file): + print( + "\033[31m conflict" + + f"\033[37m app/controllers/{controller_filename} already exists" + ) + quit() + + with open(file, "w+") as f: string = file_content.format(name=f"{name}".title()) content = format_str(string, mode=FileMode()) f.write(content) - -# @mvc.command() -# @click.option("--init", default=False) -# @with_appcontext -# def init(init): -# """ Configure your project. """ -# front_original = os.path.join(BASEDIR, "flask_vuejs") - -# if ( -# os.path.isdir("frontend") -# or os.path.isfile("package.json") -# or os.path.isfile("webpack.config.js") -# ): -# return click.echo( -# f"You already configured your application. Try running {Fore.GREEN}\ -# flask vue restore {Fore.WHITE}command before running this command again." -# ) - -# # copy directories locals to application directory -# copy_tree(os.path.join(front_original, "bootstrap"), ".") - -# return click.echo( -# f"Everything went well, now you need to execute the following commands:\ -# \n\n{Fore.GREEN}$ flask vue install\n{Fore.GREEN}$ flask vue compile\n" -# ) - - -# @vue.command() -# @with_appcontext -# def install(): -# """ Install the vue packages """ -# subprocess.check_call("npm i --silent", shell=True) - - -# @vue.command() -# @with_appcontext -# def compile(): -# """ Compile assets only once """ -# subprocess.check_call("npm run build", shell=True) -# return click.echo("\nCompiled!\n") - - -# @vue.command() -# @with_appcontext -# def watch(): -# """ Watch the vue files """ -# text = "Before execute this command, you should be set FLASK_ENV as development mode. \n\n$ {} FLASK_ENV=development\n" - -# if os.getenv("FLASK_ENV") == "development": -# subprocess.check_call("npm run watch", shell=True) - -# if platform.system() == "Windows": -# return click.echo(text.format("set")) -# return click.echo(text.format("export")) - - -# @vue.command() -# def restore(): -# """ Restore factory application """ -# files = ["webpack.config.js", "package.json", "package-lock.json"] -# dirs = ["frontend", "node_modules"] - -# for f in files: -# if os.path.isfile(f): -# os.remove(f) - -# for d in dirs: -# if os.path.isdir(d): -# shutil.rmtree(d) - -# return click.echo(f"Restored successfully!") + print( + "\033[92m create" + f"\033[37m app/controllers/{controller_filename}" + ) def init_app(app): - app.cli.add_command(generate) \ No newline at end of file + app.cli.add_command(generate) diff --git a/tests/app/controllers/telegram_controller.py b/tests/app/controllers/telegram_controller.py new file mode 100644 index 0000000..542dce2 --- /dev/null +++ b/tests/app/controllers/telegram_controller.py @@ -0,0 +1,3 @@ +class TelegramController: + def index(): + ... From 199b7fdc12115902c1c19e26e2b9639551ff422d Mon Sep 17 00:00:00 2001 From: Marcus Pereira Date: Sat, 26 Nov 2022 23:38:15 -0300 Subject: [PATCH 3/3] Remove telegram_controller --- tests/app/controllers/telegram_controller.py | 3 --- 1 file changed, 3 deletions(-) delete mode 100644 tests/app/controllers/telegram_controller.py diff --git a/tests/app/controllers/telegram_controller.py b/tests/app/controllers/telegram_controller.py deleted file mode 100644 index 542dce2..0000000 --- a/tests/app/controllers/telegram_controller.py +++ /dev/null @@ -1,3 +0,0 @@ -class TelegramController: - def index(): - ...