Permalink
Browse files

Fixed exception handling and imports.

Import statements and exception handling now conforms with PEP8, specificaly http://www.python.org/dev/peps/pep-0008/#imports and http://www.python.org/dev/peps/pep-0008/#programming-recommendations
  • Loading branch information...
1 parent c48d307 commit b1a1a6204b6821fa8b29a717a94d56f7d223a2dd Vladimir Gorej committed Nov 16, 2012
@@ -37,7 +37,7 @@ class GearmanWorkerBaseCommand(BaseCommand):
@property
def task_name(self):
"""Override task_name property in worker to indicate what task should be registered in Gearman."""
- raise NotImplementedError, 'task_name should be implemented in worker'
+ raise NotImplementedError('task_name should be implemented in worker')
@property
def exit_after_job(self):
@@ -56,7 +56,7 @@ def do_job(self, job_data):
Override this in worker to perform job.
"""
- raise NotImplementedError, 'do_job() should be implemented in worker'
+ raise NotImplementedError('do_job() should be implemented in worker')
def handle(self, *args, **options):
try:
@@ -65,7 +65,7 @@ def handle(self, *args, **options):
task_name = '{0}@{1}'.format(self.task_name, get_namespace()) if get_namespace() else self.task_name
log.info('Registering gearman task: %s', self.task_name)
worker.register_task(task_name, self._invoke_job)
- except:
+ except Exception:
log.exception('Problem with registering gearman task')
raise
@@ -92,7 +92,7 @@ def _invoke_job(self, worker, job):
self.stdout.write('%s\n' % result)
return 'OK'
- except:
+ except Exception:
log.exception('Error occured when invoking job, task: %s', self.task_name)
raise
@@ -124,11 +124,10 @@ def get_server_info(self):
# use prettytable if available, otherwise raw output
try:
from prettytable import PrettyTable
- use_prettytable = True
except ImportError:
- use_prettytable = False
+ PrettyTable = None
- if use_prettytable:
+ if PrettyTable is not None:
# use PrettyTable for output
# version
table = PrettyTable(['Gearman Server Host', 'Gearman Server Version'])
@@ -1,20 +1,24 @@
# -*- coding: utf-8 -*-
-from django.core.management.base import BaseCommand, CommandError
-from django.conf import settings
-import gearman
+
import logging
+
+from django.core.management.base import BaseCommand
+
+import django_gearman_commands.settings
from django_gearman_commands import GearmanServerInfo
+
log = logging.getLogger(__name__)
+
class Command(BaseCommand):
"""Pprint overview of Gearman server status and workers."""
help = 'Print overview of Gearman server status and workers.'
def handle(self, *args, **options):
result = ''
- for server in settings.GEARMAN_SERVERS:
+ for server in django_gearman_commands.settings.GEARMAN_SERVERS:
server_info = GearmanServerInfo(server)
result += server_info.get_server_info()
@@ -9,6 +9,7 @@
import django_gearman_commands.settings
from django_gearman_commands import get_namespace
+
log = logging.getLogger(__name__)
@@ -1,9 +1,11 @@
# -*- coding: utf-8 -*-
+
from django.core.cache import cache
-import django_gearman_commands
+
+from django_gearman_commands import GearmanWorkerBaseCommand
-class Command(django_gearman_commands.GearmanWorkerBaseCommand):
+class Command(GearmanWorkerBaseCommand):
"""Gearman worker performing 'footest' job."""
@property
@@ -1,13 +1,18 @@
# -*- coding: utf-8 -*-
+
+import logging
+import pickle
+
from django.core.management import call_command
from django.conf import settings
from django.test import TestCase
from django.core.cache import cache
from django_gearman_commands import GearmanServerInfo
-import logging
+
log = logging.getLogger(__name__)
+
class GearmanCommandsTest(TestCase):
def test_server_info(self):
@@ -40,7 +45,6 @@ def test_worker_task_data_string(self):
self.assertEqual(cache.get('footest'), 'DATA', 'Unexpected footest worker result (data string)')
def test_worker_task_data_pickled(self):
- import pickle
# submit job
call_command('gearman_submit_job', 'footest', pickle.dumps(u'DATA'))

0 comments on commit b1a1a62

Please sign in to comment.