2to3 everything

This commit is contained in:
flan 2017-11-04 02:06:42 +01:00
parent c08fb74d91
commit 0cc21101d7
10 changed files with 61 additions and 60 deletions

View File

@ -1,6 +1,6 @@
#!/usr/bin/env python #!/usr/bin/env python
from __future__ import print_function
import os import os
import sys import sys

View File

@ -50,7 +50,7 @@ class CollectionWrapper(object):
dirname = os.path.dirname(self.path) dirname = os.path.dirname(self.path)
try: try:
os.makedirs(dirname) os.makedirs(dirname)
except OSError, exc: except OSError as exc:
if exc.errno == errno.EEXIST: if exc.errno == errno.EEXIST:
pass pass
else: else:
@ -106,7 +106,7 @@ class CollectionManager(object):
def shutdown(self): def shutdown(self):
"""Close all CollectionWrappers managed by this object.""" """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] del self.collections[path]
col.close() col.close()

View File

@ -14,7 +14,8 @@
# You should have received a copy of the GNU Affero General Public License # You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>. # along with this program. If not, see <http://www.gnu.org/licenses/>.
from ConfigParser import SafeConfigParser
from configparser import SafeConfigParser
from webob.dec import wsgify from webob.dec import wsgify
from webob.exc import * from webob.exc import *
@ -41,9 +42,9 @@ from anki.consts import SYNC_ZIP_SIZE, SYNC_ZIP_COUNT
from ankisyncd.users import SimpleUserManager, SqliteUserManager from ankisyncd.users import SimpleUserManager, SqliteUserManager
try: try:
from cStringIO import StringIO from io import StringIO
except ImportError: except ImportError:
from StringIO import StringIO from io import StringIO
def old_client(cv): def old_client(cv):
if not cv: if not cv:
@ -52,7 +53,7 @@ def old_client(cv):
note = {"alpha": 0, "beta": 0} note = {"alpha": 0, "beta": 0}
client, version, platform = cv.split(',') client, version, platform = cv.split(',')
for name in note.keys(): for name in list(note.keys()):
if name in version: if name in version:
vs = version.split(name) vs = version.split(name)
version = vs[0] version = vs[0]
@ -208,8 +209,8 @@ class SyncMediaHandler(anki.sync.MediaSyncer):
MediaManager.addFilesFromZip(). MediaManager.addFilesFromZip().
""" """
if not isinstance(filename, unicode): if not isinstance(filename, str):
filename = unicode(filename, "utf8") filename = str(filename, "utf8")
# Normalize name for platform. # Normalize name for platform.
if anki.utils.isMac: # global if anki.utils.isMac: # global
@ -514,12 +515,12 @@ class SyncApp(object):
if url in SyncCollectionHandler.operations + SyncMediaHandler.operations: if url in SyncCollectionHandler.operations + SyncMediaHandler.operations:
# 'meta' passes the SYNC_VER but it isn't used in the handler # 'meta' passes the SYNC_VER but it isn't used in the handler
if url == 'meta': 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'] session.skey = req.POST['s']
if data.has_key('v'): if 'v' in data:
session.version = data['v'] session.version = data['v']
del data['v'] del data['v']
if data.has_key('cv'): if 'cv' in data:
session.client_version = data['cv'] session.client_version = data['cv']
del data['cv'] del data['cv']
@ -539,7 +540,7 @@ class SyncApp(object):
result = self._execute_handler_method_in_thread(url, data, session) result = self._execute_handler_method_in_thread(url, data, session)
# If it's a complex data type, we convert it to JSON # 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) result = json.dumps(result)
if url == 'finish': if url == 'finish':
@ -582,7 +583,7 @@ class SyncApp(object):
result = self._execute_handler_method_in_thread(url, data, session) result = self._execute_handler_method_in_thread(url, data, session)
# If it's a complex data type, we convert it to JSON # 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) result = json.dumps(result)
return result return result
@ -607,7 +608,7 @@ class SyncApp(object):
col.save() col.save()
return res 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. # Send the closure to the thread for execution.
thread = session.get_thread() thread = session.get_thread()

View File

@ -1,9 +1,9 @@
from __future__ import absolute_import
from ankisyncd.collection import CollectionWrapper, CollectionManager from ankisyncd.collection import CollectionWrapper, CollectionManager
from threading import Thread from threading import Thread
from Queue import Queue from queue import Queue
import time, logging import time, logging
@ -62,7 +62,7 @@ class ThreadingCollectionWrapper(object):
func, args, kw, return_queue = self._queue.get(True) func, args, kw, return_queue = self._queue.get(True)
if hasattr(func, 'func_name'): if hasattr(func, 'func_name'):
func_name = func.func_name func_name = func.__name__
else: else:
func_name = func.__class__.__name__ func_name = func.__class__.__name__
@ -71,7 +71,7 @@ class ThreadingCollectionWrapper(object):
try: try:
ret = self.wrapper.execute(func, args, kw, return_queue) 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', logging.error('CollectionThread[%s]: Unable to %s(*%s, **%s): %s',
self.path, func_name, repr(args), repr(kw), e, exc_info=True) 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 # 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: if return_queue is not None:
return_queue.put(ret) 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) logging.error('CollectionThread[%s]: Thread crashed! Exception: %s', self.path, e, exc_info=True)
finally: finally:
self.wrapper.close() self.wrapper.close()
@ -153,7 +153,7 @@ class ThreadingCollectionManager(CollectionManager):
small memory footprint!) """ small memory footprint!) """
while True: while True:
cur = time.time() 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: 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) logging.info('Monitor is closing collection on inactive CollectionThread[%s]', thread.path)
thread.close() thread.close()
@ -163,7 +163,7 @@ class ThreadingCollectionManager(CollectionManager):
# TODO: stop the monitor thread! # TODO: stop the monitor thread!
# stop all the threads # stop all the threads
for path, col in self.collections.items(): for path, col in list(self.collections.items()):
del self.collections[path] del self.collections[path]
col.stop() col.stop()

View File

@ -31,10 +31,10 @@ class CollectionTestBase(unittest.TestCase):
model = self.collection.models.byName(data['model']) model = self.collection.models.byName(data['model'])
note = Note(self.collection, model) note = Note(self.collection, model)
for name, value in data['fields'].items(): for name, value in list(data['fields'].items()):
note[name] = value note[name] = value
if data.has_key('tags'): if 'tags' in data:
note.setTagsFromStr(data['tags']) note.setTagsFromStr(data['tags'])
self.collection.addNote(note) self.collection.addNote(note)

View File

@ -1 +1 @@
import db_utils from . import db_utils

View File

@ -1,5 +1,5 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
from cStringIO import StringIO from io import StringIO
import json import json
import logging import logging
import logging.config import logging.config

View File

@ -70,7 +70,7 @@ def monkeypatch_db():
def patched___init__(self, path, text=None, timeout=0): def patched___init__(self, path, text=None, timeout=0):
# Code taken from Anki's DB.__init__() # Code taken from Anki's DB.__init__()
encpath = path encpath = path
if isinstance(encpath, unicode): if isinstance(encpath, str):
encpath = path.encode("utf-8") encpath = path.encode("utf-8")
# Allow more than one thread to use this connection. # Allow more than one thread to use this connection.
self._db = sqlite.connect(encpath, self._db = sqlite.connect(encpath,

View File

@ -1,5 +1,5 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
import ConfigParser import configparser
import logging import logging
import os import os
import shutil import shutil
@ -22,7 +22,7 @@ def create_server_paths():
} }
def create_sync_app(server_paths, config_path): def create_sync_app(server_paths, config_path):
config = ConfigParser.SafeConfigParser() config = configparser.SafeConfigParser()
config.read(config_path) config.read(config_path)
# Use custom files and dirs in settings. # Use custom files and dirs in settings.

View File

@ -100,7 +100,7 @@ class SyncAppFunctionalMediaTest(SyncAppFunctionalTestBase):
'media') 'media')
# Create a test file. # 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. # Add the test file to the server's collection.
helpers.server_utils.add_files_to_mediasyncer(server, 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 # The test file should be present in the server's and in the client's
# media directory. # media directory.
self.assertTrue( self.assertTrue(
filecmp.cmp(os.path.join(client.col.media.dir(), u"foo.jpg"), filecmp.cmp(os.path.join(client.col.media.dir(), "foo.jpg"),
os.path.join(server.col.media.dir(), u"foo.jpg"))) os.path.join(server.col.media.dir(), "foo.jpg")))
# Further syncing should do nothing. # Further syncing should do nothing.
self.assertEqual(client.sync(), 'noChanges') self.assertEqual(client.sync(), 'noChanges')
@ -132,7 +132,7 @@ class SyncAppFunctionalMediaTest(SyncAppFunctionalTestBase):
'media') 'media')
# Create a test file. # 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. # Add the test file to the client's media collection.
helpers.server_utils.add_files_to_mediasyncer(client, 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 # The same file should be present in both the client's and the server's
# media directory. # media directory.
self.assertTrue(filecmp.cmp(join(client.col.media.dir(), u"foo.jpg"), self.assertTrue(filecmp.cmp(join(client.col.media.dir(), "foo.jpg"),
join(server.col.media.dir(), u"foo.jpg"))) join(server.col.media.dir(), "foo.jpg")))
# Further syncing should do nothing. # Further syncing should do nothing.
self.assertEqual(client.sync(), 'noChanges') self.assertEqual(client.sync(), 'noChanges')
@ -171,8 +171,8 @@ class SyncAppFunctionalMediaTest(SyncAppFunctionalTestBase):
'media') 'media')
# Create two files and add one to the server and one to the client. # 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_client = helpers.file_utils.create_named_file("foo.jpg", "hello")
file_for_server = helpers.file_utils.create_named_file(u"bar.jpg", "goodbye") file_for_server = helpers.file_utils.create_named_file("bar.jpg", "goodbye")
helpers.server_utils.add_files_to_mediasyncer(client, helpers.server_utils.add_files_to_mediasyncer(client,
[file_for_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 # Both files should be present in the client's and in the server's
# media directories. # media directories.
self.assertTrue(isfile(join(client.col.media.dir(), u"foo.jpg"))) self.assertTrue(isfile(join(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.assertTrue(filecmp.cmp( self.assertTrue(filecmp.cmp(
join(client.col.media.dir(), u"foo.jpg"), join(client.col.media.dir(), "foo.jpg"),
join(server.col.media.dir(), u"foo.jpg")) join(server.col.media.dir(), "foo.jpg"))
) )
self.assertTrue(isfile(join(client.col.media.dir(), u"bar.jpg"))) self.assertTrue(isfile(join(client.col.media.dir(), "bar.jpg")))
self.assertTrue(isfile(join(server.col.media.dir(), u"bar.jpg"))) self.assertTrue(isfile(join(server.col.media.dir(), "bar.jpg")))
self.assertTrue(filecmp.cmp( self.assertTrue(filecmp.cmp(
join(client.col.media.dir(), u"bar.jpg"), join(client.col.media.dir(), "bar.jpg"),
join(server.col.media.dir(), u"bar.jpg")) join(server.col.media.dir(), "bar.jpg"))
) )
# Further syncing should change nothing. # Further syncing should change nothing.
@ -219,8 +219,8 @@ class SyncAppFunctionalMediaTest(SyncAppFunctionalTestBase):
# Create two files with identical names but different contents and # Create two files with identical names but different contents and
# checksums. Add one to the server and one to the client. # 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_client = helpers.file_utils.create_named_file("foo.jpg", "hello")
file_for_server = helpers.file_utils.create_named_file(u"foo.jpg", "goodbye") file_for_server = helpers.file_utils.create_named_file("foo.jpg", "goodbye")
helpers.server_utils.add_files_to_mediasyncer(client, helpers.server_utils.add_files_to_mediasyncer(client,
[file_for_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 # A version of the file should be present in both the client's and the
# server's media directory. # 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.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(os.listdir(server.col.media.dir()), ['foo.jpg'])
self.assertEqual(client.sync(), 'noChanges') self.assertEqual(client.sync(), 'noChanges')
# Both files should have the contents of the server's version. # Both files should have the contents of the server's version.
_checksum = client.col.media._checksum _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)) _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)) _checksum(file_for_server))
def test_sync_add_and_delete_on_client(self): def test_sync_add_and_delete_on_client(self):
@ -264,7 +264,7 @@ class SyncAppFunctionalMediaTest(SyncAppFunctionalTestBase):
'media') 'media')
# Create a test file. # 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. # Add the test file to client's media collection.
helpers.server_utils.add_files_to_mediasyncer(client, 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 # The same file should be present in both client's and the server's
# media directory. # media directory.
self.assertTrue(filecmp.cmp(join(client.col.media.dir(), u"foo.jpg"), self.assertTrue(filecmp.cmp(join(client.col.media.dir(), "foo.jpg"),
join(server.col.media.dir(), u"foo.jpg"))) join(server.col.media.dir(), "foo.jpg")))
# Syncing client again should do nothing. # Syncing client again should do nothing.
self.assertEqual(client.sync(), 'noChanges') self.assertEqual(client.sync(), 'noChanges')
# Remove files from client's media dir and write changes to its db. # 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? # TODO: client.col.media.findChanges() doesn't work here - why?
client.col.media._logChanges() client.col.media._logChanges()
self.assertEqual(client.col.media.syncInfo(u"foo.jpg"), (None, 1)) self.assertEqual(client.col.media.syncInfo("foo.jpg"), (None, 1))
self.assertFalse(isfile(join(client.col.media.dir(), u"foo.jpg"))) self.assertFalse(isfile(join(client.col.media.dir(), "foo.jpg")))
# Syncing client again should work. # Syncing client again should work.
self.assertEqual(client.sync(), 'OK') self.assertEqual(client.sync(), 'OK')
# server should have picked up the removal from client. # server should have picked up the removal from client.
self.assertEqual(server.col.media.syncInfo(u"foo.jpg"), (None, 0)) self.assertEqual(server.col.media.syncInfo("foo.jpg"), (None, 0))
self.assertFalse(isfile(join(server.col.media.dir(), u"foo.jpg"))) self.assertFalse(isfile(join(server.col.media.dir(), "foo.jpg")))
# Syncing client again should do nothing. # Syncing client again should do nothing.
self.assertEqual(client.sync(), 'noChanges') 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 # 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 # update its media db since the desktop client updates that, using
# findChanges(), only during syncs. # 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)) self.assertTrue(os.path.isfile(support_file))
helpers.server_utils.add_files_to_mediasyncer(client, helpers.server_utils.add_files_to_mediasyncer(client,
[support_file], [support_file],