log_source.py
changeset 76 cc3ab2c39ded
parent 73 5a7188bf2894
child 77 4287fb77e312
--- a/log_source.py	Tue Feb 10 00:19:56 2009 +0200
+++ b/log_source.py	Tue Feb 10 01:02:26 2009 +0200
@@ -2,7 +2,7 @@
     A source of IRC log files
 """
 
-import datetime, calendar, itertools
+import datetime, calendar, itertools, functools
 import os, errno
 import pytz
 
@@ -20,18 +20,54 @@
     
     def get_date (self, dt) :
         """
-            Get logs for the given date (as a datetime)
+            Get logs for the given date (as a datetime).
         """
 
         abstract
     
+    def get_date_paged (self, dt, count, page=None) :
+        """
+            Get the logs for a given date (as a datetime), divided into pages of count each. If page is given, the time
+            portion of the dt is ignored, and the lines for the given page are returned. Otherwise, if page is None,
+            then the lines for the page containing the given timestamp is returned.
+
+            The return value is a (page, max, lines) tuple.
+        """
+        
+        # how to act?
+        if page :
+            # constant skip
+            skip = (page - 1) * count
+
+        else :
+            skip = None
+
+        # collect lines
+        lines = []
+
+        # iterate using get_date
+        for line in self.get_date(dt) :
+            # skip?
+            if skip :
+                skip -= 1
+                continue
+            
+            # store line
+            lines.append(line)
+
+            # count?
+            if len(lines) >= count :
+                break
+
+        return (page, 0, lines)
+
     def get_month_days (self, dt) :
         """
             Get a set of dates, telling which days in the given month (as a datetime) have logs available
         """
 
         abstract
-    
+ 
 class LogFile (object) :
     """
         A file containing LogEvents
@@ -69,7 +105,7 @@
     
     def read_full (self) :
         """
-            Reads all LogLines. The LogLines will have a valid offset
+            Reads all LogLines. The LogLines will have a valid offset.
         """
         
         # just use our __iter__
@@ -380,7 +416,7 @@
             # open both of them
             f_begin = self._get_logfile_date(d_begin)
             f_end = self._get_logfile_date(d_end)
-
+            
             # chain together the two sources
             return itertools.chain(
                 f_begin.read_from(dtz_begin),