Add image URL's (thumb & full)
[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
b33feb18
CAW
38 Things work like this::
39
5377114c
CAW
40 >>> staticdirect = StaticDirect(
41 ... {None: "/static/",
42 ... "theme": "http://example.org/themestatic/"})
43 >>> staticdirect("css/monkeys.css")
44 "/static/css/monkeys.css"
45 >>> staticdirect("images/lollerskate.png", "theme")
46 "http://example.org/themestatic/images/lollerskate.png"
47 """
48 def __init__(self, domains):
49 self.domains = dict(
50 [(key, value.rstrip('/'))
51 for key, value in domains.iteritems()])
f75a49b2
CAW
52 self.cache = {}
53
5377114c
CAW
54 def __call__(self, filepath, domain=None):
55 if domain in self.cache and filepath in self.cache[domain]:
56 return self.cache[domain][filepath]
f75a49b2 57
5377114c
CAW
58 static_direction = self.cache.setdefault(
59 domain, {})[filepath] = self.get(filepath, domain)
f75a49b2 60 return static_direction
f75a49b2 61
5377114c 62 def get(self, filepath, domain=None):
f75a49b2 63 return '%s/%s' % (
5377114c 64 self.domains[domain], filepath.lstrip('/'))
d6d2c771
CAW
65
66
67class PluginStatic(object):
68 """Pass this into the ``'static_setup'`` hook to register your
69 plugin's static directory.
70
71 This has two mandatory attributes that you must pass in on class
72 init:
b33feb18
CAW
73
74 - *name:* this name will be both used for lookup in "urlgen" for
75 your plugin's static resources and for the subdirectory that
76 it'll be "mounted" to for serving via your web browser. It
77 *MUST* be unique. If writing a plugin bundled with MediaGoblin
78 please use the pattern 'coreplugin__foo' where 'foo' is your
79 plugin name. All external plugins should use their modulename,
80 so if your plugin is 'mg_bettertags' you should also call this
81 name 'mg_bettertags'.
82 - *file_path:* the directory your plugin's static resources are
83 located in. It's recommended that you use
84 pkg_resources.resource_filename() for this.
d6d2c771
CAW
85
86 An example of using this::
87
88 from pkg_resources import resource_filename
89 from mediagoblin.tools.staticdirect import PluginStatic
90
91 hooks = {
92 'static_setup': lambda: PluginStatic(
93 'mg_bettertags',
94 resource_filename('mg_bettertags', 'static'))
95 }
96
97 """
98 def __init__(self, name, file_path):
99 self.name = name
100 self.file_path = file_path
101
102 def __call__(self):
103 return self