comparison decoupage/formatters.py @ 79:a8a74f6bcf93

add some dates and fix some bugs!
author Jeff Hammel <jhammel@mozilla.com>
date Fri, 05 Jul 2013 11:32:59 -0700
parents ebf3d3c39cb7
children b01b6f6efd4e
comparison
equal deleted inserted replaced
78:5330cd62e179 79:a8a74f6bcf93
1 #!/usr/bin/env python 1 #!/usr/bin/env python
2 2
3 import random 3 import random
4 import sys 4 import sys
5 from datetime import datetime
5 from fnmatch import fnmatch 6 from fnmatch import fnmatch
6 from pkg_resources import iter_entry_points 7 from pkg_resources import iter_entry_points
7 8
8 ### abstract base classes for formatters 9 ### abstract base classes for formatters
9 10
74 for pattern in self.match: 75 for pattern in self.match:
75 if fnmatch(f['name'], pattern): 76 if fnmatch(f['name'], pattern):
76 _files.append(f) 77 _files.append(f)
77 break 78 break
78 else: 79 else:
79 # use only files where the description is not None 80 # use only files where the title or description is not None
80 if f['description'] is not None: 81 if (f['description'] is not None) or (f.get('title') is not None):
81 _files.append(f) 82 _files.append(f)
82 data['files'] = _files 83 data['files'] = _files
84
83 85
84 class Sort(FormatterBase): 86 class Sort(FormatterBase):
85 """ 87 """
86 determines how to sort the files in a directory; 88 determines how to sort the files in a directory;
87 right now only by case-insensitive alphabetically 89 right now only by case-insensitive alphabetically
198 f['description'] = description 200 f['description'] = description
199 else: 201 else:
200 f['title'] = f['description'] 202 f['title'] = f['description']
201 f['description'] = None 203 f['description'] = None
202 204
205 class Datestamp(FormatterBase):
206 """
207 datestamps for modified times
208 """
209 # TODO:
210 # - currently we only do modified; TODO: created/modified/etc
211 # - use modified dateutil from bitsyblog;
212 # - e.g. javascript for things like e.g. "Yesterday"
213 key = 'modified'
214
215 def __init__(self, string):
216 FormatterBase.__init__(self, string)
217
218 # check formatting string now v later
219 datetime.now().strftime(self._string)
220
221 def __call__(self, request, data):
222 for f in data['files']:
223 _datetime = f.get(self.key)
224 try:
225 datestamp = _datetime.strftime(self._string)
226 f['datestamp'] = datestamp
227 except:
228 raise # TODO: handle more better
229
203 class Links(FormatterBase): 230 class Links(FormatterBase):
204 """ 231 """
205 allow list of links per item: 232 allow list of links per item:
206 foo.html = description of foo; [PDF]=foo.pdf; [TXT]=foo.txt 233 foo.html = description of foo; [PDF]=foo.pdf; [TXT]=foo.txt
207 """ 234 """
208 235
209 fatal = False 236 fatal = False
210 defaults = { 'separator': ';' } 237 defaults = {'separator': ';'}
211 238
212 239
213 def __call__(self, request, data): 240 def __call__(self, request, data):
214 for f in data['files']: 241 for f in data['files']:
215 if f['description'] and self.separator in f['description']: 242 if f['description'] and self.separator in f['description']:
283 def __init__(self, arg): 310 def __init__(self, arg):
284 self.include = arg 311 self.include = arg
285 def __call__(self, request, data): 312 def __call__(self, request, data):
286 data['include'] = self.include 313 data['include'] = self.include
287 314
315 ###
288 316
289 def formatters(): 317 def formatters():
290 formatters = {} 318 formatters = {}
291 for entry_point in iter_entry_points('decoupage.formatters'): 319 for entry_point in iter_entry_points('decoupage.formatters'):
292 try: 320 try: