Skip to content

Commit

Permalink
pep8/pyflakes cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
mattdennewitz committed Jan 11, 2012
1 parent f70be02 commit afa836e
Show file tree
Hide file tree
Showing 16 changed files with 78 additions and 86 deletions.
3 changes: 0 additions & 3 deletions exampleproject/blog/models.py~

This file was deleted.

Empty file modified exampleproject/manage.py 100644 → 100755
Empty file.
Binary file not shown.
Binary file not shown.
Binary file removed exampleproject/media/artwork/statler_waldorf.jpg
Binary file not shown.
Binary file removed exampleproject/media/artwork/sweetums_lecture.jpg
Binary file not shown.
13 changes: 12 additions & 1 deletion exampleproject/urls.py
@@ -1,5 +1,5 @@
from django.conf import settings
from django.conf.urls.defaults import patterns, include, url

from django.contrib import admin


Expand All @@ -8,3 +8,14 @@
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
)

from django.contrib.staticfiles.urls import staticfiles_urlpatterns
urlpatterns += staticfiles_urlpatterns()

urlpatterns += patterns(
'',

url(r'^media/(?P<path>.*)$', 'django.views.static.serve', {
'document_root': settings.MEDIA_ROOT,
}),
)
26 changes: 12 additions & 14 deletions undermythumb/fields.py
@@ -1,14 +1,11 @@
import datetime
import os

