14
0
Files
python-pydrive2/modernize.patch

354 lines
12 KiB
Diff
Raw Normal View History

From 2e43e4561d965ce78dc158a02fbdb75ba6c38105 Mon Sep 17 00:00:00 2001
From: Ruslan Kuprieiev <kupruser@gmail.com>
Date: Wed, 27 Apr 2022 15:31:53 +0300
Subject: [PATCH] pydrive2: pyupgrade --py37-plus
---
pydrive2/apiattr.py | 15 ++++++---------
pydrive2/auth.py | 14 ++++++--------
pydrive2/drive.py | 2 +-
pydrive2/files.py | 10 ++++------
pydrive2/settings.py | 8 +++-----
pydrive2/test/test_drive.py | 1 -
pydrive2/test/test_file.py | 18 ++++++++----------
pydrive2/test/test_filelist.py | 7 +++----
setup.py | 4 +---
9 files changed, 32 insertions(+), 47 deletions(-)
diff --git a/pydrive2/apiattr.py b/pydrive2/apiattr.py
index 46845b1..6b66b42 100644
--- a/pydrive2/apiattr.py
+++ b/pydrive2/apiattr.py
@@ -1,7 +1,4 @@
-from six import Iterator, iteritems
-
-
-class ApiAttribute(object):
+class ApiAttribute:
"""A data descriptor that sets and returns values."""
def __init__(self, name):
@@ -32,7 +29,7 @@ def __del__(self, obj=None):
del obj.dirty[self.name]
-class ApiAttributeMixin(object):
+class ApiAttributeMixin:
"""Mixin to initialize required global variables to use ApiAttribute."""
def __init__(self):
@@ -54,7 +51,7 @@ class ApiResource(dict):
def __init__(self, *args, **kwargs):
"""Create an instance of ApiResource."""
- super(ApiResource, self).__init__()
+ super().__init__()
self.update(*args, **kwargs)
self.metadata = dict(self)
@@ -79,11 +76,11 @@ def __setitem__(self, key, val):
def __repr__(self):
"""Overwritten method of dictionary."""
dict_representation = dict.__repr__(self)
- return "%s(%s)" % (type(self).__name__, dict_representation)
+ return f"{type(self).__name__}({dict_representation})"
def update(self, *args, **kwargs):
"""Overwritten method of dictionary."""
- for k, v in iteritems(dict(*args, **kwargs)):
+ for k, v in dict(*args, **kwargs).items():
self[k] = v
def UpdateMetadata(self, metadata=None):
@@ -106,7 +103,7 @@ def GetChanges(self):
return dirty
-class ApiResourceList(ApiAttributeMixin, ApiResource, Iterator):
+class ApiResourceList(ApiAttributeMixin, ApiResource):
"""Abstract class of all api list resources.
Inherits ApiResource and builds iterator to list any API resource.
diff --git a/pydrive2/auth.py b/pydrive2/auth.py
index 644befb..a1b8b2f 100644
--- a/pydrive2/auth.py
+++ b/pydrive2/auth.py
@@ -1,8 +1,6 @@
-import socket
import webbrowser
import httplib2
import oauth2client.clientsecrets as clientsecrets
-from six.moves import input
import threading
from googleapiclient.discovery import build
@@ -141,7 +139,7 @@ def _decorated(self, *args, **kwargs):
return _decorated
-class GoogleAuth(ApiAttributeMixin, object):
+class GoogleAuth(ApiAttributeMixin):
"""Wrapper class for oauth2client library in google-api-python-client.
Loads all settings and credentials from one 'settings.yaml' file
@@ -241,13 +239,13 @@ def LocalWebserverAuth(
httpd = ClientRedirectServer(
(host_name, port), ClientRedirectHandler
)
- except socket.error:
+ except OSError:
pass
else:
success = True
break
if success:
- oauth_callback = "http://%s:%s/" % (host_name, port_number)
+ oauth_callback = f"http://{host_name}:{port_number}/"
else:
print(
"Failed to start a local web server. Please check your firewall"
@@ -381,7 +379,7 @@ def LoadCredentialsFile(self, credentials_file=None):
try:
self.credentials = self._default_storage.get()
- except IOError:
+ except OSError:
raise InvalidCredentialsError(
"Credentials file cannot be symbolic link"
)
@@ -431,7 +429,7 @@ def SaveCredentialsFile(self, credentials_file=None):
try:
storage.put(self.credentials)
- except IOError:
+ except OSError:
raise InvalidCredentialsError(
"Credentials file cannot be symbolic link"
)
@@ -538,7 +536,7 @@ def LoadServiceConfigSettings(self):
]
except KeyError:
err = "Insufficient service config in settings"
- err += "\n\nMissing: {} key.".format(config)
+ err += f"\n\nMissing: {config} key."
raise InvalidConfigError(err)
def LoadClientConfigSettings(self):
diff --git a/pydrive2/drive.py b/pydrive2/drive.py
index d8b26aa..8189ab1 100644
--- a/pydrive2/drive.py
+++ b/pydrive2/drive.py
@@ -4,7 +4,7 @@
from .auth import LoadAuth
-class GoogleDrive(ApiAttributeMixin, object):
+class GoogleDrive(ApiAttributeMixin):
"""Main Google Drive class."""
def __init__(self, auth=None):
diff --git a/pydrive2/files.py b/pydrive2/files.py
index 096f9bb..4716c7d 100644
--- a/pydrive2/files.py
+++ b/pydrive2/files.py
@@ -17,9 +17,7 @@
BLOCK_SIZE = 1024
# Usage: MIME_TYPE_TO_BOM['<Google Drive mime type>']['<download mimetype>'].
MIME_TYPE_TO_BOM = {
- "application/vnd.google-apps.document": {
- "text/plain": "\ufeff".encode("utf8")
- }
+ "application/vnd.google-apps.document": {"text/plain": "\ufeff".encode()}
}
@@ -68,7 +66,7 @@ class GoogleDriveFileList(ApiResourceList):
def __init__(self, auth=None, param=None):
"""Create an instance of GoogleDriveFileList."""
- super(GoogleDriveFileList, self).__init__(auth=auth, metadata=param)
+ super().__init__(auth=auth, metadata=param)
@LoadAuth
def _GetList(self):
@@ -98,7 +96,7 @@ def _GetList(self):
return result
-class IoBuffer(object):
+class IoBuffer:
"""Lightweight retention of one chunk."""
def __init__(self, encoding):
@@ -116,7 +114,7 @@ def read(self):
)
-class MediaIoReadable(object):
+class MediaIoReadable:
def __init__(
self,
request,
diff --git a/pydrive2/settings.py b/pydrive2/settings.py
index 8e79881..f5e84ab 100644
--- a/pydrive2/settings.py
+++ b/pydrive2/settings.py
@@ -103,9 +103,9 @@ def LoadSettingsFile(filename=SETTINGS_FILE):
:raises: SettingsError
"""
try:
- with open(filename, "r") as stream:
+ with open(filename) as stream:
data = load(stream, Loader=Loader)
- except (YAMLError, IOError) as e:
+ except (YAMLError, OSError) as e:
raise SettingsError(e)
return data
@@ -158,9 +158,7 @@ def _ValidateSettingsElement(data, struct, key):
data[key] = default
# If data exists, Check type of the data
elif type(value) is not data_type:
- raise InvalidConfigError(
- "Setting %s should be type %s" % (key, data_type)
- )
+ raise InvalidConfigError(f"Setting {key} should be type {data_type}")
# If type of this data is dict, check if structure of the data is valid.
if data_type is dict:
_ValidateSettingsStruct(data[key], struct[key]["struct"])
diff --git a/pydrive2/test/test_drive.py b/pydrive2/test/test_drive.py
index 23b58a6..81498a8 100644
--- a/pydrive2/test/test_drive.py
+++ b/pydrive2/test/test_drive.py
@@ -1,4 +1,3 @@
-# -*- coding: utf-8 -*-
import unittest
from pydrive2.auth import GoogleAuth
diff --git a/pydrive2/test/test_file.py b/pydrive2/test/test_file.py
index e83898c..bf3021a 100644
--- a/pydrive2/test/test_file.py
+++ b/pydrive2/test/test_file.py
@@ -1,4 +1,3 @@
-# -*- coding: utf-8 -*-
import filecmp
import os
import unittest
@@ -8,7 +7,6 @@
from tempfile import mkdtemp
from time import time
-from six.moves import range
import timeout_decorator
from concurrent.futures import ThreadPoolExecutor, as_completed
from googleapiclient import errors
@@ -699,22 +697,22 @@ def test_Gfile_Conversion_Add_Remove_BOM(self):
def test_InsertPrefix(self):
# Create BytesIO.
- file_obj = BytesIO("abc".encode("utf8"))
+ file_obj = BytesIO(b"abc")
original_length = len(file_obj.getvalue())
- char_to_insert = "\ufeff".encode("utf8")
+ char_to_insert = "\ufeff".encode()
# Insert the prefix.
GoogleDriveFile._InsertPrefix(file_obj, char_to_insert)
modified_length = len(file_obj.getvalue())
self.assertGreater(modified_length, original_length)
- self.assertEqual(file_obj.getvalue(), "\ufeffabc".encode("utf8"))
+ self.assertEqual(file_obj.getvalue(), "\ufeffabc".encode())
def test_InsertPrefixLarge(self):
# Create BytesIO.
test_content = "abc" * 800
file_obj = BytesIO(test_content.encode("utf-8"))
original_length = len(file_obj.getvalue())
- char_to_insert = "\ufeff".encode("utf8")
+ char_to_insert = "\ufeff".encode()
# Insert the prefix.
GoogleDriveFile._InsertPrefix(file_obj, char_to_insert)
@@ -725,22 +723,22 @@ def test_InsertPrefixLarge(self):
def test_RemovePrefix(self):
# Create BytesIO.
- file_obj = BytesIO("\ufeffabc".encode("utf8"))
+ file_obj = BytesIO("\ufeffabc".encode())
original_length = len(file_obj.getvalue())
- char_to_remove = "\ufeff".encode("utf8")
+ char_to_remove = "\ufeff".encode()
# Insert the prefix.
GoogleDriveFile._RemovePrefix(file_obj, char_to_remove)
modified_length = len(file_obj.getvalue())
self.assertLess(modified_length, original_length)
- self.assertEqual(file_obj.getvalue(), "abc".encode("utf8"))
+ self.assertEqual(file_obj.getvalue(), b"abc")
def test_RemovePrefixLarge(self):
# Create BytesIO.
test_content = "\ufeff" + "abc" * 800
file_obj = BytesIO(test_content.encode("utf8"))
original_length = len(file_obj.getvalue())
- char_to_remove = "\ufeff".encode("utf8")
+ char_to_remove = "\ufeff".encode()
# Insert the prefix.
GoogleDriveFile._RemovePrefix(file_obj, char_to_remove)
diff --git a/pydrive2/test/test_filelist.py b/pydrive2/test/test_filelist.py
index 644c13f..15918cd 100644
--- a/pydrive2/test/test_filelist.py
+++ b/pydrive2/test/test_filelist.py
@@ -1,4 +1,3 @@
-# -*- coding: utf-8 -*-
import os
import unittest
@@ -28,7 +27,7 @@ def setup_class(cls):
def test_01_Files_List_GetList(self):
drive = GoogleDrive(self.ga)
- query = "title = '{}' and trashed = false".format(self.title)
+ query = f"title = '{self.title}' and trashed = false"
for file1 in pydrive_list_item(drive, query):
found = False
for file2 in pydrive_list_item(drive, query):
@@ -38,7 +37,7 @@ def test_01_Files_List_GetList(self):
def test_02_Files_List_ForLoop(self):
drive = GoogleDrive(self.ga)
- query = "title = '{}' and trashed = false".format(self.title)
+ query = f"title = '{self.title}' and trashed = false"
files = []
for x in pydrive_list_item(
drive, query, 2
@@ -84,7 +83,7 @@ def test_File_List_Folders(self):
)
pydrive_retry(folder1.Upload)
self.file_list.append(folder1)
- query = "title = '{}' and trashed = false".format(self.title)
+ query = f"title = '{self.title}' and trashed = false"
count = 0
for file1 in pydrive_list_item(drive, query):
self.assertFileInFileList(file1)
diff --git a/setup.py b/setup.py
index 32c9364..06982d0 100644
--- a/setup.py
+++ b/setup.py
@@ -1,4 +1,3 @@
-import sys
from setuptools import setup
# Extra dependecies to run tests
@@ -11,8 +10,7 @@
"pytest-mock",
]
-if sys.version_info >= (3, 6):
- tests_requirements.append("black==22.3.0")
+tests_requirements.append("black==22.3.0")
setup(
name="PyDrive2",