Mercurial > libervia-web
diff libervia/server/pages.py @ 1246:aaf28d45ae67
pages: browser code, first draft:
- code for browser can now be specified if a page subdirectory named `_browser` exists
- the default engine used is `Brython` (not yet implemented, so it will do nothing for
now)
- build_path now uses C.SITE_NAME_DEFAULT for default site instead of empty string, to
avoid collision with other sites
- ProtectedFile transtype path (first positional argument) to str, so a pathlib.Path can
be used
author | Goffi <goffi@goffi.org> |
---|---|
date | Sun, 26 Apr 2020 22:01:13 +0200 |
parents | f511f8fbbf8a |
children | 6d49fae517ba |
line wrap: on
line diff
--- a/libervia/server/pages.py Sat Apr 25 16:08:55 2020 +0200 +++ b/libervia/server/pages.py Sun Apr 26 22:01:13 2020 +0200 @@ -16,6 +16,16 @@ # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. + +import uuid +import os.path +import urllib.request, urllib.parse, urllib.error +import time +import hashlib +import copy +from functools import reduce +from pathlib import Path + from twisted.web import server from twisted.web import resource as web_resource from twisted.web import util as web_util @@ -34,14 +44,6 @@ from libervia.server.utils import quote, SubPage from libervia.server.classes import WebsocketMeta -import uuid -import os.path -import urllib.request, urllib.parse, urllib.error -import time -import hashlib -import copy -from functools import reduce - log = getLogger(__name__) @@ -173,6 +175,7 @@ self.root_dir = root_dir self.url = url self.name = name + self.dyn_data = {} if name is not None: if (name in self.named_pages and not (replace_on_conflict and self.named_pages[name].url == url)): @@ -331,7 +334,8 @@ if os.path.isfile(meta_path): new_path = _path + [d] try: - page_data, resource = cls.createPage(host, meta_path, vhost_root, new_path) + page_data, resource = cls.createPage( + host, meta_path, vhost_root, new_path) except exceptions.ConflictError as e: if _extra_pages: # extra pages are discarded if there is already an existing page @@ -369,6 +373,18 @@ LiberviaPage.importPages( host, vhost_root, _parent=resource, _path=new_path, _extra_pages=_extra_pages) + # now we check if there is some code for browser + browser_path = Path(dir_path) / C.PAGES_BROWSER_DIR + if browser_path.is_dir(): + # for now we only handle Brython + dyn_data = { + "path": browser_path, + "url_hash": hashlib.sha256( + '/'.join(new_path).encode()).hexdigest(), + } + vhost_root.browser_modules.setdefault( + "brython", []).append(dyn_data) + resource.dyn_data['brython'] = dyn_data @classmethod def onFileChange(cls, host, file_path, flags, site_root, site_path): @@ -1257,6 +1273,16 @@ template_data['locale'] = session_data.locale if self.vhost_root.site_name: template_data['site'] = self.vhost_root.site_name + if self.dyn_data: + for data in self.dyn_data.values(): + template_data_dyn = data.get('template', {}) + try: + scripts = template_data_dyn['scripts'] + except KeyError: + pass + else: + template_data.setdefault('scripts', []).extend(scripts) + template_data.update(template_data_dyn) return self.host.renderer.render( self.template,