Delete the session cookie on an empty session.
[mediagoblin.git] / mediagoblin / tools / staticdirect.py
1 # GNU MediaGoblin -- federated, autonomous media hosting
2 # Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS.
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
17 ####################################
18 # Staticdirect infrastructure.
19 # Borrowed largely from cc.engine
20 # by Chris Webber & Creative Commons
21 #
22 # This needs documentation!
23 ####################################
24
25 import logging
26
27 _log = logging.getLogger(__name__)
28
29
30 class StaticDirect(object):
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()])
51 self.cache = {}
52
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]
56
57 static_direction = self.cache.setdefault(
58 domain, {})[filepath] = self.get(filepath, domain)
59 return static_direction
60
61 def get(self, filepath, domain=None):
62 return '%s/%s' % (
63 self.domains[domain], filepath.lstrip('/'))