Advertisement
Guest User

Untitled

a guest
Jun 24th, 2019
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.86 KB | None | 0 0
  1. ~/code/projects/amnesiacms/ cat amnesia/traversal.py
  2. # -*- coding: utf-8 -*-
  3.  
  4. import logging
  5.  
  6. from pyramid.traversal import _join_path_tuple
  7. from pyramid.traversal import resource_path_tuple
  8.  
  9. from pyramid.interfaces import VH_ROOT_KEY
  10. from pyramid.interfaces import IResourceURL
  11.  
  12. from zope.interface import implementer
  13.  
  14.  
  15. log = logging.getLogger(__name__)
  16.  
  17.  
  18. @implementer(IResourceURL)
  19. class AmnesiaResourceURL:
  20. VH_ROOT_KEY = VH_ROOT_KEY
  21.  
  22. def __init__(self, resource, request):
  23. if hasattr(resource, '_LOCALE_'):
  24. physical_path_tuple = ('', resource._LOCALE_) + resource_path_tuple(resource)[1:]
  25. else:
  26. physical_path_tuple = resource_path_tuple(resource)
  27.  
  28. physical_path = _join_path_tuple(physical_path_tuple)
  29.  
  30. if physical_path_tuple != ('',):
  31. physical_path_tuple = physical_path_tuple + ('',)
  32. physical_path = physical_path + '/'
  33.  
  34. virtual_path = physical_path
  35. virtual_path_tuple = physical_path_tuple
  36.  
  37. environ = request.environ
  38. vroot_path = environ.get(self.VH_ROOT_KEY)
  39.  
  40. # if the physical path starts with the virtual root path, trim it out
  41. # of the virtual path
  42. if vroot_path is not None:
  43. vroot_path = vroot_path.rstrip('/')
  44. if vroot_path and physical_path.startswith(vroot_path):
  45. vroot_path_tuple = tuple(vroot_path.split('/'))
  46. numels = len(vroot_path_tuple)
  47. virtual_path_tuple = ('',) + physical_path_tuple[numels:]
  48. virtual_path = physical_path[len(vroot_path):]
  49.  
  50. self.virtual_path = virtual_path # IResourceURL attr
  51. self.physical_path = physical_path # IResourceURL attr
  52. self.virtual_path_tuple = virtual_path_tuple # IResourceURL attr (1.5)
  53. self.physical_path_tuple = (
  54. physical_path_tuple
  55. ) # IResourceURL attr (1.5)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement