Skip to content

Commit

Permalink
Moves some compatibility hacks to johnny.compat
Browse files Browse the repository at this point in the history
  • Loading branch information
BertrandBordage committed Feb 5, 2014
1 parent a2a353c commit 502e4b1
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 35 deletions.
20 changes: 2 additions & 18 deletions johnny/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,11 @@

import django
from django.db.models.signals import post_save, post_delete
from django.db.models.sql import compiler

try:
from django.utils.encoding import force_bytes, force_text
from django.utils.six import string_types, text_type
except ImportError: # Django < 1.4.2
force_bytes = str
force_text = unicode
string_types = (str, unicode)
text_type = unicode


from . import localstore, signals
from . import settings
from .compat import (
force_bytes, force_text, string_types, text_type, empty_iter)
from .decorators import wraps, available_attrs
from .transaction import TransactionManager

Expand All @@ -30,13 +21,6 @@ class NotInCache(object):
no_result_sentinel = "22c52d96-156a-4638-a38d-aae0051ee9df"
local = localstore.LocalStore()

def empty_iter():
#making this a function as the empty_iter has changed between 1.4 and 1.5
if django.VERSION[:2] >= (1, 5):
return iter([])
else:
return compiler.empty_iter()


def disallowed_table(*tables):
"""Returns True if a set of tables is in the blacklist or, if a whitelist is set,
Expand Down
29 changes: 29 additions & 0 deletions johnny/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,38 @@
"""

from __future__ import unicode_literals
from django.db.models.sql import compiler


try:
from queue import Queue
except ImportError: # Python < 3.0
from Queue import Queue

import django
from django.db import transaction

try:
from django.utils.encoding import force_bytes, force_text
from django.utils.six import string_types, text_type
except ImportError: # Django < 1.4.2
force_bytes = str
force_text = unicode
string_types = (str, unicode)
text_type = unicode


__all__ = (
'Queue', 'force_bytes', 'force_text', 'string_types', 'text_type',
'empty_iter', 'is_managed', 'managed',
)


def empty_iter():
if django.VERSION[:2] >= (1, 5):
return iter([])
return compiler.empty_iter()


def is_managed(using=None):
if django.VERSION[:2] < (1, 6):
Expand Down
5 changes: 1 addition & 4 deletions johnny/localstore.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@
import threading
import warnings

try:
from django.utils.six import string_types
except ImportError: # Django < 1.4.2
string_types = (str, unicode)
from .compat import string_types


class LocalStore(threading.local):
Expand Down
5 changes: 1 addition & 4 deletions johnny/tests/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@
"""Base test class for Johnny Cache Tests."""

from __future__ import print_function
try:
from queue import Queue
except ImportError: # Python < 3.0
from Queue import Queue
from pprint import pformat

from django.test import TestCase, TransactionTestCase
Expand All @@ -16,6 +12,7 @@
from django.db.models.loading import load_app

from johnny import settings as johnny_settings
from johnny.compat import Queue
from johnny.decorators import wraps, available_attrs
from johnny.signals import qc_hit, qc_miss, qc_skip

Expand Down
6 changes: 1 addition & 5 deletions johnny/tests/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,14 @@

from __future__ import print_function
from threading import Thread
try:
from queue import Queue
except ImportError: # Python < 3.0
from Queue import Queue

from django.conf import settings
from django.core.paginator import Paginator
from django.db import connection, connections, transaction
from django.db.models import Q, Count, Sum
from johnny import middleware, settings as johnny_settings, cache
from johnny.cache import get_tables_for_query, invalidate
from johnny.compat import is_managed, managed
from johnny.compat import is_managed, managed, Queue
from johnny.signals import qc_hit, qc_miss, qc_skip
from . import base
from .testapp.models import (
Expand Down
5 changes: 1 addition & 4 deletions johnny/tests/localstore.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,11 @@
that it be tested thoroughly to make sure no changes introduce the possibility
of stale data in the cache."""

try:
from queue import Queue
except ImportError: # Python < 3.0
from Queue import Queue
from threading import Thread, current_thread
from time import sleep
from django.test import TestCase
from johnny import localstore, cache, middleware
from johnny.compat import Queue

class LocalStoreTest(TestCase):
def test_basic_operation(self):
Expand Down

0 comments on commit 502e4b1

Please sign in to comment.