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 import subprocess
from django.utils.importlib import import_module from django.utils.importlib import import_module
from django.core.exceptions import ImproperlyConfigured
from django.template.defaultfilters import slugify from django.template.defaultfilters import slugify
from django.core.exceptions import ObjectDoesNotExist
from converter.conf.settings import UNPAPER_PATH from converter.conf.settings import UNPAPER_PATH
from converter.conf.settings import OCR_OPTIONS from converter.conf.settings import OCR_OPTIONS
@@ -24,17 +23,20 @@ QUALITY_DEFAULT = 'quality_default'
QUALITY_LOW = 'quality_low' QUALITY_LOW = 'quality_low'
QUALITY_HIGH = 'quality_high' QUALITY_HIGH = 'quality_high'
QUALITY_SETTINGS = {QUALITY_DEFAULT:DEFAULT_OPTIONS, QUALITY_LOW:LOW_QUALITY_OPTIONS, QUALITY_SETTINGS = {QUALITY_DEFAULT: DEFAULT_OPTIONS,
QUALITY_HIGH:HIGH_QUALITY_OPTIONS} QUALITY_LOW: LOW_QUALITY_OPTIONS, QUALITY_HIGH: HIGH_QUALITY_OPTIONS}
def _lazy_load(fn): def _lazy_load(fn):
_cached = [] _cached = []
def _decorated(): def _decorated():
if not _cached: if not _cached:
_cached.append(fn()) _cached.append(fn())
return _cached[0] return _cached[0]
return _decorated return _decorated
@_lazy_load @_lazy_load
def _get_backend(): def _get_backend():
return import_module(GRAPHICS_BACKEND) return import_module(GRAPHICS_BACKEND)
@@ -44,6 +46,7 @@ try:
except ImportError: except ImportError:
raise ImportError('Missing or incorrect converter backend: %s' % GRAPHICS_BACKEND) raise ImportError('Missing or incorrect converter backend: %s' % GRAPHICS_BACKEND)
def cleanup(filename): def cleanup(filename):
''' tries to remove the given filename. Ignores non-existent files ''' ''' tries to remove the given filename. Ignores non-existent files '''
try: try:
@@ -51,6 +54,7 @@ def cleanup(filename):
except OSError: except OSError:
pass pass
def execute_unpaper(input_filepath, output_filepath): def execute_unpaper(input_filepath, output_filepath):
command = [] command = []
command.append(UNPAPER_PATH) command.append(UNPAPER_PATH)
@@ -97,6 +101,7 @@ def create_image_cache_filename(input_filepath, *args, **kwargs):
else: else:
return None return None
def in_image_cache(input_filepath, size, page=0, format='jpg', quality=QUALITY_DEFAULT, extra_options=''): 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) 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): 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: else:
return None 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): 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 unoconv_output = None
output_filepath = create_image_cache_filename(input_filepath, size=size, page=page, format=format, quality=quality, extra_options=extra_options) 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 return output_filepath
def get_page_count(input_filepath): def get_page_count(input_filepath):
try: try:
return int(backend.execute_identify(input_filepath, '-format %n')) 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 #TODO: send to other page number identifying program
return 1 return 1
def convert_document_for_ocr(document, page=0, format='tif'): def convert_document_for_ocr(document, page=0, format='tif'):
#Extract document file #Extract document file
input_filepath = document_save_to_temp_dir(document, document.uuid) input_filepath = document_save_to_temp_dir(document, document.uuid)
@@ -156,7 +164,7 @@ def convert_document_for_ocr(document, page=0, format='tif'):
transformation_list = [] transformation_list = []
try: try:
#Catch invalid or non existing pages #Catch invalid or non existing pages
document_page = document.documentpage_set.get(document=document, page_number=page+1) document_page = document.documentpage_set.get(document=document, page_number=page + 1)
for page_transformation in document_page.documentpagetransformation_set.all(): for page_transformation in document_page.documentpagetransformation_set.all():
if page_transformation.transformation in TRANFORMATION_CHOICES: if page_transformation.transformation in TRANFORMATION_CHOICES:
output = TRANFORMATION_CHOICES[page_transformation.transformation] % eval(page_transformation.arguments) output = TRANFORMATION_CHOICES[page_transformation.transformation] % eval(page_transformation.arguments)

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' CONVERTER_ERROR_STRING_NO_DECODER = 'No decode delegate for this image format'
def execute_identify(input_filepath, arguments): def execute_identify(input_filepath, arguments):
command = [] command = []
command.append(GM_PATH) command.append(GM_PATH)
@@ -20,6 +21,7 @@ def execute_identify(input_filepath, arguments):
raise IdentifyError(proc.stderr.readline()) raise IdentifyError(proc.stderr.readline())
return proc.stdout.read() return proc.stdout.read()
def execute_convert(input_filepath, output_filepath, quality=QUALITY_DEFAULT, arguments=None): def execute_convert(input_filepath, output_filepath, quality=QUALITY_DEFAULT, arguments=None):
command = [] command = []
command.append(GM_PATH) 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' CONVERTER_ERROR_STRING_NO_DECODER = 'no decode delegate for this image format'
def execute_identify(input_filepath, arguments): def execute_identify(input_filepath, arguments):
command = [] command = []
command.append(IM_IDENTIFY_PATH) command.append(IM_IDENTIFY_PATH)
@@ -21,6 +22,7 @@ def execute_identify(input_filepath, arguments):
raise IdentifyError(proc.stderr.readline()) raise IdentifyError(proc.stderr.readline())
return proc.stdout.read() return proc.stdout.read()
def execute_convert(input_filepath, output_filepath, quality=QUALITY_DEFAULT, arguments=None): def execute_convert(input_filepath, output_filepath, quality=QUALITY_DEFAULT, arguments=None):
command = [] command = []
command.append(IM_CONVERT_PATH) command.append(IM_CONVERT_PATH)

