1 # GNU MediaGoblin -- federated, autonomous media hosting
2 # Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS.
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.
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.
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/>.
17 ####################################
18 # Staticdirect infrastructure.
19 # Borrowed largely from cc.engine
20 # by Chris Webber & Creative Commons
22 # This needs documentation!
23 ####################################
27 _log
= logging
.getLogger(__name__
)
30 class StaticDirect(object):
32 Direct to a static resource.
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.
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"
47 def __init__(self
, domains
):
49 [(key
, value
.rstrip('/'))
50 for key
, value
in domains
.iteritems()])
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
]
57 static_direction
= self
.cache
.setdefault(
58 domain
, {})[filepath
] = self
.get(filepath
, domain
)
59 return static_direction
61 def get(self
, filepath
, domain
=None):
63 self
.domains
[domain
], filepath
.lstrip('/'))