bug fixes and better handling of plugins

This commit is contained in:
Steve Nyemba 2022-10-24 20:46:45 -05:00
parent a9b074fa9a
commit 717251bd28
4 changed files with 58 additions and 14 deletions

View File

@ -3,7 +3,7 @@ import os
import pandas as pd import pandas as pd
import transport import transport
import copy import copy
from jinja2 import Environment, BaseLoader from jinja2 import Environment, BaseLoader, FileSystemLoader
import importlib import importlib
import importlib.util import importlib.util
@ -61,6 +61,10 @@ class components :
#return ' '.join(['<div id=":id" class=":id">'.replace(':id',id),_html,'</div>']) #return ' '.join(['<div id=":id" class=":id">'.replace(':id',id),_html,'</div>'])
_html = ' '.join(['<div id=":id" class=":id">'.replace(':id',id),_html,'</div>']) _html = ' '.join(['<div id=":id" class=":id">'.replace(':id',id),_html,'</div>'])
appContext = Environment(loader=BaseLoader()).from_string(_html) appContext = Environment(loader=BaseLoader()).from_string(_html)
#
# If the rendering of the HTML happens here we should plugin custom functions (at the very least)
#
return appContext.render(**_args) return appContext.render(**_args)
# return _html # return _html
@staticmethod @staticmethod
@ -119,4 +123,17 @@ class components :
_uri = "/".join(["api",_key,_name]) _uri = "/".join(["api",_key,_name])
_map[_uri] = _pointer _map[_uri] = _pointer
return _map return _map
@staticmethod
def context(_config):
"""
adding custom variables functions to Jinja2, this function should be called after plugins are loaded
"""
_plugins = _config['plugins']
# if not location:
# env = Environment(loader=BaseLoader())
# else:
location = _config['layout']['root']
# env = Environment(loader=FileSystemLoader(location))
env = Environment(loader=BaseLoader())
# env.globals['routes'] = _config['plugins']
return env

View File

@ -11,7 +11,8 @@ import sys
import os import os
import json import json
import copy import copy
import io
import base64
from jinja2 import Environment, BaseLoader from jinja2 import Environment, BaseLoader
@ -27,16 +28,18 @@ def favicon():
@_app.route("/") @_app.route("/")
def _index (): def _index ():
global _config global _config
_args = {} _args = {'system':_config['system'],'routes':_config['plugins']}
try: try:
_args = {'system':_config['system']}
_args['layout'] = _config['layout'] _args['layout'] = _config['layout']
# _args = dict(_args,**_config['layout']) # _args = dict(_args,**_config['layout'])
# _args = copy.copy(_config) # _args = copy.copy(_config)
uri = os.sep.join([_config['layout']['root'], _config['layout']['index']]) uri = os.sep.join([_config['layout']['root'], _config['layout']['index']])
_html = cms.components.html(uri,'index') _html = cms.components.html(uri,'index',_args)
e = Environment(loader=BaseLoader()).from_string(_html) _args['index'] = _html
_args['index'] = e.render(**_args) # e = Environment(loader=BaseLoader()).from_string(_html)
# e = cms.components.context(_config).from_string(_html)
# _args['index'] = e.render(**_args)
_index_page = "index.html" _index_page = "index.html"
except Exception as e: except Exception as e:
print () print ()
@ -94,6 +97,25 @@ def _getproxy(module,name) :
return _data,_code return _data,_code
@_app.route("/api/<module>/<name>" , methods=['POST'])
def _post (module,name):
global _config
uri = '/'.join(['api',module,name])
_args = request.json
code = 404
_info = ""
if uri in _config['plugins'] and _args:
_pointer = _config['plugins'][uri]
_info = _pointer(_args)
if _info:
code = 200
_info =io.BytesIO(_info)
_info = base64.encodebytes(_info.getvalue()).decode('ascii')
return _info,code
@_app.route('/version') @_app.route('/version')
def _version (): def _version ():
global _config global _config
@ -110,7 +132,7 @@ def cms_page():
_html = cms.components.html(_uri,_id) _html = cms.components.html(_uri,_id)
e = Environment(loader=BaseLoader()).from_string(_html) e = Environment(loader=BaseLoader()).from_string(_html)
# _data = {} #cms.components.data(_config) # _data = {} #cms.components.data(_config)
_args = {'system':_config['system']} _args = {'system':_config['system'],'routes':_config['plugins']}
_html = ( e.render(**_args)) _html = ( e.render(**_args))
return _html,200 return _html,200
@ -151,6 +173,9 @@ if __name__ == '__main__' :
_map = cms.components.plugins(_config) _map = cms.components.plugins(_config)
if _map : if _map :
_config['plugins'] = _map _config['plugins'] = _map
#
# register the functions with Jinja2
cms.components.context(_config)
_args = _config['system']['app'] _args = _config['system']['app']
_app.run(**_args) _app.run(**_args)
else: else:

View File

@ -1,7 +1,9 @@
<!-- <!--
This file contains the panes for the various content on the page This file contains the panes for the various content on the page
--> -->
{{index|safe}}
{%for _name in layout.menu %} {%for _name in layout.menu %}
<div id="{{_name}}" class="{{_name}}"></div> <div id="{{_name}}" class="{{_name}}"></div>
{%endfor%} {%endfor%}
{{index|safe}}