tensorbored 2.21.0rc1769983804__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (271) hide show
  1. tensorbored/__init__.py +112 -0
  2. tensorbored/_vendor/__init__.py +0 -0
  3. tensorbored/_vendor/bleach/__init__.py +125 -0
  4. tensorbored/_vendor/bleach/_vendor/__init__.py +0 -0
  5. tensorbored/_vendor/bleach/_vendor/html5lib/__init__.py +35 -0
  6. tensorbored/_vendor/bleach/_vendor/html5lib/_ihatexml.py +289 -0
  7. tensorbored/_vendor/bleach/_vendor/html5lib/_inputstream.py +918 -0
  8. tensorbored/_vendor/bleach/_vendor/html5lib/_tokenizer.py +1735 -0
  9. tensorbored/_vendor/bleach/_vendor/html5lib/_trie/__init__.py +5 -0
  10. tensorbored/_vendor/bleach/_vendor/html5lib/_trie/_base.py +40 -0
  11. tensorbored/_vendor/bleach/_vendor/html5lib/_trie/py.py +67 -0
  12. tensorbored/_vendor/bleach/_vendor/html5lib/_utils.py +159 -0
  13. tensorbored/_vendor/bleach/_vendor/html5lib/constants.py +2946 -0
  14. tensorbored/_vendor/bleach/_vendor/html5lib/filters/__init__.py +0 -0
  15. tensorbored/_vendor/bleach/_vendor/html5lib/filters/alphabeticalattributes.py +29 -0
  16. tensorbored/_vendor/bleach/_vendor/html5lib/filters/base.py +12 -0
  17. tensorbored/_vendor/bleach/_vendor/html5lib/filters/inject_meta_charset.py +73 -0
  18. tensorbored/_vendor/bleach/_vendor/html5lib/filters/lint.py +93 -0
  19. tensorbored/_vendor/bleach/_vendor/html5lib/filters/optionaltags.py +207 -0
  20. tensorbored/_vendor/bleach/_vendor/html5lib/filters/sanitizer.py +916 -0
  21. tensorbored/_vendor/bleach/_vendor/html5lib/filters/whitespace.py +38 -0
  22. tensorbored/_vendor/bleach/_vendor/html5lib/html5parser.py +2795 -0
  23. tensorbored/_vendor/bleach/_vendor/html5lib/serializer.py +409 -0
  24. tensorbored/_vendor/bleach/_vendor/html5lib/treeadapters/__init__.py +30 -0
  25. tensorbored/_vendor/bleach/_vendor/html5lib/treeadapters/genshi.py +54 -0
  26. tensorbored/_vendor/bleach/_vendor/html5lib/treeadapters/sax.py +50 -0
  27. tensorbored/_vendor/bleach/_vendor/html5lib/treebuilders/__init__.py +88 -0
  28. tensorbored/_vendor/bleach/_vendor/html5lib/treebuilders/base.py +417 -0
  29. tensorbored/_vendor/bleach/_vendor/html5lib/treebuilders/dom.py +239 -0
  30. tensorbored/_vendor/bleach/_vendor/html5lib/treebuilders/etree.py +343 -0
  31. tensorbored/_vendor/bleach/_vendor/html5lib/treebuilders/etree_lxml.py +392 -0
  32. tensorbored/_vendor/bleach/_vendor/html5lib/treewalkers/__init__.py +154 -0
  33. tensorbored/_vendor/bleach/_vendor/html5lib/treewalkers/base.py +252 -0
  34. tensorbored/_vendor/bleach/_vendor/html5lib/treewalkers/dom.py +43 -0
  35. tensorbored/_vendor/bleach/_vendor/html5lib/treewalkers/etree.py +131 -0
  36. tensorbored/_vendor/bleach/_vendor/html5lib/treewalkers/etree_lxml.py +215 -0
  37. tensorbored/_vendor/bleach/_vendor/html5lib/treewalkers/genshi.py +69 -0
  38. tensorbored/_vendor/bleach/_vendor/parse.py +1078 -0
  39. tensorbored/_vendor/bleach/callbacks.py +32 -0
  40. tensorbored/_vendor/bleach/html5lib_shim.py +757 -0
  41. tensorbored/_vendor/bleach/linkifier.py +633 -0
  42. tensorbored/_vendor/bleach/parse_shim.py +1 -0
  43. tensorbored/_vendor/bleach/sanitizer.py +638 -0
  44. tensorbored/_vendor/bleach/six_shim.py +19 -0
  45. tensorbored/_vendor/webencodings/__init__.py +342 -0
  46. tensorbored/_vendor/webencodings/labels.py +231 -0
  47. tensorbored/_vendor/webencodings/mklabels.py +59 -0
  48. tensorbored/_vendor/webencodings/x_user_defined.py +325 -0
  49. tensorbored/assets.py +36 -0
  50. tensorbored/auth.py +102 -0
  51. tensorbored/backend/__init__.py +0 -0
  52. tensorbored/backend/application.py +604 -0
  53. tensorbored/backend/auth_context_middleware.py +38 -0
  54. tensorbored/backend/client_feature_flags.py +113 -0
  55. tensorbored/backend/empty_path_redirect.py +46 -0
  56. tensorbored/backend/event_processing/__init__.py +0 -0
  57. tensorbored/backend/event_processing/data_ingester.py +276 -0
  58. tensorbored/backend/event_processing/data_provider.py +535 -0
  59. tensorbored/backend/event_processing/directory_loader.py +142 -0
  60. tensorbored/backend/event_processing/directory_watcher.py +272 -0
  61. tensorbored/backend/event_processing/event_accumulator.py +950 -0
  62. tensorbored/backend/event_processing/event_file_inspector.py +463 -0
  63. tensorbored/backend/event_processing/event_file_loader.py +292 -0
  64. tensorbored/backend/event_processing/event_multiplexer.py +521 -0
  65. tensorbored/backend/event_processing/event_util.py +68 -0
  66. tensorbored/backend/event_processing/io_wrapper.py +223 -0
  67. tensorbored/backend/event_processing/plugin_asset_util.py +104 -0
  68. tensorbored/backend/event_processing/plugin_event_accumulator.py +721 -0
  69. tensorbored/backend/event_processing/plugin_event_multiplexer.py +522 -0
  70. tensorbored/backend/event_processing/reservoir.py +266 -0
  71. tensorbored/backend/event_processing/tag_types.py +29 -0
  72. tensorbored/backend/experiment_id.py +71 -0
  73. tensorbored/backend/experimental_plugin.py +51 -0
  74. tensorbored/backend/http_util.py +263 -0
  75. tensorbored/backend/json_util.py +70 -0
  76. tensorbored/backend/path_prefix.py +67 -0
  77. tensorbored/backend/process_graph.py +74 -0
  78. tensorbored/backend/security_validator.py +202 -0
  79. tensorbored/compat/__init__.py +69 -0
  80. tensorbored/compat/proto/__init__.py +0 -0
  81. tensorbored/compat/proto/allocation_description_pb2.py +35 -0
  82. tensorbored/compat/proto/api_def_pb2.py +82 -0
  83. tensorbored/compat/proto/attr_value_pb2.py +80 -0
  84. tensorbored/compat/proto/cluster_pb2.py +58 -0
  85. tensorbored/compat/proto/config_pb2.py +271 -0
  86. tensorbored/compat/proto/coordination_config_pb2.py +45 -0
  87. tensorbored/compat/proto/cost_graph_pb2.py +87 -0
  88. tensorbored/compat/proto/cpp_shape_inference_pb2.py +70 -0
  89. tensorbored/compat/proto/debug_pb2.py +65 -0
  90. tensorbored/compat/proto/event_pb2.py +149 -0
  91. tensorbored/compat/proto/full_type_pb2.py +74 -0
  92. tensorbored/compat/proto/function_pb2.py +157 -0
  93. tensorbored/compat/proto/graph_debug_info_pb2.py +111 -0
  94. tensorbored/compat/proto/graph_pb2.py +41 -0
  95. tensorbored/compat/proto/histogram_pb2.py +39 -0
  96. tensorbored/compat/proto/meta_graph_pb2.py +254 -0
  97. tensorbored/compat/proto/node_def_pb2.py +61 -0
  98. tensorbored/compat/proto/op_def_pb2.py +81 -0
  99. tensorbored/compat/proto/resource_handle_pb2.py +48 -0
  100. tensorbored/compat/proto/rewriter_config_pb2.py +93 -0
  101. tensorbored/compat/proto/rpc_options_pb2.py +35 -0
  102. tensorbored/compat/proto/saved_object_graph_pb2.py +193 -0
  103. tensorbored/compat/proto/saver_pb2.py +38 -0
  104. tensorbored/compat/proto/step_stats_pb2.py +116 -0
  105. tensorbored/compat/proto/struct_pb2.py +144 -0
  106. tensorbored/compat/proto/summary_pb2.py +111 -0
  107. tensorbored/compat/proto/tensor_description_pb2.py +38 -0
  108. tensorbored/compat/proto/tensor_pb2.py +68 -0
  109. tensorbored/compat/proto/tensor_shape_pb2.py +46 -0
  110. tensorbored/compat/proto/tfprof_log_pb2.py +307 -0
  111. tensorbored/compat/proto/trackable_object_graph_pb2.py +90 -0
  112. tensorbored/compat/proto/types_pb2.py +105 -0
  113. tensorbored/compat/proto/variable_pb2.py +62 -0
  114. tensorbored/compat/proto/verifier_config_pb2.py +38 -0
  115. tensorbored/compat/proto/versions_pb2.py +35 -0
  116. tensorbored/compat/tensorflow_stub/__init__.py +38 -0
  117. tensorbored/compat/tensorflow_stub/app.py +124 -0
  118. tensorbored/compat/tensorflow_stub/compat/__init__.py +131 -0
  119. tensorbored/compat/tensorflow_stub/compat/v1/__init__.py +20 -0
  120. tensorbored/compat/tensorflow_stub/dtypes.py +692 -0
  121. tensorbored/compat/tensorflow_stub/error_codes.py +169 -0
  122. tensorbored/compat/tensorflow_stub/errors.py +507 -0
  123. tensorbored/compat/tensorflow_stub/flags.py +124 -0
  124. tensorbored/compat/tensorflow_stub/io/__init__.py +17 -0
  125. tensorbored/compat/tensorflow_stub/io/gfile.py +1011 -0
  126. tensorbored/compat/tensorflow_stub/pywrap_tensorflow.py +285 -0
  127. tensorbored/compat/tensorflow_stub/tensor_shape.py +1035 -0
  128. tensorbored/context.py +129 -0
  129. tensorbored/data/__init__.py +0 -0
  130. tensorbored/data/grpc_provider.py +365 -0
  131. tensorbored/data/ingester.py +46 -0
  132. tensorbored/data/proto/__init__.py +0 -0
  133. tensorbored/data/proto/data_provider_pb2.py +517 -0
  134. tensorbored/data/proto/data_provider_pb2_grpc.py +374 -0
  135. tensorbored/data/provider.py +1365 -0
  136. tensorbored/data/server_ingester.py +301 -0
  137. tensorbored/data_compat.py +159 -0
  138. tensorbored/dataclass_compat.py +224 -0
  139. tensorbored/default.py +124 -0
  140. tensorbored/errors.py +130 -0
  141. tensorbored/lazy.py +99 -0
  142. tensorbored/main.py +48 -0
  143. tensorbored/main_lib.py +62 -0
  144. tensorbored/manager.py +487 -0
  145. tensorbored/notebook.py +441 -0
  146. tensorbored/plugin_util.py +266 -0
  147. tensorbored/plugins/__init__.py +0 -0
  148. tensorbored/plugins/audio/__init__.py +0 -0
  149. tensorbored/plugins/audio/audio_plugin.py +229 -0
  150. tensorbored/plugins/audio/metadata.py +69 -0
  151. tensorbored/plugins/audio/plugin_data_pb2.py +37 -0
  152. tensorbored/plugins/audio/summary.py +230 -0
  153. tensorbored/plugins/audio/summary_v2.py +124 -0
  154. tensorbored/plugins/base_plugin.py +367 -0
  155. tensorbored/plugins/core/__init__.py +0 -0
  156. tensorbored/plugins/core/core_plugin.py +981 -0
  157. tensorbored/plugins/custom_scalar/__init__.py +0 -0
  158. tensorbored/plugins/custom_scalar/custom_scalars_plugin.py +320 -0
  159. tensorbored/plugins/custom_scalar/layout_pb2.py +85 -0
  160. tensorbored/plugins/custom_scalar/metadata.py +35 -0
  161. tensorbored/plugins/custom_scalar/summary.py +79 -0
  162. tensorbored/plugins/debugger_v2/__init__.py +0 -0
  163. tensorbored/plugins/debugger_v2/debug_data_multiplexer.py +631 -0
  164. tensorbored/plugins/debugger_v2/debug_data_provider.py +634 -0
  165. tensorbored/plugins/debugger_v2/debugger_v2_plugin.py +504 -0
  166. tensorbored/plugins/distribution/__init__.py +0 -0
  167. tensorbored/plugins/distribution/compressor.py +158 -0
  168. tensorbored/plugins/distribution/distributions_plugin.py +116 -0
  169. tensorbored/plugins/distribution/metadata.py +19 -0
  170. tensorbored/plugins/graph/__init__.py +0 -0
  171. tensorbored/plugins/graph/graph_util.py +129 -0
  172. tensorbored/plugins/graph/graphs_plugin.py +336 -0
  173. tensorbored/plugins/graph/keras_util.py +328 -0
  174. tensorbored/plugins/graph/metadata.py +42 -0
  175. tensorbored/plugins/histogram/__init__.py +0 -0
  176. tensorbored/plugins/histogram/histograms_plugin.py +144 -0
  177. tensorbored/plugins/histogram/metadata.py +63 -0
  178. tensorbored/plugins/histogram/plugin_data_pb2.py +34 -0
  179. tensorbored/plugins/histogram/summary.py +234 -0
  180. tensorbored/plugins/histogram/summary_v2.py +292 -0
  181. tensorbored/plugins/hparams/__init__.py +14 -0
  182. tensorbored/plugins/hparams/_keras.py +93 -0
  183. tensorbored/plugins/hparams/api.py +130 -0
  184. tensorbored/plugins/hparams/api_pb2.py +208 -0
  185. tensorbored/plugins/hparams/backend_context.py +606 -0
  186. tensorbored/plugins/hparams/download_data.py +158 -0
  187. tensorbored/plugins/hparams/error.py +26 -0
  188. tensorbored/plugins/hparams/get_experiment.py +71 -0
  189. tensorbored/plugins/hparams/hparams_plugin.py +206 -0
  190. tensorbored/plugins/hparams/hparams_util_pb2.py +69 -0
  191. tensorbored/plugins/hparams/json_format_compat.py +38 -0
  192. tensorbored/plugins/hparams/list_metric_evals.py +57 -0
  193. tensorbored/plugins/hparams/list_session_groups.py +1040 -0
  194. tensorbored/plugins/hparams/metadata.py +125 -0
  195. tensorbored/plugins/hparams/metrics.py +41 -0
  196. tensorbored/plugins/hparams/plugin_data_pb2.py +69 -0
  197. tensorbored/plugins/hparams/summary.py +205 -0
  198. tensorbored/plugins/hparams/summary_v2.py +597 -0
  199. tensorbored/plugins/image/__init__.py +0 -0
  200. tensorbored/plugins/image/images_plugin.py +232 -0
  201. tensorbored/plugins/image/metadata.py +65 -0
  202. tensorbored/plugins/image/plugin_data_pb2.py +34 -0
  203. tensorbored/plugins/image/summary.py +159 -0
  204. tensorbored/plugins/image/summary_v2.py +130 -0
  205. tensorbored/plugins/mesh/__init__.py +14 -0
  206. tensorbored/plugins/mesh/mesh_plugin.py +292 -0
  207. tensorbored/plugins/mesh/metadata.py +152 -0
  208. tensorbored/plugins/mesh/plugin_data_pb2.py +37 -0
  209. tensorbored/plugins/mesh/summary.py +251 -0
  210. tensorbored/plugins/mesh/summary_v2.py +214 -0
  211. tensorbored/plugins/metrics/__init__.py +0 -0
  212. tensorbored/plugins/metrics/metadata.py +17 -0
  213. tensorbored/plugins/metrics/metrics_plugin.py +623 -0
  214. tensorbored/plugins/pr_curve/__init__.py +0 -0
  215. tensorbored/plugins/pr_curve/metadata.py +75 -0
  216. tensorbored/plugins/pr_curve/plugin_data_pb2.py +34 -0
  217. tensorbored/plugins/pr_curve/pr_curves_plugin.py +241 -0
  218. tensorbored/plugins/pr_curve/summary.py +574 -0
  219. tensorbored/plugins/profile_redirect/__init__.py +0 -0
  220. tensorbored/plugins/profile_redirect/profile_redirect_plugin.py +49 -0
  221. tensorbored/plugins/projector/__init__.py +67 -0
  222. tensorbored/plugins/projector/metadata.py +26 -0
  223. tensorbored/plugins/projector/projector_config_pb2.py +54 -0
  224. tensorbored/plugins/projector/projector_plugin.py +795 -0
  225. tensorbored/plugins/projector/tf_projector_plugin/index.js +32 -0
  226. tensorbored/plugins/projector/tf_projector_plugin/projector_binary.html +524 -0
  227. tensorbored/plugins/projector/tf_projector_plugin/projector_binary.js +15536 -0
  228. tensorbored/plugins/scalar/__init__.py +0 -0
  229. tensorbored/plugins/scalar/metadata.py +60 -0
  230. tensorbored/plugins/scalar/plugin_data_pb2.py +34 -0
  231. tensorbored/plugins/scalar/scalars_plugin.py +181 -0
  232. tensorbored/plugins/scalar/summary.py +109 -0
  233. tensorbored/plugins/scalar/summary_v2.py +124 -0
  234. tensorbored/plugins/text/__init__.py +0 -0
  235. tensorbored/plugins/text/metadata.py +62 -0
  236. tensorbored/plugins/text/plugin_data_pb2.py +34 -0
  237. tensorbored/plugins/text/summary.py +114 -0
  238. tensorbored/plugins/text/summary_v2.py +124 -0
  239. tensorbored/plugins/text/text_plugin.py +288 -0
  240. tensorbored/plugins/wit_redirect/__init__.py +0 -0
  241. tensorbored/plugins/wit_redirect/wit_redirect_plugin.py +49 -0
  242. tensorbored/program.py +910 -0
  243. tensorbored/summary/__init__.py +35 -0
  244. tensorbored/summary/_output.py +124 -0
  245. tensorbored/summary/_tf/__init__.py +14 -0
  246. tensorbored/summary/_tf/summary/__init__.py +178 -0
  247. tensorbored/summary/_writer.py +105 -0
  248. tensorbored/summary/v1.py +51 -0
  249. tensorbored/summary/v2.py +25 -0
  250. tensorbored/summary/writer/__init__.py +13 -0
  251. tensorbored/summary/writer/event_file_writer.py +291 -0
  252. tensorbored/summary/writer/record_writer.py +50 -0
  253. tensorbored/util/__init__.py +0 -0
  254. tensorbored/util/encoder.py +116 -0
  255. tensorbored/util/grpc_util.py +311 -0
  256. tensorbored/util/img_mime_type_detector.py +40 -0
  257. tensorbored/util/io_util.py +20 -0
  258. tensorbored/util/lazy_tensor_creator.py +110 -0
  259. tensorbored/util/op_evaluator.py +104 -0
  260. tensorbored/util/platform_util.py +20 -0
  261. tensorbored/util/tb_logging.py +24 -0
  262. tensorbored/util/tensor_util.py +617 -0
  263. tensorbored/util/timing.py +122 -0
  264. tensorbored/version.py +21 -0
  265. tensorbored/webfiles.zip +0 -0
  266. tensorbored-2.21.0rc1769983804.dist-info/METADATA +49 -0
  267. tensorbored-2.21.0rc1769983804.dist-info/RECORD +271 -0
  268. tensorbored-2.21.0rc1769983804.dist-info/WHEEL +5 -0
  269. tensorbored-2.21.0rc1769983804.dist-info/entry_points.txt +6 -0
  270. tensorbored-2.21.0rc1769983804.dist-info/licenses/LICENSE +739 -0
  271. tensorbored-2.21.0rc1769983804.dist-info/top_level.txt +1 -0