View File

@@ -15,9 +15,9 @@ HIGH_QUALITY_OPTIONS = getattr(settings, 'CONVERTER_HIGH_QUALITY_OPTIONS', u'-de
setting_description = { setting_description = {
'CONVERTER_IM_CONVERT_PATH':_(u'File path to imagemagick\'s convert program.'), 'CONVERTER_IM_CONVERT_PATH': _(u'File path to imagemagick\'s convert program.'),
'CONVERTER_IM_IDENTIFY_PATH':_(u'File path to imagemagick\'s identify program.'), 'CONVERTER_IM_IDENTIFY_PATH': _(u'File path to imagemagick\'s identify program.'),
'CONVERTER_GM_PATH':_(u'File path to graphicsmagick\'s program.'), 'CONVERTER_GM_PATH': _(u'File path to graphicsmagick\'s program.'),
'CONVERTER_UNPAPER_PATH':_(u'File path to unpaper program.'), 'CONVERTER_UNPAPER_PATH': _(u'File path to unpaper program.'),
'CONVERTER_GRAPHICS_BACKEND':_(u'Graphics conversion backend to use. Options are: converter.backends.imagemagick and converter.backends.graphicsmagick.'), 'CONVERTER_GRAPHICS_BACKEND': _(u'Graphics conversion backend to use. Options are: converter.backends.imagemagick and converter.backends.graphicsmagick.'),
} }

View File

@@ -1,5 +1,7 @@
#http://stackoverflow.com/questions/123198/how-do-i-copy-a-file-in-python #http://stackoverflow.com/questions/123198/how-do-i-copy-a-file-in-python
def copyfile(source, dest, buffer_size=1024*1024):
def copyfile(source, dest, buffer_size=1024 * 1024):
""" """
Copy a file from source to dest. source and dest Copy a file from source to dest. source and dest
can either be strings or any object with a read or can either be strings or any object with a read or

View File

@@ -14,7 +14,7 @@ from models import DocumentMetadataIndex, Document
if SLUGIFY_PATHS == False: if SLUGIFY_PATHS == False:
#Do not slugify path or filenames and extensions #Do not slugify path or filenames and extensions
slugify = lambda x:x slugify = lambda x: x
def document_create_fs_links(document): def document_create_fs_links(document):
@@ -22,7 +22,7 @@ def document_create_fs_links(document):
if FILESERVING_ENABLE: if FILESERVING_ENABLE:
if not document.exists(): if not document.exists():
raise Exception(_(u'Not creating metadata indexing, document not found in document storage')) raise Exception(_(u'Not creating metadata indexing, document not found in document storage'))
metadata_dict = {'document':document} metadata_dict = {'document': document}
metadata_dict.update(dict([(metadata.metadata_type.name, slugify(metadata.value)) for metadata in document.documentmetadata_set.all()])) metadata_dict.update(dict([(metadata.metadata_type.name, slugify(metadata.value)) for metadata in document.documentmetadata_set.all()]))
for metadata_index in document.document_type.metadataindex_set.all(): for metadata_index in document.document_type.metadataindex_set.all():
@@ -38,7 +38,6 @@ def document_create_fs_links(document):
else: else:
raise OSError(_(u'Unable to create metadata indexing directory: %s') % exc) 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)) next_available_filename(document, metadata_index, target_directory, slugify(document.file_filename), slugify(document.file_extension))
except NameError, exc: except NameError, exc:
warnings.append(_(u'Error in metadata indexing expression: %s') % exc) warnings.append(_(u'Error in metadata indexing expression: %s') % exc)
@@ -50,6 +49,7 @@ def document_create_fs_links(document):
return warnings return warnings
def document_delete_fs_links(document): def document_delete_fs_links(document):
if FILESERVING_ENABLE: if FILESERVING_ENABLE:
for document_metadata_index in document.documentmetadataindex_set.all(): for document_metadata_index in document.documentmetadataindex_set.all():
@@ -88,7 +88,6 @@ def document_delete_fs_links(document):
except OSError, exc: except OSError, exc:
pass pass
#Remove the directory if it is empty #Remove the directory if it is empty
try: try:
os.removedirs(path) os.removedirs(path)
@@ -117,16 +116,15 @@ def next_available_filename(document, metadata_index, path, filename, extension,
#Try again with same suffix #Try again with same suffix
return next_available_filename(document, metadata_index, path, filename, extension, suffix) return next_available_filename(document, metadata_index, path, filename, extension, suffix)
except Exception, exc: except Exception, exc:
raise Exception(_(u'Unable to create symbolic link, filename clash: %(filepath)s; %(exc)s') % {'filepath':filepath, 'exc':exc}) raise Exception(_(u'Unable to create symbolic link, filename clash: %(filepath)s; %(exc)s') % {'filepath': filepath, 'exc': exc})
else: else:
raise OSError(_(u'Unable to create symbolic link: %(filepath)s; %(exc)s') % {'filepath':filepath, 'exc':exc}) raise OSError(_(u'Unable to create symbolic link: %(filepath)s; %(exc)s') % {'filepath': filepath, 'exc': exc})
return filepath return filepath
else: else:
if suffix > MAX_RENAME_COUNT: if suffix > MAX_RENAME_COUNT:
raise Exception(_(u'Maximum rename count reached, not creating symbolic link')) raise Exception(_(u'Maximum rename count reached, not creating symbolic link'))
return next_available_filename(document, metadata_index, path, filename, extension, suffix+1) return next_available_filename(document, metadata_index, path, filename, extension, suffix + 1)
#TODO: diferentiate between evaluation error and filesystem errors #TODO: diferentiate between evaluation error and filesystem errors
@@ -155,5 +153,5 @@ def do_recreate_all_links(raise_exception=True):
errors.append('%s: %s' % (document, e)) errors.append('%s: %s' % (document, e))
for warning in create_warnings: for warning in create_warnings:
warnings.append('%s: %s' % (document, e)) warnings.append('%s: %s' % (document, warning))
return errors, warnings return errors, warnings

View File

@@ -1,9 +1,12 @@
'''Configuration options for the filesystem_serving app
'''
from django.conf import settings from django.conf import settings
from common.utils import proper_name from common.utils import proper_name
available_indexing_functions = { available_indexing_functions = {
'proper_name':proper_name 'proper_name': proper_name
} }

View File

@@ -20,9 +20,9 @@ def recreate_all_links(request):
if request.method != 'POST': if request.method != 'POST':
return render_to_response('generic_confirm.html', { return render_to_response('generic_confirm.html', {
'previous':previous, 'previous': previous,
'next':next, 'next': next,
'message':_(u'On large databases this operation may take some time to execute.'), 'message': _(u'On large databases this operation may take some time to execute.'),
}, context_instance=RequestContext(request)) }, context_instance=RequestContext(request))
else: else:
try: try:

View File

@@ -13,19 +13,19 @@ def register_multi_item_links(src, links, menu_name=None):
if one_src in object_navigation[menu_name]: if one_src in object_navigation[menu_name]:
multi_object_navigation[menu_name][one_src]['links'].extend(links) multi_object_navigation[menu_name][one_src]['links'].extend(links)
else: else:
multi_object_navigation[menu_name][one_src] = {'links':copy.copy(links)} multi_object_navigation[menu_name][one_src] = {'links': copy.copy(links)}
else: else:
if src in multi_object_navigation[menu_name]: if src in multi_object_navigation[menu_name]:
multi_object_navigation[menu_name][src]['links'].extend(links) multi_object_navigation[menu_name][src]['links'].extend(links)
else: else:
multi_object_navigation[menu_name][src] = {'links':links} multi_object_navigation[menu_name][src] = {'links': links}
else: else:
multi_object_navigation[menu_name] = {} multi_object_navigation[menu_name] = {}
if hasattr(src, '__iter__'): if hasattr(src, '__iter__'):
for one_src in src: for one_src in src:
multi_object_navigation[menu_name][one_src] = {'links':links} multi_object_navigation[menu_name][one_src] = {'links': links}
else: else:
multi_object_navigation[menu_name] = {src:{'links':links}} multi_object_navigation[menu_name] = {src: {'links': links}}
def register_links(src, links, menu_name=None): def register_links(src, links, menu_name=None):
@@ -35,26 +35,26 @@ def register_links(src, links, menu_name=None):
if one_src in object_navigation[menu_name]: if one_src in object_navigation[menu_name]:
object_navigation[menu_name][one_src]['links'].extend(links) object_navigation[menu_name][one_src]['links'].extend(links)
else: else:
object_navigation[menu_name][one_src] = {'links':copy.copy(links)} object_navigation[menu_name][one_src] = {'links': copy.copy(links)}
else: else:
if src in object_navigation[menu_name]: if src in object_navigation[menu_name]:
object_navigation[menu_name][src]['links'].extend(links) object_navigation[menu_name][src]['links'].extend(links)
else: else:
object_navigation[menu_name][src] = {'links':links} object_navigation[menu_name][src] = {'links': links}
else: else:
object_navigation[menu_name] = {} object_navigation[menu_name] = {}
if hasattr(src, '__iter__'): if hasattr(src, '__iter__'):
for one_src in src: for one_src in src:
object_navigation[menu_name][one_src] = {'links':links} object_navigation[menu_name][one_src] = {'links': links}
else: else:
object_navigation[menu_name] = {src:{'links':links}} object_navigation[menu_name] = {src: {'links': links}}
def register_menu(links): def register_menu(links):
for link in links: for link in links:
menu_links.append(link) menu_links.append(link)
menu_links.sort(lambda x, y: 1 if x>y else -1, lambda x:x['position'] if 'position' in x else 1) menu_links.sort(lambda x, y: 1 if x > y else -1, lambda x: x['position'] if 'position' in x else 1)
def register_model_list_columns(model, columns): def register_model_list_columns(model, columns):

View File

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

View File

@@ -34,15 +34,13 @@ def process_links(links, view_name, url):
active = True active = True
active_item = item active_item = item
items.append( items.append({
{ 'first': count == 0,
'first':count==0, 'active': active,
'active':active, 'url': item_view and reverse(item_view) or item_url or '#',
'url':item_view and reverse(item_view) or item_url or '#', 'text': unicode(item['text']),
'text':unicode(item['text']), 'famfam': 'famfam' in item and item['famfam'],
'famfam':'famfam' in item and item['famfam'], })
}
)
return items, active_item return items, active_item
@@ -76,6 +74,7 @@ def main_navigation(parser, token):
#http://www.djangosnippets.org/snippets/1378/ #http://www.djangosnippets.org/snippets/1378/
__all__ = ('resolve_to_name',) __all__ = ('resolve_to_name',)
def _pattern_resolve_to_name(self, path): def _pattern_resolve_to_name(self, path):
match = self.regex.search(path) match = self.regex.search(path)
if match: if match:
@@ -88,6 +87,7 @@ def _pattern_resolve_to_name(self, path):
name = "%s.%s" % (self.callback.__module__, self.callback.func_name) name = "%s.%s" % (self.callback.__module__, self.callback.func_name)
return name return name
def _resolver_resolve_to_name(self, path): def _resolver_resolve_to_name(self, path):
tried = [] tried = []
match = self.regex.search(path) match = self.regex.search(path)
@@ -109,13 +109,16 @@ def _resolver_resolve_to_name(self, path):
RegexURLPattern.resolve_to_name = _pattern_resolve_to_name RegexURLPattern.resolve_to_name = _pattern_resolve_to_name
RegexURLResolver.resolve_to_name = _resolver_resolve_to_name RegexURLResolver.resolve_to_name = _resolver_resolve_to_name
def resolve_to_name(path, urlconf=None): def resolve_to_name(path, urlconf=None):
return get_resolver(urlconf).resolve_to_name(path) return get_resolver(urlconf).resolve_to_name(path)
@register.filter @register.filter
def resolve_url_name(value): def resolve_url_name(value):
return resolve_to_name(value) return resolve_to_name(value)
def resolve_arguments(context, src_args): def resolve_arguments(context, src_args):
args = [] args = []
kwargs = {} 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): def _get_object_navigation_links(context, menu_name=None, links_dict=object_navigation):
current_path = Variable('request').resolve(context).META['PATH_INFO'] 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 = [] context_links = []
try: try:
@@ -240,9 +243,9 @@ def get_object_navigation_links(parser, token):
@register.inclusion_tag('generic_navigation.html', takes_context=True) @register.inclusion_tag('generic_navigation.html', takes_context=True)
def object_navigation_template(context): def object_navigation_template(context):
return { return {
'request':context['request'], 'request': context['request'],
'horizontal':True, 'horizontal': True,
'object_navigation_links':_get_object_navigation_links(context) 'object_navigation_links': _get_object_navigation_links(context)
} }
@@ -261,9 +264,9 @@ def get_multi_item_links(parser, token):
def get_multi_item_links_form(context): def get_multi_item_links_form(context):
new_context = copy.copy(context) new_context = copy.copy(context)
new_context.update({ new_context.update({
'form':MultiItemForm(actions=[(link['url'], link['text']) for link in _get_object_navigation_links(context, links_dict=multi_object_navigation)]), 'form': MultiItemForm(actions=[(link['url'], link['text']) for link in _get_object_navigation_links(context, links_dict=multi_object_navigation)]),
'title':_(u'Selected item actions:'), 'title': _(u'Selected item actions:'),
'form_action':reverse('multi_object_action_view'), 'form_action': reverse('multi_object_action_view'),
'submit_method':'get', 'submit_method': 'get',
}) })
return new_context return new_context

