context handling

v2.0
Steve Nyemba 7 months ago
parent bc95832fc9
commit 1523a8fd87

@ -115,11 +115,13 @@ def html (uri,_config) :
_handler = login(_config['system']['source']['auth']) _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)
_context = _config['system']['context']
_prefix = '/'.join (uri.split('/')[:-1]) _prefix = '/'.join (uri.split('/')[:-1])
_link = '/'.join(['{{context}}api/cloud/download?doc='+_prefix,'.attachments.']) _link = '/'.join(['api/cloud/download?doc='+_prefix,'.attachments.'])
if _context :
_link = f'{_context}/{_link}'
# _link = '/'.join(['api/cloud/download?doc='+_prefix,'_images']) # _link = '/'.join(['api/cloud/download?doc='+_prefix,'_images'])
@ -127,8 +129,10 @@ def html (uri,_config) :
# print ([uri,uri[-2:] ,uri[-2:] in ['md','MD','markdown']]) # print ([uri,uri[-2:] ,uri[-2:] in ['md','MD','markdown']])
_handler.logout() _handler.logout()
# if uri.endswith('.md'): # if uri.endswith('.md'):
if not _context :
_html = _html.replace(_root,('{{context}}api/cloud/download?doc='+_root)).replace('.attachments.', copy.deepcopy(_link)) _html = _html.replace(_root,('api/cloud/download?doc='+_root)).replace('.attachments.', copy.deepcopy(_link))
else:
_html = _html.replace(_root,(f'{_context}api/cloud/download?doc='+_root)).replace('.attachments.', copy.deepcopy(_link))
# _html = _html.replace('<br />','') # _html = _html.replace('<br />','')
return markdown(_html) if uri[-2:] in ['md','MD','Md','mD'] else _html return markdown(_html) if uri[-2:] in ['md','MD','Md','mD'] else _html
# def update (_config): # def update (_config):
@ -159,8 +163,11 @@ def _format (uri,_config) :
@TODO: revisit the design pattern @TODO: revisit the design pattern
""" """
return uri return uri
def plugins (): def plugins (_context):
""" """
This function publishes the plugins associated with this module This function publishes the plugins associated with this module
""" """
return {'api/cloud/download':download} key = 'api/cloud/download'
if _context :
key = f'{_context}/{key}'
return {key:download}

@ -14,7 +14,7 @@ def folders (_path,_config):
""" """
_content = os.listdir(_path) _content = os.listdir(_path)
return [_name for _name in _content if os.path.isdir(os.sep.join([_path,_name])) if not _name.startswith('_')] return [_name for _name in _content if os.path.isdir(os.sep.join([_path,_name])) if not _name.startswith('_')]
def content(_folder,_config): def content(_folder,_config,keep=[]):
""" """
:content of the folder :content of the folder
""" """
@ -29,13 +29,13 @@ def content(_folder,_config):
_menuItems = list_files(_folder,_config) #os.listdir(_folder) _menuItems = list_files(_folder,_config) #os.listdir(_folder)
# return [{'text':_name.split('.')[0].replace('_', ' ').replace('-',' ').strip(),'uri': os.sep.join([_folder,_name])} for _name in os.listdir(_folder) if not _name.startswith('_') and os.path.isfile( os.sep.join([_folder,_name]))] # return [{'text':_name.split('.')[0].replace('_', ' ').replace('-',' ').strip(),'uri': os.sep.join([_folder,_name])} for _name in os.listdir(_folder) if not _name.startswith('_') and os.path.isfile( os.sep.join([_folder,_name]))]
return [{'text':_name.split('.')[0].replace('_', ' ').replace('-',' ').strip(),'uri': os.sep.join([_path,_name])} for _name in os.listdir(_folder) if not _name[0] in ['.','_'] and os.path.isfile( os.sep.join([_folder,_name]))] return [{'text':_name.split('.')[0].replace('_', ' ').replace('-',' ').strip(),'uri': os.sep.join([_path,_name])} for _name in os.listdir(_folder) if not _name[0] in ['.','_'] and os.path.isfile( os.sep.join([_folder,_name])) and _name.split('.')[-1] in ['html','md']]
else: else:
return [] return []
def list_files(_folder,_config): def list_files(_folder,_config, keep=[]):
return [name for name in os.listdir(_folder) if name[0] not in ['.','_']] return [name for name in os.listdir(_folder) if name[0] not in ['.','_']]
def build (_config): #(_path,_content): def build (_config, keep=[]): #(_path,_content):
""" """
building the menu for the site given the content is on disk building the menu for the site given the content is on disk
:path path of the files on disk :path path of the files on disk
@ -48,6 +48,7 @@ def build (_config): #(_path,_content):
# print (_path) # print (_path)
_items = folders(_path,_config) _items = folders(_path,_config)
_subItems = [ content (os.sep.join([_path,_name]),_config)for _name in _items ] _subItems = [ content (os.sep.join([_path,_name]),_config)for _name in _items ]
_r = {} _r = {}
for _name in _items : for _name in _items :
_index = _items.index(_name) _index = _items.index(_name)
@ -85,7 +86,6 @@ def read (**_args):
_uri = request.args['uri'] # if 'location' in _layout : _uri = request.args['uri'] # if 'location' in _layout :
# _uri = os.sep.join([_layout['location'],_uri]) # _uri = os.sep.join([_layout['location'],_uri])
_uri = _realpath(_uri, _args['config']) _uri = _realpath(_uri, _args['config'])
if os.path.exists(_uri): if os.path.exists(_uri):
f = open(_uri,mode='rb') f = open(_uri,mode='rb')
_stream = f.read() _stream = f.read()
@ -103,12 +103,15 @@ def exists(**_args):
def html(_uri,_config) : def html(_uri,_config) :
# _html = (open(uri)).read() # _html = (open(uri)).read()
_path = _realpath(_uri,_config) _path = _realpath(_uri,_config)
_context = _config['system']['context']
_html = ( open(_path)).read() _html = ( open(_path)).read()
_layout = _config['layout'] _layout = _config['layout']
if 'location' in _layout : if 'location' in _layout :
if not _config :
_api = os.sep.join(['api/disk/read?uri=',_layout['root']]) _api = os.sep.join(['api/disk/read?uri=',_layout['root']])
else:
_api = os.sep.join([f'{_context}/api/disk/read?uri=',_layout['root']])
_html = _html.replace(_layout['root'],_api) _html = _html.replace(_layout['root'],_api)
_html = markdown(_html) if _uri[-2:] in ['md','MD','Md','mD'] else _html _html = markdown(_html) if _uri[-2:] in ['md','MD','Md','mD'] else _html
return _html return _html
@ -118,15 +121,23 @@ def plugins (**_args):
:path path of the files :path path of the files
:name name of the module :name name of the module
""" """
_context = _args['context']
if 'path' not in _args : if 'path' not in _args :
return {'api/disk/read':read} key = 'api/disk/read'
if _context :
key = f'{_context}/{key}'
return {key:read}
_path = _args['path'] #os.sep.join([_args['root'],'plugin']) _path = _args['path'] #os.sep.join([_args['root'],'plugin'])
if os.path.isdir(_path): if os.path.isdir(_path):
files = os.listdir(_path) files = os.listdir(_path)
if files : if files :
files = [name for name in files if name.endswith('.py')] files = [name for name in files if name.endswith('.py')]
if files: if files:
_path = os.sep.join([_path,files[0]]) _uri = [_path,files[0]]
if _context :
_uri = [_context] + _uri
_path = os.sep.join(_uri)
else: else:
return None return None
else: else:

Loading…
Cancel
Save