<commit_before># -*- coding: utf-8 -*-
'''
xpathwebdriver
Copyright (c) 2014 Juju. Inc

Code Licensed under MIT License. See LICENSE file.
'''
import rel_imp; rel_imp.init()
import imp
import importlib
import logging
import os


logger = logging.getLogger('solve_settings')


class ConfigVar(object):
    def __init__(self, doc=None, default=None, parser=None, experimental=False):
        self.value = default
        self.parser = parser or self._solve_parser(default)
        self.name = None
        self.doc = doc
        self.experimental = experimental

    def _solve_parser(self, value):
        parser = type(value)
        if parser == bool:
            parser = eval
        return parser

    def parse(self, value_str):
        return self.parser(value_str)

    def __bool__(self):
        return bool(self.value)

    def copy(self, value):
        new_cfg = ConfigVar(self.doc, None, self.parser, self.experimental)
        new_cfg.name = self.name
        new_cfg.value = value
        return new_cfg


class BaseSettings(object):
    def __init__(self):
        self._load_env_vars()

    def _load_env_vars(self):
        '''
        Support loading from environment variables
        '''
        config_vars = self._get_config_vars()
        self._wrap_raw_values(config_vars)
        self._check_mispelling_deprecated(config_vars)
        for env_var, cfg_var in config_vars.items():
            if env_var in os.environ:
                logger.debug('Using %s=%r => %s', env_var, os.environ[env_var], cfg_var.name)
                setattr(self, cfg_var.name, cfg_var.parse(os.environ[env_var]))

    def _wrap_raw_values(self, config_vars):
        for cfg in config_vars.values():
            name = cfg.name
            if hasattr(self, name):
                value = getattr(self, name)
                if not isinstance(value, ConfigVar):
                    setattr(self, name, cfg.copy(value))

    def _check_mispelling_deprecated(self, config_vars):
        name2cfg = {cfg.name:cfg for cfg in config_vars.values()}
        for n in dir(self):
            if n.startswith('_'):
                continue
            if n not in name2cfg:
                logger.warning('Config variable %r not supported (mispelled/deprecated?)', n)

    def _get_config_vars(self):
        #Import here to avoid chicken-egg problem
        from .default_settings import DefaultSettings
        config = {}
        for n in dir(DefaultSettings):
            if n.startswith('_'):
                continue
            cfg_var = getattr(self, n)
            if not isinstance(cfg_var, ConfigVar):
                cfg_var = ConfigVar(cfg_var)<commit_msg>Fix bug casting setting var<commit_after># -*- coding: utf-8 -*-
'''
xpathwebdriver
Copyright (c) 2014 Juju. Inc

Code Licensed under MIT License. See LICENSE file.
'''
import rel_imp; rel_imp.init()
import imp
import importlib
import logging
import os


logger = logging.getLogger('solve_settings')


class ConfigVar(object):
    def __init__(self, doc=None, default=None, parser=None, experimental=False):
        self.value = default
        self.parser = parser or self._solve_parser(default)
        self.name = None
        self.doc = doc
        self.experimental = experimental

    def _solve_parser(self, value):
        parser = type(value)
        if parser == bool:
            parser = eval
        return parser

    def parse(self, value_str):
        return self.parser(value_str)

    def __bool__(self):
        return bool(self.value)

    def copy(self, value):
        new_cfg = ConfigVar(self.doc, self.value, self.parser, self.experimental)
        new_cfg.name = self.name
        new_cfg.value = value
        return new_cfg


class BaseSettings(object):
    def __init__(self):
        self._load_env_vars()

    def _load_env_vars(self):
        '''
        Support loading from environment variables
        '''
        config_vars = self._get_config_vars()
        self._wrap_raw_values(config_vars)
        self._check_mispelling_deprecated(config_vars)
        for env_var, cfg_var in config_vars.items():
            if env_var in os.environ:
                logger.debug('Using %s=%r => %s', env_var, os.environ[env_var], cfg_var.name)
                setattr(self, cfg_var.name, cfg_var.parse(os.environ[env_var]))

    def _wrap_raw_values(self, config_vars):
        for cfg in config_vars.values():
            name = cfg.name
            if hasattr(self, name):
                value = getattr(self, name)
                if not isinstance(value, ConfigVar):
                    setattr(self, name, cfg.copy(value))

    def _check_mispelling_deprecated(self, config_vars):
        name2cfg = {cfg.name:cfg for cfg in config_vars.values()}
        for n in dir(self):
            if n.startswith('_'):
                continue
            if n not in name2cfg:
                logger.warning('Config variable %r not supported (mispelled/deprecated?)', n)

    def _get_config_vars(self):
        #Import here to avoid chicken-egg problem
        from .default_settings import DefaultSettings
        config = {}
        for n in dir(DefaultSettings):
            if n.startswith('_'):
                continue
            cfg_var = getattr(self, n)
            if not isinstance(cfg_var, ConfigVar):
                cfg_var = getattr(DefaultSettings, n).copy(cfg_var)
{'fraction_non_alphanumeric': 0.09666604581858819, 'fraction_numerical': 0.002607561929595828, 'mean_word_length': 2.28239608801956, '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': '4101636', 'n_tokens_mistral': 1582, 'n_tokens_neox': 1538, 'n_words': 400}