bug fixes: enhancements

master
Steve Nyemba 11 months ago
parent d450779557
commit 81b19e59cc

@ -79,8 +79,7 @@ class components :
""" """
if 'source' in _system and _system['source']['id'] == 'cloud': if 'source' in _system and _system['source']['id'] == 'cloud':
_html = cloud.html(uri,dict(_args,**{'system':_system}))
_html = cloud.html(uri,_args)
else: else:
_html = disk.html(uri) _html = disk.html(uri)
@ -105,7 +104,7 @@ class components :
_store = _args['store'] _store = _args['store']
reader = transport.factory.instance(**_store) reader = transport.factory.instance(**_store)
_queries= copy.deepcopy(_store['query']) _queries= copy.deepcopy(_store['query'])
_data = reader.read(**_query) _data = reader.read(**_queries)
return _data return _data
@staticmethod @staticmethod
def csv(uri) : def csv(uri) :
@ -130,6 +129,7 @@ class components :
return None return None
#-- We have a file ... #-- We have a file ...
_name = _args['name'] _name = _args['name']
spec = importlib.util.spec_from_file_location(_name, _path) spec = importlib.util.spec_from_file_location(_name, _path)
module = importlib.util.module_from_spec(spec) module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module) spec.loader.exec_module(module)
@ -142,29 +142,32 @@ class components :
""" """
PATH= os.sep.join([_config['layout']['root'],'_plugins']) PATH= os.sep.join([_config['layout']['root'],'_plugins'])
_map = {} _map = {}
if not os.path.exists(PATH) : # if not os.path.exists(PATH) :
return _map # return _map
if 'plugins' not in _config : if 'plugins' not in _config :
_config['plugins'] = {} _config['plugins'] = {}
_conf = _config['plugins'] _conf = _config['plugins']
for _key in _conf : for _key in _conf :
_path = os.sep.join([PATH,_key+".py"]) _path = os.sep.join([PATH,_key+".py"])
if not os.sep.path.exists(_path):
continue
for _name in _conf[_key] : for _name in _conf[_key] :
_pointer = components.load_plugin(path=_path,name=_name) _pointer = components.load_plugin(path=_path,name=_name)
if _pointer : if _pointer :
_uri = "/".join(["api",_key,_name]) _uri = "/".join(["api",_key,_name])
_map[_uri] = _pointer _map[_uri] = _pointer
# #
# Let us load the default plugins # We are adding some source specific plugins to the user-defined plugins
# This is intended to have out-of the box plugins...
#
if 'source' in _config['system'] and _config['system']['source']['id'] == 'cloud' : if 'source' in _config['system'] and _config['system']['source']['id'] == 'cloud' :
_plugins = cloud.plugins() _plugins = cloud.plugins()
else: else:
_plugins = disk.plugins() _plugins = disk.plugins()
# #
# If there are any plugins found, we should load them and use them # If there are any plugins found, we should load them and use them
if _plugins : if _plugins :
_map = dict(_map,**_plugins) _map = dict(_map,**_plugins)
return _map return _map
@ -182,3 +185,12 @@ class components :
env = Environment(loader=BaseLoader()) env = Environment(loader=BaseLoader())
# env.globals['routes'] = _config['plugins'] # env.globals['routes'] = _config['plugins']
return env return env
@staticmethod
def get_system(_config,skip_keys=[]):
_system = copy.deepcopy(_config['system'])
if skip_keys :
for key in skip_keys :
if key in _system :
del _system
return _system

