httpresourcetree.py 3.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. # Copyright 2016 OpenMarket Ltd
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. from twisted.web.resource import Resource
  15. import logging
  16. logger = logging.getLogger(__name__)
  17. def create_resource_tree(desired_tree, root_resource):
  18. """Create the resource tree for this Home Server.
  19. This in unduly complicated because Twisted does not support putting
  20. child resources more than 1 level deep at a time.
  21. Args:
  22. web_client (bool): True to enable the web client.
  23. root_resource (twisted.web.resource.Resource): The root
  24. resource to add the tree to.
  25. Returns:
  26. twisted.web.resource.Resource: the ``root_resource`` with a tree of
  27. child resources added to it.
  28. """
  29. # ideally we'd just use getChild and putChild but getChild doesn't work
  30. # unless you give it a Request object IN ADDITION to the name :/ So
  31. # instead, we'll store a copy of this mapping so we can actually add
  32. # extra resources to existing nodes. See self._resource_id for the key.
  33. resource_mappings = {}
  34. for full_path, res in desired_tree.items():
  35. logger.info("Attaching %s to path %s", res, full_path)
  36. last_resource = root_resource
  37. for path_seg in full_path.split('/')[1:-1]:
  38. if path_seg not in last_resource.listNames():
  39. # resource doesn't exist, so make a "dummy resource"
  40. child_resource = Resource()
  41. last_resource.putChild(path_seg, child_resource)
  42. res_id = _resource_id(last_resource, path_seg)
  43. resource_mappings[res_id] = child_resource
  44. last_resource = child_resource
  45. else:
  46. # we have an existing Resource, use that instead.
  47. res_id = _resource_id(last_resource, path_seg)
  48. last_resource = resource_mappings[res_id]
  49. # ===========================
  50. # now attach the actual desired resource
  51. last_path_seg = full_path.split('/')[-1]
  52. # if there is already a resource here, thieve its children and
  53. # replace it
  54. res_id = _resource_id(last_resource, last_path_seg)
  55. if res_id in resource_mappings:
  56. # there is a dummy resource at this path already, which needs
  57. # to be replaced with the desired resource.
  58. existing_dummy_resource = resource_mappings[res_id]
  59. for child_name in existing_dummy_resource.listNames():
  60. child_res_id = _resource_id(
  61. existing_dummy_resource, child_name
  62. )
  63. child_resource = resource_mappings[child_res_id]
  64. # steal the children
  65. res.putChild(child_name, child_resource)
  66. # finally, insert the desired resource in the right place
  67. last_resource.putChild(last_path_seg, res)
  68. res_id = _resource_id(last_resource, last_path_seg)
  69. resource_mappings[res_id] = res
  70. return root_resource
  71. def _resource_id(resource, path_seg):
  72. """Construct an arbitrary resource ID so you can retrieve the mapping
  73. later.
  74. If you want to represent resource A putChild resource B with path C,
  75. the mapping should looks like _resource_id(A,C) = B.
  76. Args:
  77. resource (Resource): The *parent* Resourceb
  78. path_seg (str): The name of the child Resource to be attached.
  79. Returns:
  80. str: A unique string which can be a key to the child Resource.
  81. """
  82. return "%s-%s" % (resource, path_seg)