# -*- coding: utf8 -*-

from jinja2 import FunctionLoader, FileSystemLoader

TEMPLATE_DIR = '..'
COMPONENTS_DIR = '../node_modules'

_original_template = FileSystemLoader(TEMPLATE_DIR)
_components_template = FileSystemLoader(COMPONENTS_DIR)


def _load_template(name):
    '''Loads templates starting with @ from a different directory to normal
       templates. This is as components are loaded from the node_modules
       (sigh npm) directory.'''
    if name.startswith('@'):
        return _components_template.get_source(None, name)
    return _original_template.get_source(None, name)

jinja_template_loader = FunctionLoader(_load_template)


def _read_file(filename):
    '''Tries to read a file. If it fails, it returns an empty string'''

    try:
        return open(filename).read()
    except:
        return ''
