implement line-links as UTC timestamps
authorTero Marttila <terom@fixme.fi>
Mon, 09 Feb 2009 22:17:10 +0200
changeset 72 5ade0288f2ec
parent 71 e909bde831e7
child 73 5a7188bf2894
implement line-links as UTC timestamps
handlers.py
helpers.py
log_formatter.py
log_source.py
static/irclogs.css
templates/lines.tmpl
urls.py
utils.py
--- a/handlers.py	Mon Feb 09 14:07:19 2009 +0200
+++ b/handlers.py	Mon Feb 09 22:17:10 2009 +0200
@@ -85,6 +85,30 @@
         lines           = lines,
     )
 
+@preferences.handler(prefs.formatter, prefs.timezone)
+def channel_link (request, channel, timestamp, formatter, timezone) :
+    """
+        Display channel_date for specific UTC timestamp
+    """
+
+    # convert timestamp to user's timezone
+    timestamp = timestamp.astimezone(timezone)
+
+    # get latest events
+    lines = channel.source.get_date(timestamp)
+
+    # lines
+    lines = formatter.format_html(lines)
+
+    # render
+    return templates.render_to_response("channel_date",
+        req             = request,
+        prefs           = request.prefs,
+        channel         = channel,
+        date            = timestamp,
+        lines           = lines,
+    )
+
 @preferences.handler(prefs.timezone)
 def channel_calendar (request, channel, year, month, timezone) :
     """
--- a/helpers.py	Mon Feb 09 14:07:19 2009 +0200
+++ b/helpers.py	Mon Feb 09 22:17:10 2009 +0200
@@ -6,7 +6,7 @@
 
 import qmsk.web.helpers
 
-import preferences
+import preferences, urls
 
 class Helpers (qmsk.web.helpers.Helpers) :
     """
@@ -144,4 +144,11 @@
         """
 
         return url.build(self.ctx['req'], **params)
+    
+    def utc_timestamp (self, dtz) :
+        """
+            Build an UTC timestamp from the given datetime
+        """
 
+        return urls.types['ts'].build(dtz)
+
--- a/log_formatter.py	Mon Feb 09 14:07:19 2009 +0200
+++ b/log_formatter.py	Mon Feb 09 22:17:10 2009 +0200
@@ -11,9 +11,16 @@
         Provides a method to format series of LogLines into various output formats, with varying themes.
     """
 
-    # the formatter's code name
+    # machine-readable name
     name = None
-    
+
+    # human-readable name
+    title = None
+
+    ## parameters
+    # use a fixed-width font for HTML output
+    html_fixedwidth = True
+
     def __init__ (self, tz, timestamp_fmt="%H:%M:%S") :
         """
             Initialize to format timestamps with the given timezone and timestamp
@@ -108,37 +115,35 @@
         # ...handle each line
         for line in lines :
             # using __TYPES
-            yield self._format_line_text(line, self.__FMT, full_timestamps)
+            yield line, self._format_line_text(line, self.__FMT, full_timestamps)
 
 class IrssiFormatter (IrssiTextFormatter, BaseHTMLFormatter) :
     """
         Implements plain black-and-white irssi-style formatting
     """
-
+    
+    # name
     name = 'irssi'
     title = "Irssi (plain)"
 
+    # parameters
+    html_fixedwidth = True
+
     def format_html (self, lines, full_timestamps=False) :
         """
