From 1523a8fd873646df8bc585847d9a3779a4b7541f Mon Sep 17 00:00:00 2001 From: Steve Nyemba Date: Wed, 24 Apr 2024 16:35:08 -0500 Subject: [PATCH] context handling --- cms/cloud.py | 19 +++++++++++++------ cms/disk.py | 33 ++++++++++++++++++++++----------- 2 files changed, 35 insertions(+), 17 deletions(-) diff --git a/cms/cloud.py b/cms/cloud.py index 7beed1d..718ff89 100644 --- a/cms/cloud.py +++ b/cms/cloud.py @@ -115,11 +115,13 @@ def html (uri,_config) : _handler = login(_config['system']['source']['auth']) _root = _format_root_folder(_config['layout']['root']) uri = _format_root_folder (uri) - + _context = _config['system']['context'] _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']) @@ -127,8 +129,10 @@ def html (uri,_config) : # print ([uri,uri[-2:] ,uri[-2:] in ['md','MD','markdown']]) _handler.logout() # if uri.endswith('.md'): - - _html = _html.replace(_root,('{{context}}api/cloud/download?doc='+_root)).replace('.attachments.', copy.deepcopy(_link)) + if not _context : + _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('
','') return markdown(_html) if uri[-2:] in ['md','MD','Md','mD'] else _html # def update (_config): @@ -159,8 +163,11 @@ def _format (uri,_config) : @TODO: revisit the design pattern """ return uri -def plugins (): +def plugins (_context): """ 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} diff --git a/cms/disk.py b/cms/disk.py index 98230e9..4426be5 100644 --- a/cms/disk.py +++ b/cms/disk.py @@ -14,7 +14,7 @@ def folders (_path,_config): """ _content = os.listdir(_path) 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 """ @@ -29,13 +29,13 @@ def content(_folder,_config): _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([_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: 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 ['.','_']] -def build (_config): #(_path,_content): +def build (_config, keep=[]): #(_path,_content): """ building the menu for the site given the content is on disk :path path of the files on disk @@ -47,7 +47,8 @@ def build (_config): #(_path,_content): _path = _realpath(_path,_config) # print (_path) _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 = {} for _name in _items : _index = _items.index(_name) @@ -85,7 +86,6 @@ def read (**_args): _uri = request.args['uri'] # if 'location' in _layout : # _uri = os.sep.join([_layout['location'],_uri]) _uri = _realpath(_uri, _args['config']) - if os.path.exists(_uri): f = open(_uri,mode='rb') _stream = f.read() @@ -103,12 +103,15 @@ def exists(**_args): def html(_uri,_config) : # _html = (open(uri)).read() _path = _realpath(_uri,_config) - + _context = _config['system']['context'] _html = ( open(_path)).read() _layout = _config['layout'] if 'location' in _layout : - - _api = os.sep.join(['api/disk/read?uri=',_layout['root']]) + if not _config : + _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 = markdown(_html) if _uri[-2:] in ['md','MD','Md','mD'] else _html return _html @@ -118,15 +121,23 @@ def plugins (**_args): :path path of the files :name name of the module """ + _context = _args['context'] 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']) if os.path.isdir(_path): files = os.listdir(_path) if files : files = [name for name in files if name.endswith('.py')] if files: - _path = os.sep.join([_path,files[0]]) + _uri = [_path,files[0]] + if _context : + _uri = [_context] + _uri + _path = os.sep.join(_uri) else: return None else: