From 38ec259b6e436d7868af8e99d55305340d5375ce Mon Sep 17 00:00:00 2001 From: Vincent Driessen Date: Mon, 5 May 2014 10:49:29 +0200 Subject: [PATCH] Enable the most modern Python syntax. --- examples/fib.py | 5 +++++ examples/run_example.py | 8 +++++++- examples/run_worker.py | 5 ++++- rq/__init__.py | 17 +++++++++-------- rq/compat/__init__.py | 6 +++++- rq/compat/connections.py | 7 ++++++- rq/connections.py | 8 +++++++- rq/contrib/legacy.py | 5 +++++ rq/contrib/sentry.py | 5 +++++ rq/decorators.py | 10 ++++++++-- rq/dummy.py | 6 +++++- rq/exceptions.py | 5 +++++ rq/job.py | 11 ++++++++++- rq/logutils.py | 4 ++++ rq/queue.py | 4 ++++ rq/scripts/__init__.py | 5 ++++- rq/scripts/rqgenload.py | 8 ++++++-- rq/scripts/rqinfo.py | 4 +++- rq/scripts/rqworker.py | 11 ++++++++--- rq/timeouts.py | 4 ++++ rq/utils.py | 3 +++ rq/version.py | 3 +++ rq/worker.py | 35 +++++++++++++++++++++-------------- tests/__init__.py | 11 ++++++++--- tests/dummy_settings.py | 4 ++++ tests/fixtures.py | 8 ++++++-- tests/helpers.py | 4 ++++ tests/test_connection.py | 10 +++++++--- tests/test_decorator.py | 9 +++++++-- tests/test_job.py | 28 +++++++++++++++++++--------- tests/test_queue.py | 14 ++++++++++---- tests/test_scripts.py | 7 ++++++- tests/test_sentry.py | 9 +++++++-- tests/test_worker.py | 16 +++++++++++----- 34 files changed, 230 insertions(+), 69 deletions(-) diff --git a/examples/fib.py b/examples/fib.py index 2130b3c..91606cb 100644 --- a/examples/fib.py +++ b/examples/fib.py @@ -1,3 +1,8 @@ +# -*- coding: utf-8 -*- +from __future__ import (absolute_import, division, print_function, + unicode_literals) + + def slow_fib(n): if n <= 1: return 1 diff --git a/examples/run_example.py b/examples/run_example.py index cbcc6e9..d19a6dc 100644 --- a/examples/run_example.py +++ b/examples/run_example.py @@ -1,6 +1,12 @@ +# -*- coding: utf-8 -*- +from __future__ import (absolute_import, division, print_function, + unicode_literals) + import os import time -from rq import Queue, Connection + +from rq import Connection, Queue + from fib import slow_fib diff --git a/examples/run_worker.py b/examples/run_worker.py index 7c8adae..4feb217 100644 --- a/examples/run_worker.py +++ b/examples/run_worker.py @@ -1,5 +1,8 @@ -from rq import Queue, Worker, Connection +# -*- coding: utf-8 -*- +from __future__ import (absolute_import, division, print_function, + unicode_literals) +from rq import Connection, Queue, Worker if __name__ == '__main__': # Tell rq what Redis connection to use diff --git a/rq/__init__.py b/rq/__init__.py index 94e1dd1..95050f3 100644 --- a/rq/__init__.py +++ b/rq/__init__.py @@ -1,12 +1,13 @@ -from .connections import get_current_connection -from .connections import use_connection, push_connection, pop_connection -from .connections import Connection -from .queue import Queue, get_failed_queue -from .job import cancel_job, requeue_job -from .job import get_current_job -from .worker import Worker -from .version import VERSION +# -*- coding: utf-8 -*- +from __future__ import (absolute_import, division, print_function, + unicode_literals) +from .connections import (Connection, get_current_connection, pop_connection, + push_connection, use_connection) +from .job import cancel_job, get_current_job, requeue_job +from .queue import get_failed_queue, Queue +from .version import VERSION +from .worker import Worker __all__ = [ 'use_connection', 'get_current_connection', diff --git a/rq/compat/__init__.py b/rq/compat/__init__.py index a7817b4..1571dbb 100644 --- a/rq/compat/__init__.py +++ b/rq/compat/__init__.py @@ -1,3 +1,7 @@ +# -*- coding: utf-8 -*- +from __future__ import (absolute_import, division, print_function, + unicode_literals) + import sys @@ -35,7 +39,7 @@ else: root = max(roots) # prefer __lt__ to __le__ to __gt__ to __ge__ for opname, opfunc in convert[root]: if opname not in roots: - opfunc.__name__ = opname + opfunc.__name__ = str(opname) opfunc.__doc__ = getattr(int, opname).__doc__ setattr(cls, opname, opfunc) return cls diff --git a/rq/compat/connections.py b/rq/compat/connections.py index 0374b5b..ac22af3 100644 --- a/rq/compat/connections.py +++ b/rq/compat/connections.py @@ -1,6 +1,11 @@ -from redis import Redis, StrictRedis +# -*- coding: utf-8 -*- +from __future__ import (absolute_import, division, print_function, + unicode_literals) + from functools import partial +from redis import Redis, StrictRedis + def fix_return_type(func): # deliberately no functools.wraps() call here, since the function being diff --git a/rq/connections.py b/rq/connections.py index ee07070..ae4d036 100644 --- a/rq/connections.py +++ b/rq/connections.py @@ -1,7 +1,13 @@ +# -*- coding: utf-8 -*- +from __future__ import (absolute_import, division, print_function, + unicode_literals) + from contextlib import contextmanager + from redis import StrictRedis -from .local import LocalStack, release_local + from .compat.connections import patch_connection +from .local import LocalStack, release_local class NoRedisConnectionException(Exception): diff --git a/rq/contrib/legacy.py b/rq/contrib/legacy.py index 2a20b9f..447c002 100644 --- a/rq/contrib/legacy.py +++ b/rq/contrib/legacy.py @@ -1,3 +1,8 @@ +# -*- coding: utf-8 -*- +from __future__ import (absolute_import, division, print_function, + unicode_literals) + + import logging from rq import get_current_connection from rq import Worker diff --git a/rq/contrib/sentry.py b/rq/contrib/sentry.py index 4b776d1..84b9ef9 100644 --- a/rq/contrib/sentry.py +++ b/rq/contrib/sentry.py @@ -1,3 +1,8 @@ +# -*- coding: utf-8 -*- +from __future__ import (absolute_import, division, print_function, + unicode_literals) + + def register_sentry(client, worker): """Given a Raven client and an RQ worker, registers exception handlers with the worker so exceptions are logged to Sentry. diff --git a/rq/decorators.py b/rq/decorators.py index 90aebf7..b3dba8d 100644 --- a/rq/decorators.py +++ b/rq/decorators.py @@ -1,8 +1,14 @@ +# -*- coding: utf-8 -*- +from __future__ import (absolute_import, division, print_function, + unicode_literals) + from functools import wraps -from .queue import Queue + +from rq.compat import string_types + from .connections import resolve_connection +from .queue import Queue from .worker import DEFAULT_RESULT_TTL -from rq.compat import string_types class job(object): diff --git a/rq/dummy.py b/rq/dummy.py index ee9022b..cdd1afc 100644 --- a/rq/dummy.py +++ b/rq/dummy.py @@ -1,8 +1,12 @@ +# -*- coding: utf-8 -*- """ Some dummy tasks that are well-suited for generating load for testing purposes. """ -import time +from __future__ import (absolute_import, division, print_function, + unicode_literals) + import random +import time def do_nothing(): diff --git a/rq/exceptions.py b/rq/exceptions.py index 25e4f0e..94b22bf 100644 --- a/rq/exceptions.py +++ b/rq/exceptions.py @@ -1,3 +1,8 @@ +# -*- coding: utf-8 -*- +from __future__ import (absolute_import, division, print_function, + unicode_literals) + + class NoSuchJobError(Exception): pass diff --git a/rq/job.py b/rq/job.py index b39abaf..045b57a 100644 --- a/rq/job.py +++ b/rq/job.py @@ -1,3 +1,7 @@ +# -*- coding: utf-8 -*- +from __future__ import (absolute_import, division, print_function, + unicode_literals) + import inspect from uuid import uuid4 try: @@ -13,7 +17,12 @@ from rq.compat import text_type, decode_redis_hash, as_text, string_types def enum(name, *sequential, **named): values = dict(zip(sequential, range(len(sequential))), **named) - return type(name, (), values) + + # NOTE: Yes, we *really* want to cast using str() here. + # On Python 2 type() requires a byte string (which is str() on Python 2). + # On Python 3 it does not matter, so we'll use str(), which acts as + # a no-op. + return type(str(name), (), values) Status = enum('Status', QUEUED='queued', FINISHED='finished', FAILED='failed', diff --git a/rq/logutils.py b/rq/logutils.py index 25e9774..40c1db2 100644 --- a/rq/logutils.py +++ b/rq/logutils.py @@ -1,3 +1,7 @@ +# -*- coding: utf-8 -*- +from __future__ import (absolute_import, division, print_function, + unicode_literals) + import logging # Make sure that dictConfig is available diff --git a/rq/queue.py b/rq/queue.py index 317d0f2..3b7bec3 100644 --- a/rq/queue.py +++ b/rq/queue.py @@ -1,3 +1,7 @@ +# -*- coding: utf-8 -*- +from __future__ import (absolute_import, division, print_function, + unicode_literals) + import uuid from .connections import resolve_connection diff --git a/rq/scripts/__init__.py b/rq/scripts/__init__.py index cab1f97..a797286 100644 --- a/rq/scripts/__init__.py +++ b/rq/scripts/__init__.py @@ -1,10 +1,13 @@ +# -*- coding: utf-8 -*- +from __future__ import (absolute_import, division, print_function, + unicode_literals) + import importlib import os from functools import partial from warnings import warn import redis - from rq import use_connection from rq.utils import first diff --git a/rq/scripts/rqgenload.py b/rq/scripts/rqgenload.py index b643c49..97bb5de 100755 --- a/rq/scripts/rqgenload.py +++ b/rq/scripts/rqgenload.py @@ -1,7 +1,11 @@ #!/usr/bin/env python +# -*- coding: utf-8 -*- +from __future__ import (absolute_import, division, print_function, + unicode_literals) + import optparse -from rq import use_connection, Queue -from rq import dummy + +from rq import dummy, Queue, use_connection def parse_args(): diff --git a/rq/scripts/rqinfo.py b/rq/scripts/rqinfo.py index f9c179d..2f6af7a 100755 --- a/rq/scripts/rqinfo.py +++ b/rq/scripts/rqinfo.py @@ -1,12 +1,14 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- +from __future__ import (absolute_import, division, print_function, + unicode_literals) + import argparse import os import sys import time from redis.exceptions import ConnectionError - from rq import get_failed_queue, Queue, Worker from rq.scripts import (add_standard_arguments, read_config_file, setup_default_arguments, setup_redis) diff --git a/rq/scripts/rqworker.py b/rq/scripts/rqworker.py index c7e3fce..84f68f7 100644 --- a/rq/scripts/rqworker.py +++ b/rq/scripts/rqworker.py @@ -1,15 +1,20 @@ #!/usr/bin/env python +# -*- coding: utf-8 -*- +from __future__ import (absolute_import, division, print_function, + unicode_literals) + import argparse import logging import logging.config import os import sys -from rq import Queue -from rq.logutils import setup_loghandlers from redis.exceptions import ConnectionError +from rq import Queue from rq.contrib.legacy import cleanup_ghosts -from rq.scripts import add_standard_arguments, read_config_file, setup_default_arguments, setup_redis +from rq.logutils import setup_loghandlers +from rq.scripts import (add_standard_arguments, read_config_file, + setup_default_arguments, setup_redis) from rq.utils import import_attribute logger = logging.getLogger(__name__) diff --git a/rq/timeouts.py b/rq/timeouts.py index a96d891..ae0fd48 100644 --- a/rq/timeouts.py +++ b/rq/timeouts.py @@ -1,3 +1,7 @@ + # -*- coding: utf-8 -*- +from __future__ import (absolute_import, division, print_function, + unicode_literals) + import signal diff --git a/rq/utils.py b/rq/utils.py index a4260d9..e1786cb 100644 --- a/rq/utils.py +++ b/rq/utils.py @@ -5,6 +5,9 @@ Miscellaneous helper functions. The formatter for ANSI colored console output is heavily based on Pygments terminal colorizing code, originally by Georg Brandl. """ +from __future__ import (absolute_import, division, print_function, + unicode_literals) + import importlib import datetime import logging diff --git a/rq/version.py b/rq/version.py index 1376913..1497719 100644 --- a/rq/version.py +++ b/rq/version.py @@ -1 +1,4 @@ +# -*- coding: utf-8 -*- +from __future__ import (absolute_import, division, print_function, + unicode_literals) VERSION = '0.4.2' diff --git a/rq/worker.py b/rq/worker.py index 11df407..7bc7113 100644 --- a/rq/worker.py +++ b/rq/worker.py @@ -1,26 +1,33 @@ -import sys -import os +# -*- coding: utf-8 -*- +from __future__ import (absolute_import, division, print_function, + unicode_literals) + import errno +import logging +import os import random -import time -try: - from procname import setprocname -except ImportError: - def setprocname(*args, **kwargs): # noqa - pass -import socket import signal +import socket +import sys +import time import traceback -import logging -from .queue import Queue, get_failed_queue + +from rq.compat import as_text, text_type + from .connections import get_current_connection +from .exceptions import DequeueTimeout, NoQueueError from .job import Job, Status -from .utils import make_colorizer, utcnow, utcformat from .logutils import setup_loghandlers -from .exceptions import NoQueueError, DequeueTimeout +from .queue import get_failed_queue, Queue from .timeouts import UnixSignalDeathPenalty +from .utils import make_colorizer, utcformat, utcnow from .version import VERSION -from rq.compat import text_type, as_text + +try: + from procname import setprocname +except ImportError: + def setprocname(*args, **kwargs): # noqa + pass green = make_colorizer('darkgreen') yellow = make_colorizer('darkyellow') diff --git a/tests/__init__.py b/tests/__init__.py index ac66204..e704eaa 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -1,13 +1,18 @@ +# -*- coding: utf-8 -*- +from __future__ import (absolute_import, division, print_function, + unicode_literals) + import logging + +from redis import StrictRedis +from rq import pop_connection, push_connection from rq.compat import is_python_version + if is_python_version((2, 7), (3, 2)): import unittest else: import unittest2 as unittest # noqa -from redis import StrictRedis -from rq import push_connection, pop_connection - def find_empty_redis_database(): """Tries to connect to a random Redis database (starting from 4), and diff --git a/tests/dummy_settings.py b/tests/dummy_settings.py index 1404250..dbc935f 100644 --- a/tests/dummy_settings.py +++ b/tests/dummy_settings.py @@ -1 +1,5 @@ +# -*- coding: utf-8 -*- +from __future__ import (absolute_import, division, print_function, + unicode_literals) + REDIS_HOST = "testhost.example.com" diff --git a/tests/fixtures.py b/tests/fixtures.py index eaf426e..a375562 100644 --- a/tests/fixtures.py +++ b/tests/fixtures.py @@ -1,11 +1,15 @@ +# -*- coding: utf-8 -*- """ This file contains all jobs that are used in tests. Each of these test fixtures has a slighty different characteristics. """ +from __future__ import (absolute_import, division, print_function, + unicode_literals) + import time -from rq import Connection + +from rq import Connection, get_current_job from rq.decorators import job -from rq import get_current_job def say_hello(name=None): diff --git a/tests/helpers.py b/tests/helpers.py index 16f1717..3ecf0dd 100644 --- a/tests/helpers.py +++ b/tests/helpers.py @@ -1,3 +1,7 @@ +# -*- coding: utf-8 -*- +from __future__ import (absolute_import, division, print_function, + unicode_literals) + from datetime import timedelta diff --git a/tests/test_connection.py b/tests/test_connection.py index e0d149d..445e33f 100644 --- a/tests/test_connection.py +++ b/tests/test_connection.py @@ -1,7 +1,11 @@ -from tests import RQTestCase, find_empty_redis_database +# -*- coding: utf-8 -*- +from __future__ import (absolute_import, division, print_function, + unicode_literals) + +from rq import Connection, Queue + +from tests import find_empty_redis_database, RQTestCase from tests.fixtures import do_nothing -from rq import Queue -from rq import Connection def new_connection(): diff --git a/tests/test_decorator.py b/tests/test_decorator.py index 2521e2e..91c53e9 100644 --- a/tests/test_decorator.py +++ b/tests/test_decorator.py @@ -1,10 +1,15 @@ -from tests import RQTestCase -from tests.fixtures import decorated_job +# -*- coding: utf-8 -*- +from __future__ import (absolute_import, division, print_function, + unicode_literals) from rq.decorators import job from rq.job import Job from rq.worker import DEFAULT_RESULT_TTL +from tests import RQTestCase +from tests.fixtures import decorated_job + + class TestDecorator(RQTestCase): def setUp(self): diff --git a/tests/test_job.py b/tests/test_job.py index e93b8cb..bbe5f7a 100644 --- a/tests/test_job.py +++ b/tests/test_job.py @@ -1,16 +1,23 @@ +# -*- coding: utf-8 -*- +from __future__ import (absolute_import, division, print_function, + unicode_literals) + from datetime import datetime + +from rq.compat import as_text, PY2 +from rq.exceptions import NoSuchJobError, UnpickleError +from rq.job import get_current_job, Job +from rq.queue import Queue +from rq.utils import utcformat + from tests import RQTestCase -from tests.fixtures import Number, some_calculation, say_hello, access_self +from tests.fixtures import access_self, Number, say_hello, some_calculation from tests.helpers import strip_microseconds + try: from cPickle import loads, dumps except ImportError: from pickle import loads, dumps -from rq.compat import as_text -from rq.job import Job, get_current_job -from rq.exceptions import NoSuchJobError, UnpickleError -from rq.queue import Queue -from rq.utils import utcformat class TestJob(RQTestCase): @@ -240,16 +247,19 @@ class TestJob(RQTestCase): def test_description_is_persisted(self): """Ensure that job's custom description is set properly""" - job = Job.create(func=say_hello, args=('Lionel',), description=u'Say hello!') + job = Job.create(func=say_hello, args=('Lionel',), description='Say hello!') job.save() Job.fetch(job.id, connection=self.testconn) - self.assertEqual(job.description, u'Say hello!') + self.assertEqual(job.description, 'Say hello!') # Ensure job description is constructed from function call string job = Job.create(func=say_hello, args=('Lionel',)) job.save() Job.fetch(job.id, connection=self.testconn) - self.assertEqual(job.description, "tests.fixtures.say_hello('Lionel')") + if PY2: + self.assertEqual(job.description, "tests.fixtures.say_hello(u'Lionel')") + else: + self.assertEqual(job.description, "tests.fixtures.say_hello('Lionel')") def test_job_access_within_job_function(self): """The current job is accessible within the job function.""" diff --git a/tests/test_queue.py b/tests/test_queue.py index 8106b8d..a999b5e 100644 --- a/tests/test_queue.py +++ b/tests/test_queue.py @@ -1,9 +1,15 @@ -from tests import RQTestCase -from tests.fixtures import Number, div_by_zero, echo, say_hello, some_calculation -from rq import Queue, get_failed_queue +# -*- coding: utf-8 -*- +from __future__ import (absolute_import, division, print_function, + unicode_literals) + +from rq import get_failed_queue, Queue +from rq.exceptions import InvalidJobOperationError from rq.job import Job, Status from rq.worker import Worker -from rq.exceptions import InvalidJobOperationError + +from tests import RQTestCase +from tests.fixtures import (div_by_zero, echo, Number, say_hello, + some_calculation) class TestQueue(RQTestCase): diff --git a/tests/test_scripts.py b/tests/test_scripts.py index ae661c7..88689c3 100644 --- a/tests/test_scripts.py +++ b/tests/test_scripts.py @@ -1,9 +1,14 @@ +# -*- coding: utf-8 -*- +from __future__ import (absolute_import, division, print_function, + unicode_literals) + from rq.compat import is_python_version +from rq.scripts import read_config_file + if is_python_version((2, 7), (3, 2)): from unittest import TestCase else: from unittest2 import TestCase # noqa -from rq.scripts import read_config_file class TestScripts(TestCase): diff --git a/tests/test_sentry.py b/tests/test_sentry.py index 3fa4737..ba8d1ec 100644 --- a/tests/test_sentry.py +++ b/tests/test_sentry.py @@ -1,7 +1,12 @@ -from tests import RQTestCase -from rq import Queue, Worker, get_failed_queue +# -*- coding: utf-8 -*- +from __future__ import (absolute_import, division, print_function, + unicode_literals) + +from rq import get_failed_queue, Queue, Worker from rq.contrib.sentry import register_sentry +from tests import RQTestCase + class FakeSentry(object): def captureException(self, *args, **kwds): diff --git a/tests/test_worker.py b/tests/test_worker.py index b7147fe..1255442 100644 --- a/tests/test_worker.py +++ b/tests/test_worker.py @@ -1,12 +1,18 @@ +# -*- coding: utf-8 -*- +from __future__ import (absolute_import, division, print_function, + unicode_literals) + import os -from tests import RQTestCase, slow -from tests.fixtures import say_hello, div_by_zero, create_file, \ - create_file_after_timeout -from tests.helpers import strip_microseconds -from rq import Queue, Worker, get_failed_queue + +from rq import get_failed_queue, Queue, Worker from rq.compat import as_text from rq.job import Job, Status +from tests import RQTestCase, slow +from tests.fixtures import (create_file, create_file_after_timeout, div_by_zero, + say_hello) +from tests.helpers import strip_microseconds + class TestWorker(RQTestCase): def test_create_worker(self):