Browse code

Move s3_quote to Utils module

Florent Viard authored on 2020/04/14 06:22:03
Showing 3 changed files
... ...
@@ -14,15 +14,12 @@ import base64
14 14
 
15 15
 from . import Config
16 16
 from logging import debug
17
-from .Utils import encode_to_s3, time_to_epoch, deunicodise, decode_from_s3, check_bucket_name_dns_support
17
+from .Utils import (encode_to_s3, time_to_epoch, deunicodise, decode_from_s3,
18
+                    check_bucket_name_dns_support, s3_quote)
18 19
 from .SortedDict import SortedDict
19 20
 
20 21
 import datetime
21
-try:
22
-    # python 3 support
23
-    from urllib import quote
24
-except ImportError:
25
-    from urllib.parse import quote
22
+
26 23
 
27 24
 from hashlib import sha1, sha256
28 25
 
... ...
@@ -250,29 +247,6 @@ def sign_request_v4(method='GET', host='', canonical_uri='/', params=None,
250 250
     return new_headers
251 251
 __all__.append("sign_request_v4")
252 252
 
253
-def s3_quote(param, quote_backslashes=True, unicode_output=False):
254
-    """
255
-    URI encode every byte. UriEncode() must enforce the following rules:
256
-    - URI encode every byte except the unreserved characters: 'A'-'Z', 'a'-'z', '0'-'9', '-', '.', '_', and '~'.
257
-    - The space character is a reserved character and must be encoded as "%20" (and not as "+").
258
-    - Each URI encoded byte is formed by a '%' and the two-digit hexadecimal value of the byte.
259
-    - Letters in the hexadecimal value must be uppercase, for example "%1A".
260
-    - Encode the forward slash character, '/', everywhere except in the object key name.
261
-    For example, if the object key name is photos/Jan/sample.jpg, the forward slash in the key name is not encoded.
262
-    """
263
-    if quote_backslashes:
264
-        safe_chars = "~"
265
-    else:
266
-        safe_chars = "~/"
267
-    param = encode_to_s3(param)
268
-    param = quote(param, safe=safe_chars)
269
-    if unicode_output:
270
-        param = decode_from_s3(param)
271
-    else:
272
-        param = encode_to_s3(param)
273
-    return param
274
-__all__.append("s3_quote")
275
-
276 253
 def checksum_sha256_file(filename, offset=0, size=None):
277 254
     try:
278 255
         hash = sha256()
... ...
@@ -33,7 +33,11 @@ try:
33 33
 except ImportError:
34 34
     from md5 import md5
35 35
 
36
-from .Utils import *
36
+from .Utils import (getListFromXml, getTextFromXml, getRootTagName,
37
+                    convertHeaderTupleListToDict, hash_file_md5, unicodise,
38
+                    deunicodise, decode_from_s3, encode_to_s3, s3_quote,
39
+                    check_bucket_name, check_bucket_name_dns_support,
40
+                    getHostnameFromBucket, calculateChecksum)
37 41
 from .SortedDict import SortedDict
38 42
 from .AccessLog import AccessLog
39 43
 from .ACL import ACL, GranteeLogDelivery
... ...
@@ -44,7 +48,7 @@ from .MultiPart import MultiPartUpload
44 44
 from .S3Uri import S3Uri
45 45
 from .ConnMan import ConnMan
46 46
 from .Crypto import (sign_request_v2, sign_request_v4, checksum_sha256_file,
47
-                     checksum_sha256_buffer, s3_quote, format_param_str)
47
+                     checksum_sha256_buffer, format_param_str)
48 48
 
49 49
 try:
50 50
     from ctypes import ArgumentError
... ...
@@ -386,6 +386,29 @@ def encode_to_s3(string, errors = "replace"):
386 386
         raise UnicodeEncodeError("Conversion from unicode failed: %r" % string)
387 387
 __all__.append("encode_to_s3")
388 388
 
389
+def s3_quote(param, quote_backslashes=True, unicode_output=False):
390
+    """
391
+    URI encode every byte. UriEncode() must enforce the following rules:
392
+    - URI encode every byte except the unreserved characters: 'A'-'Z', 'a'-'z', '0'-'9', '-', '.', '_', and '~'.
393
+    - The space character is a reserved character and must be encoded as "%20" (and not as "+").
394
+    - Each URI encoded byte is formed by a '%' and the two-digit hexadecimal value of the byte.
395
+    - Letters in the hexadecimal value must be uppercase, for example "%1A".
396
+    - Encode the forward slash character, '/', everywhere except in the object key name.
397
+    For example, if the object key name is photos/Jan/sample.jpg, the forward slash in the key name is not encoded.
398
+    """
399
+    if quote_backslashes:
400
+        safe_chars = "~"
401
+    else:
402
+        safe_chars = "~/"
403
+    param = encode_to_s3(param)
404
+    param = quote(param, safe=safe_chars)
405
+    if unicode_output:
406
+        param = decode_from_s3(param)
407
+    else:
408
+        param = encode_to_s3(param)
409
+    return param
410
+__all__.append("s3_quote")
411
+
389 412
 ## Low level methods
390 413
 def urlencode_string(string, urlencoding_mode = None, unicode_output=False):
391 414
     string = encode_to_s3(string)