changeset 100:82ee67b95a0c

sort by index order now works
author Jeff Hammel <k0scist@gmail.com>
date Tue, 23 Aug 2016 19:30:03 -0700
parents 2c7c4fda1a14
children 747c7e337c56
files decoupage/web.py setup.py
diffstat 2 files changed, 26 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/decoupage/web.py
+++ b/decoupage/web.py
@@ -29,27 +29,38 @@ from paste.fileapp import FileApp
 from pkg_resources import iter_entry_points
 from pkg_resources import load_entry_point
 from pkg_resources import resource_filename
 from webob import Request, Response, exc
 
 transformers = transformers()
 string = (str, unicode)
 
+class FileSorter(object):
+    def __init__(self, *keys):
+        self.keys = keys
+    def __call__(self, item):
+        try:
+            index = self.keys.index(item['name'])
+        except ValueError:
+            index = len(self.keys)
+        return (index, item['name'].lower(), item['name'])
+
 
 class Decoupage(object):
 
     ### class level variables
     defaults = { 'auto_reload': False,
                  'configuration': None,
                  'directory': None, # directory to serve
                  'cascade': True, # whether to cascade configuration
                  'template': 'index.html', # XXX see below
                  'template_directories': '', # list of directories to look for templates
                  'charset': 'utf-8', # content encoding for index.html files; -> `Content-Type: text/html; charset=ISO-8859-1`
+                 'file_sorter': FileSorter
                  }
 
     def __init__(self, **kw):
 
         # set defaults from app configuration
         for key, default_value in self.defaults.items():
 
             value = kw.get(key, default_value)
@@ -242,16 +253,23 @@ class Decoupage(object):
             kw['charset'] = self.charset
 
         # return response
         return Response(content_type='text/html', body=res, **kw)
 
 
     ### internal methods
 
+    def file_sort_key(filename, keys):
+        try:
+            index = keys.index(filename)
+        except ValueError:
+            index = len(keys)
+        return (index, filename.lower(), filename)
+
     def filedata(self, path, directory, conf=None):
         conf = conf or OrderedDict()
         files = []
 
         # get data for files
         filenames = os.listdir(directory)
         for i in filenames:
             filepath = os.path.join(directory, i)
@@ -277,26 +295,31 @@ class Decoupage(object):
             data = {'path' : '%s/%s' % (path.rstrip('/'), i),
                     'name': i,
                     'modified': modified,
                     'type': filetype}
             if filetype == 'file':
                 data['size'] =  os.path.getsize(filepath)
             files.append(data)
 
-        # TODO: deal with other links in conf
         for i in conf:
 
             if i in filenames or i.startswith('/'):
                 continue
+            # TODO: deal with other links in conf;
+            # this actually doesn't work because the ':' is magical to .ini files
             if i.startswith('http://') or i.startswith('https://'):
                 files.append({'path': i,
                               'name': i,
                               'type': link})
 
+        # TODO: sort files
+        files = sorted(files, key=self.file_sorter(*conf.keys()))
+
+        # get the description
         for f in files:
             f['description'] = conf.get(f['name'], None)
 
         return files
 
     def conf(self, path, cascade=None):
         """returns configuration dictionary appropriate to a path"""
         if cascade is None:
--- a/setup.py
+++ b/setup.py
@@ -1,17 +1,17 @@
 from setuptools import setup
 
 # use README as long_description
 try:
     description = file("README.txt").read()
 except IOError:
     description = ''
 
-version = '0.13.3'
+version = '0.14.0'
 
 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',
@@ -21,17 +21,17 @@ setup(name='decoupage',
       include_package_data=True,
       zip_safe=False,
       install_requires=[
           # -*- Extra requirements: -*-
           'WebOb',
           'Paste',
           'PasteScript',
           'genshi',
-          'martINI>=0.4',
+          'martINI>=0.4.2',
           'contenttransformer>=0.3.3',
           'PyRSS2Gen',
          ],
       dependency_links=['http://www.dalkescientific.com/Python/PyRSS2Gen-1.0.0.tar.gz#egg=PyRSS2Gen'],
       entry_points="""
       [console_scripts]
       create-decoupage-index = decoupage.create_index:main
       decoupage-templates = decoupage.templates:main