from jinja2 import TemplateNotFound

from partial import scanner, templating
from partial.scanner import ClassNotFound

def partial(partialName, context=None):
    if context is None: context = {}
    request = scanner.LOCAL.request
    (megamodule, name) = partialName.split(".")

    try:
        partialClass = scanner.getClass(megamodule, name)
    except ClassNotFound:
        try:
            return templating.renderTemplate(partialName, context)
        except TemplateNotFound:
            raise PartialNotFound()

    partial = partialClass(request)
    return partial.render(**context)

class PartialNotFound(Exception):
    pass