From 60250f22c8e144494f372338c16a2167cccb319d Mon Sep 17 00:00:00 2001 From: Xavier Ordoquy Date: Thu, 3 Jan 2013 11:41:07 +0100 Subject: [PATCH] Move the various compat things to the compat module. --- rest_framework/compat.py | 24 ++++++++++++++++--- rest_framework/fields.py | 7 ++---- rest_framework/parsers.py | 3 +-- rest_framework/relations.py | 5 +--- rest_framework/request.py | 1 - rest_framework/response.py | 4 ++-- rest_framework/serializers.py | 3 +-- rest_framework/settings.py | 6 ++--- rest_framework/templatetags/rest_framework.py | 8 +++---- rest_framework/tests/files.py | 5 ++-- rest_framework/tests/generics.py | 4 +--- rest_framework/tests/htmlrenderer.py | 3 +-- rest_framework/tests/renderers.py | 2 +- rest_framework/tests/request.py | 3 +-- rest_framework/tests/response.py | 4 +--- rest_framework/utils/__init__.py | 10 ++------ 16 files changed, 43 insertions(+), 49 deletions(-) diff --git a/rest_framework/compat.py b/rest_framework/compat.py index 9b38c2084..5924cd6d3 100644 --- a/rest_framework/compat.py +++ b/rest_framework/compat.py @@ -4,16 +4,34 @@ versions of django/python, and compatibility wrappers around optional packages. """ # flake8: noqa from __future__ import unicode_literals -import six import django +# Try to import six from Django, fallback to six itself (1.3.x) +try: + from django.utils import six +except: + import six + # location of patterns, url, include changes in 1.4 onwards try: from django.conf.urls import patterns, url, include except: from django.conf.urls.defaults import patterns, url, include +# Handle django.utils.encoding rename: +# smart_unicode -> smart_text +# force_unicode -> force_text +try: + from django.utils.encoding import smart_text +except ImportError: + from django.utils.encoding import smart_unicode as smart_text +try: + from django.utils.encoding import force_text +except ImportError: + from django.utils.encoding import force_unicode as force_text + + # django-filter is optional try: import django_filters @@ -25,9 +43,9 @@ except: try: import cStringIO.StringIO as StringIO except ImportError: - from six import StringIO + StringIO = six.StringIO -from six import BytesIO +BytesIO = six.BytesIO # urlparse compat import (Required because it changed in python 3.x) diff --git a/rest_framework/fields.py b/rest_framework/fields.py index e59cc9b42..adea5bf5a 100644 --- a/rest_framework/fields.py +++ b/rest_framework/fields.py @@ -1,6 +1,5 @@ from __future__ import unicode_literals -import six import copy import datetime @@ -14,14 +13,12 @@ from django.conf import settings from django import forms from django.forms import widgets from django.utils.encoding import is_protected_type -try: - from django.utils.encoding import smart_text -except ImportError: - from django.utils.encoding import smart_unicode as smart_text from django.utils.translation import ugettext_lazy as _ from rest_framework.compat import parse_date, parse_datetime from rest_framework.compat import timezone from rest_framework.compat import BytesIO +from rest_framework.compat import six +from rest_framework.compat import smart_text def is_simple_callable(obj): diff --git a/rest_framework/parsers.py b/rest_framework/parsers.py index d5cfaaf88..7c01006ab 100644 --- a/rest_framework/parsers.py +++ b/rest_framework/parsers.py @@ -5,14 +5,13 @@ They give us a generic way of being able to handle various media types on the request, such as form content or json encoded data. """ -import six - from django.http import QueryDict from django.http.multipartparser import MultiPartParser as DjangoMultiPartParser from django.http.multipartparser import MultiPartParserError from django.utils import simplejson as json from rest_framework.compat import yaml, ETParseError from rest_framework.exceptions import ParseError +from rest_framework.compat import six from xml.etree import ElementTree as ET from xml.parsers.expat import ExpatError import datetime diff --git a/rest_framework/relations.py b/rest_framework/relations.py index 33d3732f3..b7a6e0c10 100644 --- a/rest_framework/relations.py +++ b/rest_framework/relations.py @@ -6,13 +6,10 @@ from django.core.urlresolvers import resolve, get_script_prefix from django import forms from django.forms import widgets from django.forms.models import ModelChoiceIterator -try: - from django.utils.encoding import smart_text -except ImportError: - from django.utils.encoding import smart_unicode as smart_text from rest_framework.fields import Field, WritableField from rest_framework.reverse import reverse from rest_framework.compat import urlparse +from rest_framework.compat import smart_text ##### Relational fields ##### diff --git a/rest_framework/request.py b/rest_framework/request.py index c50ae5ad7..048a1c41d 100644 --- a/rest_framework/request.py +++ b/rest_framework/request.py @@ -9,7 +9,6 @@ The wrapped request then offers a richer API, in particular : - full support of PUT method, including support for file uploads - form overloading of HTTP method, content type and content """ -import six from rest_framework.compat import BytesIO from django.http.multipartparser import parse_header diff --git a/rest_framework/response.py b/rest_framework/response.py index cad95611c..0a484c4a1 100644 --- a/rest_framework/response.py +++ b/rest_framework/response.py @@ -1,8 +1,8 @@ -import six - from django.core.handlers.wsgi import STATUS_CODE_TEXT from django.template.response import SimpleTemplateResponse +from rest_framework.compat import six + class Response(SimpleTemplateResponse): """ diff --git a/rest_framework/serializers.py b/rest_framework/serializers.py index 9f35f77cf..663f166b9 100644 --- a/rest_framework/serializers.py +++ b/rest_framework/serializers.py @@ -1,5 +1,3 @@ -import six - import copy import datetime import types @@ -8,6 +6,7 @@ from django.db import models from django.forms import widgets from django.utils.datastructures import SortedDict from rest_framework.compat import get_concrete_model +from rest_framework.compat import six # Note: We do the following so that users of the framework can use this style: # diff --git a/rest_framework/settings.py b/rest_framework/settings.py index 9e73bbfb7..186833b5f 100644 --- a/rest_framework/settings.py +++ b/rest_framework/settings.py @@ -19,8 +19,7 @@ back to the defaults. """ from django.conf import settings from django.utils import importlib -from six import string_types - +from rest_framework.compat import six USER_SETTINGS = getattr(settings, 'REST_FRAMEWORK', None) @@ -100,7 +99,7 @@ def perform_import(val, setting_name): If the given setting is a string import notation, then perform the necessary import or imports. """ - if isinstance(val, string_types): + if isinstance(val, six.string_types): return import_from_string(val, setting_name) elif isinstance(val, (list, tuple)): return [import_from_string(item, setting_name) for item in val] @@ -118,6 +117,7 @@ def import_from_string(val, setting_name): module = importlib.import_module(module_path) return getattr(module, class_name) except: + raise msg = "Could not import '%s' for API setting '%s'" % (val, setting_name) raise ImportError(msg) diff --git a/rest_framework/templatetags/rest_framework.py b/rest_framework/templatetags/rest_framework.py index 52c7a59ce..4205e57cc 100644 --- a/rest_framework/templatetags/rest_framework.py +++ b/rest_framework/templatetags/rest_framework.py @@ -1,16 +1,14 @@ from __future__ import unicode_literals, absolute_import -import six from django import template from django.core.urlresolvers import reverse from django.http import QueryDict -try: - from django.utils.encoding import force_text -except ImportError: - from django.utils.encoding import force_unicode as force_text from django.utils.html import escape from django.utils.safestring import SafeData, mark_safe from rest_framework.compat import urlparse +from rest_framework.compat import force_text +from rest_framework.compat import six + import re import string diff --git a/rest_framework/tests/files.py b/rest_framework/tests/files.py index 42e8ed5fc..ca6bc9050 100644 --- a/rest_framework/tests/files.py +++ b/rest_framework/tests/files.py @@ -1,11 +1,10 @@ -from rest_framework.compat import BytesIO - import datetime -import six from django.test import TestCase from rest_framework import serializers +from rest_framework.compat import BytesIO +from rest_framework.compat import six class UploadedFile(object): diff --git a/rest_framework/tests/generics.py b/rest_framework/tests/generics.py index a877574ef..215de0c45 100644 --- a/rest_framework/tests/generics.py +++ b/rest_framework/tests/generics.py @@ -1,14 +1,12 @@ from __future__ import unicode_literals -import six - from django.db import models from django.test import TestCase from django.test.client import RequestFactory from django.utils import simplejson as json from rest_framework import generics, serializers, status from rest_framework.tests.models import BasicModel, Comment, SlugBasedModel - +from rest_framework.compat import six factory = RequestFactory() diff --git a/rest_framework/tests/htmlrenderer.py b/rest_framework/tests/htmlrenderer.py index d4662465e..34caa208b 100644 --- a/rest_framework/tests/htmlrenderer.py +++ b/rest_framework/tests/htmlrenderer.py @@ -1,5 +1,3 @@ -import six - from django.core.exceptions import PermissionDenied from django.http import Http404 from django.test import TestCase @@ -9,6 +7,7 @@ from rest_framework.compat import patterns, url from rest_framework.decorators import api_view, renderer_classes from rest_framework.renderers import TemplateHTMLRenderer from rest_framework.response import Response +from rest_framework.compat import six @api_view(('GET',)) diff --git a/rest_framework/tests/renderers.py b/rest_framework/tests/renderers.py index b02fccf48..724053360 100644 --- a/rest_framework/tests/renderers.py +++ b/rest_framework/tests/renderers.py @@ -1,6 +1,5 @@ import pickle import re -import six from django.core.cache import cache from django.test import TestCase @@ -16,6 +15,7 @@ from rest_framework.parsers import YAMLParser, XMLParser from rest_framework.settings import api_settings from rest_framework.compat import StringIO +from rest_framework.compat import six import datetime from decimal import Decimal diff --git a/rest_framework/tests/request.py b/rest_framework/tests/request.py index fe5116a81..7d4575bb9 100644 --- a/rest_framework/tests/request.py +++ b/rest_framework/tests/request.py @@ -1,8 +1,6 @@ """ Tests for content parsing, and form-overloaded content parsing. """ -import six - from django.contrib.auth.models import User from django.contrib.auth import authenticate, login, logout from django.contrib.sessions.middleware import SessionMiddleware @@ -22,6 +20,7 @@ from rest_framework.request import Request from rest_framework.response import Response from rest_framework.settings import api_settings from rest_framework.views import APIView +from rest_framework.compat import six factory = RequestFactory() diff --git a/rest_framework/tests/response.py b/rest_framework/tests/response.py index fd153f400..453488d00 100644 --- a/rest_framework/tests/response.py +++ b/rest_framework/tests/response.py @@ -1,6 +1,3 @@ -import unittest -import six - from django.test import TestCase from rest_framework.compat import patterns, url, include from rest_framework.response import Response @@ -12,6 +9,7 @@ from rest_framework.renderers import ( BrowsableAPIRenderer ) from rest_framework.settings import api_settings +from rest_framework.compat import six class MockPickleRenderer(BaseRenderer): diff --git a/rest_framework/utils/__init__.py b/rest_framework/utils/__init__.py index 458793539..1603f9725 100644 --- a/rest_framework/utils/__init__.py +++ b/rest_framework/utils/__init__.py @@ -1,13 +1,7 @@ - -import six - -try: - from django.utils.encoding import smart_text -except ImportError: - from django.utils.encoding import smart_unicode as smart_text - from django.utils.xmlutils import SimplerXMLGenerator from rest_framework.compat import StringIO +from rest_framework.compat import six +from rest_framework.compat import smart_text import re import xml.etree.ElementTree as ET