from django.core.exceptions import ImproperlyConfigured
from django.db.models import signals
from django.db.models.fields.files import (ImageField,
from django.db.models.fields.files import (ImageField,
ImageFieldFile,
ImageFileDescriptor)
from django.utils.encoding import force_unicode, smart_str
from django.utils.encoding import force_unicode

from undermythumb.files import (ThumbnailFieldFile,
from undermythumb.files import (ThumbnailFieldFile,
ImageWithThumbnailsFieldFile)


Expand All @@ -24,7 +21,7 @@ def traverse_fallback_path(instance, fallback_path):

value = instance
path_bits = fallback_path.split('.')

while path_bits:
bit = path_bits.pop(0)

Expand All @@ -49,8 +46,8 @@ class FallbackFieldDescriptor(ImageFileDescriptor):

def __get__(self, instance, owner):
"""Returns a field's image. If no image is found, this descriptor
inspects and traverses its field's ``fallback_path``, to find and return
whatever lies at the end of the path.
inspects and traverses its field's ``fallback_path``,
to find and return whatever lies at the end of the path.
"""

# if this particular field is empty, return the url
Expand All @@ -65,7 +62,7 @@ def __get__(self, instance, owner):
and hasattr(value, 'url')):
value._empty = False
return value

# this value has no content. mark it as empty.
value._empty = True

Expand All @@ -78,7 +75,7 @@ def __get__(self, instance, owner):
return value

# using the instance, trace through the fallback path
mirror_value = traverse_fallback_path(instance,
mirror_value = traverse_fallback_path(instance,
self.field.fallback_path)

if mirror_value is None:
Expand All @@ -95,13 +92,14 @@ class ImageWithThumbnailsField(ImageField):
attr_class = ImageWithThumbnailsFieldFile
descriptor_class = FallbackFieldDescriptor

def __init__(self, thumbnails=None, fallback_path=None, *args, **kwargs):
def __init__(self, thumbnails=None, fallback_path=None, *args, **kwargs):
super(ImageWithThumbnailsField, self).__init__(*args, **kwargs)

self.thumbnails = thumbnails or []
self.fallback_path = fallback_path

def get_thumbnail_filename(self, instance, original, key, ext):
# return filename
base, _ext = os.path.splitext(force_unicode(original))
return '%s-%s%s' % (base, key, ext)

Expand Down Expand Up @@ -138,11 +136,11 @@ def get_db_prep_value(self, value, connection, prepared=False):
return None

# we only want ImageFieldFile instances given to *this* field
if ((type(value) == ImageFieldFile) and
if ((type(value) == ImageFieldFile) and
(value.field == self) and
(hasattr(value, '_empty') and not value._empty)):
return unicode(value)

return None

def south_field_triple(self):
Expand Down
10 changes: 5 additions & 5 deletions undermythumb/files.py
@@ -1,7 +1,7 @@
from django.db.models.fields.files import ImageFieldFile


__all__ = ['ThumbnailFieldFile', 'ImageWithThumbnailsFieldFile']
__all__ = ('ThumbnailFieldFile', 'ImageWithThumbnailsFieldFile')


class ThumbnailSet(object):
Expand All @@ -25,18 +25,18 @@ def _populate(self):
ext = '.%s' % renderer.format

name = self.field.get_thumbnail_filename(
instance=self.instance,
instance=self.instance,
original=self.file,
key=key,
ext=ext)
key=key,
ext=ext)

thumbnail = ThumbnailFieldFile(
attname,
renderer,
self.instance,
self.field,
name)

self._cache[attname] = thumbnail

def clear_cache(self):
Expand Down
10 changes: 5 additions & 5 deletions undermythumb/management/commands/createthumbnails.py
Expand Up @@ -32,20 +32,20 @@ def handle(self, *args, **options):
if model is None:
raise ValueError
except (ValueError, AttributeError):
raise CommandError('Invalid content type %s' % content_type_path)
raise CommandError('Invalid content type %s' % content_type_path)

try:
field = model._meta.get_field_by_name(field_name)[0]
except FieldDoesNotExist:
raise CommandError('Invalid field name %s' % field_name)
raise CommandError('Invalid field name %s' % field_name)

if sizes is None:
raise CommandError('Must specify sizes, -s or --size')

thumbnails = [t[0] for t in field.thumbnails for s in sizes]
invalid_sizes = [s for s in sizes if s not in thumbnails]
if invalid_sizes:
raise CommandError('No thumbnails for sizes %r' % invalid_sizes)
raise CommandError('No thumbnails for sizes %r' % invalid_sizes)

objects = model._default_manager.only(field_name)
for obj in objects:
Expand All @@ -54,15 +54,15 @@ def handle(self, *args, **options):

self.stdout.write('Done.\n')


def create_thumbnails(self, field_instance, sizes):
thumbnails = [t for t in field_instance.thumbnails
if t.attname in sizes]
if t.attname in sizes]

if not thumbnails:
return

self.stdout.write('Reading %s ...\n' % field_instance.url)

try:
# Ignore bad files, this should log in the future.
content = ContentFile(field_instance.read())
Expand Down
26 changes: 11 additions & 15 deletions undermythumb/renderers.py
@@ -1,8 +1,4 @@
try:
from cStringIO import StringIO
except ImportError:
from StringIO import StringIO

from cStringIO import StringIO
import struct

from django.core.files.base import ContentFile
Expand All @@ -11,7 +7,7 @@


class BaseRenderer(object):
"""Base class for renderers.
"""Base class for renderers.
Subclass this to build your own renderers.
"""
Expand Down Expand Up @@ -57,7 +53,7 @@ def generate(self, content):
return self._create_content_file(rendered)

def _render(self, image):
"""Renders the image. Override this method when creating
"""Renders the image. Override this method when creating
a custom renderer.
"""

Expand Down Expand Up @@ -99,7 +95,7 @@ def __init__(self, width, height, constrain=True, upscale=False,
def _render(self, image):
dw, dh = self.width, self.height
sw, sh = image.size

if self.constrain:
sr = float(sw) / float(sh)
if sw > sh:
Expand All @@ -108,7 +104,7 @@ def _render(self, image):
dh = dw * sr

# resize if the source dimensions are smaller than the desired,
# or the user has approved scaling the image above
# or the user has approved scaling the image above
# it's original dimensions
if ((((dw > sw) or (dh > sh)) and self.upscale) or
((dw < sw) or (dh < sh))):
Expand All @@ -118,8 +114,8 @@ def _render(self, image):


class LetterboxRenderer(ResizeRenderer):
"""Resizes an image to a given width and height using the ``ResizeRenderer``,
and places it on a colored canvas.
"""Resizes an image to a given width and height using the
``ResizeRenderer``, and places it on a colored canvas.
Cavas color is defined as a hexidecimal color value.
Expand All @@ -128,9 +124,9 @@ class LetterboxRenderer(ResizeRenderer):
LetterboxRenderer(150, 150, bg_color='#000000')
"""
def __init__(self, width, height, bg_color='#FFFFFF', *args, **kwargs):
super(LetterboxRenderer, self).__init__(width, height, *args,

def __init__(self, width, height, bg_color='#FFFFFF', *args, **kwargs):
super(LetterboxRenderer, self).__init__(width, height, *args,
**kwargs)

# convert hex string to rgba quad
Expand All @@ -143,7 +139,7 @@ def _render(self, image):
src_w, src_h = image.size

# place image on canvas and save
canvas = Image.new('RGBA', (self.width, self.height), self.bg_color)
canvas = Image.new('RGBA', (self.width, self.height), self.bg_color)
paste_x = (self.width - src_w) / 2
paste_y = (self.height - src_h) / 2
canvas.paste(image, (paste_x, paste_y))
Expand Down
21 changes: 9 additions & 12 deletions undermythumb/tests/models.py
@@ -1,7 +1,3 @@
import os

from django.conf import settings
from django.core.files.storage import FileSystemStorage
from django.db import models

from undermythumb.fields import ImageWithThumbnailsField, ImageFallbackField
Expand All @@ -12,20 +8,21 @@ class BlogPost(models.Model):
title = models.CharField(max_length=100)

# an image with thumbnails
artwork = ImageWithThumbnailsField(max_length=255,
upload_to='artwork/',
thumbnails=(('homepage_image', CropRenderer(300, 150)),
('pagination_image', CropRenderer(150, 75))))
artwork = ImageWithThumbnailsField(
max_length=255,
upload_to='artwork/',
thumbnails=(('homepage_image', CropRenderer(300, 150)),
('pagination_image', CropRenderer(150, 75))))

# an override field, capable of rolling up a path
# when it has no value. useful for overriding
# when it has no value. useful for overriding
# auto-generated thumbnails. the fallback path
# should point to an ImageFieldFile of some sort.
#
# under the hood, this is just an ImageField
homepage_image = ImageFallbackField(fallback_path='artwork.thumbnails.homepage_image',
upload_to='artwork/')
homepage_image = ImageFallbackField(
fallback_path='artwork.thumbnails.homepage_image',
upload_to='artwork/')

def __unicode__(self):
return self.title

5 changes: 2 additions & 3 deletions undermythumb/tests/run_tests.py
Expand Up @@ -6,12 +6,11 @@

os.environ['DJANGO_SETTINGS_MODULE'] = 'undermythumb.tests.test_settings'

parent = os.path.realpath(os.path.join(os.path.dirname(__file__),
os.path.pardir,
parent = os.path.realpath(os.path.join(os.path.dirname(__file__),
os.path.pardir,
os.path.pardir))
sys.path.insert(0, parent)

from django.conf import settings
from django.test.simple import run_tests


Expand Down
2 changes: 1 addition & 1 deletion undermythumb/tests/storage.py
Expand Up @@ -5,7 +5,7 @@


class FileSystemOverwriteStorage(FileSystemStorage):

def get_available_name(self, name):
if self.exists(name):
os.remove(os.path.join(settings.MEDIA_ROOT, name))
Expand Down

0 comments on commit afa836e

Please sign in to comment.