def generate_model(): project = get_project_name() try: req = ['flask-sqlalchemy'] config = f"{project}/config" with open(f"{project}/model.py", "w") as _app: _app.write(standard_model.replace("**project", project)) with open(f"{project}/__init__.py", "r") as _app: _main = _app.read() _main = _main.replace("app = Flask(__name__, instance_relative_config=False)", f"app = Flask(__name__, instance_relative_config=False)\n\nfrom {project}.model import *\ndb.init_app(app)", 1) with open(f"{project}/__init__.py", "w") as _app: _app.write(_main) with open(f"{config}/dev.py", "r") as _app: _main = _app.read() _main += "\nSQLALCHEMY_DATABASE_URI = 'sqlite:///:memory:' #REPLACE WITH YOUR ACTUAL DB URI" with open(f"{config}/prod.py", "r") as _app: _main = _app.read() _main += "\nSQLALCHEMY_DATABASE_URI = 'sqlite:///:memory:' #REPLACE WITH YOUR ACTUAL DB URI" install(req) print(f'Model successfully generated') return True except Exception as e: print(e) return False
def generate_blueprint(): project = get_project_name() blueprint_name = args.blueprint try: blueprint = f"{project}/{blueprint_name}" os.makedirs(blueprint) templates = f"{blueprint}/templates" os.makedirs(templates) static = f"{blueprint}/static" os.makedirs(static) with open(f"{blueprint}/__init__.py", "w") as _app: _app.write('') with open(f"{blueprint}/routes.py", "w") as _app: _app.write(standard_blueprint_setup.replace("**my_blueprint", blueprint_name).replace("**project", project)) with open(f"{project}/__init__.py", "r") as _app: _main = _app.read() if "app.register_blueprint" in _main: _main = _main.replace("app.register_blueprint", f"from {project}.{blueprint_name}.routes import {blueprint_name}\napp.register_blueprint({blueprint_name})\napp.register_blueprint", 1) elif "#Blueprints" in _main: _main = _main.replace("#Blueprints", f"#Blueprints\nfrom {project}.{blueprint_name}.routes import {blueprint_name}\napp.register_blueprint({blueprint_name})", 1) else: _main = _main.replace("@app.", f"#Blueprints\nfrom {project}.{blueprint_name}.routes import {blueprint_name}\napp.register_blueprint({blueprint_name})\n\n@app.", 1) with open(f"{project}/__init__.py", "w") as _app: _app.write(_main) print(f'Blueprint: "{blueprint_name}" successfully generated') return True except Exception as e: print(e) return False
def destroy_marshmallow(): project = get_project_name() try: subprocess.call(f"rm r {project}/marshmallow.py", shell=True) req = ['flask-marshmallow', 'marshmallow-sqlalchemy'] uninstall(req) print(f'Marshmallow successfully destroyed') return True except Exception as e: print(e) return False
def generate_marshmallow(): project = get_project_name() try: req = ['flask-marshmallow', 'marshmallow-sqlalchemy'] with open(f"{project}/marshmallow.py", "w") as _app: _app.write(standard_marshmallow.replace("**project", project)) install(req) print(f'Marshmallow successfully generated') return True except Exception as e: print(e) return False
def do_work(): #Run if VENV is activated if os.environ.get('VIRTUAL_ENV'): if args.init: #Create .flask_setup file with project name inside return initialize_setup(args.init) elif args.build: return build_app(args.build) #Check for generate request first elif args.generate: if args.generate == 'blueprint': return generate_blueprint() elif args.generate == 'marshmallow': return generate_marshmallow() elif args.generate == 'model': return generate_model() #Check for destroy request next elif args.destroy: if args.destroy == 'blueprint': return destroy_blueprint() elif args.destroy == 'marshmallow': return destroy_marshmallow() elif args.destroy == 'model': return destroy_model() elif args.destroy == get_project_name(): return destroy_project() elif args.install: return install([args.install]) elif args.uninstall: return uninstall([args.uninstall]) return helper() print('Please make sure you have virtual environment installed and activated') return False
def build_basic_app(api=None): try: project = get_project_name() blueprint_name = args.blueprint if args.blueprint else api req = ["flask"] #requirements blueprint_name = args.blueprint os.mkdir(project) templates = f"{project}/templates" os.makedirs(templates) static = f"{project}/static" os.makedirs(static) config = "config" os.makedirs(config) with open(f"{templates}/index.html", "w") as _app: _app.write('<h1>Welcome to index page</h1>') with open(f"{templates}/404.html", "w") as _app: _app.write('<h1>Welcome to 404 page</h1>') with open(f"{config}/dev.py", "w") as _config: _config.write(configuration) with open(f"{config}/prod.py", "w") as _config: _config.write(configuration) if blueprint_name: blueprint = f"{project}/{blueprint_name}" blueprint_templates = f"{blueprint}/templates/{blueprint_name}" os.makedirs(blueprint) os.makedirs(blueprint_templates) with open(f"{project}/__init__.py", "w") as _app: _app.write( basic_blueprint_app.replace('**project', project).replace( "**my_blueprint", blueprint_name)) with open(f"{blueprint}/__init__.py", "w") as _app: _app.write('') with open(f"{blueprint}/routes.py", "w") as _app: _app.write( basic_blueprint_setup.replace("**my_blueprint", blueprint_name)) with open(f"{blueprint_templates}/index.html", "w") as _app: _app.write('<h1>Welcome to index page from blueprint</h1>') else: with open(f"{project}/__init__.py", "w") as _app: _app.write(basic_app.replace("**project", project)) install(req) print('App built successfully') return True except Exception as e: print(e) print(f'Perhaps the project: "{project}" already exists') return False
def destroy_model(): project = get_project_name() try: subprocess.call(f"rm r {project}/model.py", shell=True) req = ['flask-sqlalchemy'] with open(f"{project}/__init__.py", "r") as _app: _main = _app.read() _main = _main.replace(f"from {project}.model import *\ndb.init_app(app)", "") with open(f"{project}/__init__.py", "w") as _app: _app.write(_main) uninstall(req) print(f'Model successfully generated') return True except Exception as e: print(e) return False
def destroy_blueprint(): project = get_project_name() blueprint_name = args.blueprint try: blueprint = f"{project}/{blueprint_name}" subprocess.call(f"rm -r {blueprint}", shell=True) with open(f"{project}/__init__.py", "r") as _app: _main = _app.read() _main = _main.replace(f"from {project}.{blueprint_name}.routes import {blueprint_name}\n", "") _main = _main.replace(f"app.register_blueprint({blueprint_name})\n", "") with open(f"{project}/__init__.py", "w") as _app: _app.write(_main) print(f'Blueprint: "{blueprint_name}" successfully destroyed') return True except Exception as e: print(e) return False
def build_standard_app(): try: project = get_project_name() req = [ "flask", "flask-sqlalchemy", "flask-login", "flask-migrate", "flask-script", "flask-wtf", "arrow" ] #requirements blueprint_name = args.blueprint os.mkdir(project) templates = f"{project}/templates" os.makedirs(templates) static = f"{project}/static" os.makedirs(static) config = "config" os.makedirs(config) with open(f"{templates}/index.html", "w") as _app: _app.write('<h1>Welcome to index page</h1>') with open(f"{templates}/login.html", "w") as _app: _app.write('<h1>Welcome to login page</h1>') with open(f"{templates}/protected.html", "w") as _app: _app.write('<h1>Welcome to protected page</h1>') with open(f"{templates}/404.html", "w") as _app: _app.write('<h1>Welcome to 404 page</h1>') with open(f"{config}/dev.py", "w") as _config: _config.write(configuration) with open(f"{config}/prod.py", "w") as _config: _config.write(configuration) with open(f"{project}/model.py", "w") as _app: _app.write(standard_model.replace("**project", project)) with open(f"{project}/methods.py", "w") as _app: _app.write(standard_methods.replace("**project", project)) with open(f"{project}/filters.py", "w") as _app: _app.write(standard_filters.replace("**project", project)) if blueprint_name: blueprint = f"{project}/{blueprint_name}" blueprint_templates = f"{blueprint}/templates/{blueprint_name}" os.makedirs(blueprint) os.makedirs(blueprint_templates) with open(f"{project}/__init__.py", "w") as _app: _app.write( standard_blueprint_app.replace('**project', project).replace( "**my_blueprint", blueprint_name)) with open(f"{blueprint}/__init__.py", "w") as _app: _app.write('') with open(f"{blueprint}/routes.py", "w") as _app: _app.write( standard_blueprint_setup.replace("**my_blueprint", blueprint_name).replace( "**project", project)) with open(f"{blueprint_templates}/index.html", "w") as _app: _app.write('<h1>Welcome to index page from blueprint</h1>') with open(f"{project}/marshmallow.py", "w") as _app: _app.write(standard_marshmallow.replace("**project", project)) else: with open(f"{project}/__init__.py", "w") as _app: _app.write(standard_app.replace("**project", project)) install(req) print('App built successfully') return True except Exception as e: print(e) print(f'Perhaps the project: "{project}" already exists') return False