implement prev/next_date in LogSource
authorTero Marttila <terom@fixme.fi>
Wed, 11 Feb 2009 22:24:55 +0200
changeset 111 95c0c49d76aa
parent 110 37e67ec434f3
child 112 090192b64d7e
implement prev/next_date in LogSource
handlers.py
log_source.py
templates/channel_date.tmpl
--- a/handlers.py	Wed Feb 11 22:01:53 2009 +0200
+++ b/handlers.py	Wed Feb 11 22:24:55 2009 +0200
@@ -201,7 +201,7 @@
     """
 
     # fix date timezone
-    date = date.replace(tzinfo=timezone)
+    date = timezone.localize(date)
 
     # get that day's events, either paged or not
     if page :
@@ -230,6 +230,10 @@
             count           = count,
             max             = max,
             lines           = lines,
+
+            # for prev/next date
+            date_next       = channel.source.get_next_date(date),
+            date_prev       = channel.source.get_prev_date(date),
         )
 
 @preferences.handler(prefs.formatter, prefs.count)
--- a/log_source.py	Wed Feb 11 22:01:53 2009 +0200
+++ b/log_source.py	Wed Feb 11 22:24:55 2009 +0200
@@ -8,6 +8,9 @@
 
 import config, utils
 
+# a timedelta that represents one day
+ONE_DAY = datetime.timedelta(days=1)
+
 class LogSourceDecoder (object) :
     """
         Handles decoding of LogSource lines
@@ -157,7 +160,7 @@
 
     def get_month_days (self, dt) :
         """
-            Return a sequence of dates, telling which days in the given month (as a datetime) have logs available
+            Return an ordered sequence of dates, telling which days in the given month (as a datetime) have logs available.
         """
 
         abstract
@@ -175,6 +178,20 @@
         """
 
         abstract
+    
+    def get_prev_date (self, dt) :
+        """
+            Get the next distinct date of logs available preceeding the given date, or None
+        """
+
+        abstract
+
+    def get_next_date (self, dt) :
+        """
+            Get the next distinct date of logs following the given date, or None.
+        """
+        
+        abstract
 
 class LogFile (object) :
     """
@@ -416,21 +433,21 @@
             else :
                 raise
     
-    def _iter_logfile_dates (self, after=None, until=None) :
+    def _iter_logfile_dates (self, after=None, until=None, reverse=False) :
         """
             Yields a series of naive datetime objects representing the logfiles that are available, in time order.
-
-            If after is given, only dates from said date onwards will be returned
-            If until is given, only dates up to and including said date will be returned
+            
+            Parameters :
+                after   only dates from said date onwards will be returned
+                until   only dates up to and including said date will be returned
+                reverse the dates are returned in reverse order instead. Note that the meaning of after/until doesn't change
         """
 
         # listdir
         filenames = os.listdir(self.path)
 
         # sort
-        filenames.sort()
-
-        
+        filenames.sort(reverse=reverse)
 
         # iter files
         for filename in filenames :
@@ -443,13 +460,16 @@
                 continue
 
             else :
-                if (not after or date >= after) and (not until or date <= until) :
+                if (after and date < after) or (until and date > until) :
+                    # ignore
+                    continue
+                
+                else :
+#                    print
+#                    print "iter_logfile_dates: after=%s, until=%s, reverse=%s -> %s" % (after, until, reverse, date)
+
                     # yield
                     yield date
-                
-                else :
-                    # ignore
-                    continue
             
     def _iter_date_reverse (self, dt=None) :
         """
@@ -465,9 +485,6 @@
             # convert to target timezone
             dtz = dt.astimezone(self.tz)
 
-        # our timedelta
-        ONE_DAY = datetime.timedelta(1)
-        
         # iterate unto infinity
         while True :
             # yield
@@ -526,7 +543,7 @@
 
     def get_latest (self, count) :
         """
-            Uses _iter_backwards + _get_logfile_date to read the yield the given lines from as many logfiles as needed
+            Uses _logfile_reverse to read the yield the given lines from as many logfiles as needed
         """
 
         # read the events into here
@@ -645,3 +662,27 @@
             for line in logfile.read_full() :
                 yield line
 
+    def get_prev_date (self, dt) :
+        """
+            Just use _iter_logfile_dates
+        """
+        
+        # use for to "iter" once
+        for log_date in self._iter_logfile_dates(until=dt - ONE_DAY, reverse=True) :
+            return log_date
+        
+        else :
+            return None
+
+    def get_next_date (self, dt) :
+        """
+            Just use _iter_logfile_dates
+        """
+        
+        # use for to "iter" once
+        for log_date in self._iter_logfile_dates(after=dt + ONE_DAY) :
+            return log_date
+        
+        else :
+            return None
+
--- a/templates/channel_date.tmpl	Wed Feb 11 22:01:53 2009 +0200
+++ b/templates/channel_date.tmpl	Wed Feb 11 22:24:55 2009 +0200
@@ -5,10 +5,14 @@
 <%def name="paginate_date()">
     <%call expr="paginate.paginate(urls.channel_date, count, page, max, channel=channel, date=date)">
         <%def name="left()">
-            <a href="${h.build_url(urls.channel_date, channel=channel, date=h.prev_date(date))}">&laquo; ${h.fmt_date(h.prev_date(date))}</a>
+        % if date_prev :
+            <a href="${h.build_url(urls.channel_date, channel=channel, date=date_prev)}">&laquo; ${h.fmt_date(date_prev)}</a>
+        % endif
         </%def>
         <%def name="right()">
-            <a href="${h.build_url(urls.channel_date, channel=channel, date=h.next_date(date))}">${h.fmt_date(h.next_date(date))} &raquo;</a>
+        % if date_next :
+            <a href="${h.build_url(urls.channel_date, channel=channel, date=date_next)}">${h.fmt_date(date_next)} &raquo;</a>
+        % endif
         </%def>
     </%call>
 </%def>