Source

GardeRobe / garderobe / controllers / skin.py

import logging
from os.path import join, isfile
from glob import glob
from ConfigObject import ConfigObject

from pylons import config
from pylons import request, response, session, tmpl_context as c
from pylons.controllers.util import abort, redirect

from webob import exc
from paste.fileapp import FileApp
from mako.exceptions import TopLevelLookupException
from garderobe.lib.base import BaseController, render
from garderobe.lib import helpers as h

log = logging.getLogger(__name__)

class SkinController(BaseController):

    def index(self, theme='default', filename=None):
        """serve filename in theme
        """

        themes_dir = h.themes_dir()

        if theme != 'default':
            themes = [theme, 'default']
        else:
            themes = ['default']

        configfiles = []

        for name in themes:
            dirname = join(themes_dir, name)
            if isfile(join(dirname, filename)):
                app = FileApp(join(dirname, filename))
                return app(request.environ, self.start_response)
            if isfile(join(dirname, 'config.ini')):
                configfiles.append(join(dirname, 'config.ini'))

        conf = ConfigObject()
        configfiles.reverse()
        conf.read(configfiles)

        if filename.endswith('.css'):
            response.content_type = 'text/css; charset=utf-8'
        elif filename.endswith('.js'):
            response.content_type = 'text/javascript; charset=utf-8'

        for theme in themes:
            template = '%s/%s' % (theme, filename)
            try:
                return render('/%s.mako' % template,
                              extra_vars=dict(p=conf))
            except TopLevelLookupException:
                pass
        return ''


    def static(self, theme='default', dirname='images', filename=None):

        themes_dir = h.themes_dir()

        if theme != 'default':
            themes = [theme, 'default']
        else:
            themes = ['default']

        configfiles = []

        for name in themes:
            dirname_ = join(themes_dir, name, dirname)
            path = join(dirname_, filename)
            if isfile(path):
                app = FileApp(path)
                return app(request.environ, self.start_response)

        raise exc.HTTPNotFound()