aboutsummaryrefslogtreecommitdiffstats
path: root/mediagoblin/tools/theme.py
blob: 79fd91e1b2e956d6a903d6d080fb93bb41d9ea74 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# GNU MediaGoblin -- federated, autonomous media hosting
# Copyright (C) 2011, 2012 MediaGoblin contributors.  See AUTHORS.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

"""
"""


import pkg_resources
import os

from configobj import ConfigObj


BUILTIN_THEME_DIR = pkg_resources.resource_filename('mediagoblin', 'themes')


def themedata_for_theme_dir(name, theme_dir):
    """
    Given a theme directory, extract important theme information.
    """
    # open config
    config = ConfigObj(os.path.join(theme_dir, 'theme.ini')).get('theme', {})

    templates_dir = os.path.join(theme_dir, 'templates')
    if not os.path.exists(templates_dir):
        templates_dir = None

    assets_dir = os.path.join(theme_dir, 'assets')
    if not os.path.exists(assets_dir):
        assets_dir = None

    themedata = {
        'name': config.get('name', name),
        'description': config.get('description'),
        'licensing': config.get('licensing'),
        'dir': theme_dir,
        'templates_dir': templates_dir,
        'assets_dir': assets_dir,
        'config': config}

    return themedata


def register_themes(app_config, builtin_dir=BUILTIN_THEME_DIR):
    """
    Register all themes relevant to this application.
    """
    registry = {}

    def _install_themes_in_dir(directory):
        for themedir in os.listdir(directory):
            abs_themedir = os.path.join(directory, themedir)
            if not os.path.isdir(abs_themedir):
                continue

            themedata = themedata_for_theme_dir(themedir, abs_themedir)
            registry[themedir] = themedata

    # Built-in themes
    if os.path.exists(builtin_dir):
        _install_themes_in_dir(builtin_dir)

    # Installed themes
    theme_install_dir = app_config.get('theme_install_dir')
    if theme_install_dir and os.path.exists(theme_install_dir):
        _install_themes_in_dir(theme_install_dir)

    current_theme_name = app_config.get('theme')
    try:
        current_theme = registry[current_theme_name]
    except KeyError:
        current_theme = None

    return registry, current_theme