@ -6,8 +6,15 @@ import nextcloud_client as nc
import copy import copy
from mistune import markdown from mistune import markdown
import time
_CLOUDHANDLER = None _CLOUDHANDLER = None
def login(**_args):
nx = nc.Client(_args['url'])
nx.login(_args['uid'],_args['token'])
time.sleep(1)
return nx
def _format_root_folder (_root): def _format_root_folder (_root):
if _root[0] == '/' : if _root[0] == '/' :
_root = _root[1:] _root = _root[1:]
@ -21,22 +28,23 @@ def content(_args):
:token :token
:folder :folder
""" """
global _CLOUDHANDLER # global _CLOUDHANDLER
_handler = nc.Client(_args['url']) # if not _CLOUDHANDLER :
_handler.login(_args['uid'],_args['token']) # _handler = nc.Client(_args['url'])
_CLOUDHANDLER = _handler # _handler.login(_args['uid'],_args['token'])
_files = _handler.list(_args['folder'],10) # _CLOUDHANDLER = _handler
_handler = login(**_args)
_root = _args['folder'] _root = _args['folder']
if _root.startswith('/') : if _root.startswith('/') :
_root = _root[1:] _root = _root[1:]
if _root.endswith('/') : if _root.endswith('/') :
_root = _root[:-1] _root = _root[:-1]
_files = _handler.list(_root,30)
_menu = {} #[_args['folder']] + [_item for _item in _files if _item.file_type == 'dir' and _item.name[0] not in ['.','_']] _menu = {} #[_args['folder']] + [_item for _item in _files if _item.file_type == 'dir' and _item.name[0] not in ['.','_']]
_menu = {} #dict.fromkeys(_menu,[]) _menu = {} #dict.fromkeys(_menu,[])
for _item in _files : for _item in _files :
_folder = _item.path.split(_item.name)[0].strip() _folder = _item.path.split(_item.name)[0].strip()
_folder = _folder.replace(_root,'').replace('/','') _folder = _folder.replace(_root,'').replace('/','')
if _item.name[0] in ['.','_'] or _folder == '': if _item.name[0] in ['.','_'] or _folder == '':
continue ; continue ;
@ -60,6 +68,7 @@ def content(_args):
# clean up the content ... # clean up the content ...
_keys = [_name for _name in _menu.keys() if _name.startswith('_')] _keys = [_name for _name in _menu.keys() if _name.startswith('_')]
[_menu.pop(_name) for _name in _keys] [_menu.pop(_name) for _name in _keys]
_handler.logout()
return _menu return _menu
@ -72,10 +81,12 @@ def build(_config):
_args = copy.deepcopy(_config['system']['source']['auth']) _args = copy.deepcopy(_config['system']['source']['auth'])
_args['folder'] = _config['layout']['root'] _args['folder'] = _config['layout']['root']
# update(_config) # update(_config)
return content(_args) _menu = content(_args)
return _menu
def html (uri,_config) : def html (uri,_config) :
global _CLOUDHANDLER # global _CLOUDHANDLER
_handler = _CLOUDHANDLER # _handler = _CLOUDHANDLER
_handler = login(**_config['system']['source']['auth'])
_root = _format_root_folder(_config['layout']['root']) _root = _format_root_folder(_config['layout']['root'])
uri = _format_root_folder (uri) uri = _format_root_folder (uri)
@ -85,7 +96,10 @@ def html (uri,_config) :
# _link = '/'.join(['api/cloud/download?doc='+_prefix,'_images']) # _link = '/'.join(['api/cloud/download?doc='+_prefix,'_images'])
_html = _handler.get_file_contents(uri).decode('utf-8').replace('.attachments.',_link) _html = _handler.get_file_contents(uri).decode('utf-8').replace('.attachments.',_link)
# print ([uri,uri[-2:] ,uri[-2:] in ['md','MD','markdown']]) # print ([uri,uri[-2:] ,uri[-2:] in ['md','MD','markdown']])
return markdown(_html) if uri[-2:] in ['md','MD','Md','mD'] else _html.replace(_root,('{{context}}api/cloud/download?doc='+_root)) _handler.logout()
# if uri.endswith('.md'):
# _html = _html.replace(_root,('{{context}}api/cloud/download?doc='+_root))
return markdown(_html) if uri[-2:] in ['md','MD','Md','mD'] else _html
# def update (_config): # def update (_config):
# """ # """
# This function updates the configuration provided by loading default plugins # This function updates the configuration provided by loading default plugins
@ -95,13 +109,14 @@ def html (uri,_config) :
# _config['plugins'] = plugins () # _config['plugins'] = plugins ()
# return _config # return _config
def download(**_args): def download(**_args):
_handler = _CLOUDHANDLER _auth = _args['config']['system']['source']['auth']
_handler = login(**_auth)
if _args['doc'][-2:] in ['md','ht']: if _args['doc'][-2:] in ['md','ht']:
_stream = html(_args['doc'],_args['config']) _stream = html(_args['doc'],_args['config'])
else: else:
_stream = _handler.get_file_contents(_args['doc']) _stream = _handler.get_file_contents(_args['doc'])
_handler.logout()
return _stream return _stream
pass pass

