cleanup
[mediagoblin.git] / mediagoblin / tools / staticdirect.py
CommitLineData
8e1e744d 1# GNU MediaGoblin -- federated, autonomous media hosting
cf29e8a8 2# Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS.
f75a49b2
CAW
3#
4# This program is free software: you can redistribute it and/or modify
5# it under the terms of the GNU Affero General Public License as published by
6# the Free Software Foundation, either version 3 of the License, or
7# (at your option) any later version.
8#
9# This program is distributed in the hope that it will be useful,
10# but WITHOUT ANY WARRANTY; without even the implied warranty of
11# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12# GNU Affero General Public License for more details.
13#
14# You should have received a copy of the GNU Affero General Public License
15# along with this program. If not, see <http://www.gnu.org/licenses/>.
16
f75a49b2
CAW
17####################################
18# Staticdirect infrastructure.
19# Borrowed largely from cc.engine
20# by Chris Webber & Creative Commons
285ffedd 21#
f75a49b2
CAW
22# This needs documentation!
23####################################
24
1b876ac2
E
25import logging
26
27_log = logging.getLogger(__name__)
f75a49b2 28
285ffedd 29
f75a49b2 30class StaticDirect(object):
5377114c
CAW
31 """
32 Direct to a static resource.
33
34 This StaticDirect class can take a series of "domains" to
35 staticdirect to. In general, you should supply a None domain, as
36 that's the "default" domain.
37
38 Things work like this:
39 >>> staticdirect = StaticDirect(
40 ... {None: "/static/",
41 ... "theme": "http://example.org/themestatic/"})
42 >>> staticdirect("css/monkeys.css")
43 "/static/css/monkeys.css"
44 >>> staticdirect("images/lollerskate.png", "theme")
45 "http://example.org/themestatic/images/lollerskate.png"
46 """
47 def __init__(self, domains):
48 self.domains = dict(
49 [(key, value.rstrip('/'))
50 for key, value in domains.iteritems()])
f75a49b2
CAW
51 self.cache = {}
52
5377114c
CAW
53 def __call__(self, filepath, domain=None):
54 if domain in self.cache and filepath in self.cache[domain]:
55 return self.cache[domain][filepath]
f75a49b2 56
5377114c
CAW
57 static_direction = self.cache.setdefault(
58 domain, {})[filepath] = self.get(filepath, domain)
f75a49b2 59 return static_direction
f75a49b2 60
5377114c 61 def get(self, filepath, domain=None):
f75a49b2 62 return '%s/%s' % (
5377114c 63 self.domains[domain], filepath.lstrip('/'))
d6d2c771
CAW
64
65
66class PluginStatic(object):
67 """Pass this into the ``'static_setup'`` hook to register your
68 plugin's static directory.
69
70 This has two mandatory attributes that you must pass in on class
71 init:
72 - name: this name will be both used for lookup in "urlgen" for
73 your plugin's static resources and for the subdirectory that
74 it'll be "mounted" to for serving via your web browser. It
75 *MUST* be unique. If writing a plugin bundled with MediaGoblin
76 please use the pattern 'coreplugin__foo' where 'foo' is your
77 plugin name. All external plugins should use their modulename,
78 so if your plugin is 'mg_bettertags' you should also call this
79 name 'mg_bettertags'.
80 - file_path: the directory your plugin's static resources are
81 located in. It's recommended that you use
82 pkg_resources.resource_filename() for this.
83
84 An example of using this::
85
86 from pkg_resources import resource_filename
87 from mediagoblin.tools.staticdirect import PluginStatic
88
89 hooks = {
90 'static_setup': lambda: PluginStatic(
91 'mg_bettertags',
92 resource_filename('mg_bettertags', 'static'))
93 }
94
95 """
96 def __init__(self, name, file_path):
97 self.name = name
98 self.file_path = file_path
99
100 def __call__(self):
101 return self