<commit_before>from abc import ABC
from abc import abstractmethod
from collections import OrderedDict

from absl import flags
from termcolor import cprint
from typing import ClassVar

from typing import List

from flagmaker.settings import SettingBlock
from .exceptions import FlagMakerConfigurationError
from .exceptions import FlagMakerInputError
from .sanity import Validator
from .settings import SettingOption
from .settings import SettingOptions
from .building_blocks import SettingsInterface

FLAGS = flags.FLAGS


class AbstractSettings(SettingsInterface):
    """Settings Base Class

    Loaded from the Config class. Used to generate flags for an app.
    """

    args: List[SettingBlock] = None
    flattened_args: dict = {}

    def start(self):
        """Bootstraps the settings loading process

        Called from load_settings. Should not be called directly.
        """
        for block in self.args:
            for k, s in block.settings.items():
                s.set_value(init=FLAGS.get_flag_value(k, None))
                self.flattened_args[k] = s

    def load_settings(self):
        self.start()
        first = True
        interactive_mode = self.args[0].settings.pop('interactive')
        for block in self.args:
            header_shown = False
            if block.conditional is not None and not block.conditional(self):
                continue
            for k, setting in block.settings.items():
                if setting.maybe_needs_input():
                    if not interactive_mode and setting.default:
                        setting.set_value(init=setting.default)
                        continue
                    if first:
                        cprint('Interactive Setup', attrs=['bold'])
                        first = False
                    if not header_shown:
                        cprint(block.name, attrs=['bold'])
                        print('=============================')
                        header_shown = True
                    if setting.include_in_interactive:
                        setting.set_value(prompt=setting.get_prompt(k))
        return self

    def assign_flags(self) -> flags:
        for block in self.args:
            for k, setting in block.settings.items():
                kwargs = {
                    'default': None,
                }
                if not setting.include_in_interactive:
                    kwargs['default'] = setting.default
                setting.method(k, help=setting.help, **kwargs)
                self.flattened_args[k] = setting
        return FLAGS

    def __getitem__(self, item):
        return self.flattened_args[item]

    def get_settings(self):
        # system settings
        settings = [SettingBlock('System Settings', {
            'interactive': SettingOption.create(
                self,
                'Enter Interactive Mode even to verify default values',
                default=False,
                include_in_interactive=False,<commit_msg>store<commit_after>from abc import ABC
from abc import abstractmethod
from collections import OrderedDict

from absl import flags
from termcolor import cprint
from typing import ClassVar

from typing import List

from flagmaker.settings import SettingBlock
from .exceptions import FlagMakerConfigurationError
from .exceptions import FlagMakerInputError
from .sanity import Validator
from .settings import SettingOption
from .settings import SettingOptions
from .building_blocks import SettingsInterface

FLAGS = flags.FLAGS


class AbstractSettings(SettingsInterface):
    """Settings Base Class

    Loaded from the Config class. Used to generate flags for an app.
    """

    args: List[SettingBlock] = None
    flattened_args: dict = {}

    def start(self):
        """Bootstraps the settings loading process

        Called from load_settings. Should not be called directly.
        """
        for block in self.args:
            for k, s in block.settings.items():
                s.set_value(init=FLAGS.get_flag_value(k, None))
                self.flattened_args[k] = s

    def load_settings(self):
        self.start()
        first = True
        interactive_mode = self.args[0].settings.pop('interactive')
        for block in self.args:
            header_shown = False
            if block.conditional is not None and not block.conditional(self):
                continue
            for k, setting in block.settings.items():
                if setting.maybe_needs_input():
                    if not interactive_mode and setting.default:
                        setting.set_value(init=setting.default)
                        continue
                    if first:
                        cprint('Interactive Setup', attrs=['bold', 'underline'])
                        print('===============================')
                        first = False
                    if not header_shown:
                        cprint(block.name, attrs=['underline'])
                        header_shown = True
                    if setting.include_in_interactive:
                        setting.set_value(prompt=setting.get_prompt(k))
        return self

    def assign_flags(self) -> flags:
        for block in self.args:
            for k, setting in block.settings.items():
                kwargs = {
                    'default': None,
                }
                if not setting.include_in_interactive:
                    kwargs['default'] = setting.default
                setting.method(k, help=setting.help, **kwargs)
                self.flattened_args[k] = setting
        return FLAGS

    def __getitem__(self, item):
        return self.flattened_args[item]

    def get_settings(self):
        # system settings
        settings = [SettingBlock('System Settings', {
            'interactive': SettingOption.create(
                self,
                'Enter Interactive Mode even to verify default values',
                default=False,
                include_in_interactive=False,
{'fraction_non_alphanumeric': 0.0809238949817215, 'fraction_numerical': 0.00033233632436025255, 'mean_word_length': 1.8909702209414025, 'pattern_counts': {'":': 0, '<': 3, '<?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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1739057', 'n_tokens_mistral': 1488, 'n_tokens_neox': 1403, 'n_words': 432}