<commit_before>#!/usr/bin/env python
"""
_dockerstache_

Main function to invoke dockerstache as a lib call

"""
import os

from .dotfile import Dotfile
from .templates import process_templates
from .context import Context
from . import get_logger

LOGGER = get_logger()


def run(**options):
    """
    _run_

    Run the dockerstache process to render templates
    based on the options provided

    """
    with Dotfile(options) as conf:
        if conf['context'] is None:
            msg = "No context file has been provided"
            LOGGER.error(msg)
            raise RuntimeError(msg)
        if not os.path.exists(conf['context_path']):
            msg = "Context file {} not found".format(conf['context_path'])
            LOGGER.error(msg)
            raise RuntimeError(msg)
        LOGGER.info(
            (
                "{{dockerstache}}: In: {}\n"
                "{{dockerstache}}: Out: {}\n"
                "{{dockerstache}}: Context: {}\n"
                "{{dockerstache}}: Defaults: {}\n"
            ).format(conf['input'], conf['output'], conf['context'], conf['defaults'])
        )
        context = Context(conf['context'], conf['defaults'])
        context.load()

        process_templates(
            conf['input'],
            conf['output'],
            context
            )
    return dict(conf)<commit_msg>support extend context<commit_after>#!/usr/bin/env python
"""
_dockerstache_

Main function to invoke dockerstache as a lib call

"""
import os

from .dotfile import Dotfile
from .templates import process_templates
from .context import Context
from . import get_logger

LOGGER = get_logger()


def run(**options):
    """
    _run_

    Run the dockerstache process to render templates
    based on the options provided

    If extend_context is passed as options it will be used to
    extend the context with the contents of the dictionary provided
    via context.update(extend_context)

    """
    with Dotfile(options) as conf:
        if conf['context'] is None:
            msg = "No context file has been provided"
            LOGGER.error(msg)
            raise RuntimeError(msg)
        if not os.path.exists(conf['context_path']):
            msg = "Context file {} not found".format(conf['context_path'])
            LOGGER.error(msg)
            raise RuntimeError(msg)
        LOGGER.info(
            (
                "{{dockerstache}}: In: {}\n"
                "{{dockerstache}}: Out: {}\n"
                "{{dockerstache}}: Context: {}\n"
                "{{dockerstache}}: Defaults: {}\n"
            ).format(conf['input'], conf['output'], conf['context'], conf['defaults'])
        )
        context = Context(conf['context'], conf['defaults'])
        context.load()
        if 'extend_context' in options:
            LOGGER.info("{{dockerstache}} Extended context provided")
            context.update(options['extend_context'])

        process_templates(
            conf['input'],
            conf['output'],
            context
            )
    return dict(conf)
{'fraction_non_alphanumeric': 0.12151067323481117, 'fraction_numerical': 0.0, 'mean_word_length': 2.202944269190326, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3724268', 'n_tokens_mistral': 855, 'n_tokens_neox': 821, 'n_words': 237}