diff 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
line wrap: on
line diff
--- a/decoupage/formatters.py	Tue Apr 16 00:45:31 2013 -0700
+++ b/decoupage/formatters.py	Fri Jul 05 11:32:59 2013 -0700
@@ -2,6 +2,7 @@
 
 import random
 import sys
+from datetime import datetime
 from fnmatch import fnmatch
 from pkg_resources import iter_entry_points
 
@@ -76,11 +77,12 @@
                         _files.append(f)
                         break
             else:
-                # use only files where the description is not None
-                if f['description'] is not None:
+                # use only files where the title or description is not None
+                if (f['description'] is not None) or (f.get('title') is not None):
                     _files.append(f)
         data['files'] = _files
 
+
 class Sort(FormatterBase):
     """
     determines how to sort the files in a directory;
@@ -200,6 +202,31 @@
                 f['title'] = f['description']
                 f['description'] = None
 
+class Datestamp(FormatterBase):
+    """
+    datestamps for modified times
+    """
+    # TODO:
+    # - currently we only do modified; TODO: created/modified/etc
+    # - use modified dateutil from bitsyblog;
+    # - e.g. javascript for things like e.g. "Yesterday"
+    key = 'modified'
+
+    def __init__(self, string):
+        FormatterBase.__init__(self, string)
+
+        # check formatting string now v later
+        datetime.now().strftime(self._string)
+
+    def __call__(self, request, data):
+        for f in data['files']:
+            _datetime = f.get(self.key)
+            try:
+                datestamp = _datetime.strftime(self._string)
+                f['datestamp'] = datestamp
+            except:
+                raise # TODO: handle more better
+
 class Links(FormatterBase):
     """
     allow list of links per item:
@@ -207,7 +234,7 @@
     """
 
     fatal = False
-    defaults = { 'separator': ';' }
+    defaults = {'separator': ';'}
 
 
     def __call__(self, request, data):
@@ -285,6 +312,7 @@
     def __call__(self, request, data):
         data['include'] = self.include
 
+###
 
 def formatters():
     formatters = {}