45b1587adc3336d8af8b408c5d805671a8ec722d
1 from collections
import namedtuple
3 from pkg_resources
import resource_filename
8 from spline
.lib
import helpers
9 from spline
.lib
.plugin
import PluginBase
, PluginLink
, Priority
11 import splinext
.frontpage
.controllers
.frontpage
13 class FrontPageUpdate(object):
14 """Base class ('interface') for an updated thing that may appear on the
17 Subclasses should implement the `time` and `template` properties.
22 FrontPageRSS
= namedtuple('FrontPageRSS',
23 ['time', 'entry', 'template', 'category', 'content', 'icon'])
25 def rss_hook(limit
, url
, title
, icon
=None):
26 """Front page handler for news feeds."""
27 feed
= feedparser
.parse(url
)
30 for entry
in feed
.entries
:
31 # Try to find something to show! Default to the summary, if there is
32 # one, or try to generate one otherwise
34 if 'summary' in entry
:
35 content
= entry
.summary
36 elif 'content' in entry
:
37 content
= entry
.content
[0].value
39 content
= helpers
.literal(content
)
41 update
= FrontPageRSS(
42 time
= datetime
.datetime(*entry
.published_parsed
[:6]),
44 template
= '/front_page/rss.mako',
49 updates
.append(update
)
54 FrontPageGit
= namedtuple('FrontPageGit',
55 ['time', 'gitweb', 'log', 'tag', 'template', 'category', 'icon'])
56 FrontPageGitCommit
= namedtuple('FrontPageGitCommit',
57 ['hash', 'author', 'time', 'subject', 'repo'])
59 def git_hook(limit
, title
, gitweb
, repo_paths
, repo_names
,
60 tag_pattern
=None, icon
=None):
62 """Front page handler for repository history."""
63 # Repo stuff can be space-delimited lists...
64 repo_paths
= repo_paths
.split()
65 repo_names
= repo_names
.split()
67 # Fetch the main repo's git tags
70 '--git-dir=' + repo_paths
[0],
74 args
.append(tag_pattern
)
76 proc
= subprocess
.Popen(args
, stdout
=subprocess
.PIPE
)
77 git_output
, _
= proc
.communicate()
78 tags
= git_output
.strip().split('\n')
80 # Tags come out in alphabetical order, which means earliest first. Reverse
81 # it to make the slicing easier
83 # Only history from tag to tag is actually interesting, so get the most
84 # recent $limit tags but skip the earliest
85 interesting_tags
= tags
[:-1][:limit
]
88 for tag
, since_tag
in zip(interesting_tags
, tags
[1:]):
91 for repo_path
, repo_name
in zip(repo_paths
, repo_names
):
92 # Grab an easily-parsed history: fields delimited by nulls.
93 # Hash, author's name, commit timestamp, subject.
96 '--git-dir=' + repo_path
,
98 '--pretty=%h%x00%an%x00%at%x00%s',
99 "{0}..{1}".format(since_tag
, tag
),
101 proc
= subprocess
.Popen(git_log_args
, stdout
=subprocess
.PIPE
)
102 for line
in proc
.stdout
:
103 hash, author
, time
, subject
= line
.strip().split('\x00')
108 time
= datetime
.datetime
.fromtimestamp(int(time
)),
114 # LASTLY, get the date when this tag was actually created
118 '--format=%(taggerdate:raw)',
121 tag_timestamp
, _
= subprocess
.Popen(args
, stdout
=subprocess
.PIPE
) \
123 tag_unixtime
, tag_timezone
= tag_timestamp
.split(None, 1)
125 update
= FrontPageGit(
126 time
= datetime
.datetime
.fromtimestamp(int(tag_unixtime
)),
129 template
= '/front_page/git.mako',
134 updates
.append(update
)
139 def add_routes_hook(map, *args
, **kwargs
):
140 """Hook to inject some of our behavior into the routes configuration."""
141 map.connect('/', controller
='frontpage', action
='index')
144 class FrontPagePlugin(PluginBase
):
145 def controllers(self
):
147 frontpage
= splinext
.frontpage
.controllers
.frontpage
.FrontPageController
,
150 def template_dirs(self
):
152 (resource_filename(__name__
, 'templates'), Priority
.FIRST
)
157 ('routes_mapping', Priority
.NORMAL
, add_routes_hook
),
158 ('frontpage_updates_rss', Priority
.NORMAL
, rss_hook
),
159 ('frontpage_updates_git', Priority
.NORMAL
, git_hook
),