Added create_module script
This commit is contained in:
parent
3015220a12
commit
b5ad1ecf70
1 changed files with 151 additions and 0 deletions
151
paramecio2/scripts/create_module.py
Normal file
151
paramecio2/scripts/create_module.py
Normal file
|
|
@ -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()
|
||||
Loading…
Add table
Add a link
Reference in a new issue