comparison libervia/server/pages.py @ 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 0e4e413eb8db
comparison
equal deleted inserted replaced
1276:cad8f24e23d4 1277:2e4fcd31f2a9
447 @param site_root(LiberviaRootResource): root of the site 447 @param site_root(LiberviaRootResource): root of the site
448 @param site_path(unicode): absolute path of the site 448 @param site_path(unicode): absolute path of the site
449 """ 449 """
450 if flags == ['create']: 450 if flags == ['create']:
451 return 451 return
452 path = file_path.path.decode('utf-8') 452 path = Path(file_path.path.decode())
453 base_name = os.path.basename(path) 453 base_name = path.name
454 if base_name != "page_meta.py": 454 if base_name != "page_meta.py":
455 # we only handle libervia pages 455 # we only handle libervia pages
456 return 456 return
457 457
458 log.debug("{flags} event(s) received for {file_path}".format( 458 log.debug("{flags} event(s) received for {file_path}".format(
459 flags=", ".join(flags), file_path=file_path)) 459 flags=", ".join(flags), file_path=file_path))
460 460
461 dir_path = os.path.dirname(path) 461 dir_path = path.parent
462 if not dir_path.startswith(str(site_path)): 462
463 raise exceptions.InternalError("watched file should start with site path") 463 if dir_path == site_path:
464
465 path_elts = [p for p in dir_path[len(site_path):].split('/') if p]
466 if not path_elts:
467 return 464 return
465
466 if not site_path in dir_path.parents:
467 raise exceptions.InternalError("watched file should be in a subdirectory of site path")
468
469 path_elts = list(dir_path.relative_to(site_path).parts)
468 470
469 if path_elts[0] == C.PAGES_DIR: 471 if path_elts[0] == C.PAGES_DIR:
470 # a page has been modified 472 # a page has been modified
471 del path_elts[0] 473 del path_elts[0]
472 if not path_elts: 474 if not path_elts:
474 return 476 return
475 # we retrieve page by starting from site root and finding each path element 477 # we retrieve page by starting from site root and finding each path element
476 parent = page = site_root 478 parent = page = site_root
477 new_page = False 479 new_page = False
478 for idx, child_name in enumerate(path_elts): 480 for idx, child_name in enumerate(path_elts):
481 child_name = child_name.encode()
479 try: 482 try:
480 try: 483 try:
481 page = page.original.children[child_name] 484 page = page.original.children[child_name]
482 except AttributeError: 485 except AttributeError:
483 page = page.children[child_name] 486 page = page.children[child_name]
505 # EncodingResourceWrapper should probably be removed 508 # EncodingResourceWrapper should probably be removed
506 resource.children = page.children 509 resource.children = page.children
507 except Exception as e: 510 except Exception as e:
508 log.warning(_("Can't create page: {reason}").format(reason=e)) 511 log.warning(_("Can't create page: {reason}").format(reason=e))
509 else: 512 else:
510 url_elt = path_elts[-1] 513 url_elt = path_elts[-1].encode()
511 if not new_page: 514 if not new_page:
512 # the page was already existing, we remove it 515 # the page was already existing, we remove it
513 del parent.children[url_elt] 516 del parent.children[url_elt]
514 # we can now add the new page 517 # we can now add the new page
515 parent.putChild(url_elt, resource) 518 parent.putChild(url_elt, resource)