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/>.
24 from configobj
import ConfigObj
27 BUILTIN_THEME_DIR
= pkg_resources
.resource_filename('mediagoblin', 'themes')
30 def themedata_for_theme_dir(name
, theme_dir
):
32 Given a theme directory, extract important theme information.
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
):
41 assets_dir
= os
.path
.join(theme_dir
, 'assets')
42 if not os
.path
.exists(assets_dir
):
46 'name': config
.get('name', name
),
47 'description': config
.get('description'),
48 'licensing': config
.get('licensing'),
50 'templates_dir': templates_dir
,
51 'assets_dir': assets_dir
,
57 def register_themes(app_config
, builtin_dir
=BUILTIN_THEME_DIR
):
59 Register all themes relevant to this application.
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
):
69 themedata
= themedata_for_theme_dir(themedir
, abs_themedir
)
70 registry
[themedir
] = themedata
73 if os
.path
.exists(builtin_dir
):
74 _install_themes_in_dir(builtin_dir
)
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
]
88 return registry
, current_theme