routing.py 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. # GNU MediaGoblin -- federated, autonomous media hosting
  2. # Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS.
  3. #
  4. # This program is free software: you can redistribute it and/or modify
  5. # it under the terms of the GNU Affero General Public License as published by
  6. # the Free Software Foundation, either version 3 of the License, or
  7. # (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU Affero General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU Affero General Public License
  15. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. import logging
  17. import six
  18. from six.moves.urllib.parse import urlparse
  19. from werkzeug.routing import Map, Rule
  20. from mediagoblin.tools.common import import_component
  21. _log = logging.getLogger(__name__)
  22. url_map = Map()
  23. class MGRoute(Rule):
  24. def __init__(self, endpoint, url, controller, match_slash=True):
  25. Rule.__init__(self, url, endpoint=endpoint)
  26. self.gmg_controller = controller
  27. self.match_slash = match_slash
  28. def empty(self):
  29. new_rule = Rule.empty(self)
  30. new_rule.gmg_controller = self.gmg_controller
  31. return new_rule
  32. def match(self, path, *args, **kwargs):
  33. if not (self.match_slash or path.endswith("/")):
  34. path = path + "/"
  35. return super(MGRoute, self).match(path, *args, **kwargs)
  36. def endpoint_to_controller(rule):
  37. endpoint = rule.endpoint
  38. view_func = rule.gmg_controller
  39. _log.debug('endpoint: {0} view_func: {1}'.format(endpoint, view_func))
  40. # import the endpoint, or if it's already a callable, call that
  41. if isinstance(view_func, six.string_types):
  42. view_func = import_component(view_func)
  43. rule.gmg_controller = view_func
  44. return view_func
  45. def add_route(endpoint, url, controller, *args, **kwargs):
  46. """
  47. Add a route to the url mapping
  48. """
  49. url_map.add(MGRoute(endpoint, url, controller, *args, **kwargs))
  50. def mount(mountpoint, routes):
  51. """
  52. Mount a bunch of routes to this mountpoint
  53. """
  54. for endpoint, url, controller in routes:
  55. url = "%s/%s" % (mountpoint.rstrip('/'), url.lstrip('/'))
  56. add_route(endpoint, url, controller)
  57. def extract_url_arguments(url, urlmap):
  58. """
  59. This extracts the URL arguments from a given URL
  60. """
  61. parsed_url = urlparse(url)
  62. map_adapter = urlmap.bind(
  63. server_name=parsed_url.netloc,
  64. script_name=parsed_url.path,
  65. url_scheme=parsed_url.scheme,
  66. path_info=parsed_url.path
  67. )
  68. return map_adapter.match()[1]