e113e74c2e84cc406b55368644ef5e17b4e12b21
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(*args
, **kwargs
):
20 """Hook to load all the known sources and stuff them in config. Run once,
23 # Extract source definitions from config and store as source_name => config
24 update_config
= defaultdict(dict)
26 '(?x) ^ spline-frontpage [.] sources [.] (\w+) (?: [.] (\w+) )? $')
27 for key
, val
in config
.iteritems():
28 # Match against spline-frontpage.source.(source).(key)
29 match
= key_rx
.match(key
)
33 source_name
, subkey
= match
.groups()
35 # This is the type declaration; use a special key
38 update_config
[source_name
][subkey
] = val
40 # Figure out the global limit and expiration time, with reasonable
41 # defaults. Make sure they're integers.
42 global_limit
= int(config
.get('spline-frontpage.limit', 10))
43 # max_age is optional and can be None
45 global_max_age
= int(config
['spline-frontpage.max_age'])
49 config
['spline-frontpage.limit'] = global_limit
50 config
['spline-frontpage.max_age'] = global_max_age
52 # Ask plugins to turn configuration into source objects
54 for source
, source_config
in update_config
.iteritems():
55 hook_name
= 'frontpage_updates_' + source_config
['__type__']
56 del source_config
['__type__'] # don't feed this to constructor!
58 # Default to global limit and max age. Source takes care of making
59 # integers and whatnot
60 source_config
.setdefault('limit', global_limit
)
61 source_config
.setdefault('max_age', global_max_age
)
63 # Hooks return a list of sources; combine with running list
64 sources
+= run_hooks(hook_name
, **source_config
)
66 # Save the list of sources, and done
67 config
['spline-frontpage.sources'] = sources
70 class FrontPagePlugin(PluginBase
):
71 def controllers(self
):
73 frontpage
= splinext
.frontpage
.controllers
.frontpage
.FrontPageController
,
76 def template_dirs(self
):
78 (resource_filename(__name__
, 'templates'), Priority
.FIRST
)
83 ('routes_mapping', Priority
.NORMAL
, add_routes_hook
),
84 ('after_setup', Priority
.NORMAL
, load_sources_hook
),
85 ('frontpage_updates_rss', Priority
.NORMAL
, FeedSource
),
86 ('frontpage_updates_git', Priority
.NORMAL
, GitSource
),