Remove usage of six.moves.binary_type.

This commit is contained in:
rht 2017-11-09 09:03:33 +01:00 committed by Tim Abbott
parent 5937141264
commit 5ee40bf718
8 changed files with 27 additions and 36 deletions

View File

@ -1,5 +1,4 @@
from six import binary_type
from typing import Any, Callable, Dict, List, Tuple, Text
# This file needs to be different from cache.py because cache.py
@ -33,7 +32,7 @@ def message_fetch_objects():
id__gt=max_id - MESSAGE_CACHE_SIZE)
def message_cache_items(items_for_remote_cache, message):
# type: (Dict[Text, Tuple[binary_type]], Message) -> None
# type: (Dict[Text, Tuple[bytes]], Message) -> None
'''
Note: this code is untested, and the caller has been
commented out for a while.

View File

@ -20,7 +20,6 @@ from zerver.lib.send_email import FromAddress
from zerver.models import Stream, Recipient, \
get_user_profile_by_id, get_display_recipient, get_personal_recipient, \
Message, Realm, UserProfile, get_system_bot
from six import binary_type
import talon
from talon import quotations
@ -221,7 +220,7 @@ def get_message_part_by_type(message, content_type):
for idx, part in enumerate(message.walk()):
if part.get_content_type() == content_type:
content = part.get_payload(decode=True)
assert isinstance(content, binary_type)
assert isinstance(content, bytes)
if charsets[idx]:
return content.decode(charsets[idx], errors="ignore")
return None
@ -267,7 +266,7 @@ def extract_and_upload_attachments(message, realm):
filename = part.get_filename()
if filename:
attachment = part.get_payload(decode=True)
if isinstance(attachment, binary_type):
if isinstance(attachment, bytes):
s3_url = upload_message_image(filename, len(attachment), content_type,
attachment,
user_profile,

View File

@ -5,7 +5,6 @@ import zlib
from django.utils.translation import ugettext as _
from django.utils.timezone import now as timezone_now
from six import binary_type
from zerver.lib.avatar import get_avatar_field
import zerver.lib.bugdown as bugdown
@ -111,16 +110,16 @@ def sew_messages_and_reactions(messages, reactions):
def extract_message_dict(message_bytes):
# type: (binary_type) -> Dict[str, Any]
# type: (bytes) -> Dict[str, Any]
return dict_with_str_keys(ujson.loads(zlib.decompress(message_bytes).decode("utf-8")))
def stringify_message_dict(message_dict):
# type: (Dict[str, Any]) -> binary_type
# type: (Dict[str, Any]) -> bytes
return zlib.compress(force_bytes(ujson.dumps(message_dict)))
@cache_with_key(to_dict_cache_key, timeout=3600*24)
def message_to_dict_json(message):
# type: (Message) -> binary_type
# type: (Message) -> bytes
return MessageDict.to_dict_uncached(message)
class MessageDict:
@ -178,7 +177,7 @@ class MessageDict:
@staticmethod
def to_dict_uncached(message):
# type: (Message) -> binary_type
# type: (Message) -> bytes
dct = MessageDict.to_dict_uncached_helper(message)
return stringify_message_dict(dct)

View File

@ -58,7 +58,6 @@ import time
import ujson
import unittest
import urllib
from six import binary_type
from zerver.lib.str_utils import NonBinaryStr
from moto import mock_s3_deprecated
@ -134,13 +133,13 @@ def simulated_empty_cache():
@contextmanager
def queries_captured(include_savepoints=False):
# type: (Optional[bool]) -> Generator[List[Dict[str, Union[str, binary_type]]], None, None]
# type: (Optional[bool]) -> Generator[List[Dict[str, Union[str, bytes]]], None, None]
'''
Allow a user to capture just the queries executed during
the with statement.
'''
queries = [] # type: List[Dict[str, Union[str, binary_type]]]
queries = [] # type: List[Dict[str, Union[str, bytes]]]
def wrapper_execute(self, action, sql, params=()):
# type: (TimeTrackingCursor, Callable[[NonBinaryStr, Iterable[Any]], None], NonBinaryStr, Iterable[Any]) -> None

View File

@ -28,7 +28,6 @@ import base64
import os
import re
from PIL import Image, ImageOps
from six import binary_type
import io
import random
import logging
@ -89,7 +88,7 @@ class ExceededQuotaError(JsonableError):
code = ErrorCode.QUOTA_EXCEEDED
def resize_avatar(image_data, size=DEFAULT_AVATAR_SIZE):
# type: (binary_type, int) -> binary_type
# type: (bytes, int) -> bytes
try:
im = Image.open(io.BytesIO(image_data))
im = ImageOps.fit(im, (size, size), Image.ANTIALIAS)
@ -101,7 +100,7 @@ def resize_avatar(image_data, size=DEFAULT_AVATAR_SIZE):
def resize_emoji(image_data, size=DEFAULT_EMOJI_SIZE):
# type: (binary_type, int) -> binary_type
# type: (bytes, int) -> bytes
try:
im = Image.open(io.BytesIO(image_data))
image_format = im.format
@ -127,7 +126,7 @@ def resize_emoji(image_data, size=DEFAULT_EMOJI_SIZE):
class ZulipUploadBackend:
def upload_message_image(self, uploaded_file_name, uploaded_file_size,
content_type, file_data, user_profile, target_realm=None):
# type: (Text, int, Optional[Text], binary_type, UserProfile, Optional[Realm]) -> Text
# type: (Text, int, Optional[Text], bytes, UserProfile, Optional[Realm]) -> Text
raise NotImplementedError()
def upload_avatar_image(self, user_file, acting_user_profile, target_user_profile):
@ -184,7 +183,7 @@ def upload_image_to_s3(
content_type,
user_profile,
contents):
# type: (NonBinaryStr, Text, Optional[Text], UserProfile, binary_type) -> None
# type: (NonBinaryStr, Text, Optional[Text], UserProfile, bytes) -> None
conn = S3Connection(settings.S3_KEY, settings.S3_SECRET_KEY)
bucket = get_bucket(conn, bucket_name)
@ -259,7 +258,7 @@ class S3UploadBackend(ZulipUploadBackend):
def upload_message_image(self, uploaded_file_name, uploaded_file_size,
content_type, file_data, user_profile, target_realm=None):
# type: (Text, int, Optional[Text], binary_type, UserProfile, Optional[Realm]) -> Text
# type: (Text, int, Optional[Text], bytes, UserProfile, Optional[Realm]) -> Text
bucket_name = settings.S3_AUTH_UPLOADS_BUCKET
if target_realm is None:
target_realm = user_profile.realm
@ -434,7 +433,7 @@ def mkdirs(path):
os.makedirs(dirname)
def write_local_file(type, path, file_data):
# type: (Text, Text, binary_type) -> None
# type: (Text, Text, bytes) -> None
file_path = os.path.join(settings.LOCAL_UPLOADS_DIR, type, path)
mkdirs(file_path)
with open(file_path, 'wb') as f:
@ -451,7 +450,7 @@ def get_local_file_path(path_id):
class LocalUploadBackend(ZulipUploadBackend):
def upload_message_image(self, uploaded_file_name, uploaded_file_size,
content_type, file_data, user_profile, target_realm=None):
# type: (Text, int, Optional[Text], binary_type, UserProfile, Optional[Realm]) -> Text
# type: (Text, int, Optional[Text], bytes, UserProfile, Optional[Realm]) -> Text
# Split into 256 subdirectories to prevent directories from getting too big
path = "/".join([
str(user_profile.realm_id),
@ -574,7 +573,7 @@ def upload_emoji_image(emoji_file, emoji_file_name, user_profile):
def upload_message_image(uploaded_file_name, uploaded_file_size,
content_type, file_data, user_profile, target_realm=None):
# type: (Text, int, Optional[Text], binary_type, UserProfile, Optional[Realm]) -> Text
# type: (Text, int, Optional[Text], bytes, UserProfile, Optional[Realm]) -> Text
return upload_backend.upload_message_image(uploaded_file_name, uploaded_file_size,
content_type, file_data, user_profile, target_realm=target_realm)

View File

@ -1,7 +1,6 @@
# -*- coding: utf-8 -*-
from typing import Any, Callable, List, Optional, Sequence, TypeVar, Iterable, Set, Tuple, Text
from six import binary_type
import base64
import errno
import hashlib
@ -88,7 +87,7 @@ def run_in_batches(all_list, batch_size, callback, sleep_time = 0, logger = None
sleep(sleep_time)
def make_safe_digest(string, hash_func=hashlib.sha1):
# type: (Text, Callable[[binary_type], Any]) -> Text
# type: (Text, Callable[[bytes], Any]) -> Text
"""
return a hex digest of `string`.
"""

View File

@ -1,5 +1,4 @@
from six import binary_type
from typing import Any, AnyStr, Callable, Dict, Iterable, List, MutableMapping, Optional, Text
from django.conf import settings
@ -225,7 +224,7 @@ def write_log_line(log_data, path, method, remote_ip, email, client_name,
error_data = u''
elif isinstance(error_content_list[0], Text):
error_data = u''.join(error_content_list)
elif isinstance(error_content_list[0], binary_type):
elif isinstance(error_content_list[0], bytes):
error_data = repr(b''.join(error_content_list))
if len(error_data) > 100:
error_data = u"[content more than 100 characters]"

View File

@ -22,16 +22,14 @@ from boto.s3.connection import S3Connection
from requests import ConnectionError, Response
from typing import Dict, Text, Tuple, Optional, Union
from six import binary_type
def force_str(s: Union[Text, binary_type], encoding: Text='utf-8') -> str:
def force_str(s: Union[Text, bytes], encoding: Text='utf-8') -> str:
"""converts a string to a native string"""
if isinstance(s, str):
return s
elif isinstance(s, Text):
return s.encode(str(encoding))
elif isinstance(s, binary_type):
elif isinstance(s, bytes):
return s.decode(encoding)
else:
raise TypeError("force_str expects a string type")
@ -42,7 +40,7 @@ class Uploader:
self.path_template = "{realm_id}/emoji/{emoji_file_name}"
self.emoji_size = (64, 64)
def upload_files(self, response: Response, resized_image: binary_type,
def upload_files(self, response: Response, resized_image: bytes,
dst_path_id: Text) -> None:
raise NotImplementedError()
@ -51,7 +49,7 @@ class Uploader:
file_name = ''.join((emoji_name, image_ext))
return file_name, self.path_template.format(realm_id=realm_id, emoji_file_name=file_name)
def resize_emoji(self, image_data: binary_type) -> Optional[binary_type]:
def resize_emoji(self, image_data: bytes) -> Optional[bytes]:
im = Image.open(io.BytesIO(image_data))
format_ = im.format
if format_ == 'GIF' and im.is_animated:
@ -91,12 +89,12 @@ class LocalUploader(Uploader):
if not os.path.isdir(dirname):
os.makedirs(dirname)
def write_local_file(self, path: Text, file_data: binary_type) -> None:
def write_local_file(self, path: Text, file_data: bytes) -> None:
self.mkdirs(path)
with open(path, 'wb') as f:
f.write(file_data)
def upload_files(self, response: Response, resized_image: binary_type,
def upload_files(self, response: Response, resized_image: bytes,
dst_path_id: Text) -> None:
dst_file = os.path.join(settings.LOCAL_UPLOADS_DIR, 'avatars', dst_path_id)
if resized_image:
@ -113,13 +111,13 @@ class S3Uploader(Uploader):
bucket_name = settings.S3_AVATAR_BUCKET
self.bucket = conn.get_bucket(bucket_name, validate=False)
def upload_to_s3(self, path: Text, file_data: binary_type,
def upload_to_s3(self, path: Text, file_data: bytes,
headers: Optional[Dict[Text, Text]]) -> None:
key = Key(self.bucket)
key.key = path
key.set_contents_from_string(force_str(file_data), headers=headers)
def upload_files(self, response: Response, resized_image: binary_type,
def upload_files(self, response: Response, resized_image: bytes,
dst_path_id: Text) -> None:
headers = None # type: Optional[Dict[Text, Text]]
content_type = response.headers.get(str("Content-Type")) or guess_type(dst_path_id)[0]