View File

@@ -13,6 +13,7 @@ from permissions import PERMISSION_ROLE_VIEW, PERMISSION_ROLE_EDIT, \
from models import Permission, Role from models import Permission, Role
def register_permissions(namespace, permissions): def register_permissions(namespace, permissions):
if permissions: if permissions:
for permission in permissions: for permission in permissions:
@@ -25,6 +26,7 @@ def register_permissions(namespace, permissions):
#Special case for ./manage.py syncdb #Special case for ./manage.py syncdb
pass pass
#TODO: Handle anonymous users #TODO: Handle anonymous users
def check_permissions(requester, namespace, permission_list): def check_permissions(requester, namespace, permission_list):
if isinstance(requester, User): if isinstance(requester, User):
@@ -71,10 +73,10 @@ def check_elements(requester, requester_list):
register_permissions('permissions', [ register_permissions('permissions', [
{'name':PERMISSION_ROLE_VIEW, 'label':_(u'View roles')}, {'name': PERMISSION_ROLE_VIEW, 'label':_(u'View roles')},
{'name':PERMISSION_ROLE_EDIT, 'label':_(u'Edit roles')}, {'name': PERMISSION_ROLE_EDIT, 'label':_(u'Edit roles')},
{'name':PERMISSION_ROLE_CREATE, 'label':_(u'Create roles')}, {'name': PERMISSION_ROLE_CREATE, 'label':_(u'Create roles')},
{'name':PERMISSION_ROLE_DELETE, 'label':_(u'Delete roles')}, {'name': PERMISSION_ROLE_DELETE, 'label':_(u'Delete roles')},
{'name':PERMISSION_PERMISSION_GRANT, 'label':_(u'Grant permissions')}, {'name': PERMISSION_PERMISSION_GRANT, 'label':_(u'Grant permissions')},
{'name':PERMISSION_PERMISSION_REVOKE, 'label':_(u'Revoke permissions')}, {'name': PERMISSION_PERMISSION_REVOKE, 'label':_(u'Revoke permissions')},
]) ])

View File

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

View File

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