@ -31,7 +31,7 @@ def _index ():
_args = {} _args = {}
if 'plugins' in _config : if 'plugins' in _config :
_args['routes']=['plugins'] _args['routes']=['plugins']
_system = copy.deepcopy(_config['system']) _system = cms.components.get_system(_config) #copy.deepcopy(_config['system'])
try: try:
_args['layout'] = _config['layout'] _args['layout'] = _config['layout']
@ -157,14 +157,13 @@ def cms_page():
_args = {'layout':_config['layout']} _args = {'layout':_config['layout']}
if 'plugins' in _config: if 'plugins' in _config:
_args['routes'] = _config['plugins'] _args['routes'] = _config['plugins']
_system = copy.deepcopy(_config['system'])
_system = cms.components.get_system(_config)
_html = cms.components.html(_uri,_id,_args,_system) _html = cms.components.html(_uri,_id,_args,_system)
e = Environment(loader=BaseLoader()).from_string(_html) e = Environment(loader=BaseLoader()).from_string(_html)
# _data = {} #cms.components.data(_config) # _data = {} #cms.components.data(_config)
if 'source' in _system : _system = cms.components.get_system(_config)
del _system['source']
_args['system'] = _system _args['system'] = _system
_html = e.render(**_args) _html = e.render(**_args)
@ -175,13 +174,16 @@ def _cms_page ():
_uri = request.args['uri'] _uri = request.args['uri']
_uri = os.sep.join([_config['layout']['root'],_uri]) _uri = os.sep.join([_config['layout']['root'],_uri])
_title = request.args['title'] if 'title' in request.args else '' _title = request.args['title'] if 'title' in request.args else ''
_args = {'system':_config['system']} _args = {'system':cms.components.get_system(_config) }
if 'plugins' in _config: if 'plugins' in _config:
_args['routes'] = _config['plugins'] _args['routes'] = _config['plugins']
_html = cms.components.html(_uri,_title,_args) _html = cms.components.html(_uri,_title,_args)
e = Environment(loader=BaseLoader()).from_string(_html) e = Environment(loader=BaseLoader()).from_string(_html)
return e.render(**_args),200 return e.render(**_args),200
@_app.route('/reload',methods=['POST'])
def reload():
pass
# #
# Let us bootup the application # Let us bootup the application
SYS_ARGS = {} SYS_ARGS = {}
@ -221,8 +223,10 @@ if __name__ == '__main__' :
if 'order' in _config['layout'] and 'menu' in _config['layout']['order']: if 'order' in _config['layout'] and 'menu' in _config['layout']['order']:
_sortedmenu = {} _sortedmenu = {}
for _name in _config['layout']['order']['menu'] : for _name in _config['layout']['order']['menu'] :
if _name in _menu :
_sortedmenu[_name] = _menu[_name] _sortedmenu[_name] = _menu[_name]
_menu = _sortedmenu
_menu = _sortedmenu if _sortedmenu else _menu
_config['layout']['menu'] = _menu #cms.components.menu(_config) _config['layout']['menu'] = _menu #cms.components.menu(_config)
# if 'data' in _config : # if 'data' in _config :
# _config['data'] = cms.components.data(_config['data']) # _config['data'] = cms.components.data(_config['data'])

@ -3,4 +3,4 @@
Hard code whatever you want here Hard code whatever you want here
@TODO: Perhaps have code to add things here onload @TODO: Perhaps have code to add things here onload
--> -->
<iframe src="/api/cloud/download?doc=/www/_news/index.html"></iframe> <iframe src="/api/cloud/download?doc=/www/data-transport/quick-start.md"></iframe>

Loading…
Cancel
Save