Don't raise when an RSS source is down.
[zzz-spline-frontpage.git] / splinext / frontpage / sources.py
index e28dc38..c4c2c54 100644 (file)
@@ -4,6 +4,7 @@ implementations.
 
 from collections import namedtuple
 import datetime
+import re
 import subprocess
 from subprocess import PIPE
 from urllib2 import URLError
@@ -55,7 +56,7 @@ class Source(object):
     The template will be passed one parameter: the update object, ``update``.
     """
 
-    def __init__(self, title, icon, link, limit=None, max_age=None):
+    def __init__(self, config, title, icon, link, limit=None, max_age=None):
         self.title = title
         self.icon = icon
         self.link = link
@@ -96,6 +97,8 @@ class CachedSource(Source):
     and the results are cached.  ``poll`` then returns the contents of the
     cache.
 
+    ``_poll`` may return None, in which case the cache will be left unchanged.
+
     You must define a ``_cache_key`` method that returns a key uniquely
     identifying this object.  Your key will be combined with the class name, so
     it only needs to be unique for that source, not globally.
@@ -120,13 +123,9 @@ class CachedSource(Source):
             # Too early!
             return
 
-        try:
-            updates = self._poll(self.limit, self.max_age)
+        updates = self._poll(self.limit, self.max_age)
+        if updates is not None:
             cache.get_cache('spline-frontpage')[self.cache_key()] = updates
-        except Exception:
-            # Hmm, polling broke.  Be conservative and don't do anything; old
-            # data is probably still OK for now
-            pass
 
         return
 
@@ -170,7 +169,7 @@ class FeedSource(CachedSource):
         if feed.bozo and isinstance(feed.bozo_exception, URLError):
             # Feed is DOWN.  Bail here; otherwise, old entries might be lost
             # just because, say, Bulbanews is down yet again
-            raise feed.bozo_exception
+            return None
 
         if not self.title:
             self.title = feed.feed.title
@@ -197,6 +196,11 @@ class FeedSource(CachedSource):
                 # If there be a summary, cheerfully trust that it's actually a
                 # summary
                 content = entry.summary
+            elif 'content' in entry and \
+                len(entry.content[0].value) <= self.SUMMARY_LENGTH:
+
+                # Full content is short; use as-is!
+                content = entry.content[0].value
             elif 'content' in entry:
                 # Full content is way too much, especially for my giant blog posts.
                 # Cut this down to some arbitrary number of characters, then feed
@@ -240,7 +244,7 @@ class FeedSource(CachedSource):
 
 FrontPageGit = namedtuple('FrontPageGit', ['source', 'time', 'log', 'tag'])
 FrontPageGitCommit = namedtuple('FrontPageGitCommit',
-    ['hash', 'author', 'time', 'subject', 'repo'])
+    ['hash', 'author', 'email', 'time', 'subject', 'repo'])
 
 class GitSource(CachedSource):
     """Represents a git repository.
@@ -265,13 +269,20 @@ class GitSource(CachedSource):
         Base URL to a gitweb installation, so commit ids can be linked to the
         commit proper.
 
+    ``bug_tracker``
+        URL to a bug tracker; anything matching "#xxx" will be converted into a
+        link to this.  Should contain a "{0}", which will be replaced by the
+        bug number.
+
     ``tag_pattern``
         Optional.  A shell glob pattern used to filter the tags.
     """
 
     template = '/front_page/git.mako'
 
-    def __init__(self, repo_paths, repo_names, gitweb, tag_pattern=None, **kwargs):
+    def __init__(self, repo_paths, repo_names, gitweb, bug_tracker=None,
+        tag_pattern=None, **kwargs):
+
         kwargs.setdefault('title', None)
         super(GitSource, self).__init__(**kwargs)
 
@@ -280,6 +291,7 @@ class GitSource(CachedSource):
         self.repo_names = repo_names.split()
 
         self.gitweb = gitweb
+        self.bug_tracker = bug_tracker
         self.tag_pattern = tag_pattern
 
     def _cache_key(self):
@@ -333,16 +345,25 @@ class GitSource(CachedSource):
                     'git',
                     '--git-dir=' + repo_path,
                     'log',
-                    '--pretty=%h%x00%an%x00%at%x00%s',
+                    '--pretty=%h%x00%an%x00%aE%x00%at%x00%s',
                     "{0}..{1}".format(since_tag, tag),
                 ]
                 proc = subprocess.Popen(git_log_args, stdout=PIPE)
                 for line in proc.stdout:
-                    hash, author, time, subject = line.strip().split('\x00')
+                    hash, author, email, time, subject \
+                        = line.strip().decode('utf8').split('\x00')
+
+                    # Convert bug numbers in subject to URLs
+                    if self.bug_tracker:
+                        subject = helpers.literal(
+                            re.sub(u'#(\d+)', self._linkify_bug_number, subject)
+                        )
+
                     commits.append(
                         FrontPageGitCommit(
                             hash = hash,
                             author = author,
+                            email = email,
                             time = datetime.datetime.fromtimestamp(int(time)),
                             subject = subject,
                             repo = repo_name,
@@ -358,3 +379,10 @@ class GitSource(CachedSource):
             updates.append(update)
 
         return updates
+
+    def _linkify_bug_number(self, match):
+        """Regex replace function for changing bug numbers into links."""
+        n = match.group(1)
+        bug_url = self.bug_tracker.format(match.group(1))
+        return helpers.literal(
+            u"""<a href="{0}">{1}</a>""".format(bug_url, match.group(0)))