Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

299 lignes
9.4KB

  1. # -*- coding: utf-8 -*-
  2. # Copyright (c) 2017 Ian Stapleton Cordasco
  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
  12. # implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. """Module containing the logic for the URIBuilder object."""
  16. from . import compat
  17. from . import normalizers
  18. from . import uri
  19. class URIBuilder(object):
  20. """Object to aid in building up a URI Reference from parts.
  21. .. note::
  22. This object should be instantiated by the user, but it's recommended
  23. that it is not provided with arguments. Instead, use the available
  24. method to populate the fields.
  25. """
  26. def __init__(self, scheme=None, userinfo=None, host=None, port=None,
  27. path=None, query=None, fragment=None):
  28. """Initialize our URI builder.
  29. :param str scheme:
  30. (optional)
  31. :param str userinfo:
  32. (optional)
  33. :param str host:
  34. (optional)
  35. :param int port:
  36. (optional)
  37. :param str path:
  38. (optional)
  39. :param str query:
  40. (optional)
  41. :param str fragment:
  42. (optional)
  43. """
  44. self.scheme = scheme
  45. self.userinfo = userinfo
  46. self.host = host
  47. self.port = port
  48. self.path = path
  49. self.query = query
  50. self.fragment = fragment
  51. def __repr__(self):
  52. """Provide a convenient view of our builder object."""
  53. formatstr = ('URIBuilder(scheme={b.scheme}, userinfo={b.userinfo}, '
  54. 'host={b.host}, port={b.port}, path={b.path}, '
  55. 'query={b.query}, fragment={b.fragment})')
  56. return formatstr.format(b=self)
  57. def add_scheme(self, scheme):
  58. """Add a scheme to our builder object.
  59. After normalizing, this will generate a new URIBuilder instance with
  60. the specified scheme and all other attributes the same.
  61. .. code-block:: python
  62. >>> URIBuilder().add_scheme('HTTPS')
  63. URIBuilder(scheme='https', userinfo=None, host=None, port=None,
  64. path=None, query=None, fragment=None)
  65. """
  66. scheme = normalizers.normalize_scheme(scheme)
  67. return URIBuilder(
  68. scheme=scheme,
  69. userinfo=self.userinfo,
  70. host=self.host,
  71. port=self.port,
  72. path=self.path,
  73. query=self.query,
  74. fragment=self.fragment,
  75. )
  76. def add_credentials(self, username, password):
  77. """Add credentials as the userinfo portion of the URI.
  78. .. code-block:: python
  79. >>> URIBuilder().add_credentials('root', 's3crete')
  80. URIBuilder(scheme=None, userinfo='root:s3crete', host=None,
  81. port=None, path=None, query=None, fragment=None)
  82. >>> URIBuilder().add_credentials('root', None)
  83. URIBuilder(scheme=None, userinfo='root', host=None,
  84. port=None, path=None, query=None, fragment=None)
  85. """
  86. if username is None:
  87. raise ValueError('Username cannot be None')
  88. userinfo = normalizers.normalize_username(username)
  89. if password is not None:
  90. userinfo = '{}:{}'.format(
  91. userinfo,
  92. normalizers.normalize_password(password),
  93. )
  94. return URIBuilder(
  95. scheme=self.scheme,
  96. userinfo=userinfo,
  97. host=self.host,
  98. port=self.port,
  99. path=self.path,
  100. query=self.query,
  101. fragment=self.fragment,
  102. )
  103. def add_host(self, host):
  104. """Add hostname to the URI.
  105. .. code-block:: python
  106. >>> URIBuilder().add_host('google.com')
  107. URIBuilder(scheme=None, userinfo=None, host='google.com',
  108. port=None, path=None, query=None, fragment=None)
  109. """
  110. return URIBuilder(
  111. scheme=self.scheme,
  112. userinfo=self.userinfo,
  113. host=normalizers.normalize_host(host),
  114. port=self.port,
  115. path=self.path,
  116. query=self.query,
  117. fragment=self.fragment,
  118. )
  119. def add_port(self, port):
  120. """Add port to the URI.
  121. .. code-block:: python
  122. >>> URIBuilder().add_port(80)
  123. URIBuilder(scheme=None, userinfo=None, host=None, port='80',
  124. path=None, query=None, fragment=None)
  125. >>> URIBuilder().add_port(443)
  126. URIBuilder(scheme=None, userinfo=None, host=None, port='443',
  127. path=None, query=None, fragment=None)
  128. """
  129. port_int = int(port)
  130. if port_int < 0:
  131. raise ValueError(
  132. 'ports are not allowed to be negative. You provided {}'.format(
  133. port_int,
  134. )
  135. )
  136. if port_int > 65535:
  137. raise ValueError(
  138. 'ports are not allowed to be larger than 65535. '
  139. 'You provided {}'.format(
  140. port_int,
  141. )
  142. )
  143. return URIBuilder(
  144. scheme=self.scheme,
  145. userinfo=self.userinfo,
  146. host=self.host,
  147. port='{}'.format(port_int),
  148. path=self.path,
  149. query=self.query,
  150. fragment=self.fragment,
  151. )
  152. def add_path(self, path):
  153. """Add a path to the URI.
  154. .. code-block:: python
  155. >>> URIBuilder().add_path('sigmavirus24/rfc3985')
  156. URIBuilder(scheme=None, userinfo=None, host=None, port=None,
  157. path='/sigmavirus24/rfc3986', query=None, fragment=None)
  158. >>> URIBuilder().add_path('/checkout.php')
  159. URIBuilder(scheme=None, userinfo=None, host=None, port=None,
  160. path='/checkout.php', query=None, fragment=None)
  161. """
  162. if not path.startswith('/'):
  163. path = '/{}'.format(path)
  164. return URIBuilder(
  165. scheme=self.scheme,
  166. userinfo=self.userinfo,
  167. host=self.host,
  168. port=self.port,
  169. path=normalizers.normalize_path(path),
  170. query=self.query,
  171. fragment=self.fragment,
  172. )
  173. def add_query_from(self, query_items):
  174. """Generate and add a query a dictionary or list of tuples.
  175. .. code-block:: python
  176. >>> URIBuilder().add_query_from({'a': 'b c'})
  177. URIBuilder(scheme=None, userinfo=None, host=None, port=None,
  178. path=None, query='a=b+c', fragment=None)
  179. >>> URIBuilder().add_query_from([('a', 'b c')])
  180. URIBuilder(scheme=None, userinfo=None, host=None, port=None,
  181. path=None, query='a=b+c', fragment=None)
  182. """
  183. query = normalizers.normalize_query(compat.urlencode(query_items))
  184. return URIBuilder(
  185. scheme=self.scheme,
  186. userinfo=self.userinfo,
  187. host=self.host,
  188. port=self.port,
  189. path=self.path,
  190. query=query,
  191. fragment=self.fragment,
  192. )
  193. def add_query(self, query):
  194. """Add a pre-formated query string to the URI.
  195. .. code-block:: python
  196. >>> URIBuilder().add_query('a=b&c=d')
  197. URIBuilder(scheme=None, userinfo=None, host=None, port=None,
  198. path=None, query='a=b&c=d', fragment=None)
  199. """
  200. return URIBuilder(
  201. scheme=self.scheme,
  202. userinfo=self.userinfo,
  203. host=self.host,
  204. port=self.port,
  205. path=self.path,
  206. query=normalizers.normalize_query(query),
  207. fragment=self.fragment,
  208. )
  209. def add_fragment(self, fragment):
  210. """Add a fragment to the URI.
  211. .. code-block:: python
  212. >>> URIBuilder().add_fragment('section-2.6.1')
  213. URIBuilder(scheme=None, userinfo=None, host=None, port=None,
  214. path=None, query=None, fragment='section-2.6.1')
  215. """
  216. return URIBuilder(
  217. scheme=self.scheme,
  218. userinfo=self.userinfo,
  219. host=self.host,
  220. port=self.port,
  221. path=self.path,
  222. query=self.query,
  223. fragment=normalizers.normalize_fragment(fragment),
  224. )
  225. def finalize(self):
  226. """Create a URIReference from our builder.
  227. .. code-block:: python
  228. >>> URIBuilder().add_scheme('https').add_host('github.com'
  229. ... ).add_path('sigmavirus24/rfc3986').finalize().unsplit()
  230. 'https://github.com/sigmavirus24/rfc3986'
  231. >>> URIBuilder().add_scheme('https').add_host('github.com'
  232. ... ).add_path('sigmavirus24/rfc3986').add_credentials(
  233. ... 'sigmavirus24', 'not-re@l').finalize().unsplit()
  234. 'https://sigmavirus24:not-re%40l@github.com/sigmavirus24/rfc3986'
  235. """
  236. return uri.URIReference(
  237. self.scheme,
  238. normalizers.normalize_authority(
  239. (self.userinfo, self.host, self.port)
  240. ),
  241. self.path,
  242. self.query,
  243. self.fragment,
  244. )