PEP8 and Pylint cleanups

This commit is contained in:
Roberto Rosario
2011-04-07 17:21:15 -04:00
parent d54fd98ec5
commit 61fb876a81
15 changed files with 144 additions and 122 deletions

View File

@@ -2,9 +2,8 @@ import os
import subprocess
from django.utils.importlib import import_module
from django.core.exceptions import ImproperlyConfigured
from django.template.defaultfilters import slugify
from django.core.exceptions import ObjectDoesNotExist
from converter.conf.settings import UNPAPER_PATH
from converter.conf.settings import OCR_OPTIONS
@@ -24,17 +23,20 @@ QUALITY_DEFAULT = 'quality_default'
QUALITY_LOW = 'quality_low'
QUALITY_HIGH = 'quality_high'
QUALITY_SETTINGS = {QUALITY_DEFAULT:DEFAULT_OPTIONS, QUALITY_LOW:LOW_QUALITY_OPTIONS,
QUALITY_HIGH:HIGH_QUALITY_OPTIONS}
QUALITY_SETTINGS = {QUALITY_DEFAULT: DEFAULT_OPTIONS,
QUALITY_LOW: LOW_QUALITY_OPTIONS, QUALITY_HIGH: HIGH_QUALITY_OPTIONS}
def _lazy_load(fn):
_cached = []
def _decorated():
if not _cached:
_cached.append(fn())
return _cached[0]
return _decorated
@_lazy_load
def _get_backend():
return import_module(GRAPHICS_BACKEND)
@@ -44,6 +46,7 @@ try:
except ImportError:
raise ImportError('Missing or incorrect converter backend: %s' % GRAPHICS_BACKEND)
def cleanup(filename):
''' tries to remove the given filename. Ignores non-existent files '''
try:
@@ -51,6 +54,7 @@ def cleanup(filename):
except OSError:
pass
def execute_unpaper(input_filepath, output_filepath):
command = []
command.append(UNPAPER_PATH)
@@ -97,6 +101,7 @@ def create_image_cache_filename(input_filepath, *args, **kwargs):
else:
return None
def in_image_cache(input_filepath, size, page=0, format='jpg', quality=QUALITY_DEFAULT, extra_options=''):
output_filepath = create_image_cache_filename(input_filepath, size=size, page=page, format=format, quality=quality, extra_options=extra_options)
if os.path.exists(output_filepath):
@@ -104,6 +109,7 @@ def in_image_cache(input_filepath, size, page=0, format='jpg', quality=QUALITY_D
else:
return None
def convert(input_filepath, size, quality=QUALITY_DEFAULT, cache=True, page=0, format='jpg', extra_options='', mimetype=None, extension=None, cleanup_files=True):
unoconv_output = None
output_filepath = create_image_cache_filename(input_filepath, size=size, page=page, format=format, quality=quality, extra_options=extra_options)
@@ -132,6 +138,7 @@ def convert(input_filepath, size, quality=QUALITY_DEFAULT, cache=True, page=0, f
return output_filepath
def get_page_count(input_filepath):
try:
return int(backend.execute_identify(input_filepath, '-format %n'))
@@ -139,6 +146,7 @@ def get_page_count(input_filepath):
#TODO: send to other page number identifying program
return 1
def convert_document_for_ocr(document, page=0, format='tif'):
#Extract document file
input_filepath = document_save_to_temp_dir(document, document.uuid)

View File

@@ -7,6 +7,7 @@ from converter.exceptions import ConvertError, UnknownFormat, IdentifyError
CONVERTER_ERROR_STRING_NO_DECODER = 'No decode delegate for this image format'
def execute_identify(input_filepath, arguments):
command = []
command.append(GM_PATH)
@@ -20,6 +21,7 @@ def execute_identify(input_filepath, arguments):
raise IdentifyError(proc.stderr.readline())
return proc.stdout.read()
def execute_convert(input_filepath, output_filepath, quality=QUALITY_DEFAULT, arguments=None):
command = []
command.append(GM_PATH)

View File

@@ -9,6 +9,7 @@ from converter.exceptions import ConvertError, UnknownFormat, \
CONVERTER_ERROR_STRING_NO_DECODER = 'no decode delegate for this image format'
def execute_identify(input_filepath, arguments):
command = []
command.append(IM_IDENTIFY_PATH)
@@ -21,6 +22,7 @@ def execute_identify(input_filepath, arguments):
raise IdentifyError(proc.stderr.readline())
return proc.stdout.read()
def execute_convert(input_filepath, output_filepath, quality=QUALITY_DEFAULT, arguments=None):
command = []
command.append(IM_CONVERT_PATH)

View File

@@ -1,4 +1,6 @@
#http://stackoverflow.com/questions/123198/how-do-i-copy-a-file-in-python
def copyfile(source, dest, buffer_size=1024 * 1024):
"""
Copy a file from source to dest. source and dest

View File

@@ -38,7 +38,6 @@ def document_create_fs_links(document):
else:
raise OSError(_(u'Unable to create metadata indexing directory: %s') % exc)
next_available_filename(document, metadata_index, target_directory, slugify(document.file_filename), slugify(document.file_extension))
except NameError, exc:
warnings.append(_(u'Error in metadata indexing expression: %s') % exc)
@@ -50,6 +49,7 @@ def document_create_fs_links(document):
return warnings
def document_delete_fs_links(document):
if FILESERVING_ENABLE:
for document_metadata_index in document.documentmetadataindex_set.all():
@@ -88,7 +88,6 @@ def document_delete_fs_links(document):
except OSError, exc:
pass
#Remove the directory if it is empty
try:
os.removedirs(path)
@@ -118,7 +117,6 @@ def next_available_filename(document, metadata_index, path, filename, extension,
return next_available_filename(document, metadata_index, path, filename, extension, suffix)
except Exception, exc:
raise Exception(_(u'Unable to create symbolic link, filename clash: %(filepath)s; %(exc)s') % {'filepath': filepath, 'exc': exc})
else:
raise OSError(_(u'Unable to create symbolic link: %(filepath)s; %(exc)s') % {'filepath': filepath, 'exc': exc})
@@ -155,5 +153,5 @@ def do_recreate_all_links(raise_exception=True):
errors.append('%s: %s' % (document, e))
for warning in create_warnings:
warnings.append('%s: %s' % (document, e))
warnings.append('%s: %s' % (document, warning))
return errors, warnings

View File

@@ -1,3 +1,6 @@
'''Configuration options for the filesystem_serving app
'''
from django.conf import settings
from common.utils import proper_name

View File

@@ -1,5 +1,6 @@
from django import forms
from django.utils.translation import ugettext as _
from django.template.defaultfilters import capfirst
class MultiItemForm(forms.Form):

View File

@@ -34,15 +34,13 @@ def process_links(links, view_name, url):
active = True
active_item = item
items.append(
{
items.append({
'first': count == 0,
'active': active,
'url': item_view and reverse(item_view) or item_url or '#',
'text': unicode(item['text']),
'famfam': 'famfam' in item and item['famfam'],
}
)
})
return items, active_item
@@ -76,6 +74,7 @@ def main_navigation(parser, token):
#http://www.djangosnippets.org/snippets/1378/
__all__ = ('resolve_to_name',)
def _pattern_resolve_to_name(self, path):
match = self.regex.search(path)
if match:
@@ -88,6 +87,7 @@ def _pattern_resolve_to_name(self, path):
name = "%s.%s" % (self.callback.__module__, self.callback.func_name)
return name
def _resolver_resolve_to_name(self, path):
tried = []
match = self.regex.search(path)
@@ -109,13 +109,16 @@ def _resolver_resolve_to_name(self, path):
RegexURLPattern.resolve_to_name = _pattern_resolve_to_name
RegexURLResolver.resolve_to_name = _resolver_resolve_to_name
def resolve_to_name(path, urlconf=None):
return get_resolver(urlconf).resolve_to_name(path)
@register.filter
def resolve_url_name(value):
return resolve_to_name(value)
def resolve_arguments(context, src_args):
args = []
kwargs = {}
@@ -172,7 +175,7 @@ def resolve_links(context, links, current_view, current_path):
def _get_object_navigation_links(context, menu_name=None, links_dict=object_navigation):
current_path = Variable('request').resolve(context).META['PATH_INFO']
current_view = resolve_to_name(current_path)#.get_full_path())
current_view = resolve_to_name(current_path)
context_links = []
try:

View File

@@ -13,6 +13,7 @@ from permissions import PERMISSION_ROLE_VIEW, PERMISSION_ROLE_EDIT, \
from models import Permission, Role
def register_permissions(namespace, permissions):
if permissions:
for permission in permissions:
@@ -25,6 +26,7 @@ def register_permissions(namespace, permissions):
#Special case for ./manage.py syncdb
pass
#TODO: Handle anonymous users
def check_permissions(requester, namespace, permission_list):
if isinstance(requester, User):

View File

@@ -4,10 +4,12 @@ from django.core.files.storage import FileSystemStorage
from storage.conf.settings import FILESTORAGE_LOCATION
class FileBasedStorage(FileSystemStorage):
'''Simple wrapper for the stock Django FileSystemStorage class
'''
separator = os.path.sep
def __init__(self, *args, **kwargs):
super(FileBasedStorage, self).__init__(*args, **kwargs)
self.location = FILESTORAGE_LOCATION

View File

@@ -20,4 +20,3 @@ os.environ['CELERY_LOADER'] = 'django'
from django.core.handlers.wsgi import WSGIHandler
application = WSGIHandler()