Skip to content

Commit

Permalink
simplify non-empty string check (#951)
Browse files Browse the repository at this point in the history
  • Loading branch information
balamurugana authored Aug 6, 2020
1 parent c6c43ab commit a7d7382
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 49 deletions.
55 changes: 27 additions & 28 deletions minio/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,13 @@
from .fold_case_dict import FoldCaseDict
from .helpers import (DEFAULT_PART_SIZE, MAX_MULTIPART_COUNT, MAX_PART_SIZE,
MAX_POOL_SIZE, MIN_PART_SIZE, amzprefix_user_metadata,
check_bucket_name, dump_http, get_md5_base64digest,
get_s3_region_from_endpoint, get_scheme_host,
get_sha256_hexdigest, get_target_url, is_amz_header,
is_non_empty_string, is_supported_header,
is_valid_endpoint, is_valid_notification_config,
is_valid_policy_type, is_valid_sse_c_object,
is_valid_sse_object, mkdir_p, optimal_part_info,
read_full)
check_bucket_name, check_non_empty_string, dump_http,
get_md5_base64digest, get_s3_region_from_endpoint,
get_scheme_host, get_sha256_hexdigest, get_target_url,
is_amz_header, is_supported_header, is_valid_endpoint,
is_valid_notification_config, is_valid_policy_type,
is_valid_sse_c_object, is_valid_sse_object, mkdir_p,
optimal_part_info, read_full)
from .parsers import (parse_assume_role, parse_copy_object,
parse_get_bucket_notification, parse_list_buckets,
parse_list_multipart_uploads, parse_list_object_versions,
Expand Down Expand Up @@ -241,7 +240,7 @@ def select_object_content(self, bucket_name, object_name, opts):
data = client.select_object_content('foo', 'test.csv', options)
"""
check_bucket_name(bucket_name)
is_non_empty_string(object_name)
check_non_empty_string(object_name)

content = xml_marshal_select(opts)
url_values = {
Expand Down Expand Up @@ -774,7 +773,7 @@ def fget_object(self, bucket_name, object_name, file_path,
)
"""
check_bucket_name(bucket_name)
is_non_empty_string(object_name)
check_non_empty_string(object_name)

if os.path.isdir(file_path):
raise OSError("file is a directory.")
Expand Down Expand Up @@ -868,7 +867,7 @@ def get_object(self, bucket_name, object_name, request_headers=None,
response.release_conn()
"""
check_bucket_name(bucket_name)
is_non_empty_string(object_name)
check_non_empty_string(object_name)

return self._get_partial_object(
bucket_name,
Expand Down Expand Up @@ -907,7 +906,7 @@ def get_partial_object(self, bucket_name, object_name, offset=0, length=0,
response.release_conn()
"""
check_bucket_name(bucket_name)
is_non_empty_string(object_name)
check_non_empty_string(object_name)

return self._get_partial_object(
bucket_name,
Expand Down Expand Up @@ -952,8 +951,8 @@ def copy_object(self, bucket_name, object_name, object_source,
)
"""
check_bucket_name(bucket_name)
is_non_empty_string(object_name)
is_non_empty_string(object_source)
check_non_empty_string(object_name)
check_non_empty_string(object_source)

headers = {}

Expand Down Expand Up @@ -1011,7 +1010,7 @@ def put_object(self, bucket_name, object_name, data, length,
"""
is_valid_sse_object(sse)
check_bucket_name(bucket_name)
is_non_empty_string(object_name)
check_non_empty_string(object_name)

if progress:
if not isinstance(progress, Thread):
Expand Down Expand Up @@ -1179,7 +1178,7 @@ def stat_object(self, bucket_name, object_name, sse=None, version_id=None,
"""

check_bucket_name(bucket_name)
is_non_empty_string(object_name)
check_non_empty_string(object_name)

headers = {}
if sse:
Expand Down Expand Up @@ -1237,7 +1236,7 @@ def remove_object(self, bucket_name, object_name, version_id=None):
)
"""
check_bucket_name(bucket_name)
is_non_empty_string(object_name)
check_non_empty_string(object_name)

# No reason to store successful response, for errors
# relevant exceptions are thrown.
Expand Down Expand Up @@ -1306,7 +1305,7 @@ def remove_objects(self, bucket_name, objects_iter):
def check_name(name):
if not isinstance(name, basestring):
name = name[0]
is_non_empty_string(name)
check_non_empty_string(name)
return True

while True:
Expand Down Expand Up @@ -1436,8 +1435,8 @@ def _list_object_parts(self, bucket_name, object_name, upload_id):
:param upload_id: Upload id of the previously uploaded object name.
"""
check_bucket_name(bucket_name)
is_non_empty_string(object_name)
is_non_empty_string(upload_id)
check_non_empty_string(object_name)
check_non_empty_string(upload_id)

query = {
'uploadId': upload_id,
Expand Down Expand Up @@ -1474,7 +1473,7 @@ def remove_incomplete_upload(self, bucket_name, object_name):
minio.remove_incomplete_upload("my-bucketname", "my-objectname")
"""
check_bucket_name(bucket_name)
is_non_empty_string(object_name)
check_non_empty_string(object_name)

uploads = self._list_incomplete_uploads(bucket_name, object_name,
recursive=True,
Expand Down Expand Up @@ -1544,7 +1543,7 @@ def presigned_url(self, method,
print(url)
"""
check_bucket_name(bucket_name)
is_non_empty_string(object_name)
check_non_empty_string(object_name)

if (expires.total_seconds() < 1 or
expires.total_seconds() > _MAX_EXPIRY_TIME):
Expand Down Expand Up @@ -1740,7 +1739,7 @@ def _get_partial_object(self, bucket_name, object_name,
"""
is_valid_sse_c_object(sse)
check_bucket_name(bucket_name)
is_non_empty_string(object_name)
check_non_empty_string(object_name)

headers = request_headers or {}

Expand Down Expand Up @@ -1783,7 +1782,7 @@ def _do_put_object(self, bucket_name, object_name, part_data,
:param progress: A progress object
"""
check_bucket_name(bucket_name)
is_non_empty_string(object_name)
check_non_empty_string(object_name)

# Accept only bytes - otherwise we need to know how to encode
# the data to bytes before storing in the object.
Expand Down Expand Up @@ -1866,7 +1865,7 @@ def _stream_put_object(self, bucket_name, object_name,
:param part_size: Multipart part size
"""
check_bucket_name(bucket_name)
is_non_empty_string(object_name)
check_non_empty_string(object_name)
if not callable(getattr(data, 'read')):
raise ValueError(
'Invalid input data does not implement'
Expand Down Expand Up @@ -1977,7 +1976,7 @@ def _new_multipart_upload(self, bucket_name, object_name,
:return: Returns an upload id.
"""
check_bucket_name(bucket_name)
is_non_empty_string(object_name)
check_non_empty_string(object_name)

headers = {}
if metadata:
Expand All @@ -2003,8 +2002,8 @@ def _complete_multipart_upload(self, bucket_name, object_name,
:param uploaded_parts: Key, Value dictionary of uploaded parts.
"""
check_bucket_name(bucket_name)
is_non_empty_string(object_name)
is_non_empty_string(upload_id)
check_non_empty_string(object_name)
check_non_empty_string(upload_id)

# Order uploaded parts as required by S3 specification
ordered_parts = []
Expand Down
6 changes: 3 additions & 3 deletions minio/copy_conditions.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
except ImportError:
from collections.abc import MutableMapping

from .helpers import is_non_empty_string
from .helpers import check_non_empty_string

# CopyCondition explanation:
# http://docs.aws.amazon.com/AmazonS3/latest/API/RESTObjectCOPY.html
Expand Down Expand Up @@ -75,12 +75,12 @@ def __len__(self):

def set_match_etag(self, etag):
"""Set ETag match condition."""
is_non_empty_string(etag)
check_non_empty_string(etag)
self._store["X-Amz-Copy-Source-If-Match"] = etag

def set_match_etag_except(self, etag):
"""Set ETag not match condition."""
is_non_empty_string(etag)
check_non_empty_string(etag)
self._store["X-Amz-Copy-Source-If-None-Match"] = etag

def set_unmodified_since(self, mod_time):
Expand Down
22 changes: 7 additions & 15 deletions minio/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -400,21 +400,13 @@ def check_bucket_name(bucket_name, strict=False):
' Bucket: {0}'.format(bucket_name))


def is_non_empty_string(input_string):
"""
Validate if non empty string
:param input_string: Input is a *str*.
:return: True if input is string and non empty.
Raise :exc:`Exception` otherwise.
"""
def check_non_empty_string(string):
"""Check whether given string is not empty."""
try:
if not input_string.strip():
if not string.strip():
raise ValueError()
except AttributeError as error:
raise TypeError(error)

return True
except AttributeError as exc:
raise TypeError(exc)


def is_valid_policy_type(policy):
Expand All @@ -429,7 +421,7 @@ def is_valid_policy_type(policy):
if not isinstance(policy, string_type):
raise TypeError('policy can only be of type str')

is_non_empty_string(policy)
check_non_empty_string(policy)

return True

Expand Down Expand Up @@ -587,7 +579,7 @@ def encode_object_name(object_name):
:param object_name: Un-encoded object name.
:return: URL encoded input object name.
"""
is_non_empty_string(object_name)
check_non_empty_string(object_name)
return quote(object_name)


Expand Down
6 changes: 3 additions & 3 deletions minio/post_policy.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
import json

from .error import InvalidArgumentError
from .helpers import check_bucket_name, is_non_empty_string
from .helpers import check_bucket_name, check_non_empty_string


# Policy explanation:
Expand Down Expand Up @@ -66,7 +66,7 @@ def set_key(self, key):
:param key: set key name.
"""
is_non_empty_string(key)
check_non_empty_string(key)

self.policies.append(('eq', '$key', key))
self.form_data['key'] = key
Expand All @@ -78,7 +78,7 @@ def set_key_startswith(self, key_startswith):
:param key_startswith: set key prefix name.
"""
is_non_empty_string(key_startswith)
check_non_empty_string(key_startswith)

self.policies.append(('starts-with', '$key', key_startswith))
self.form_data['key'] = key_startswith
Expand Down

0 comments on commit a7d7382

Please sign in to comment.