diff libervia/server/server.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 079e8eb6e327
children a1606e2a92eb
line wrap: on
line diff
--- a/libervia/server/server.py	Sat Apr 25 16:08:55 2020 +0200
+++ b/libervia/server/server.py	Sun Apr 26 22:01:13 2020 +0200
@@ -125,13 +125,13 @@
 class ProtectedFile(static.File):
     """A static.File class which doesn't show directory listing"""
 
-    def __init__(self, *args, **kwargs):
+    def __init__(self, path, *args, **kwargs):
         if "defaultType" not in kwargs and len(args) < 2:
             # defaultType is second positional argument, and Twisted uses it
             # in File.createSimilarFile, so we set kwargs only if it is missing
             # in kwargs and it is not in a positional argument
             kwargs["defaultType"] = "application/octet-stream"
-        super(ProtectedFile, self).__init__(*args, **kwargs)
+        super(ProtectedFile, self).__init__(str(path), *args, **kwargs)
 
     def directoryListing(self):
         return web_resource.NoResource()
@@ -159,10 +159,19 @@
         self.site_name = site_name
         self.site_path = site_path
         self.named_pages = {}
+        self.browser_modules = {}
         self.uri_callbacks = {}
         self.pages_redirects = {}
         self.cached_urls = {}
         self.main_menu = None
+        build_path = host.getBuildPath(site_name)
+        build_path.mkdir(parents=True, exist_ok=True)
+        self.putChild(
+            C.BUILD_DIR.encode(),
+            ProtectedFile(
+                build_path,
+                defaultType="application/octet-stream"),
+        )
 
     def __str__(self):
         return ("Root resource for {host_name} using {site_name} at {site_path} and "
@@ -755,8 +764,8 @@
         # self.sat_root is official Libervia site
         root_path = os.path.join(default_site_path, C.TEMPLATE_STATIC_DIR)
         self.sat_root = default_root = LiberviaRootResource(
-            host=self, host_name='', site_name='', site_path=default_site_path,
-            path=root_path)
+            host=self, host_name='', site_name='',
+            site_path=default_site_path, path=root_path)
         if self.options['dev_mode']:
             self.files_watcher.watchDir(
                 default_site_path, auto_add=True, recursive=True,
@@ -772,6 +781,9 @@
         existing_vhosts = {b'': default_root}
 
         for host_name, site_name in self.options["vhosts_dict"].items():
+            if site_name == C.SITE_NAME_DEFAULT:
+                raise ValueError(
+                    f"{C.DEFAULT_SITE_NAME} is reserved and can't be used in vhosts_dict")
             encoded_site_name = site_name.encode('utf-8')
             try:
                 site_path = self.renderer.sites_paths[site_name]
@@ -805,12 +817,6 @@
                         site_path, auto_add=True, recursive=True,
                         callback=LiberviaPage.onFileChange, site_root=res,
                         site_path=site_path)
-                res.putChild(
-                    C.BUILD_DIR.encode('utf-8'),
-                    ProtectedFile(
-                        self.getBuildPath(site_name),
-                        defaultType="application/octet-stream"),
-                )
 
                 LiberviaPage.importPages(self, res)
                 # FIXME: default pages are accessible if not overriden by external website
@@ -1277,15 +1283,15 @@
         """Generate build path for a given site name
 
         @param site_name(unicode): name of the site
-        @return (unicode): path to the build directory
+        @return (Path): path to the build directory
         """
         build_path_elts = [
             config.getConfig(self.main_conf, "", "local_dir"),
             C.CACHE_DIR,
             C.LIBERVIA_CACHE,
-            regex.pathEscape(site_name)]
-        build_path = "/".join(build_path_elts)
-        return os.path.abspath(os.path.expanduser(build_path))
+            regex.pathEscape(site_name or C.SITE_NAME_DEFAULT)]
+        build_path = Path("/".join(build_path_elts))
+        return build_path.expanduser().resolve()
 
     def getExtBaseURLData(self, request):
         """Retrieve external base URL Data