From 26b8752c460fc7f68e9934609be4d7955acd9fc1 Mon Sep 17 00:00:00 2001 From: "Bala.FA" Date: Thu, 9 Apr 2020 14:21:42 +0530 Subject: [PATCH] remove unused imports and use pythonic approach --- minio/select/helpers.py | 6 +-- minio/select/reader.py | 81 ++++++++++++++++++----------------------- 2 files changed, 37 insertions(+), 50 deletions(-) diff --git a/minio/select/helpers.py b/minio/select/helpers.py index 32f4e9e5..34d9f751 100644 --- a/minio/select/helpers.py +++ b/minio/select/helpers.py @@ -50,11 +50,7 @@ def validate_crc(current_value, expected_value): ''' Validate through CRC check ''' - crc_current = calculate_crc(current_value) - crc_expected = byte_int(expected_value) - if crc_current == crc_expected: - return True - return False + return calculate_crc(current_value) == byte_int(expected_value) def byte_int(data_bytes): diff --git a/minio/select/reader.py b/minio/select/reader.py index f7b20b31..37ece1f9 100644 --- a/minio/select/reader.py +++ b/minio/select/reader.py @@ -30,14 +30,10 @@ import io import sys -from binascii import crc32 from xml.etree import ElementTree -from xml.etree.ElementTree import ParseError -from .helpers import (EVENT_RECORDS, EVENT_PROGRESS, - EVENT_STATS, EVENT_CONT, - EVENT, EVENT_CONTENT_TYPE, - EVENT_END, ERROR) +from .helpers import (EVENT_RECORDS, EVENT_STATS, + EVENT, EVENT_CONTENT_TYPE, ERROR) from .helpers import (validate_crc, calculate_crc, byte_int) from .errors import (SelectMessageError, SelectCRCValidationError) @@ -55,22 +51,22 @@ def _extract_header(header_bytes): header_name_byte_length = byte_int( header_bytes[header_byte_parsed:header_byte_parsed+1]) header_byte_parsed += 1 - header_name = \ - header_bytes[header_byte_parsed: - header_byte_parsed+header_name_byte_length] + header_name = header_bytes[ + header_byte_parsed:header_byte_parsed+header_name_byte_length + ] header_byte_parsed += header_name_byte_length # Header Value Type is of 1 bytes and is skipped header_byte_parsed += 1 - value_string_byte_length = \ - byte_int(header_bytes[header_byte_parsed: - header_byte_parsed+2]) + value_string_byte_length = byte_int( + header_bytes[header_byte_parsed:header_byte_parsed+2] + ) header_byte_parsed += 2 - header_value = \ - header_bytes[header_byte_parsed: - header_byte_parsed+value_string_byte_length] + header_value = header_bytes[ + header_byte_parsed:header_byte_parsed+value_string_byte_length + ] header_byte_parsed += value_string_byte_length - header_map[header_name.decode("utf-8").lstrip(":")] = \ - header_value.decode("utf-8").lstrip(":") + header_map[header_name.decode( + "utf-8").lstrip(":")] = header_value.decode("utf-8").lstrip(":") return header_map @@ -126,12 +122,12 @@ def __extract_message(self): crc_bytes = io.BytesIO() total_bytes_len = self.response.read(4) - if len(total_bytes_len) == 0: + if not total_bytes_len: return {} total_length = byte_int(total_bytes_len) header_bytes_len = self.response.read(4) - if len(header_bytes_len) == 0: + if not header_bytes_len: return {} header_len = byte_int(header_bytes_len) @@ -150,7 +146,7 @@ def __extract_message(self): crc_bytes.write(prelude_bytes_crc) header_bytes = self.response.read(header_len) - if len(header_bytes) == 0: + if not header_bytes: raise SelectMessageError( "Premature truncation of select message header" + ", server is sending corrupt message?") @@ -161,40 +157,36 @@ def __extract_message(self): payload_length = total_length - header_len - int(16) payload_bytes = b'' event_type = header_map["event-type"] + if header_map["message-type"] == ERROR: raise SelectMessageError( header_map["error-code"] + ":\"" + header_map["error-message"] + "\"") - elif header_map["message-type"] == EVENT: - if event_type == EVENT_END: - pass - elif event_type == EVENT_CONT: - pass - elif event_type == EVENT_STATS: - content_type = header_map["content-type"] - if content_type != EVENT_CONTENT_TYPE: - raise SelectMessageError( - "Unrecognized content-type {0}".format(content_type)) - else: - payload_bytes = self.response.read(payload_length) - self.stat = _parse_stats(payload_bytes) - - elif event_type == EVENT_RECORDS: - payload_bytes = self.response.read(payload_length) - else: + + if header_map["message-type"] != EVENT: raise SelectMessageError( "Unrecognized message-type {0}".format( header_map["message-type"]) ) + if event_type == EVENT_STATS: + content_type = header_map["content-type"] + if content_type != EVENT_CONTENT_TYPE: + raise SelectMessageError( + "Unrecognized content-type {0}".format(content_type)) + + payload_bytes = self.response.read(payload_length) + self.stat = _parse_stats(payload_bytes) + elif event_type == EVENT_RECORDS: + payload_bytes = self.response.read(payload_length) + crc_bytes.write(payload_bytes) message_crc = self.response.read(4) - if len(message_crc) == 0: + if not message_crc: return {} - if not validate_crc(crc_bytes.getvalue(), - message_crc): + if not validate_crc(crc_bytes.getvalue(), message_crc): raise SelectCRCValidationError( {"Checksum Mismatch, MessageCRC of " + str(calculate_crc(crc_bytes.getvalue())) + @@ -213,14 +205,13 @@ def stream(self, num_bytes=32*1024): caller should call self.close() to close the stream. """ while not self.response.isclosed(): - if len(self.remaining_bytes) == 0: + if not self.remaining_bytes: message = self.__extract_message() - if EVENT_RECORDS in message: - self.remaining_bytes = message.get(EVENT_RECORDS, b'') - else: - # For all other events continue + if EVENT_RECORDS not in message: continue + self.remaining_bytes = message.get(EVENT_RECORDS, b'') + result = self.remaining_bytes if num_bytes < len(self.remaining_bytes): result = self.remaining_bytes[:num_bytes]