diff --git a/paramecio2/scripts/create_module.py b/paramecio2/scripts/create_module.py new file mode 100644 index 0000000..2c49b7d --- /dev/null +++ b/paramecio2/scripts/create_module.py @@ -0,0 +1,151 @@ +#!/usr/bin/env python3 + +import traceback +import argparse +import os,sys +import shutil +import getpass +from pathlib import Path +from importlib import import_module +from paramecio2.libraries.slugify import slugify +import json + +sys.path.insert(0, os.path.realpath('.')) + +try: + + from settings import config + +except: + + pass + + +def start(): + """Module for create new modules for paramecio + """ + + parser=argparse.ArgumentParser(description='A tool for create new modules for paramecio') + + parser.add_argument('--folder', help='The folder where the new paramecio module is located', required=True) + + args=parser.parse_args() + + workdir=os.path.dirname(os.path.abspath(__file__)) + + # Create directory + + real_path=os.path.basename(slugify(args.folder)) + + path=Path('modules/'+real_path) + + + path.mkdir(0o755, True) + + #open('modules/'+args.path+'/__init__.py', 'a').close() + with open('modules/'+real_path+'/__init__.py', 'a') as f: + f.write("from flask import Blueprint\n\n") + f.write("{}_app=Blueprint('{}_app', __name__)\n\n".format(real_path, real_path)) + f.write("@{}_app.route('/{}')\n".format(real_path, real_path)) + f.write("def {}_home():\n".format(real_path)) + f.write(" return {'hello': 'world'}") + + modules_json={} + + if os.path.isfile('settings/modules.json'): + + with open('settings/modules.json') as f: + + #apps={'monit2': ['modules.monit2', 'monit2_app', '/'], 'welcome': ['paramecio2.modules.welcome', 'welcome_app', '/'], 'pastafari2': ['modules.pastafari2', 'pastafari_app', '/'], 'apache': ['modules.apache', 'apache_app', '/'], 'mariadb': ['modules.mariadb', 'mariadb_app', '/'], 'apiv1': ['modules.apiv1', 'apiv1_app', '/'], 'admin': ['paramecio2.modules.admin', 'admin_app', '/']} + + json_text=f.read() + + if json_text!='': + modules_json=json.loads(json_text) + + with open('settings/modules.json', 'w+') as f: + modules_json[real_path]=['modules.'+real_path, real_path+'_app', '/'] + + f.write(json.dumps(modules_json, indent=4)) + + print('Created the new module. Please, reload the WSGI server and go to url /'+real_path) + + """ + except: + + print('Error: cannot create the directory. Check if exists and if you have permissions') + exit(1) + """ + #Create base controller file + + #f=open('modules/'+args.path+'/index.py', 'w') + """ + try: + shutil.copy(workdir+'/examples/app.py', 'modules/'+args.path+'/__init__.py') + + except: + + print('Error: cannot copy controller example. Check if you have permissions') + exit(1) + """ + + # Regenerate modules + + #regenerate_modules_config() +""" +def regenerate_modules_config(): + + print("Regenerating modules configuration...") + + modules=[] + + modules.append("#!/usr/bin/env python3\n\n") + modules.append("list_modules=[]\n\n") + + for module in config.modules: + + try: + + controller_path=import_module(module) + + controller_base=os.path.dirname(controller_path.__file__) + + base_module=module.split('.')[-1] + + dir_controllers=os.listdir(controller_base) + + modules.append('from '+module+' import ') + + arr_controllers=[] + + for controller in dir_controllers: + + if controller.find('.py')!=-1 and controller.find('__init__')==-1: + + controller_py=controller.replace('.py', '') + + arr_controllers.append(controller_py) + + #load(module+'.'+controller_py) + + + modules.append(", ".join(arr_controllers)) + + modules.append("\n\n") + + #add_func_static_module(controller_base) + + except: + + print("Exception in user code:") + print("-"*60) + traceback.print_exc(file=sys.stdout) + print("-"*60) + exit(1) + + with open('./settings/modules.py', 'w') as f: + f.write("".join(modules)) +""" + +if __name__=="__main__": + start() diff --git a/pyproject.toml b/pyproject.toml index 0d5b190..408216f 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -43,7 +43,8 @@ Documentation = "https://docs.cuchulu.com/paramecio2/" [project.scripts] paramecio2 = "paramecio2.console:start" -paramecio2db = "parmecio2.libraries.db.dbadmin:start" +paramecio2db = "paramecio2.libraries.db.dbadmin:start" +paramecio2cm = "paramecio2.scripts.create_module:start" [tool.pytest.ini_options] testpaths = ["tests"]