urls.py 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. import re
  2. from urllib import urlencode, unquote_plus, quote_plus
  3. from common import pickle_dict, unpickle_dict
  4. # TODO: Use regular Exceptions
  5. class AmbiguousUrlException(Exception):
  6. pass
  7. class NotFoundException(Exception):
  8. pass
  9. class UrlRule(object):
  10. '''This object stores the various properties related to a routing URL rule.
  11. It also provides a few methods to create URLs from the rule or to match a
  12. given URL against a rule.
  13. :param url_rule: The relative url pattern for the rule. It may include
  14. <var_name> to denote where dynamic variables should be
  15. matched.
  16. :param view_func: The function that should be bound to this rule. This
  17. should be an actual function object.
  18. .. warning:: The function signature should match any
  19. variable names in the provided url_rule.
  20. :param name: The name of the url rule. This is used in the reverse process
  21. of creating urls for a given rule.
  22. :param options: A dict containing any default values for the url rule.
  23. '''
  24. def __init__(self, url_rule, view_func, name, options=None, cache=True, update=False, content_type="", view_mode=0, goto_top=True):
  25. self._name = name
  26. self._url_rule = url_rule
  27. self._view_func = view_func
  28. self._goto_top = goto_top
  29. self._cache = cache
  30. self._update = update
  31. self._content_type = content_type
  32. self._view_mode = view_mode
  33. self._options = {}
  34. if options:
  35. self._options.update(options)
  36. self._keywords = re.findall(r'\<(.+?)\>', url_rule)
  37. # change <> to {} for use with str.format()
  38. self._url_format = self._url_rule.replace('<', '{').replace('>', '}')
  39. # Make a regex pattern for matching incoming URLs
  40. rule = self._url_rule
  41. if rule != '/':
  42. # Except for a path of '/', the trailing slash is optional.
  43. rule = self._url_rule.rstrip('/') + '/?'
  44. p = rule.replace('<', '(?P<').replace('>', '>[^/]+?)')
  45. try:
  46. self._regex = re.compile('^' + p + '$')
  47. except re.error, e:
  48. raise ValueError, ('There was a problem creating this URL rule. '
  49. 'Ensure you do not have any unpaired angle '
  50. 'brackets: "<" or ">"')
  51. def __eq__(self, other):
  52. return (
  53. (self._name, self._url_rule, self._view_func, self._options) ==
  54. (other._name, other._url_rule, other._view_func, other._options)
  55. )
  56. def __ne__(self, other):
  57. return not self.__eq__(other)
  58. def match(self, path):
  59. '''Attempts to match a url to the given path. If successful, a tuple is
  60. returned. The first item is the matchd function and the second item is
  61. a dictionary containing items to be passed to the function parsed from
  62. the provided path.
  63. If the provided path does not match this url rule then a
  64. NotFoundException is raised.
  65. '''
  66. m = self._regex.search(path)
  67. if not m:
  68. raise NotFoundException
  69. # urlunencode the values
  70. items = dict((key, unquote_plus(val))
  71. for key, val in m.groupdict().items())
  72. # unpickle any items if present
  73. items = unpickle_dict(items)
  74. # We need to update our dictionary with default values provided in
  75. # options if the keys don't already exist.
  76. return self._view_func, items
  77. def _make_path(self, items):
  78. '''Returns a relative path for the given dictionary of items.
  79. Uses this url rule's url pattern and replaces instances of <var_name>
  80. with the appropriate value from the items dict.
  81. '''
  82. for key, val in items.items():
  83. if not isinstance(val, basestring):
  84. raise content_typeError, ('Value "%s" for key "%s" must be an instance'
  85. ' of basestring' % (val, key))
  86. items[key] = quote_plus(val)
  87. try:
  88. path = self._url_format.format(**items)
  89. except AttributeError:
  90. # Old version of python
  91. path = self._url_format
  92. for key, val in items.items():
  93. path = path.replace('{%s}' % key, val)
  94. return path
  95. def _make_qs(self, items):
  96. '''Returns a query string for the given dictionary of items. All keys
  97. and values in the provided items will be urlencoded. If necessary, any
  98. python objects will be pickled before being urlencoded.
  99. '''
  100. return urlencode(pickle_dict(items))
  101. def make_path_qs(self, items):
  102. '''Returns a relative path complete with query string for the given
  103. dictionary of items.
  104. Any items with keys matching this rule's url pattern will be inserted
  105. into the path. Any remaining items will be appended as query string
  106. parameters.
  107. All items will be urlencoded. Any items which are not instances of
  108. basestring, or int/long will be pickled before being urlencoded.
  109. .. warning:: The pickling of items only works for key/value pairs which
  110. will be in the query string. This behavior should only be
  111. used for the simplest of python objects. It causes the
  112. URL to get very lengthy (and unreadable) and XBMC has a
  113. hard limit on URL length. See the caching section if you
  114. need to persist a large amount of data between requests.
  115. '''
  116. # Convert any ints and longs to strings
  117. for key, val in items.items():
  118. if isinstance(val, (int, long)):
  119. items[key] = str(val)
  120. # First use our defaults passed when registering the rule
  121. url_items = {}
  122. # Now update with any items explicitly passed to url_for
  123. url_items.update((key, val) for key, val in items.items()
  124. if key in self._keywords)
  125. # Create the path
  126. path = self._make_path(url_items)
  127. # Extra arguments get tacked on to the query string
  128. qs_items = dict((key, val) for key, val in items.items()
  129. if key not in self._keywords)
  130. qs = self._make_qs(qs_items)
  131. if qs:
  132. return '?'.join([path, qs])
  133. return path
  134. @property
  135. def regex(self):
  136. '''The regex for matching paths against this url rule.'''
  137. return self._regex
  138. @property
  139. def view_func(self):
  140. '''The bound function'''
  141. return self._view_func
  142. @property
  143. def url_format(self):
  144. '''The url pattern'''
  145. return self._url_format
  146. @property
  147. def name(self):
  148. '''The name of this url rule.'''
  149. return self._name
  150. @property
  151. def keywords(self):
  152. '''The list of path keywords for this url rule.'''
  153. return self._keywords
  154. @property
  155. def options(self):
  156. return self._options
  157. @property
  158. def cache(self):
  159. return self._cache
  160. @property
  161. def update(self):
  162. return self._update
  163. @property
  164. def content_type(self):
  165. return self._content_type
  166. @property
  167. def view_mode(self):
  168. return self._view_mode
  169. @property
  170. def goto_top(self):
  171. return self._goto_top