@@ -0,0 +1,1078 @@
1
+ """Parse (absolute and relative) URLs.
2
+
3
+ urlparse module is based upon the following RFC specifications.
4
+
5
+ RFC 3986 (STD66): "Uniform Resource Identifiers" by T. Berners-Lee, R. Fielding
6
+ and L. Masinter, January 2005.
7
+
8
+ RFC 2732 : "Format for Literal IPv6 Addresses in URL's by R.Hinden, B.Carpenter
9
+ and L.Masinter, December 1999.
10
+
11
+ RFC 2396: "Uniform Resource Identifiers (URI)": Generic Syntax by T.
12
+ Berners-Lee, R. Fielding, and L. Masinter, August 1998.
13
+
14
+ RFC 2368: "The mailto URL scheme", by P.Hoffman , L Masinter, J. Zawinski, July 1998.
15
+
16
+ RFC 1808: "Relative Uniform Resource Locators", by R. Fielding, UC Irvine, June
17
+ 1995.
18
+
19
+ RFC 1738: "Uniform Resource Locators (URL)" by T. Berners-Lee, L. Masinter, M.
20
+ McCahill, December 1994
21
+
22
+ RFC 3986 is considered the current standard and any future changes to
23
+ urlparse module should conform with it. The urlparse module is
24
+ currently not entirely compliant with this RFC due to defacto
25
+ scenarios for parsing, and for backward compatibility purposes, some
26
+ parsing quirks from older RFCs are retained. The testcases in
27
+ test_urlparse.py provides a good indicator of parsing behavior.
28
+ """
29
+
30
+ import re
31
+ import sys
32
+ import collections
33
+
34
+ __all__ = ["urlparse", "urlunparse", "urljoin", "urldefrag",
35
+ "urlsplit", "urlunsplit", "urlencode", "parse_qs",
36
+ "parse_qsl", "quote", "quote_plus", "quote_from_bytes",
37
+ "unquote", "unquote_plus", "unquote_to_bytes",
38
+ "DefragResult", "ParseResult", "SplitResult",
39
+ "DefragResultBytes", "ParseResultBytes", "SplitResultBytes"]
40
+
41
+ # A classification of schemes.
42
+ # The empty string classifies URLs with no scheme specified,
43
+ # being the default value returned by “urlsplit” and “urlparse”.
44
+
45
+ uses_relative = ['', 'ftp', 'http', 'gopher', 'nntp', 'imap',
46
+ 'wais', 'file', 'https', 'shttp', 'mms',
47
+ 'prospero', 'rtsp', 'rtspu', 'sftp',
48
+ 'svn', 'svn+ssh', 'ws', 'wss']
49
+
50
+ uses_netloc = ['', 'ftp', 'http', 'gopher', 'nntp', 'telnet',
51
+ 'imap', 'wais', 'file', 'mms', 'https', 'shttp',
52
+ 'snews', 'prospero', 'rtsp', 'rtspu', 'rsync',
53
+ 'svn', 'svn+ssh', 'sftp', 'nfs', 'git', 'git+ssh',
54
+ 'ws', 'wss']
55
+
56
+ uses_params = ['', 'ftp', 'hdl', 'prospero', 'http', 'imap',
57
+ 'https', 'shttp', 'rtsp', 'rtspu', 'sip', 'sips',
58
+ 'mms', 'sftp', 'tel']
59
+
60
+ # These are not actually used anymore, but should stay for backwards
61
+ # compatibility. (They are undocumented, but have a public-looking name.)
62
+
63
+ non_hierarchical = ['gopher', 'hdl', 'mailto', 'news',
64
+ 'telnet', 'wais', 'imap', 'snews', 'sip', 'sips']
65
+
66
+ uses_query = ['', 'http', 'wais', 'imap', 'https', 'shttp', 'mms',
67
+ 'gopher', 'rtsp', 'rtspu', 'sip', 'sips']
68
+
69
+ uses_fragment = ['', 'ftp', 'hdl', 'http', 'gopher', 'news',
70
+ 'nntp', 'wais', 'https', 'shttp', 'snews',
71
+ 'file', 'prospero']
72
+
73
+ # Characters valid in scheme names
74
+ scheme_chars = ('abcdefghijklmnopqrstuvwxyz'
75
+ 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
76
+ '0123456789'
77
+ '+-.')
78
+
79
+ # Unsafe bytes to be removed per WHATWG spec
80
+ _UNSAFE_URL_BYTES_TO_REMOVE = ['\t', '\r', '\n']
81
+
82
+ # XXX: Consider replacing with functools.lru_cache
83
+ MAX_CACHE_SIZE = 20
84
+ _parse_cache = {}
85
+
86
+ def clear_cache():
87
+ """Clear the parse cache and the quoters cache."""
88
+ _parse_cache.clear()
89
+ _safe_quoters.clear()
90
+
91
+
92
+ # Helpers for bytes handling
93
+ # For 3.2, we deliberately require applications that
94
+ # handle improperly quoted URLs to do their own
95
+ # decoding and encoding. If valid use cases are
96
+ # presented, we may relax this by using latin-1
97
+ # decoding internally for 3.3
98
+ _implicit_encoding = 'ascii'
99
+ _implicit_errors = 'strict'
100
+
101
+ def _noop(obj):
102
+ return obj
103
+
104
+ def _encode_result(obj, encoding=_implicit_encoding,
105
+ errors=_implicit_errors):
106
+ return obj.encode(encoding, errors)
107
+
108
+ def _decode_args(args, encoding=_implicit_encoding,
109
+ errors=_implicit_errors):
110
+ return tuple(x.decode(encoding, errors) if x else '' for x in args)
111
+
112
+ def _coerce_args(*args):
113
+ # Invokes decode if necessary to create str args
114
+ # and returns the coerced inputs along with
115
+ # an appropriate result coercion function
116
+ # - noop for str inputs
117
+ # - encoding function otherwise
118
+ str_input = isinstance(args[0], str)
119
+ for arg in args[1:]:
120
+ # We special-case the empty string to support the
121
+ # "scheme=''" default argument to some functions
122
+ if arg and isinstance(arg, str) != str_input:
123
+ raise TypeError("Cannot mix str and non-str arguments")
124
+ if str_input:
125
+ return args + (_noop,)
126
+ return _decode_args(args) + (_encode_result,)
127
+
128
+ # Result objects are more helpful than simple tuples
129
+ class _ResultMixinStr(object):
130
+ """Standard approach to encoding parsed results from str to bytes"""
131
+ __slots__ = ()
132
+
133
+ def encode(self, encoding='ascii', errors='strict'):
134
+ return self._encoded_counterpart(*(x.encode(encoding, errors) for x in self))
135
+
136
+
137
+ class _ResultMixinBytes(object):
138
+ """Standard approach to decoding parsed results from bytes to str"""
139
+ __slots__ = ()
140
+
141
+ def decode(self, encoding='ascii', errors='strict'):
142
+ return self._decoded_counterpart(*(x.decode(encoding, errors) for x in self))
143
+
144
+
145
+ class _NetlocResultMixinBase(object):
146
+ """Shared methods for the parsed result objects containing a netloc element"""
147
+ __slots__ = ()
148
+
149
+ @property
150
+ def username(self):
151
+ return self._userinfo[0]
152
+
153
+ @property
154
+ def password(self):
155
+ return self._userinfo[1]
156
+
157
+ @property
158
+ def hostname(self):
159
+ hostname = self._hostinfo[0]
160
+ if not hostname:
161
+ return None
162
+ # Scoped IPv6 address may have zone info, which must not be lowercased
163
+ # like http://[fe80::822a:a8ff:fe49:470c%tESt]:1234/keys
164
+ separator = '%' if isinstance(hostname, str) else b'%'
165
+ hostname, percent, zone = hostname.partition(separator)
166
+ return hostname.lower() + percent + zone
167
+
168
+ @property
169
+ def port(self):
170
+ port = self._hostinfo[1]
171
+ if port is not None:
172
+ port = int(port, 10)
173
+ if not ( 0 <= port <= 65535):
174
+ raise ValueError("Port out of range 0-65535")
175
+ return port
176
+
177
+
178
+ class _NetlocResultMixinStr(_NetlocResultMixinBase, _ResultMixinStr):
179
+ __slots__ = ()
180
+
181
+ @property
182
+ def _userinfo(self):
183
+ netloc = self.netloc
184
+ userinfo, have_info, hostinfo = netloc.rpartition('@')
185
+ if have_info:
186
+ username, have_password, password = userinfo.partition(':')
187
+ if not have_password:
188
+ password = None
189
+ else:
190
+ username = password = None
191
+ return username, password
192
+
193
+ @property
194
+ def _hostinfo(self):
195
+ netloc = self.netloc
196
+ _, _, hostinfo = netloc.rpartition('@')
197
+ _, have_open_br, bracketed = hostinfo.partition('[')
198
+ if have_open_br:
199
+ hostname, _, port = bracketed.partition(']')
200
+ _, _, port = port.partition(':')
201
+ else:
202
+ hostname, _, port = hostinfo.partition(':')
203
+ if not port:
204
+ port = None
205
+ return hostname, port
206
+
207
+
208
+ class _NetlocResultMixinBytes(_NetlocResultMixinBase, _ResultMixinBytes):
209
+ __slots__ = ()
210
+
211
+ @property
212
+ def _userinfo(self):
213
+ netloc = self.netloc
214
+ userinfo, have_info, hostinfo = netloc.rpartition(b'@')
215
+ if have_info:
216
+ username, have_password, password = userinfo.partition(b':')
217
+ if not have_password:
218
+ password = None
219
+ else:
220
+ username = password = None
221
+ return username, password
222
+
223
+ @property
224
+ def _hostinfo(self):
225
+ netloc = self.netloc
226
+ _, _, hostinfo = netloc.rpartition(b'@')
227
+ _, have_open_br, bracketed = hostinfo.partition(b'[')
228
+ if have_open_br:
229
+ hostname, _, port = bracketed.partition(b']')
230
+ _, _, port = port.partition(b':')
231
+ else:
232
+ hostname, _, port = hostinfo.partition(b':')
233
+ if not port:
234
+ port = None
235
+ return hostname, port
236
+
237
+
238
+ from collections import namedtuple
239
+
240
+ _DefragResultBase = namedtuple('DefragResult', 'url fragment')
241
+ _SplitResultBase = namedtuple(
242
+ 'SplitResult', 'scheme netloc path query fragment')
243
+ _ParseResultBase = namedtuple(
244
+ 'ParseResult', 'scheme netloc path params query fragment')
245
+
246
+ _DefragResultBase.__doc__ = """
247
+ DefragResult(url, fragment)
248
+
249
+ A 2-tuple that contains the url without fragment identifier and the fragment
250
+ identifier as a separate argument.
251
+ """
252
+
253
+ _DefragResultBase.url.__doc__ = """The URL with no fragment identifier."""
254
+
255
+ _DefragResultBase.fragment.__doc__ = """
256
+ Fragment identifier separated from URL, that allows indirect identification of a
257
+ secondary resource by reference to a primary resource and additional identifying
258
+ information.
259
+ """
260
+
261
+ _SplitResultBase.__doc__ = """
262
+ SplitResult(scheme, netloc, path, query, fragment)
263
+
264
+ A 5-tuple that contains the different components of a URL. Similar to
265
+ ParseResult, but does not split params.
266
+ """
267
+
268
+ _SplitResultBase.scheme.__doc__ = """Specifies URL scheme for the request."""
269
+
270
+ _SplitResultBase.netloc.__doc__ = """
271
+ Network location where the request is made to.
272
+ """
273
+
274
+ _SplitResultBase.path.__doc__ = """
275
+ The hierarchical path, such as the path to a file to download.
276
+ """
277
+
278
+ _SplitResultBase.query.__doc__ = """
279
+ The query component, that contains non-hierarchical data, that along with data
280
+ in path component, identifies a resource in the scope of URI's scheme and
281
+ network location.
282
+ """
283
+
284
+ _SplitResultBase.fragment.__doc__ = """
285
+ Fragment identifier, that allows indirect identification of a secondary resource
286
+ by reference to a primary resource and additional identifying information.
287
+ """
288
+
289
+ _ParseResultBase.__doc__ = """
290
+ ParseResult(scheme, netloc, path, params, query, fragment)
291
+
292
+ A 6-tuple that contains components of a parsed URL.
293
+ """
294
+
295
+ _ParseResultBase.scheme.__doc__ = _SplitResultBase.scheme.__doc__
296
+ _ParseResultBase.netloc.__doc__ = _SplitResultBase.netloc.__doc__
297
+ _ParseResultBase.path.__doc__ = _SplitResultBase.path.__doc__
298
+ _ParseResultBase.params.__doc__ = """
299
+ Parameters for last path element used to dereference the URI in order to provide
300
+ access to perform some operation on the resource.
301
+ """
302
+
303
+ _ParseResultBase.query.__doc__ = _SplitResultBase.query.__doc__
304
+ _ParseResultBase.fragment.__doc__ = _SplitResultBase.fragment.__doc__
305
+
306
+
307
+ # For backwards compatibility, alias _NetlocResultMixinStr
308
+ # ResultBase is no longer part of the documented API, but it is
309
+ # retained since deprecating it isn't worth the hassle
310
+ ResultBase = _NetlocResultMixinStr
311
+
312
+ # Structured result objects for string data
313
+ class DefragResult(_DefragResultBase, _ResultMixinStr):
314
+ __slots__ = ()
315
+ def geturl(self):
316
+ if self.fragment:
317
+ return self.url + '#' + self.fragment
318
+ else:
319
+ return self.url
320
+
321
+ class SplitResult(_SplitResultBase, _NetlocResultMixinStr):
322
+ __slots__ = ()
323
+ def geturl(self):
324
+ return urlunsplit(self)
325
+
326
+ class ParseResult(_ParseResultBase, _NetlocResultMixinStr):
327
+ __slots__ = ()
328
+ def geturl(self):
329
+ return urlunparse(self)
330
+
331
+ # Structured result objects for bytes data
332
+ class DefragResultBytes(_DefragResultBase, _ResultMixinBytes):
333
+ __slots__ = ()
334
+ def geturl(self):
335
+ if self.fragment:
336
+ return self.url + b'#' + self.fragment
337
+ else:
338
+ return self.url
339
+
340
+ class SplitResultBytes(_SplitResultBase, _NetlocResultMixinBytes):
341
+ __slots__ = ()
342
+ def geturl(self):
343
+ return urlunsplit(self)
344
+
345
+ class ParseResultBytes(_ParseResultBase, _NetlocResultMixinBytes):
346
+ __slots__ = ()
347
+ def geturl(self):
348
+ return urlunparse(self)
349
+
350
+ # Set up the encode/decode result pairs
351
+ def _fix_result_transcoding():
352
+ _result_pairs = (
353
+ (DefragResult, DefragResultBytes),
354
+ (SplitResult, SplitResultBytes),
355
+ (ParseResult, ParseResultBytes),
356
+ )
357
+ for _decoded, _encoded in _result_pairs:
358
+ _decoded._encoded_counterpart = _encoded
359
+ _encoded._decoded_counterpart = _decoded
360
+
361
+ _fix_result_transcoding()
362
+ del _fix_result_transcoding
363
+
364
+ def urlparse(url, scheme='', allow_fragments=True):
365
+ """Parse a URL into 6 components:
366
+ <scheme>://<netloc>/<path>;<params>?<query>#<fragment>
367
+ Return a 6-tuple: (scheme, netloc, path, params, query, fragment).
368
+ Note that we don't break the components up in smaller bits
369
+ (e.g. netloc is a single string) and we don't expand % escapes."""
370
+ url, scheme, _coerce_result = _coerce_args(url, scheme)
371
+ splitresult = urlsplit(url, scheme, allow_fragments)
372
+ scheme, netloc, url, query, fragment = splitresult
373
+ if scheme in uses_params and ';' in url:
374
+ url, params = _splitparams(url)
375
+ else:
376
+ params = ''
377
+ result = ParseResult(scheme, netloc, url, params, query, fragment)
378
+ return _coerce_result(result)
379
+
380
+ def _splitparams(url):
381
+ if '/' in url:
382
+ i = url.find(';', url.rfind('/'))
383
+ if i < 0:
384
+ return url, ''
385
+ else:
386
+ i = url.find(';')
387
+ return url[:i], url[i+1:]
388
+
389
+ def _splitnetloc(url, start=0):
390
+ delim = len(url) # position of end of domain part of url, default is end
391
+ for c in '/?#': # look for delimiters; the order is NOT important
392
+ wdelim = url.find(c, start) # find first of this delim
393
+ if wdelim >= 0: # if found
394
+ delim = min(delim, wdelim) # use earliest delim position
395
+ return url[start:delim], url[delim:] # return (domain, rest)
396
+
397
+ def _checknetloc(netloc):
398
+ if not netloc or not any(ord(c) > 127 for c in netloc):
399
+ return
400
+ # looking for characters like \u2100 that expand to 'a/c'
401
+ # IDNA uses NFKC equivalence, so normalize for this check
402
+ import unicodedata
403
+ n = netloc.replace('@', '') # ignore characters already included
404
+ n = n.replace(':', '') # but not the surrounding text
405
+ n = n.replace('#', '')
406
+ n = n.replace('?', '')
407
+ netloc2 = unicodedata.normalize('NFKC', n)
408
+ if n == netloc2:
409
+ return
410
+ for c in '/?#@:':
411
+ if c in netloc2:
412
+ raise ValueError("netloc '" + netloc + "' contains invalid " +
413
+ "characters under NFKC normalization")
414
+
415
+ def _remove_unsafe_bytes_from_url(url):
416
+ for b in _UNSAFE_URL_BYTES_TO_REMOVE:
417
+ url = url.replace(b, "")
418
+ return url
419
+
420
+ def urlsplit(url, scheme='', allow_fragments=True):
421
+ """Parse a URL into 5 components:
422
+ <scheme>://<netloc>/<path>?<query>#<fragment>
423
+ Return a 5-tuple: (scheme, netloc, path, query, fragment).
424
+ Note that we don't break the components up in smaller bits
425
+ (e.g. netloc is a single string) and we don't expand % escapes."""
426
+ url, scheme, _coerce_result = _coerce_args(url, scheme)
427
+ url = _remove_unsafe_bytes_from_url(url)
428
+ scheme = _remove_unsafe_bytes_from_url(scheme)
429
+ allow_fragments = bool(allow_fragments)
430
+ key = url, scheme, allow_fragments, type(url), type(scheme)
431
+ cached = _parse_cache.get(key, None)
432
+ if cached:
433
+ return _coerce_result(cached)
434
+ if len(_parse_cache) >= MAX_CACHE_SIZE: # avoid runaway growth
435
+ clear_cache()
436
+ netloc = query = fragment = ''
437
+ i = url.find(':')
438
+ if i > 0:
439
+ if url[:i] == 'http': # optimize the common case
440
+ scheme = url[:i].lower()
441
+ url = url[i+1:]
442
+ if url[:2] == '//':
443
+ netloc, url = _splitnetloc(url, 2)
444
+ if (('[' in netloc and ']' not in netloc) or
445
+ (']' in netloc and '[' not in netloc)):
446
+ raise ValueError("Invalid IPv6 URL")
447
+ if allow_fragments and '#' in url:
448
+ url, fragment = url.split('#', 1)
449
+ if '?' in url:
450
+ url, query = url.split('?', 1)
451
+ _checknetloc(netloc)
452
+ v = SplitResult(scheme, netloc, url, query, fragment)
453
+ _parse_cache[key] = v
454
+ return _coerce_result(v)
455
+ for c in url[:i]:
456
+ if c not in scheme_chars:
457
+ break
458
+ else:
459
+ # make sure "url" is not actually a port number (in which case
460
+ # "scheme" is really part of the path)
461
+ rest = url[i+1:]
462
+ if not rest or any(c not in '0123456789' for c in rest):
463
+ # not a port number
464
+ scheme, url = url[:i].lower(), rest
465
+
466
+ if url[:2] == '//':
467
+ netloc, url = _splitnetloc(url, 2)
468
+ if (('[' in netloc and ']' not in netloc) or
469
+ (']' in netloc and '[' not in netloc)):
470
+ raise ValueError("Invalid IPv6 URL")
471
+ if allow_fragments and '#' in url:
472
+ url, fragment = url.split('#', 1)
473
+ if '?' in url:
474
+ url, query = url.split('?', 1)
475
+ _checknetloc(netloc)
476
+ v = SplitResult(scheme, netloc, url, query, fragment)
477
+ _parse_cache[key] = v
478
+ return _coerce_result(v)
479
+
480
+ def urlunparse(components):
481
+ """Put a parsed URL back together again. This may result in a
482
+ slightly different, but equivalent URL, if the URL that was parsed
483
+ originally had redundant delimiters, e.g. a ? with an empty query
484
+ (the draft states that these are equivalent)."""
485
+ scheme, netloc, url, params, query, fragment, _coerce_result = (
486
+ _coerce_args(*components))
487
+ if params:
488
+ url = "%s;%s" % (url, params)
489
+ return _coerce_result(urlunsplit((scheme, netloc, url, query, fragment)))
490
+
491
+ def urlunsplit(components):
492
+ """Combine the elements of a tuple as returned by urlsplit() into a
493
+ complete URL as a string. The data argument can be any five-item iterable.
494
+ This may result in a slightly different, but equivalent URL, if the URL that
495
+ was parsed originally had unnecessary delimiters (for example, a ? with an
496
+ empty query; the RFC states that these are equivalent)."""
497
+ scheme, netloc, url, query, fragment, _coerce_result = (
498
+ _coerce_args(*components))
499
+ if netloc or (scheme and scheme in uses_netloc and url[:2] != '//'):
500
+ if url and url[:1] != '/': url = '/' + url
501
+ url = '//' + (netloc or '') + url
502
+ if scheme:
503
+ url = scheme + ':' + url
504
+ if query:
505
+ url = url + '?' + query
506
+ if fragment:
507
+ url = url + '#' + fragment
508
+ return _coerce_result(url)
509
+
510
+ def urljoin(base, url, allow_fragments=True):
511
+ """Join a base URL and a possibly relative URL to form an absolute
512
+ interpretation of the latter."""
513
+ if not base:
514
+ return url
515
+ if not url:
516
+ return base
517
+
518
+ base, url, _coerce_result = _coerce_args(base, url)
519
+ bscheme, bnetloc, bpath, bparams, bquery, bfragment = \
520
+ urlparse(base, '', allow_fragments)
521
+ scheme, netloc, path, params, query, fragment = \
522
+ urlparse(url, bscheme, allow_fragments)
523
+
524
+ if scheme != bscheme or scheme not in uses_relative:
525
+ return _coerce_result(url)
526
+ if scheme in uses_netloc:
527
+ if netloc:
528
+ return _coerce_result(urlunparse((scheme, netloc, path,
529
+ params, query, fragment)))
530
+ netloc = bnetloc
531
+
532
+ if not path and not params:
533
+ path = bpath
534
+ params = bparams
535
+ if not query:
536
+ query = bquery
537
+ return _coerce_result(urlunparse((scheme, netloc, path,
538
+ params, query, fragment)))
539
+
540
+ base_parts = bpath.split('/')
541
+ if base_parts[-1] != '':
542
+ # the last item is not a directory, so will not be taken into account
543
+ # in resolving the relative path
544
+ del base_parts[-1]
545
+
546
+ # for rfc3986, ignore all base path should the first character be root.
547
+ if path[:1] == '/':
548
+ segments = path.split('/')
549
+ else:
550
+ segments = base_parts + path.split('/')
551
+ # filter out elements that would cause redundant slashes on re-joining
552
+ # the resolved_path
553
+ segments[1:-1] = filter(None, segments[1:-1])
554
+
555
+ resolved_path = []
556
+
557
+ for seg in segments:
558
+ if seg == '..':
559
+ try:
560
+ resolved_path.pop()
561
+ except IndexError:
562
+ # ignore any .. segments that would otherwise cause an IndexError
563
+ # when popped from resolved_path if resolving for rfc3986
564
+ pass
565
+ elif seg == '.':
566
+ continue
567
+ else:
568
+ resolved_path.append(seg)
569
+
570
+ if segments[-1] in ('.', '..'):
571
+ # do some post-processing here. if the last segment was a relative dir,
572
+ # then we need to append the trailing '/'
573
+ resolved_path.append('')
574
+
575
+ return _coerce_result(urlunparse((scheme, netloc, '/'.join(
576
+ resolved_path) or '/', params, query, fragment)))
577
+
578
+
579
+ def urldefrag(url):
580
+ """Removes any existing fragment from URL.
581
+
582
+ Returns a tuple of the defragmented URL and the fragment. If
583
+ the URL contained no fragments, the second element is the
584
+ empty string.
585
+ """
586
+ url, _coerce_result = _coerce_args(url)
587
+ if '#' in url:
588
+ s, n, p, a, q, frag = urlparse(url)
589
+ defrag = urlunparse((s, n, p, a, q, ''))
590
+ else:
591
+ frag = ''
592
+ defrag = url
593
+ return _coerce_result(DefragResult(defrag, frag))
594
+
595
+ _hexdig = '0123456789ABCDEFabcdef'
596
+ _hextobyte = None
597
+
598
+ def unquote_to_bytes(string):
599
+ """unquote_to_bytes('abc%20def') -> b'abc def'."""
600
+ # Note: strings are encoded as UTF-8. This is only an issue if it contains
601
+ # unescaped non-ASCII characters, which URIs should not.
602
+ if not string:
603
+ # Is it a string-like object?
604
+ string.split
605
+ return b''
606
+ if isinstance(string, str):
607
+ string = string.encode('utf-8')
608
+ bits = string.split(b'%')
609
+ if len(bits) == 1:
610
+ return string
611
+ res = [bits[0]]
612
+ append = res.append
613
+ # Delay the initialization of the table to not waste memory
614
+ # if the function is never called
615
+ global _hextobyte
616
+ if _hextobyte is None:
617
+ _hextobyte = {(a + b).encode(): bytes([int(a + b, 16)])
618
+ for a in _hexdig for b in _hexdig}
619
+ for item in bits[1:]:
620
+ try:
621
+ append(_hextobyte[item[:2]])
622
+ append(item[2:])
623
+ except KeyError:
624
+ append(b'%')
625
+ append(item)
626
+ return b''.join(res)
627
+
628
+ _asciire = re.compile('([\x00-\x7f]+)')
629
+
630
+ def unquote(string, encoding='utf-8', errors='replace'):
631
+ """Replace %xx escapes by their single-character equivalent. The optional
632
+ encoding and errors parameters specify how to decode percent-encoded
633
+ sequences into Unicode characters, as accepted by the bytes.decode()
634
+ method.
635
+ By default, percent-encoded sequences are decoded with UTF-8, and invalid
636
+ sequences are replaced by a placeholder character.
637
+
638
+ unquote('abc%20def') -> 'abc def'.
639
+ """
640
+ if '%' not in string:
641
+ string.split
642
+ return string
643
+ if encoding is None:
644
+ encoding = 'utf-8'
645
+ if errors is None:
646
+ errors = 'replace'
647
+ bits = _asciire.split(string)
648
+ res = [bits[0]]
649
+ append = res.append
650
+ for i in range(1, len(bits), 2):
651
+ append(unquote_to_bytes(bits[i]).decode(encoding, errors))
652
+ append(bits[i + 1])
653
+ return ''.join(res)
654
+
655
+
656
+ def parse_qs(qs, keep_blank_values=False, strict_parsing=False,
657
+ encoding='utf-8', errors='replace', max_num_fields=None, separator='&'):
658
+ """Parse a query given as a string argument.
659
+
660
+ Arguments:
661
+
662
+ qs: percent-encoded query string to be parsed
663
+
664
+ keep_blank_values: flag indicating whether blank values in
665
+ percent-encoded queries should be treated as blank strings.
666
+ A true value indicates that blanks should be retained as
667
+ blank strings. The default false value indicates that
668
+ blank values are to be ignored and treated as if they were
669
+ not included.
670
+
671
+ strict_parsing: flag indicating what to do with parsing errors.
672
+ If false (the default), errors are silently ignored.
673
+ If true, errors raise a ValueError exception.
674
+
675
+ encoding and errors: specify how to decode percent-encoded sequences
676
+ into Unicode characters, as accepted by the bytes.decode() method.
677
+
678
+ max_num_fields: int. If set, then throws a ValueError if there
679
+ are more than n fields read by parse_qsl().
680
+
681
+ separator: str. The symbol to use for separating the query arguments.
682
+ Defaults to &.
683
+
684
+ Returns a dictionary.
685
+ """
686
+ parsed_result = {}
687
+ pairs = parse_qsl(qs, keep_blank_values, strict_parsing,
688
+ encoding=encoding, errors=errors,
689
+ max_num_fields=max_num_fields, separator=separator)
690
+ for name, value in pairs:
691
+ if name in parsed_result:
692
+ parsed_result[name].append(value)
693
+ else:
694
+ parsed_result[name] = [value]
695
+ return parsed_result
696
+
697
+
698
+ def parse_qsl(qs, keep_blank_values=False, strict_parsing=False,
699
+ encoding='utf-8', errors='replace', max_num_fields=None, separator='&'):
700
+ """Parse a query given as a string argument.
701
+
702
+ Arguments:
703
+
704
+ qs: percent-encoded query string to be parsed
705
+
706
+ keep_blank_values: flag indicating whether blank values in
707
+ percent-encoded queries should be treated as blank strings.
708
+ A true value indicates that blanks should be retained as blank
709
+ strings. The default false value indicates that blank values
710
+ are to be ignored and treated as if they were not included.
711
+
712
+ strict_parsing: flag indicating what to do with parsing errors. If
713
+ false (the default), errors are silently ignored. If true,
714
+ errors raise a ValueError exception.
715
+
716
+ encoding and errors: specify how to decode percent-encoded sequences
717
+ into Unicode characters, as accepted by the bytes.decode() method.
718
+
719
+ max_num_fields: int. If set, then throws a ValueError
720
+ if there are more than n fields read by parse_qsl().
721
+
722
+ separator: str. The symbol to use for separating the query arguments.
723
+ Defaults to &.
724
+
725
+ Returns a list, as G-d intended.
726
+ """
727
+ qs, _coerce_result = _coerce_args(qs)
728
+
729
+ if not separator or (not isinstance(separator, (str, bytes))):
730
+ raise ValueError("Separator must be of type string or bytes.")
731
+
732
+ # If max_num_fields is defined then check that the number of fields
733
+ # is less than max_num_fields. This prevents a memory exhaustion DOS
734
+ # attack via post bodies with many fields.
735
+ if max_num_fields is not None:
736
+ num_fields = 1 + qs.count(separator)
737
+ if max_num_fields < num_fields:
738
+ raise ValueError('Max number of fields exceeded')
739
+
740
+ pairs = [s1 for s1 in qs.split(separator)]
741
+ r = []
742
+ for name_value in pairs:
743
+ if not name_value and not strict_parsing:
744
+ continue
745
+ nv = name_value.split('=', 1)
746
+ if len(nv) != 2:
747
+ if strict_parsing:
748
+ raise ValueError("bad query field: %r" % (name_value,))
749
+ # Handle case of a control-name with no equal sign
750
+ if keep_blank_values:
751
+ nv.append('')
752
+ else:
753
+ continue
754
+ if len(nv[1]) or keep_blank_values:
755
+ name = nv[0].replace('+', ' ')
756
+ name = unquote(name, encoding=encoding, errors=errors)
757
+ name = _coerce_result(name)
758
+ value = nv[1].replace('+', ' ')
759
+ value = unquote(value, encoding=encoding, errors=errors)
760
+ value = _coerce_result(value)
761
+ r.append((name, value))
762
+ return r
763
+
764
+ def unquote_plus(string, encoding='utf-8', errors='replace'):
765
+ """Like unquote(), but also replace plus signs by spaces, as required for
766
+ unquoting HTML form values.
767
+
768
+ unquote_plus('%7e/abc+def') -> '~/abc def'
769
+ """
770
+ string = string.replace('+', ' ')
771
+ return unquote(string, encoding, errors)
772
+
773
+ _ALWAYS_SAFE = frozenset(b'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
774
+ b'abcdefghijklmnopqrstuvwxyz'
775
+ b'0123456789'
776
+ b'_.-')
777
+ _ALWAYS_SAFE_BYTES = bytes(_ALWAYS_SAFE)
778
+ _safe_quoters = {}
779
+
780
+ class Quoter(collections.defaultdict):
781
+ """A mapping from bytes (in range(0,256)) to strings.
782
+
783
+ String values are percent-encoded byte values, unless the key < 128, and
784
+ in the "safe" set (either the specified safe set, or default set).
785
+ """
786
+ # Keeps a cache internally, using defaultdict, for efficiency (lookups
787
+ # of cached keys don't call Python code at all).
788
+ def __init__(self, safe):
789
+ """safe: bytes object."""
790
+ self.safe = _ALWAYS_SAFE.union(safe)
791
+
792
+ def __repr__(self):
793
+ # Without this, will just display as a defaultdict
794
+ return "<%s %r>" % (self.__class__.__name__, dict(self))
795
+
796
+ def __missing__(self, b):
797
+ # Handle a cache miss. Store quoted string in cache and return.
798
+ res = chr(b) if b in self.safe else '%{:02X}'.format(b)
799
+ self[b] = res
800
+ return res
801
+
802
+ def quote(string, safe='/', encoding=None, errors=None):
803
+ """quote('abc def') -> 'abc%20def'
804
+
805
+ Each part of a URL, e.g. the path info, the query, etc., has a
806
+ different set of reserved characters that must be quoted.
807
+
808
+ RFC 2396 Uniform Resource Identifiers (URI): Generic Syntax lists
809
+ the following reserved characters.
810
+
811
+ reserved = ";" | "/" | "?" | ":" | "@" | "&" | "=" | "+" |
812
+ "$" | ","
813
+
814
+ Each of these characters is reserved in some component of a URL,
815
+ but not necessarily in all of them.
816
+
817
+ By default, the quote function is intended for quoting the path
818
+ section of a URL. Thus, it will not encode '/'. This character
819
+ is reserved, but in typical usage the quote function is being
820
+ called on a path where the existing slash characters are used as
821
+ reserved characters.
822
+
823
+ string and safe may be either str or bytes objects. encoding and errors
824
+ must not be specified if string is a bytes object.
825
+
826
+ The optional encoding and errors parameters specify how to deal with
827
+ non-ASCII characters, as accepted by the str.encode method.
828
+ By default, encoding='utf-8' (characters are encoded with UTF-8), and
829
+ errors='strict' (unsupported characters raise a UnicodeEncodeError).
830
+ """
831
+ if isinstance(string, str):
832
+ if not string:
833
+ return string
834
+ if encoding is None:
835
+ encoding = 'utf-8'
836
+ if errors is None:
837
+ errors = 'strict'
838
+ string = string.encode(encoding, errors)
839
+ else:
840
+ if encoding is not None:
841
+ raise TypeError("quote() doesn't support 'encoding' for bytes")
842
+ if errors is not None:
843
+ raise TypeError("quote() doesn't support 'errors' for bytes")
844
+ return quote_from_bytes(string, safe)
845
+
846
+ def quote_plus(string, safe='', encoding=None, errors=None):
847
+ """Like quote(), but also replace ' ' with '+', as required for quoting
848
+ HTML form values. Plus signs in the original string are escaped unless
849
+ they are included in safe. It also does not have safe default to '/'.
850
+ """
851
+ # Check if ' ' in string, where string may either be a str or bytes. If
852
+ # there are no spaces, the regular quote will produce the right answer.
853
+ if ((isinstance(string, str) and ' ' not in string) or
854
+ (isinstance(string, bytes) and b' ' not in string)):
855
+ return quote(string, safe, encoding, errors)
856
+ if isinstance(safe, str):
857
+ space = ' '
858
+ else:
859
+ space = b' '
860
+ string = quote(string, safe + space, encoding, errors)
861
+ return string.replace(' ', '+')
862
+
863
+ def quote_from_bytes(bs, safe='/'):
864
+ """Like quote(), but accepts a bytes object rather than a str, and does
865
+ not perform string-to-bytes encoding. It always returns an ASCII string.
866
+ quote_from_bytes(b'abc def\x3f') -> 'abc%20def%3f'
867
+ """
868
+ if not isinstance(bs, (bytes, bytearray)):
869
+ raise TypeError("quote_from_bytes() expected bytes")
870
+ if not bs:
871
+ return ''
872
+ if isinstance(safe, str):
873
+ # Normalize 'safe' by converting to bytes and removing non-ASCII chars
874
+ safe = safe.encode('ascii', 'ignore')
875
+ else:
876
+ safe = bytes([c for c in safe if c < 128])
877
+ if not bs.rstrip(_ALWAYS_SAFE_BYTES + safe):
878
+ return bs.decode()
879
+ try:
880
+ quoter = _safe_quoters[safe]
881
+ except KeyError:
882
+ _safe_quoters[safe] = quoter = Quoter(safe).__getitem__
883
+ return ''.join([quoter(char) for char in bs])
884
+
885
+ def urlencode(query, doseq=False, safe='', encoding=None, errors=None,
886
+ quote_via=quote_plus):
887
+ """Encode a dict or sequence of two-element tuples into a URL query string.
888
+
889
+ If any values in the query arg are sequences and doseq is true, each
890
+ sequence element is converted to a separate parameter.
891
+
892
+ If the query arg is a sequence of two-element tuples, the order of the
893
+ parameters in the output will match the order of parameters in the
894
+ input.
895
+
896
+ The components of a query arg may each be either a string or a bytes type.
897
+
898
+ The safe, encoding, and errors parameters are passed down to the function
899
+ specified by quote_via (encoding and errors only if a component is a str).
900
+ """
901
+
902
+ if hasattr(query, "items"):
903
+ query = query.items()
904
+ else:
905
+ # It's a bother at times that strings and string-like objects are
906
+ # sequences.
907
+ try:
908
+ # non-sequence items should not work with len()
909
+ # non-empty strings will fail this
910
+ if len(query) and not isinstance(query[0], tuple):
911
+ raise TypeError
912
+ # Zero-length sequences of all types will get here and succeed,
913
+ # but that's a minor nit. Since the original implementation
914
+ # allowed empty dicts that type of behavior probably should be
915
+ # preserved for consistency
916
+ except TypeError:
917
+ ty, va, tb = sys.exc_info()
918
+ raise TypeError("not a valid non-string sequence "
919
+ "or mapping object").with_traceback(tb)
920
+
921
+ l = []
922
+ if not doseq:
923
+ for k, v in query:
924
+ if isinstance(k, bytes):
925
+ k = quote_via(k, safe)
926
+ else:
927
+ k = quote_via(str(k), safe, encoding, errors)
928
+
929
+ if isinstance(v, bytes):
930
+ v = quote_via(v, safe)
931
+ else:
932
+ v = quote_via(str(v), safe, encoding, errors)
933
+ l.append(k + '=' + v)
934
+ else:
935
+ for k, v in query:
936
+ if isinstance(k, bytes):
937
+ k = quote_via(k, safe)
938
+ else:
939
+ k = quote_via(str(k), safe, encoding, errors)
940
+
941
+ if isinstance(v, bytes):
942
+ v = quote_via(v, safe)
943
+ l.append(k + '=' + v)
944
+ elif isinstance(v, str):
945
+ v = quote_via(v, safe, encoding, errors)
946
+ l.append(k + '=' + v)
947
+ else:
948
+ try:
949
+ # Is this a sufficient test for sequence-ness?
950
+ x = len(v)
951
+ except TypeError:
952
+ # not a sequence
953
+ v = quote_via(str(v), safe, encoding, errors)
954
+ l.append(k + '=' + v)
955
+ else:
956
+ # loop over the sequence
957
+ for elt in v:
958
+ if isinstance(elt, bytes):
959
+ elt = quote_via(elt, safe)
960
+ else:
961
+ elt = quote_via(str(elt), safe, encoding, errors)
962
+ l.append(k + '=' + elt)
963
+ return '&'.join(l)
964
+
965
+ def to_bytes(url):
966
+ """to_bytes(u"URL") --> 'URL'."""
967
+ # Most URL schemes require ASCII. If that changes, the conversion
968
+ # can be relaxed.
969
+ # XXX get rid of to_bytes()
970
+ if isinstance(url, str):
971
+ try:
972
+ url = url.encode("ASCII").decode()
973
+ except UnicodeError:
974
+ raise UnicodeError("URL " + repr(url) +
975
+ " contains non-ASCII characters")
976
+ return url
977
+
978
+ def unwrap(url):
979
+ """unwrap('<URL:type://host/path>') --> 'type://host/path'."""
980
+ url = str(url).strip()
981
+ if url[:1] == '<' and url[-1:] == '>':
982
+ url = url[1:-1].strip()
983
+ if url[:4] == 'URL:': url = url[4:].strip()
984
+ return url
985
+
986
+ _typeprog = None
987
+ def splittype(url):
988
+ """splittype('type:opaquestring') --> 'type', 'opaquestring'."""
989
+ global _typeprog
990
+ if _typeprog is None:
991
+ _typeprog = re.compile('([^/:]+):(.*)', re.DOTALL)
992
+
993
+ match = _typeprog.match(url)
994
+ if match:
995
+ scheme, data = match.groups()
996
+ return scheme.lower(), data
997
+ return None, url
998
+
999
+ _hostprog = None
1000
+ def splithost(url):
1001
+ """splithost('//host[:port]/path') --> 'host[:port]', '/path'."""
1002
+ global _hostprog
1003
+ if _hostprog is None:
1004
+ _hostprog = re.compile('//([^/#?]*)(.*)', re.DOTALL)
1005
+
1006
+ match = _hostprog.match(url)
1007
+ if match:
1008
+ host_port, path = match.groups()
1009
+ if path and path[0] != '/':
1010
+ path = '/' + path
1011
+ return host_port, path
1012
+ return None, url
1013
+
1014
+ def splituser(host):
1015
+ """splituser('user[:passwd]@host[:port]') --> 'user[:passwd]', 'host[:port]'."""
1016
+ user, delim, host = host.rpartition('@')
1017
+ return (user if delim else None), host
1018
+
1019
+ def splitpasswd(user):
1020
+ """splitpasswd('user:passwd') -> 'user', 'passwd'."""
1021
+ user, delim, passwd = user.partition(':')
1022
+ return user, (passwd if delim else None)
1023
+
1024
+ # splittag('/path#tag') --> '/path', 'tag'
1025
+ _portprog = None
1026
+ def splitport(host):
1027
+ """splitport('host:port') --> 'host', 'port'."""
1028
+ global _portprog
1029
+ if _portprog is None:
1030
+ _portprog = re.compile('(.*):([0-9]*)$', re.DOTALL)
1031
+
1032
+ match = _portprog.match(host)
1033
+ if match:
1034
+ host, port = match.groups()
1035
+ if port:
1036
+ return host, port
1037
+ return host, None
1038
+
1039
+ def splitnport(host, defport=-1):
1040
+ """Split host and port, returning numeric port.
1041
+ Return given default port if no ':' found; defaults to -1.
1042
+ Return numerical port if a valid number are found after ':'.
1043
+ Return None if ':' but not a valid number."""
1044
+ host, delim, port = host.rpartition(':')
1045
+ if not delim:
1046
+ host = port
1047
+ elif port:
1048
+ try:
1049
+ nport = int(port)
1050
+ except ValueError:
1051
+ nport = None
1052
+ return host, nport
1053
+ return host, defport
1054
+
1055
+ def splitquery(url):
1056
+ """splitquery('/path?query') --> '/path', 'query'."""
1057
+ path, delim, query = url.rpartition('?')
1058
+ if delim:
1059
+ return path, query
1060
+ return url, None
1061
+
1062
+ def splittag(url):
1063
+ """splittag('/path#tag') --> '/path', 'tag'."""
1064
+ path, delim, tag = url.rpartition('#')
1065
+ if delim:
1066
+ return path, tag
1067
+ return url, None
1068
+
1069
+ def splitattr(url):
1070
+ """splitattr('/path;attr1=value1;attr2=value2;...') ->
1071
+ '/path', ['attr1=value1', 'attr2=value2', ...]."""
1072
+ words = url.split(';')
1073
+ return words[0], words[1:]
1074
+
1075
+ def splitvalue(attr):
1076
+ """splitvalue('attr=value') --> 'attr', 'value'."""
1077
+ attr, delim, value = attr.partition('=')
1078
+ return attr, (value if delim else None)