import jinja2


def jinja(source, environ, destination=None):
    """ Render a Jinja configuration file, supports file handle or path
    """
    close_source = close_destination = False
    if type(source) is str:
        source = open(source, "r")
        close_source = True
    if type(destination) is str:
        destination = open(destination, "w")
        close_destination = True
    result = jinja2.Template(source.read()).render(environ)
    if close_source:
        source.close()
    if destination is not None:
        destination.write(result)
        if close_destination:
            destination.close()
    return result