Permalink
Browse files

Fix some PEP8 inconsistancies

Fixes #202
  • Loading branch information...
2 parents 35522aa + 71d6464 commit 4f5e7552f0e718543b0f06f327718efa3820315d @SmileyChris committed Feb 5, 2013
View
@@ -77,8 +77,8 @@ def __setattr__(self, attr, value):
# reverted.
if attr not in self._added:
try:
- self._changed.setdefault(attr,
- getattr(django_settings, attr))
+ self._changed.setdefault(
+ attr, getattr(django_settings, attr))
except AttributeError:
self._added.append(attr)
return setattr(django_settings, attr, value)
@@ -92,8 +92,8 @@ class Settings(AppSettings):
THUMBNAIL_DEBUG = False
- THUMBNAIL_DEFAULT_STORAGE = 'easy_thumbnails.storage.'\
- 'ThumbnailFileSystemStorage'
+ THUMBNAIL_DEFAULT_STORAGE = (
+ 'easy_thumbnails.storage.ThumbnailFileSystemStorage')
THUMBNAIL_MEDIA_ROOT = ''
THUMBNAIL_MEDIA_URL = ''
@@ -28,7 +28,8 @@ def process_image(source, processor_options, processors=None):
"""
processor_options = _use_default_options(processor_options)
if processors is None:
- processors = [utils.dynamic_import(name)
+ processors = [
+ utils.dynamic_import(name)
for name in settings.THUMBNAIL_PROCESSORS]
image = source
for processor in processors:
@@ -72,7 +73,8 @@ def generate_source_image(source_file, processor_options, generators=None):
processor_options = _use_default_options(processor_options)
was_closed = source_file.closed
if generators is None:
- generators = [utils.dynamic_import(name)
+ generators = [
+ utils.dynamic_import(name)
for name in settings.THUMBNAIL_SOURCE_GENERATORS]
try:
source = source_file
View
@@ -1,6 +1,6 @@
from django.core.files.base import File, ContentFile
-from django.core.files.storage import get_storage_class, default_storage, \
- Storage
+from django.core.files.storage import (
+ get_storage_class, default_storage, Storage)
from django.db.models.fields.files import ImageFieldFile, FieldFile
import os
@@ -57,17 +57,19 @@ def get_thumbnailer(obj, relative_name=None):
obj = None
if not relative_name:
- raise ValueError("If object is not a FieldFile or Thumbnailer "
- "instance, the relative name must be provided")
+ raise ValueError(
+ "If object is not a FieldFile or Thumbnailer instance, the "
+ "relative name must be provided")
if isinstance(obj, File):
obj = obj.file
if isinstance(obj, Storage) or obj == default_storage:
source_storage = obj
obj = None
- return Thumbnailer(file=obj, name=relative_name,
- source_storage=source_storage, remote_source=obj is not None)
+ return Thumbnailer(
+ file=obj, name=relative_name, source_storage=source_storage,
+ remote_source=obj is not None)
def save_thumbnail(thumbnail_file, storage):
@@ -122,10 +124,10 @@ class ThumbnailFile(ImageFieldFile):
field (i.e. an ``ImageFieldFile`` object).
"""
def __init__(self, name, file=None, storage=None, thumbnail_options=None,
- *args, **kwargs):
+ *args, **kwargs):
fake_field = FakeField(storage=storage)
- super(ThumbnailFile, self).__init__(FakeInstance(), fake_field, name,
- *args, **kwargs)
+ super(ThumbnailFile, self).__init__(
+ FakeInstance(), fake_field, name, *args, **kwargs)
del self.field
if file:
self.file = file
@@ -166,9 +168,9 @@ def tag(self, alt='', use_size=None, **attrs):
:param alt: The ``alt=""`` text for the tag. Defaults to ``''``.
- :param use_size: Whether to get the size of the thumbnail image for use in
- the tag attributes. If ``None`` (default), it will be ``True`` or
- ``False`` depending on whether the file storage is local or not.
+ :param use_size: Whether to get the size of the thumbnail image for use
+ in the tag attributes. If ``None`` (default), it will be ``True``
+ or ``False`` depending on whether the file storage is local or not.
All other keyword parameters are added as (properly escaped) extra
attributes to the `img` tag.
@@ -243,8 +245,8 @@ class Thumbnailer(File):
thumbnail_processors = None
def __init__(self, file=None, name=None, source_storage=None,
- thumbnail_storage=None, remote_source=False, generate=True, *args,
- **kwargs):
+ thumbnail_storage=None, remote_source=False, generate=True,
+ *args, **kwargs):
super(Thumbnailer, self).__init__(file, name, *args, **kwargs)
self.source_storage = source_storage or default_storage
if not thumbnail_storage:
@@ -258,7 +260,8 @@ def __init__(self, file=None, name=None, source_storage=None,
# Set default properties. For backwards compatibilty, check to see
# if the attribute exists already (it could be set as a class property
# on a subclass) before getting it from settings.
- for default in ('basedir', 'subdir', 'prefix', 'quality', 'extension',
+ for default in (
+ 'basedir', 'subdir', 'prefix', 'quality', 'extension',
'preserve_extensions', 'transparency_extension',
'check_cache_miss'):
attr_name = 'thumbnail_%s' % default
@@ -296,14 +299,16 @@ def generate_thumbnail(self, thumbnail_options):
self.thumbnail_processors)
quality = thumbnail_options.get('quality', self.thumbnail_quality)
- filename = self.get_thumbnail_name(thumbnail_options,
+ filename = self.get_thumbnail_name(
+ thumbnail_options,
transparent=utils.is_transparent(thumbnail_image))
- data = engine.save_image(thumbnail_image, filename=filename,
- quality=quality).read()
+ img = engine.save_image(
+ thumbnail_image, filename=filename, quality=quality)
+ data = img.read()
- thumbnail = ThumbnailFile(filename, file=ContentFile(data),
- storage=self.thumbnail_storage,
+ thumbnail = ThumbnailFile(
+ filename, file=ContentFile(data), storage=self.thumbnail_storage,
thumbnail_options=thumbnail_options)
thumbnail.image = thumbnail_image
thumbnail._committed = False
@@ -319,10 +324,11 @@ def get_thumbnail_name(self, thumbnail_options, transparent=False):
path, source_filename = os.path.split(self.name)
source_extension = os.path.splitext(source_filename)[1][1:]
filename = '%s%s' % (self.thumbnail_prefix, source_filename)
- if self.thumbnail_preserve_extensions == True or \
- (self.thumbnail_preserve_extensions and \
- source_extension.lower() in self.thumbnail_preserve_extensions):
- extension = source_extension
+ preserve_extensions = self.thumbnail_preserve_extensions
+ if preserve_extensions and (
+ preserve_extensions is True or
+ source_extension.lower() in preserve_extensions):
+ extension = source_extension
elif transparent:
extension = self.thumbnail_transparency_extension
else:
@@ -347,7 +353,7 @@ def get_thumbnail_name(self, thumbnail_options, transparent=False):
filename_parts = [filename]
if ('%(opts)s' in self.thumbnail_basedir or
- '%(opts)s' in self.thumbnail_subdir):
+ '%(opts)s' in self.thumbnail_subdir):
if extension != source_extension:
filename_parts.append(extension)
else:
@@ -383,15 +389,15 @@ def get_thumbnail(self, thumbnail_options, save=True, generate=None):
names = (opaque_name, transparent_name)
for filename in names:
if self.thumbnail_exists(filename):
- return ThumbnailFile(name=filename,
- storage=self.thumbnail_storage,
+ return ThumbnailFile(
+ name=filename, storage=self.thumbnail_storage,
thumbnail_options=thumbnail_options)
if generate is None:
generate = self.generate
if not generate:
- signals.thumbnail_missed.send(sender=self,
- options=thumbnail_options)
+ signals.thumbnail_missed.send(
+ sender=self, options=thumbnail_options)
return
thumbnail = self.generate_thumbnail(thumbnail_options)
@@ -533,7 +539,7 @@ def delete_thumbnails(self, source_cache=None):
deleted = 0
if source_cache:
thumbnail_storage_hash = utils.get_storage_hash(
- self.thumbnail_storage)
+ self.thumbnail_storage)
for thumbnail_cache in source_cache.thumbnails.all():
# Only attempt to delete the file if it was stored using the
# same storage as is currently used.
@@ -554,7 +560,7 @@ def get_thumbnails(self, *args, **kwargs):
source_cache = self.get_source_cache()
if source_cache:
thumbnail_storage_hash = utils.get_storage_hash(
- self.thumbnail_storage)
+ self.thumbnail_storage)
for thumbnail_cache in source_cache.thumbnails.all():
# Only iterate files which are stored using the current
# thumbnail storage.
@@ -43,8 +43,8 @@ def all_thumbnails(path, recursive=True, prefix=None, subdir=None):
source_path, source_filename = os.path.split(source_filename)
if not source_filename.startswith(prefix):
continue
- source_filename = os.path.join(source_path,
- source_filename[len(prefix):])
+ source_filename = os.path.join(
+ source_path, source_filename[len(prefix):])
d['options'] = d['options'] and d['options'].split('_') or []
if subdir and rel_dir.endswith(subdir):
rel_dir = rel_dir[:-len(subdir)]
@@ -11,7 +11,8 @@
except NameError:
from sets import Set as set # For Python 2.3
-thumb_re = re.compile(r'^%s(.*)\.\d{1,}x\d{1,}_[-\w]*q([1-9]\d?|100)\.jpg' %
+thumb_re = re.compile(
+ r'^%s(.*)\.\d{1,}x\d{1,}_[-\w]*q([1-9]\d?|100)\.jpg' %
settings.THUMBNAIL_PREFIX)
@@ -51,12 +52,12 @@ def clean_up():
# org_fn_alt is the expected original filename with extension
org_fn = m.group(1)
org_fn_exists = os.path.isfile(
- os.path.join(settings.MEDIA_ROOT, path, org_fn))
+ os.path.join(settings.MEDIA_ROOT, path, org_fn))
usc_pos = org_fn.rfind("_")
if usc_pos != -1:
- org_fn_alt = "%s.%s" % (org_fn[0:usc_pos],
- org_fn[usc_pos + 1:])
+ org_fn_alt = "%s.%s" % (
+ org_fn[0:usc_pos], org_fn[usc_pos + 1:])
org_fn_alt_exists = os.path.isfile(
os.path.join(settings.MEDIA_ROOT, path, org_fn_alt))
else:
@@ -16,5 +16,5 @@ def __init__(self, location=None, base_url=None, *args, **kwargs):
location = settings.THUMBNAIL_MEDIA_ROOT or None
if base_url is None:
base_url = settings.THUMBNAIL_MEDIA_URL or None
- super(ThumbnailFileSystemStorage, self).__init__(location, base_url,
- *args, **kwargs)
+ super(ThumbnailFileSystemStorage, self).__init__(
+ location, base_url, *args, **kwargs)
@@ -1,7 +1,7 @@
import re
-from django.template import Library, Node, VariableDoesNotExist, \
- TemplateSyntaxError
+from django.template import (
+ Library, Node, VariableDoesNotExist, TemplateSyntaxError)
from django.utils.html import escape
from easy_thumbnails import utils
@@ -50,14 +50,13 @@ def render(self, context):
source = self.source_var.resolve(context)
except VariableDoesNotExist:
if raise_errors:
- raise VariableDoesNotExist("Variable '%s' does not exist." %
- self.source_var)
+ raise VariableDoesNotExist(
+ "Variable '%s' does not exist." % self.source_var)
return self.bail_out(context)
if not source:
if raise_errors:
raise TemplateSyntaxError(
- "Variable '%s' is an invalid source." % self.source_var
- )
+ "Variable '%s' is an invalid source." % self.source_var)
return self.bail_out(context)
# Resolve the thumbnail option values.
try:
@@ -87,16 +86,16 @@ def render(self, context):
opts['quality'] = int(opts['quality'])
except (TypeError, ValueError):
if raise_errors:
- raise TemplateSyntaxError("%r is an invalid quality." %
- opts['quality'])
+ raise TemplateSyntaxError(
+ "%r is an invalid quality." % opts['quality'])
return self.bail_out(context)
try:
thumbnail = get_thumbnailer(source).get_thumbnail(opts)
except Exception, e:
if raise_errors:
- raise TemplateSyntaxError(u"Couldn't get the thumbnail %s: %s" %
- (source, e))
+ raise TemplateSyntaxError(
+ u"Couldn't get the thumbnail %s: %s" % (source, e))
return self.bail_out(context)
# Return the thumbnail file url, or put the file on the context.
if self.context_name is None:
@@ -168,7 +167,8 @@ def thumbnail(parser, token):
context_name = None
if len(args) < 3:
- raise TemplateSyntaxError("Invalid syntax. Expected "
+ raise TemplateSyntaxError(
+ "Invalid syntax. Expected "
"'{%% %s source size [option1 option2 ...] %%}' or "
"'{%% %s source size [option1 option2 ...] as variable %%}'" %
(tag, tag))
@@ -233,7 +233,8 @@ def thumbnailer_passive(obj):
{% if avatar_thumb %}
<img src="{{ avatar_thumb.url }}" alt="" />
{% else %}
- <img src="{% static 'img/default-avatar-small.png' %}" alt="" />
+ <img src="{% static 'img/default-avatar-small.png' %}"
+ alt="" />
{% endif %}
{% endwith %}
{% endwith %}
View
@@ -108,7 +108,7 @@ def tearDown(self):
return super(BaseTest, self).tearDown()
def create_image(self, storage, filename, size=(800, 600),
- image_mode='RGB', image_format='JPEG'):
+ image_mode='RGB', image_format='JPEG'):
"""
Generate a test image, returning the filename that it was saved as.
@@ -76,7 +76,8 @@ def test_partial_target(self):
self.assertEqual(
aliases.get('banner', target='some_app.Profile.avatar'),
{'size': (600, 80), 'crop': True})
- self.assertEqual(aliases.get('banner', target='some_app.Profile'),
+ self.assertEqual(
+ aliases.get('banner', target='some_app.Profile'),
{'size': (600, 80), 'crop': True})
self.assertEqual(aliases.get('banner', target='some_app'), None)
@@ -95,29 +96,33 @@ def test_target_fallback(self):
None)
def test_all(self):
- self.assertEqual(aliases.all(),
+ self.assertEqual(
+ aliases.all(),
{
'large': {'size': (500, 500)},
'medium': {'size': (300, 300)},
'small': {'size': (100, 100)},
})
- self.assertEqual(aliases.all('unknown_app'),
+ self.assertEqual(
+ aliases.all('unknown_app'),
{
'large': {'size': (500, 500)},
'medium': {'size': (300, 300)},
'small': {'size': (100, 100)},
})
- self.assertEqual(aliases.all('some_app.Profile'),
+ self.assertEqual(
+ aliases.all('some_app.Profile'),
{
'banner': {'size': (600, 80), 'crop': True},
'large': {'size': (200, 200)},
'medium': {'size': (300, 300)},
'small': {'size': (100, 100)},
})
- self.assertEqual(aliases.all('some_app.Profile.avatar'),
+ self.assertEqual(
+ aliases.all('some_app.Profile.avatar'),
{
'avatar': {'size': (80, 80), 'crop': True},
'banner': {'size': (600, 80), 'crop': True},
@@ -174,8 +179,8 @@ def setUp(self):
settings.MEDIA_ROOT = self.storage.temporary_location
def tearDown(self):
- signals.saved_file.disconnect(self.get_signal_handler(),
- sender=Profile)
+ signals.saved_file.disconnect(
+ self.get_signal_handler(), sender=Profile)
super(GenerationBase, self).tearDown()
def fake_save(self, instance):
Oops, something went wrong.

0 comments on commit 4f5e755

Please sign in to comment.