comparison decoupage/web.py @ 100:82ee67b95a0c

sort by index order now works
author Jeff Hammel <k0scist@gmail.com>
date Tue, 23 Aug 2016 19:30:03 -0700
parents abf23c2e4e82
children 747c7e337c56
comparison
equal deleted inserted replaced
99:2c7c4fda1a14 100:82ee67b95a0c
32 from webob import Request, Response, exc 32 from webob import Request, Response, exc
33 33
34 transformers = transformers() 34 transformers = transformers()
35 string = (str, unicode) 35 string = (str, unicode)
36 36
37 class FileSorter(object):
38 def __init__(self, *keys):
39 self.keys = keys
40 def __call__(self, item):
41 try:
42 index = self.keys.index(item['name'])
43 except ValueError:
44 index = len(self.keys)
45 return (index, item['name'].lower(), item['name'])
46
37 47
38 class Decoupage(object): 48 class Decoupage(object):
39 49
40 ### class level variables 50 ### class level variables
41 defaults = { 'auto_reload': False, 51 defaults = { 'auto_reload': False,
43 'directory': None, # directory to serve 53 'directory': None, # directory to serve
44 'cascade': True, # whether to cascade configuration 54 'cascade': True, # whether to cascade configuration
45 'template': 'index.html', # XXX see below 55 'template': 'index.html', # XXX see below
46 'template_directories': '', # list of directories to look for templates 56 'template_directories': '', # list of directories to look for templates
47 'charset': 'utf-8', # content encoding for index.html files; -> `Content-Type: text/html; charset=ISO-8859-1` 57 'charset': 'utf-8', # content encoding for index.html files; -> `Content-Type: text/html; charset=ISO-8859-1`
58 'file_sorter': FileSorter
48 } 59 }
49 60
50 def __init__(self, **kw): 61 def __init__(self, **kw):
51 62
52 # set defaults from app configuration 63 # set defaults from app configuration
245 return Response(content_type='text/html', body=res, **kw) 256 return Response(content_type='text/html', body=res, **kw)
246 257
247 258
248 ### internal methods 259 ### internal methods
249 260
261 def file_sort_key(filename, keys):
262 try:
263 index = keys.index(filename)
264 except ValueError:
265 index = len(keys)
266 return (index, filename.lower(), filename)
267
250 def filedata(self, path, directory, conf=None): 268 def filedata(self, path, directory, conf=None):
251 conf = conf or OrderedDict() 269 conf = conf or OrderedDict()
252 files = [] 270 files = []
253 271
254 # get data for files 272 # get data for files
280 'type': filetype} 298 'type': filetype}
281 if filetype == 'file': 299 if filetype == 'file':
282 data['size'] = os.path.getsize(filepath) 300 data['size'] = os.path.getsize(filepath)
283 files.append(data) 301 files.append(data)
284 302
285 # TODO: deal with other links in conf
286 for i in conf: 303 for i in conf:
287 304
288 if i in filenames or i.startswith('/'): 305 if i in filenames or i.startswith('/'):
289 continue 306 continue
307 # TODO: deal with other links in conf;
308 # this actually doesn't work because the ':' is magical to .ini files
290 if i.startswith('http://') or i.startswith('https://'): 309 if i.startswith('http://') or i.startswith('https://'):
291 files.append({'path': i, 310 files.append({'path': i,
292 'name': i, 311 'name': i,
293 'type': link}) 312 'type': link})
294 313
314 # TODO: sort files
315 files = sorted(files, key=self.file_sorter(*conf.keys()))
316
317 # get the description
295 for f in files: 318 for f in files:
296 f['description'] = conf.get(f['name'], None) 319 f['description'] = conf.get(f['name'], None)
297 320
298 return files 321 return files
299 322