From 0cc21101d708eda2d7574c4e6696554c1ef730f1 Mon Sep 17 00:00:00 2001 From: flan Date: Sat, 4 Nov 2017 02:06:42 +0100 Subject: [PATCH] 2to3 everything --- ankisyncctl.py | 2 +- ankisyncd/collection.py | 4 +-- ankisyncd/sync_app.py | 25 ++++++------- ankisyncd/thread.py | 14 ++++---- tests/collection_test_base.py | 4 +-- tests/helpers/__init__.py | 2 +- tests/helpers/file_utils.py | 2 +- tests/helpers/monkey_patches.py | 2 +- tests/helpers/server_utils.py | 4 +-- tests/test_web_media.py | 62 ++++++++++++++++----------------- 10 files changed, 61 insertions(+), 60 deletions(-) diff --git a/ankisyncctl.py b/ankisyncctl.py index 96e34ff..59a7775 100755 --- a/ankisyncctl.py +++ b/ankisyncctl.py @@ -1,6 +1,6 @@ #!/usr/bin/env python -from __future__ import print_function + import os import sys diff --git a/ankisyncd/collection.py b/ankisyncd/collection.py index f0ed914..b8cd0c2 100644 --- a/ankisyncd/collection.py +++ b/ankisyncd/collection.py @@ -50,7 +50,7 @@ class CollectionWrapper(object): dirname = os.path.dirname(self.path) try: os.makedirs(dirname) - except OSError, exc: + except OSError as exc: if exc.errno == errno.EEXIST: pass else: @@ -106,7 +106,7 @@ class CollectionManager(object): def shutdown(self): """Close all CollectionWrappers managed by this object.""" - for path, col in self.collections.items(): + for path, col in list(self.collections.items()): del self.collections[path] col.close() diff --git a/ankisyncd/sync_app.py b/ankisyncd/sync_app.py index 61ff8e5..855aa4d 100644 --- a/ankisyncd/sync_app.py +++ b/ankisyncd/sync_app.py @@ -14,7 +14,8 @@ # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . -from ConfigParser import SafeConfigParser + +from configparser import SafeConfigParser from webob.dec import wsgify from webob.exc import * @@ -41,9 +42,9 @@ from anki.consts import SYNC_ZIP_SIZE, SYNC_ZIP_COUNT from ankisyncd.users import SimpleUserManager, SqliteUserManager try: - from cStringIO import StringIO + from io import StringIO except ImportError: - from StringIO import StringIO + from io import StringIO def old_client(cv): if not cv: @@ -52,7 +53,7 @@ def old_client(cv): note = {"alpha": 0, "beta": 0} client, version, platform = cv.split(',') - for name in note.keys(): + for name in list(note.keys()): if name in version: vs = version.split(name) version = vs[0] @@ -208,8 +209,8 @@ class SyncMediaHandler(anki.sync.MediaSyncer): MediaManager.addFilesFromZip(). """ - if not isinstance(filename, unicode): - filename = unicode(filename, "utf8") + if not isinstance(filename, str): + filename = str(filename, "utf8") # Normalize name for platform. if anki.utils.isMac: # global @@ -514,12 +515,12 @@ class SyncApp(object): if url in SyncCollectionHandler.operations + SyncMediaHandler.operations: # 'meta' passes the SYNC_VER but it isn't used in the handler if url == 'meta': - if session.skey == None and req.POST.has_key('s'): + if session.skey == None and 's' in req.POST: session.skey = req.POST['s'] - if data.has_key('v'): + if 'v' in data: session.version = data['v'] del data['v'] - if data.has_key('cv'): + if 'cv' in data: session.client_version = data['cv'] del data['cv'] @@ -539,7 +540,7 @@ class SyncApp(object): result = self._execute_handler_method_in_thread(url, data, session) # If it's a complex data type, we convert it to JSON - if type(result) not in (str, unicode): + if type(result) not in (str, str): result = json.dumps(result) if url == 'finish': @@ -582,7 +583,7 @@ class SyncApp(object): result = self._execute_handler_method_in_thread(url, data, session) # If it's a complex data type, we convert it to JSON - if type(result) not in (str, unicode): + if type(result) not in (str, str): result = json.dumps(result) return result @@ -607,7 +608,7 @@ class SyncApp(object): col.save() return res - run_func.func_name = method_name # More useful debugging messages. + run_func.__name__ = method_name # More useful debugging messages. # Send the closure to the thread for execution. thread = session.get_thread() diff --git a/ankisyncd/thread.py b/ankisyncd/thread.py index cf891fb..edba54f 100644 --- a/ankisyncd/thread.py +++ b/ankisyncd/thread.py @@ -1,9 +1,9 @@ -from __future__ import absolute_import + from ankisyncd.collection import CollectionWrapper, CollectionManager from threading import Thread -from Queue import Queue +from queue import Queue import time, logging @@ -62,7 +62,7 @@ class ThreadingCollectionWrapper(object): func, args, kw, return_queue = self._queue.get(True) if hasattr(func, 'func_name'): - func_name = func.func_name + func_name = func.__name__ else: func_name = func.__class__.__name__ @@ -71,7 +71,7 @@ class ThreadingCollectionWrapper(object): try: ret = self.wrapper.execute(func, args, kw, return_queue) - except Exception, e: + except Exception as e: logging.error('CollectionThread[%s]: Unable to %s(*%s, **%s): %s', self.path, func_name, repr(args), repr(kw), e, exc_info=True) # we return the Exception which will be raise'd on the other end @@ -79,7 +79,7 @@ class ThreadingCollectionWrapper(object): if return_queue is not None: return_queue.put(ret) - except Exception, e: + except Exception as e: logging.error('CollectionThread[%s]: Thread crashed! Exception: %s', self.path, e, exc_info=True) finally: self.wrapper.close() @@ -153,7 +153,7 @@ class ThreadingCollectionManager(CollectionManager): small memory footprint!) """ while True: cur = time.time() - for path, thread in self.collections.items(): + for path, thread in list(self.collections.items()): if thread.running and thread.wrapper.opened() and thread.qempty() and cur - thread.last_timestamp >= self.monitor_inactivity: logging.info('Monitor is closing collection on inactive CollectionThread[%s]', thread.path) thread.close() @@ -163,7 +163,7 @@ class ThreadingCollectionManager(CollectionManager): # TODO: stop the monitor thread! # stop all the threads - for path, col in self.collections.items(): + for path, col in list(self.collections.items()): del self.collections[path] col.stop() diff --git a/tests/collection_test_base.py b/tests/collection_test_base.py index fe3b869..f5655f2 100644 --- a/tests/collection_test_base.py +++ b/tests/collection_test_base.py @@ -31,10 +31,10 @@ class CollectionTestBase(unittest.TestCase): model = self.collection.models.byName(data['model']) note = Note(self.collection, model) - for name, value in data['fields'].items(): + for name, value in list(data['fields'].items()): note[name] = value - if data.has_key('tags'): + if 'tags' in data: note.setTagsFromStr(data['tags']) self.collection.addNote(note) diff --git a/tests/helpers/__init__.py b/tests/helpers/__init__.py index e118e2d..0db0522 100644 --- a/tests/helpers/__init__.py +++ b/tests/helpers/__init__.py @@ -1 +1 @@ -import db_utils \ No newline at end of file +from . import db_utils \ No newline at end of file diff --git a/tests/helpers/file_utils.py b/tests/helpers/file_utils.py index f831114..0e6b71a 100644 --- a/tests/helpers/file_utils.py +++ b/tests/helpers/file_utils.py @@ -1,5 +1,5 @@ # -*- coding: utf-8 -*- -from cStringIO import StringIO +from io import StringIO import json import logging import logging.config diff --git a/tests/helpers/monkey_patches.py b/tests/helpers/monkey_patches.py index 63792b6..5e8b76c 100644 --- a/tests/helpers/monkey_patches.py +++ b/tests/helpers/monkey_patches.py @@ -70,7 +70,7 @@ def monkeypatch_db(): def patched___init__(self, path, text=None, timeout=0): # Code taken from Anki's DB.__init__() encpath = path - if isinstance(encpath, unicode): + if isinstance(encpath, str): encpath = path.encode("utf-8") # Allow more than one thread to use this connection. self._db = sqlite.connect(encpath, diff --git a/tests/helpers/server_utils.py b/tests/helpers/server_utils.py index 8bfdfb9..63da2cb 100644 --- a/tests/helpers/server_utils.py +++ b/tests/helpers/server_utils.py @@ -1,5 +1,5 @@ # -*- coding: utf-8 -*- -import ConfigParser +import configparser import logging import os import shutil @@ -22,7 +22,7 @@ def create_server_paths(): } def create_sync_app(server_paths, config_path): - config = ConfigParser.SafeConfigParser() + config = configparser.SafeConfigParser() config.read(config_path) # Use custom files and dirs in settings. diff --git a/tests/test_web_media.py b/tests/test_web_media.py index 3311040..0ee6481 100644 --- a/tests/test_web_media.py +++ b/tests/test_web_media.py @@ -100,7 +100,7 @@ class SyncAppFunctionalMediaTest(SyncAppFunctionalTestBase): 'media') # Create a test file. - temp_file_path = helpers.file_utils.create_named_file(u"foo.jpg", "hello") + temp_file_path = helpers.file_utils.create_named_file("foo.jpg", "hello") # Add the test file to the server's collection. helpers.server_utils.add_files_to_mediasyncer(server, @@ -114,8 +114,8 @@ class SyncAppFunctionalMediaTest(SyncAppFunctionalTestBase): # The test file should be present in the server's and in the client's # media directory. self.assertTrue( - filecmp.cmp(os.path.join(client.col.media.dir(), u"foo.jpg"), - os.path.join(server.col.media.dir(), u"foo.jpg"))) + filecmp.cmp(os.path.join(client.col.media.dir(), "foo.jpg"), + os.path.join(server.col.media.dir(), "foo.jpg"))) # Further syncing should do nothing. self.assertEqual(client.sync(), 'noChanges') @@ -132,7 +132,7 @@ class SyncAppFunctionalMediaTest(SyncAppFunctionalTestBase): 'media') # Create a test file. - temp_file_path = helpers.file_utils.create_named_file(u"foo.jpg", "hello") + temp_file_path = helpers.file_utils.create_named_file("foo.jpg", "hello") # Add the test file to the client's media collection. helpers.server_utils.add_files_to_mediasyncer(client, @@ -145,8 +145,8 @@ class SyncAppFunctionalMediaTest(SyncAppFunctionalTestBase): # The same file should be present in both the client's and the server's # media directory. - self.assertTrue(filecmp.cmp(join(client.col.media.dir(), u"foo.jpg"), - join(server.col.media.dir(), u"foo.jpg"))) + self.assertTrue(filecmp.cmp(join(client.col.media.dir(), "foo.jpg"), + join(server.col.media.dir(), "foo.jpg"))) # Further syncing should do nothing. self.assertEqual(client.sync(), 'noChanges') @@ -171,8 +171,8 @@ class SyncAppFunctionalMediaTest(SyncAppFunctionalTestBase): 'media') # Create two files and add one to the server and one to the client. - file_for_client = helpers.file_utils.create_named_file(u"foo.jpg", "hello") - file_for_server = helpers.file_utils.create_named_file(u"bar.jpg", "goodbye") + file_for_client = helpers.file_utils.create_named_file("foo.jpg", "hello") + file_for_server = helpers.file_utils.create_named_file("bar.jpg", "goodbye") helpers.server_utils.add_files_to_mediasyncer(client, [file_for_client], @@ -187,17 +187,17 @@ class SyncAppFunctionalMediaTest(SyncAppFunctionalTestBase): # Both files should be present in the client's and in the server's # media directories. - self.assertTrue(isfile(join(client.col.media.dir(), u"foo.jpg"))) - self.assertTrue(isfile(join(server.col.media.dir(), u"foo.jpg"))) + self.assertTrue(isfile(join(client.col.media.dir(), "foo.jpg"))) + self.assertTrue(isfile(join(server.col.media.dir(), "foo.jpg"))) self.assertTrue(filecmp.cmp( - join(client.col.media.dir(), u"foo.jpg"), - join(server.col.media.dir(), u"foo.jpg")) + join(client.col.media.dir(), "foo.jpg"), + join(server.col.media.dir(), "foo.jpg")) ) - self.assertTrue(isfile(join(client.col.media.dir(), u"bar.jpg"))) - self.assertTrue(isfile(join(server.col.media.dir(), u"bar.jpg"))) + self.assertTrue(isfile(join(client.col.media.dir(), "bar.jpg"))) + self.assertTrue(isfile(join(server.col.media.dir(), "bar.jpg"))) self.assertTrue(filecmp.cmp( - join(client.col.media.dir(), u"bar.jpg"), - join(server.col.media.dir(), u"bar.jpg")) + join(client.col.media.dir(), "bar.jpg"), + join(server.col.media.dir(), "bar.jpg")) ) # Further syncing should change nothing. @@ -219,8 +219,8 @@ class SyncAppFunctionalMediaTest(SyncAppFunctionalTestBase): # Create two files with identical names but different contents and # checksums. Add one to the server and one to the client. - file_for_client = helpers.file_utils.create_named_file(u"foo.jpg", "hello") - file_for_server = helpers.file_utils.create_named_file(u"foo.jpg", "goodbye") + file_for_client = helpers.file_utils.create_named_file("foo.jpg", "hello") + file_for_server = helpers.file_utils.create_named_file("foo.jpg", "goodbye") helpers.server_utils.add_files_to_mediasyncer(client, [file_for_client], @@ -235,17 +235,17 @@ class SyncAppFunctionalMediaTest(SyncAppFunctionalTestBase): # A version of the file should be present in both the client's and the # server's media directory. - self.assertTrue(isfile(join(client.col.media.dir(), u"foo.jpg"))) + self.assertTrue(isfile(join(client.col.media.dir(), "foo.jpg"))) self.assertEqual(os.listdir(client.col.media.dir()), ['foo.jpg']) - self.assertTrue(isfile(join(server.col.media.dir(), u"foo.jpg"))) + self.assertTrue(isfile(join(server.col.media.dir(), "foo.jpg"))) self.assertEqual(os.listdir(server.col.media.dir()), ['foo.jpg']) self.assertEqual(client.sync(), 'noChanges') # Both files should have the contents of the server's version. _checksum = client.col.media._checksum - self.assertEqual(_checksum(join(client.col.media.dir(), u"foo.jpg")), + self.assertEqual(_checksum(join(client.col.media.dir(), "foo.jpg")), _checksum(file_for_server)) - self.assertEqual(_checksum(join(server.col.media.dir(), u"foo.jpg")), + self.assertEqual(_checksum(join(server.col.media.dir(), "foo.jpg")), _checksum(file_for_server)) def test_sync_add_and_delete_on_client(self): @@ -264,7 +264,7 @@ class SyncAppFunctionalMediaTest(SyncAppFunctionalTestBase): 'media') # Create a test file. - temp_file_path = helpers.file_utils.create_named_file(u"foo.jpg", "hello") + temp_file_path = helpers.file_utils.create_named_file("foo.jpg", "hello") # Add the test file to client's media collection. helpers.server_utils.add_files_to_mediasyncer(client, @@ -277,26 +277,26 @@ class SyncAppFunctionalMediaTest(SyncAppFunctionalTestBase): # The same file should be present in both client's and the server's # media directory. - self.assertTrue(filecmp.cmp(join(client.col.media.dir(), u"foo.jpg"), - join(server.col.media.dir(), u"foo.jpg"))) + self.assertTrue(filecmp.cmp(join(client.col.media.dir(), "foo.jpg"), + join(server.col.media.dir(), "foo.jpg"))) # Syncing client again should do nothing. self.assertEqual(client.sync(), 'noChanges') # Remove files from client's media dir and write changes to its db. - os.remove(join(client.col.media.dir(), u"foo.jpg")) + os.remove(join(client.col.media.dir(), "foo.jpg")) # TODO: client.col.media.findChanges() doesn't work here - why? client.col.media._logChanges() - self.assertEqual(client.col.media.syncInfo(u"foo.jpg"), (None, 1)) - self.assertFalse(isfile(join(client.col.media.dir(), u"foo.jpg"))) + self.assertEqual(client.col.media.syncInfo("foo.jpg"), (None, 1)) + self.assertFalse(isfile(join(client.col.media.dir(), "foo.jpg"))) # Syncing client again should work. self.assertEqual(client.sync(), 'OK') # server should have picked up the removal from client. - self.assertEqual(server.col.media.syncInfo(u"foo.jpg"), (None, 0)) - self.assertFalse(isfile(join(server.col.media.dir(), u"foo.jpg"))) + self.assertEqual(server.col.media.syncInfo("foo.jpg"), (None, 0)) + self.assertFalse(isfile(join(server.col.media.dir(), "foo.jpg"))) # Syncing client again should do nothing. self.assertEqual(client.sync(), 'noChanges') @@ -312,7 +312,7 @@ class SyncAppFunctionalMediaTest(SyncAppFunctionalTestBase): # Add a test image file to the client's media collection but don't # update its media db since the desktop client updates that, using # findChanges(), only during syncs. - support_file = helpers.file_utils.get_asset_path(u'blue.jpg') + support_file = helpers.file_utils.get_asset_path('blue.jpg') self.assertTrue(os.path.isfile(support_file)) helpers.server_utils.add_files_to_mediasyncer(client, [support_file],