<commit_before># encoding: UTF-8
from .exceptions import Error
from .application import Application
from .language import Language
from .api.client import Client
from .dictionary import Hashtable
from .dictionary.translations import Dictionary
from .dictionary.source import SourceDictionary
from .translation import Key
from .rules.contexts.gender import Gender
from .decoration import system_tags as system_decoration_tags
from .decoration.parser import parse as parse_decoration
from .tools import Renderable
from argparse import ArgumentError


__author__ = '<EMAIL>'


class ContextNotConfigured(Error):
    pass


class Context(object):
    """ Execution context """
    def __init__(self, **kwargs):
        self.language = None
        self.missed_keys = None
        self.dict = None
        if len(kwargs):
            self.configure(**kwargs)


    def configure(self, token = None, locale = None, source = None, application_id = None, client = None, decoration_tags = None, use_fallback_dictionary = True):
        """ Configure tranlation
            Args:
                token (string): API token
                locale (string): selected locale
                application_id (int): API application id (use default if None)
                preload (boolean): preload all tranlations
                client (Client): custom API client
                decoration_tags (TagsFactory): custom decoration tags
                use_fallback_dictionary (Boolean): use fallback dictionart
        """
        self.language = self.build_language(locale,
                                            self.build_application(application_id,
                                                                   self.build_client(token, client)))
        handle = Exception if use_fallback_dictionary else None
        try:
            if source:
                self.dict = SourceDictionary(language = self.language, source = source)
            else:
                self.dict = Dictionary()
        except handle:
            self.dict = Hashtable({})
        # Init decoration tags:
        self.decoration_tags = decoration_tags or system_decoration_tags
        return self

    def build_language(self, locale, app):
        """ Build application from configuration
            Args:
                locale (string): locale (if None - use application default)
                app (Application): Application
            Returns:
                Language
        """<commit_msg>Allow to pass dictionary instance in source variable<commit_after># encoding: UTF-8
from .exceptions import Error
from .application import Application
from .language import Language
from .api.client import Client
from .dictionary import Hashtable
from .dictionary.translations import Dictionary
from .dictionary.source import SourceDictionary
from .translation import Key
from .rules.contexts.gender import Gender
from .decoration import system_tags as system_decoration_tags
from .decoration.parser import parse as parse_decoration
from .tools import Renderable
from argparse import ArgumentError
from .dictionary import AbstactDictionary


__author__ = '<EMAIL>'


class ContextNotConfigured(Error):
    pass


class Context(object):
    """ Execution context """
    def __init__(self, **kwargs):
        self.language = None
        self.missed_keys = None
        self.dict = None
        if len(kwargs):
            self.configure(**kwargs)


    def configure(self, token = None, locale = None, source = None, application_id = None, client = None, decoration_tags = None, use_fallback_dictionary = True):
        """ Configure tranlation
            Args:
                token (string): API token
                locale (string): selected locale
                application_id (int): API application id (use default if None)
                preload (boolean): preload all tranlations
                client (Client): custom API client
                decoration_tags (TagsFactory): custom decoration tags
                use_fallback_dictionary (Boolean): use fallback dictionart
        """
        self.language = self.build_language(locale,
                                            self.build_application(application_id,
                                                                   self.build_client(token, client)))
        handle = Exception if use_fallback_dictionary else None
        try:
            if isinstance(source, Abstrac):
                # dictionary instance passed:
                self.dict = source
            elif source:
                self.dict = SourceDictionary(language = self.language, source = source)
            else:
                self.dict = Dictionary()
        except handle:
            self.dict = Hashtable({})
        # Init decoration tags:
        self.decoration_tags = decoration_tags or system_decoration_tags
        return self

    def build_language(self, locale, app):
        """ Build application from configuration
            Args:
                locale (string): locale (if None - use application default)
                app (Application): Application
            Returns:
                Language
        """
{'fraction_non_alphanumeric': 0.06791795665634674, 'fraction_numerical': 0.0003869969040247678, 'mean_word_length': 2.144160583941606, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5474871', 'n_tokens_mistral': 1211, 'n_tokens_neox': 1129, 'n_words': 405}