-            Just uses format_txt, but wraps in <pre></pre>
+            Just uses format_txt, but processes links, etc
         """
         
-        # open pre
-        yield "<pre>"
-        
         # format using IrssiTextFormatter
-        for line in self.format_txt(lines, full_timestamps) :
+        for line, txt in self.format_txt(lines, full_timestamps) :
             # escape HTML
-            line = xml.sax.saxutils.escape(line)
+            html = xml.sax.saxutils.escape(txt)
 
             # process links
-            line = self._process_links(line)
+            html = self._process_links(html)
 
             # yield
-            yield line
-
-        # close pre
-        yield "</pre>"
+            yield line, html
 
 # define formatters by name
 FORMATTERS = {
--- a/log_source.py	Mon Feb 09 14:07:19 2009 +0200
+++ b/log_source.py	Mon Feb 09 22:17:10 2009 +0200
@@ -358,6 +358,11 @@
         # as dates
         d_begin = dtz_begin.date() 
         d_end = dtz_end.date()
+        
+#        print
+#        print "LogDirectory.get_date - %s" % dt
+#        print "\t   %s %s" % (d_begin, dtz_begin)
+#        print "\t-> %s %s" % (d_end, dtz_end)
 
         # if they're the same, just pull the full log for that date
         if d_begin == d_end :
--- a/static/irclogs.css	Mon Feb 09 14:07:19 2009 +0200
+++ b/static/irclogs.css	Mon Feb 09 22:17:10 2009 +0200
@@ -296,3 +296,14 @@
 div#lines a {
     color: black;
 }
+
+div#lines a[id] {
+    color: #ffffff;
+    text-decoration: none;
+}
+
+div#lines a[id]:hover,
+div#lines a[id]:target {
+    color: #000000;
+}
+
--- a/templates/lines.tmpl	Mon Feb 09 14:07:19 2009 +0200
+++ b/templates/lines.tmpl	Mon Feb 09 22:17:10 2009 +0200
@@ -1,5 +1,13 @@
+<% formatter = prefs['formatter'] %>
+
 <div id="lines">
-% for index, line in enumerate(lines):
-${line}
+% if formatter.html_fixedwidth :
+<pre>
+% endif
+% for line, html in lines:
+<a href="${h.build_url(urls.channel_link, channel=channel, timestamp=line.timestamp)}#${h.utc_timestamp(line.timestamp)}" id="${h.utc_timestamp(line.timestamp)}">&raquo;&raquo; </a>${html}
 % endfor
+% if formatter.html_fixedwidth :
+</pre>
+% endif
 </div>
--- a/urls.py	Mon Feb 09 14:07:19 2009 +0200
+++ b/urls.py	Mon Feb 09 22:17:10 2009 +0200
@@ -15,16 +15,20 @@
 # for configuration
 import channels
 
+# our URLTypes
+types   = dict(
+    # LogChannel
+    cid     = utils.URLChannelName(channels.channel_list.dict()),
+
+    # datetime
+    date    = utils.URLDateType('%Y-%m-%d'),
+
+    # UTC timestamp
+    ts      = utils.URLTimestampType(),
+)
+
 # our URLConfig
-urls = url = urltree.URLConfig(
-    type_dict   = dict(
-        # LogChannel
-        cid     = utils.URLChannelName(channels.channel_list.dict()),
-
-        # datetime
-        date    = utils.URLDateType('%Y-%m-%d'),
-    )
-)
+urls = url = urltree.URLConfig(type_dict=types)
 
 # urls
 index               = url('/',                                                              handlers.index                              )
@@ -32,6 +36,7 @@
 channel_select      = url('/channel_select/?channel:cid',                                   handlers.channel_select                     )
 channel             = url('/channels/{channel:cid}',                                        handlers.channel_last,      count=20        )
 channel_last        = url('/channels/{channel:cid}/last/{count:int=100}',                   handlers.channel_last                       )
+channel_link        = url('/channels/{channel:cid}/link/{timestamp:ts}',                    handlers.channel_link                       )
 channel_calendar    = url('/channels/{channel:cid}/calendar/{year:int=0}/{month:int=0}',    handlers.channel_calendar                   )
 channel_date        = url('/channels/{channel:cid}/date/{date:date}',                       handlers.channel_date                       )
 channel_search      = url('/channels/{channel:cid}/search/?q=&count:int=&skip:int=',        handlers.channel_search                     )
--- a/utils.py	Mon Feb 09 14:07:19 2009 +0200
+++ b/utils.py	Mon Feb 09 22:17:10 2009 +0200
@@ -2,7 +2,7 @@
     Miscellaneous things
 """
 
-import datetime
+import datetime, calendar, pytz
 
 from qmsk.web.urltree import URLType
 
@@ -58,3 +58,22 @@
 
         return date.strftime(self.date_fmt)
 
+class URLTimestampType (URLType) :
+    """
+        Handles an integer UNIX timestamp as an UTC datetime
+    """
+
+    def parse (self, timestamp_str) :
+        """
+            timestamp_str -> pytz.utc datetime.datetime
+        """
+
+        return datetime.datetime.utcfromtimestamp(int(timestamp_str)).replace(tzinfo=pytz.utc)
+    
+    def build (self, dtz) :
+        """
+            pytz.utc datetime.datetime -> timestamp_str
+        """
+
+        return str(calendar.timegm(dtz.utctimetuple()))
+