changeset 1277:2e4fcd31f2a9

pages: use Path in onFileChange + fixed encoding issue
author Goffi <goffi@goffi.org>
date Fri, 19 Jun 2020 16:47:50 +0200
parents cad8f24e23d4
children 4385a75e3962
files libervia/server/pages.py
diffstat 1 files changed, 12 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/libervia/server/pages.py	Fri Jun 19 16:47:50 2020 +0200
+++ b/libervia/server/pages.py	Fri Jun 19 16:47:50 2020 +0200
@@ -449,8 +449,8 @@
         """
         if flags == ['create']:
             return
-        path = file_path.path.decode('utf-8')
-        base_name = os.path.basename(path)
+        path = Path(file_path.path.decode())
+        base_name = path.name
         if base_name != "page_meta.py":
             # we only handle libervia pages
             return
@@ -458,13 +458,15 @@
         log.debug("{flags} event(s) received for {file_path}".format(
             flags=", ".join(flags), file_path=file_path))
 
-        dir_path = os.path.dirname(path)
-        if not dir_path.startswith(str(site_path)):
-            raise exceptions.InternalError("watched file should start with site path")
+        dir_path = path.parent
+
+        if dir_path == site_path:
+            return
 
-        path_elts = [p for p in dir_path[len(site_path):].split('/') if p]
-        if not path_elts:
-            return
+        if not site_path in dir_path.parents:
+            raise exceptions.InternalError("watched file should be in a subdirectory of site path")
+
+        path_elts = list(dir_path.relative_to(site_path).parts)
 
         if path_elts[0] == C.PAGES_DIR:
             # a page has been modified
@@ -476,6 +478,7 @@
             parent = page = site_root
             new_page = False
             for idx, child_name in enumerate(path_elts):
+                child_name = child_name.encode()
                 try:
                     try:
                         page = page.original.children[child_name]
@@ -507,7 +510,7 @@
             except Exception as e:
                 log.warning(_("Can't create page: {reason}").format(reason=e))
             else:
-                url_elt = path_elts[-1]
+                url_elt = path_elts[-1].encode()
                 if not new_page:
                     # the page was already existing, we remove it
                     del parent.children[url_elt]