1 from collections
import defaultdict
, namedtuple
2 from pkg_resources
import resource_filename
6 from pylons
import config
8 from spline
.lib
import helpers
9 from spline
.lib
.plugin
import PluginBase
, PluginLink
, Priority
10 from spline
.lib
.plugin
.load
import run_hooks
12 import splinext
.frontpage
.controllers
.frontpage
13 from splinext
.frontpage
.sources
import FeedSource
, GitSource
15 def add_routes_hook(map, *args
, **kwargs
):
16 """Hook to inject some of our behavior into the routes configuration."""
17 map.connect('/', controller
='frontpage', action
='index')
19 def load_sources_hook(config
, *args
, **kwargs
):
20 """Hook to load all the known sources and stuff them in config. Run once,
23 Frontpage hooks are also passed the `config` hash, as it's not available
26 # Extract source definitions from config and store as source_name => config
27 update_config
= defaultdict(dict)
29 '(?x) ^ spline-frontpage [.] sources [.] (\w+) (?: [.] (\w+) )? $')
30 for key
, val
in config
.iteritems():
31 # Match against spline-frontpage.source.(source).(key)
32 match
= key_rx
.match(key
)
36 source_name
, subkey
= match
.groups()
38 # This is the type declaration; use a special key
41 update_config
[source_name
][subkey
] = val
43 # Figure out the global limit and expiration time, with reasonable
44 # defaults. Make sure they're integers.
45 global_limit
= int(config
.get('spline-frontpage.limit', 10))
46 # max_age is optional and can be None
48 global_max_age
= int(config
['spline-frontpage.max_age'])
52 config
['spline-frontpage.limit'] = global_limit
53 config
['spline-frontpage.max_age'] = global_max_age
55 # Ask plugins to turn configuration into source objects
57 for source
, source_config
in update_config
.iteritems():
58 hook_name
= 'frontpage_updates_' + source_config
['__type__']
59 del source_config
['__type__'] # don't feed this to constructor!
61 # Default to global limit and max age. Source takes care of making
62 # integers and whatnot
63 source_config
.setdefault('limit', global_limit
)
64 source_config
.setdefault('max_age', global_max_age
)
66 # Hooks return a list of sources; combine with running list
67 sources
+= run_hooks(hook_name
, config
=config
, **source_config
)
69 # Save the list of sources, and done
70 config
['spline-frontpage.sources'] = sources
72 def source_cron_hook(*args
, **kwargs
):
73 """Hook to pass on cron tics to all sources, should they need it for e.g.
76 for source
in config
['spline-frontpage.sources']:
77 source
.do_cron(*args
, **kwargs
)
79 class FrontPagePlugin(PluginBase
):
80 def controllers(self
):
82 frontpage
= splinext
.frontpage
.controllers
.frontpage
.FrontPageController
,
85 def template_dirs(self
):
87 (resource_filename(__name__
, 'templates'), Priority
.FIRST
)
92 ('routes_mapping', Priority
.NORMAL
, add_routes_hook
),
93 ('after_setup', Priority
.NORMAL
, load_sources_hook
),
94 ('cron', Priority
.NORMAL
, source_cron_hook
),
95 ('frontpage_updates_rss', Priority
.NORMAL
, FeedSource
),
96 ('frontpage_updates_git', Priority
.NORMAL
, GitSource
),