Remove <br/> from safe string indicating that CSRF cooking is missing.
[larjonas-mediagoblin.git] / mediagoblin / tools / theme.py
blob97b041a61a5cb2cb769dcbc9c21b51e591ddb7d6
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 """
21 import pkg_resources
22 import os
24 from configobj import ConfigObj
27 BUILTIN_THEME_DIR = pkg_resources.resource_filename('mediagoblin', 'themes')
30 def themedata_for_theme_dir(name, theme_dir):
31 """
32 Given a theme directory, extract important theme information.
33 """
34 # open config
35 config = ConfigObj(os.path.join(theme_dir, 'theme.ini')).get('theme', {})
37 templates_dir = os.path.join(theme_dir, 'templates')
38 if not os.path.exists(templates_dir):
39 templates_dir = None
41 assets_dir = os.path.join(theme_dir, 'assets')
42 if not os.path.exists(assets_dir):
43 assets_dir = None
45 themedata = {
46 'name': config.get('name', name),
47 'description': config.get('description'),
48 'licensing': config.get('licensing'),
49 'dir': theme_dir,
50 'templates_dir': templates_dir,
51 'assets_dir': assets_dir,
52 'config': config}
54 return themedata
57 def register_themes(app_config, builtin_dir=BUILTIN_THEME_DIR):
58 """
59 Register all themes relevant to this application.
60 """
61 registry = {}
63 def _install_themes_in_dir(directory):
64 for themedir in os.listdir(directory):
65 abs_themedir = os.path.join(directory, themedir)
66 if not os.path.isdir(abs_themedir):
67 continue
69 themedata = themedata_for_theme_dir(themedir, abs_themedir)
70 registry[themedir] = themedata
72 # Built-in themes
73 if os.path.exists(builtin_dir):
74 _install_themes_in_dir(builtin_dir)
76 # Installed themes
77 theme_install_dir = app_config.get('theme_install_dir')
78 if theme_install_dir and os.path.exists(theme_install_dir):
79 _install_themes_in_dir(theme_install_dir)
81 current_theme_name = app_config.get('theme')
82 if current_theme_name \
83 and registry.has_key(current_theme_name):
84 current_theme = registry[current_theme_name]
85 else:
86 current_theme = None
88 return registry, current_theme