#!/usr/bin/env python
"""Provide the empy templating engine."""

from __future__ import print_function


import em

from . import Engine


class EmpyEngine(Engine):

    """Empy templating engine."""

    handle = 'empy'

    def __init__(self, template, **kwargs):
        """Initialize empy template."""
        super(EmpyEngine, self).__init__(**kwargs)

        self.template = template

    def apply(self, mapping):
        """Apply a mapping of name-value-pairs to a template."""
        return em.expand(self.template, mapping)

#!/usr/bin/env python
"""Provide the empy templating engine."""

from __future__ import print_function

import os.path

import em

from . import Engine


class SubsystemWrapper(em.Subsystem):

    """Wrap EmPy's Subsystem class.

    Allows to open files relative to a base directory.
    """

    def __init__(self, basedir=None, **kwargs):
        """Initialize Subsystem plus a possible base directory."""
        super(SubsystemWrapper, self).__init__(**kwargs)

        self.basedir = basedir

    def open(self, name, *args, **kwargs):
        """Open file, possibly relative to a base directory."""
        if self.basedir is not None:
            name = os.path.join(self.basedir, name)

        return super(SubsystemWrapper, self).open(name, *args, **kwargs)


class EmpyEngine(Engine):

    """Empy templating engine."""

    handle = 'empy'

    def __init__(self, template, dirname=None, **kwargs):
        """Initialize empy template."""
        super(EmpyEngine, self).__init__(**kwargs)

        if dirname is not None:
            # FIXME: This is a really bad idea, as it works like a global.
            # Blame EmPy.
            em.theSubsystem = SubsystemWrapper(basedir=dirname)

        self.template = template

    def apply(self, mapping):
        """Apply a mapping of name-value-pairs to a template."""
        return em.expand(self.template, mapping)

Add base directory capability to empy engine.