<commit_before>from importlib import import_module
from importlib.machinery import SourceFileLoader
from itertools import chain

from .config import Config, RawConfig
from .task import Task
from .util import abs_path, get_hr, printer


class TaskRunner:

    def __init__(self, config_file=None, env=None, options=None, tasks_module='tasks.py',
                 default_echo=False, default_hide=None, debug=False):
        self.config_file = config_file
        self.env = env
        self.options = options if options is not None else {}
        self.tasks_module = tasks_module
        self.default_echo = default_echo
        self.default_hide = default_hide
        self.debug = debug

    def run(self, args):
        all_tasks = self.load_tasks(self.tasks_module)
        tasks_to_run = self.get_tasks_to_run(all_tasks, args)
        configs = {}

        for task, task_args in tasks_to_run:
            self.print_debug('Task to run:', task.name, task_args)

        for task, task_args in tasks_to_run:
            task_env = self.env or task.default_env
            if task_env not in configs:
                configs[task_env] = self.load_config(task_env)
            task_config = configs[task_env]
            task.run(task_config, task_args)

    def load_config(self, env=None):
        config = Config(
            config_file=self.config_file,
            env=env or self.env,
            run=RawConfig(echo=self.default_echo, hide=self.default_hide),
            debug=self.debug,
            _interpolate=False,
        )
        config._update_dotted(self.options)
        config._interpolate()
        return config

    def load_tasks(self, tasks_module):
        if tasks_module.endswith('.py'):
            tasks_module = abs_path(tasks_module)<commit_msg>Simplify creation of Config in TaskRunner.load_config()<commit_after>from importlib import import_module
from importlib.machinery import SourceFileLoader
from itertools import chain

from .config import Config, RawConfig
from .task import Task
from .util import abs_path, get_hr, printer


class TaskRunner:

    def __init__(self, config_file=None, env=None, options=None, tasks_module='tasks.py',
                 default_echo=False, default_hide=None, debug=False):
        self.config_file = config_file
        self.env = env
        self.options = options if options is not None else {}
        self.tasks_module = tasks_module
        self.default_echo = default_echo
        self.default_hide = default_hide
        self.debug = debug

    def run(self, args):
        all_tasks = self.load_tasks(self.tasks_module)
        tasks_to_run = self.get_tasks_to_run(all_tasks, args)
        configs = {}

        for task, task_args in tasks_to_run:
            self.print_debug('Task to run:', task.name, task_args)

        for task, task_args in tasks_to_run:
            task_env = self.env or task.default_env
            if task_env not in configs:
                configs[task_env] = self.load_config(task_env)
            task_config = configs[task_env]
            task.run(task_config, task_args)

    def load_config(self, env=None):
        return Config(
            config_file=self.config_file,
            env=env or self.env,
            run=RawConfig(echo=self.default_echo, hide=self.default_hide),
            debug=self.debug,
            _overrides=self.options,
        )

    def load_tasks(self, tasks_module):
        if tasks_module.endswith('.py'):
            tasks_module = abs_path(tasks_module)
{'fraction_non_alphanumeric': 0.08036477628954118, 'fraction_numerical': 0.0, 'mean_word_length': 2.574338085539715, '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': '1316934', 'n_tokens_mistral': 1043, 'n_tokens_neox': 1023, 'n_words': 242}