comparison src/server/server.py @ 925:e00151140f77

server (pages): URIs handling: a page can now handle specific URIs by using uri_handlers variable: it's a dict mapping URIs (type/subtype) tuples to a callback. The callback must generate the URI path (relative to the page) including the needed URI data
author Goffi <goffi@goffi.org>
date Mon, 03 Apr 2017 01:00:29 +0200
parents 94f88277c2e7
children 612e33fd32a8
comparison
equal deleted inserted replaced
924:94f88277c2e7 925:e00151140f77
1420 1420
1421 1421
1422 class LiberviaPage(web_resource.Resource): 1422 class LiberviaPage(web_resource.Resource):
1423 isLeaf = True # we handle subpages ourself 1423 isLeaf = True # we handle subpages ourself
1424 named_pages = {} 1424 named_pages = {}
1425 uri_callbacks = {}
1425 1426
1426 def __init__(self, host, root_dir, name=None, redirect=None, access=None, parse_url=None, 1427 def __init__(self, host, root_dir, name=None, redirect=None, access=None, parse_url=None,
1427 prepare_render=None, render=None, template=None): 1428 prepare_render=None, render=None, template=None):
1428 """initiate LiberviaPages 1429 """initiate LiberviaPages
1429 1430
1495 if render is not None: 1496 if render is not None:
1496 log.error(_(u"render can't be used at the same time as template")) 1497 log.error(_(u"render can't be used at the same time as template"))
1497 if parse_url is not None and not callable(parse_url): 1498 if parse_url is not None and not callable(parse_url):
1498 log.error(_(u"parse_url must be a callable")) 1499 log.error(_(u"parse_url must be a callable"))
1499 1500
1500 @staticmethod 1501 @classmethod
1501 def importPages(host, parent=None, path=None): 1502 def importPages(cls, host, parent=None, path=None):
1502 if path is None: 1503 if path is None:
1503 path = [] 1504 path = []
1504 if parent is None: 1505 if parent is None:
1505 root_dir = os.path.join(os.path.dirname(libervia.__file__), C.PAGES_DIR) 1506 root_dir = os.path.join(os.path.dirname(libervia.__file__), C.PAGES_DIR)
1506 parent = host 1507 parent = host
1529 render=page_data.get('render'), 1530 render=page_data.get('render'),
1530 template=page_data.get('template')) 1531 template=page_data.get('template'))
1531 parent.putChild(d, resource) 1532 parent.putChild(d, resource)
1532 new_path = path + [d] 1533 new_path = path + [d]
1533 log.info(u"Added /{path} page".format(path=u'[...]/'.join(new_path))) 1534 log.info(u"Added /{path} page".format(path=u'[...]/'.join(new_path)))
1535 if 'uri_handlers' in page_data:
1536 if not isinstance(page_data, dict):
1537 log.error(_(u'uri_handlers must be a dict'))
1538 else:
1539 for uri_tuple, cb_name in page_data['uri_handlers'].iteritems():
1540 if len(uri_tuple) != 2 or not isinstance(cb_name, basestring):
1541 log.error(_(u"invalid uri_tuple"))
1542 continue
1543 log.info(_(u'setting {}/{} URIs handler').format(*uri_tuple))
1544 try:
1545 cb = page_data[cb_name]
1546 except KeyError:
1547 log.error(_(u'missing {name} method to handle {1}/{2}').format(
1548 name = cb_name, *uri_tuple))
1549 continue
1550 else:
1551 cls.registerURI(uri_tuple, cb, new_path)
1552
1534 LiberviaPage.importPages(host, resource, new_path) 1553 LiberviaPage.importPages(host, resource, new_path)
1554
1555 @classmethod
1556 def registerURI(cls, uri_tuple, get_uri_cb, pre_path):
1557 """register a URI handler
1558
1559 @param uri_tuple(tuple[unicode, unicode]): type or URIs handler
1560 type/subtype as returned by tools/common/parseXMPPUri
1561 @param get_uri_cb(callable): method which take uri_data dict as only argument
1562 and return path with correct arguments relative to page itself
1563 @param pre_path(list[unicode]): prefix path to reference the handler page
1564 """
1565 if uri_tuple in cls.uri_callbacks:
1566 log.info(_(u"{}/{} URIs are already handled, replacing by the new handler").format(*uri_tuple))
1567 cls.uri_callbacks[uri_tuple] = {u'callback': get_uri_cb,
1568 u'pre_path': pre_path}
1535 1569
1536 def getChildWithDefault(self, path, request): 1570 def getChildWithDefault(self, path, request):
1537 # we handle children ourselves 1571 # we handle children ourselves
1538 raise exceptions.InternalError(u"this method should not be used with LiberviaPage") 1572 raise exceptions.InternalError(u"this method should not be used with LiberviaPage")
1539 1573