changeset 67:fdb77c57bd22

make sort formatter more extensible and add a random method
author Jeff Hammel <jhammel@mozilla.com>
date Fri, 18 Nov 2011 22:34:08 -0800
parents 5d34e9d14666
children 4094bee13154
files decoupage/formatters.py setup.py
diffstat 2 files changed, 21 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/decoupage/formatters.py
+++ b/decoupage/formatters.py
@@ -1,10 +1,11 @@
 #!/usr/bin/env python
 
+import random
 import sys
 from fnmatch import fnmatch
 from pkg_resources import iter_entry_points
 
 ### abstract base classes for formatters
 
 class FormatterBase(object):
     """
@@ -74,32 +75,45 @@ class All(object):
                         _files.append(f)
                         break
             else:
                 # use only files where the description is not None
                 if f['description'] is not None:
                     _files.append(f)
         data['files'] = _files
         
-class Sort(object):
+class Sort(FormatterBase):
     """
     determines how to sort the files in a directory; 
     right now only by case-insensitive alphabetically
     * reverse : reverse the order of the sorting
     """
+    defaults = {'order': 'name'}
+    
     def __init__(self, pattern):
-        self.args = [i.strip() for i in pattern.split(',')]
+        FormatterBase.__init__(self, pattern)
+        self.orders = {'name': self.name,
+                       'random': self.random,
+                       }
     
     def __call__(self, request, data):
 
-        data['files'] = sorted(data['files'], key=lambda x: x['name'].lower())
+        sort = self.orders.get(request.GET.get('order', self.order), self.name)
+        data['files'] = sort(data['files'])
 
         if 'reverse' in self.args:
             data['files'] = list(reversed(data['files']))
-    
+
+    def name(self, files):
+        return sorted(files, key=lambda x: x['name'].lower())
+
+    def random(self, files):
+        random.shuffle(files)
+        return files
+
 
 class Order(object):
     """
     put the files in a particular order
     """
     def __init__(self, pattern):
         if '=' in pattern:
             key, value = pattern.split('=', 1)
@@ -182,18 +196,18 @@ class Links(FormatterBase):
 
     def __call__(self, request, data):
         for f in data['files']:
             if f['description'] and self.separator in f['description']:
                 f['description'], links = f['description'].split(self.separator, 1)
                 links = links.split(self.separator)
                 assert min(['=' in link for link in links])
                 links = [ link.split('=', 1) for link in links ]
-                f['links'] = [ { 'text': text, 'link': link }
-                             for text, link in links ]
+                f['links'] = [{ 'text': text, 'link': link }
+                              for text, link in links]
 
 
 class Up(object):
     """
     provides an up link to the path above:
     /up = ..
     """
 
--- a/setup.py
+++ b/setup.py
@@ -1,17 +1,17 @@
 from setuptools import setup, find_packages
 
 # use README as long_description
 try:
     description = file("README.txt").read()
 except IOError:
     description = ''
 
-version = '0.10.2'
+version = '0.10.3'
 
 setup(name='decoupage',
       version=version,
       description="Decoupage is the art of decorating an object by gluing colored paper cutouts onto it in combination with special paint effects ... The software decoupage lets you stitch together index pages from filesystem content",
       long_description=description,
       classifiers=[], # Get strings from http://www.python.org/pypi?%3Aaction=list_classifiers
       author='Jeff Hammel',
       author_email='k0scist@gmail.com',