From 828fc6300a5e9d1cf8cd10e875945ce7a8c3a1ab Mon Sep 17 00:00:00 2001 From: Christopher Allan Webber Date: Sat, 30 Jun 2012 17:34:04 -0500 Subject: [PATCH] Early version of theme registry code --- mediagoblin/app.py | 4 ++ mediagoblin/tools/theme.py | 79 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+) create mode 100644 mediagoblin/tools/theme.py diff --git a/mediagoblin/app.py b/mediagoblin/app.py index 97080ed8..33acbba0 100644 --- a/mediagoblin/app.py +++ b/mediagoblin/app.py @@ -24,6 +24,7 @@ from webob import Request, exc from mediagoblin import routing, meddleware, __version__ from mediagoblin.tools import common, translate, template from mediagoblin.tools.response import render_404 +from mediagoblin.tools.theme import register_themes from mediagoblin.tools import request as mg_request from mediagoblin.mg_globals import setup_globals from mediagoblin.init.celery import setup_celery_from_config @@ -73,6 +74,9 @@ class MediaGoblinApp(object): # Set up the database self.connection, self.db = setup_database() + # Register themes + self.theme_registry = register_themes(app_config) + # Get the template environment self.template_loader = get_jinja_loader( app_config.get('local_templates')) diff --git a/mediagoblin/tools/theme.py b/mediagoblin/tools/theme.py new file mode 100644 index 00000000..dc83e0ff --- /dev/null +++ b/mediagoblin/tools/theme.py @@ -0,0 +1,79 @@ +# 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 . + +""" +""" + + +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')) + + 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'), + '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 + _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) + + return registry -- 2.25.1