Exemplo n.º 1
0
def modelspec_reader(modelfile, mode='extended'):
    '''!
    Function using ConfigParser (in Python Standard Library) to 
    read a AdvanceSyn model specification file.

    @param modelfile String: Relative path to the model specification 
    file.
    @param mode String: Type of interpolation mode for ConfigParser. 
    Default = 'extended'. Allowable values are 'extended' (for 
    extended interpolation) and 'basic' (for basic interpolation).
    @return: ConfigParser object.
    '''
    if mode == 'extended':
        spec = ConfigParser(interpolation=ExtendedInterpolation(),
                            allow_no_value=True,
                            delimiters=('=', ':'),
                            comment_prefixes=('#', ';'),
                            inline_comment_prefixes=('#', ';'),
                            empty_lines_in_values=True,
                            strict=False)
    if mode == 'basic':
        spec = ConfigParser(interpolation=BasicInterpolation(),
                            allow_no_value=True,
                            delimiters=('=', ':'),
                            comment_prefixes=('#', ';'),
                            inline_comment_prefixes=('#', ';'),
                            empty_lines_in_values=True,
                            strict=False)
    spec.optionxform = str
    modelspec = open(modelfile).read()
    spec.read_string(modelspec)
    return spec
Exemplo n.º 2
0
def specobj_reader(specobj, mode='extended'):
    '''!
    Function using ConfigParser (in Python Standard Library) to 
    read a AdvanceSyn model specification as a dictionary-type object.

    @param specobj Dictionary: Model specification in a dictionary / 
    dictionary-like format.
    @param mode String: Type of interpolation mode for ConfigParser. 
    Default = 'extended'. Allowable values are 'extended' (for 
    extended interpolation) and 'basic' (for basic interpolation).
    @return: ConfigParser object.
    '''
    if mode == 'extended':
        spec = ConfigParser(interpolation=ExtendedInterpolation(),
                            allow_no_value=True,
                            delimiters=('=', ':'),
                            comment_prefixes=('#', ';'),
                            inline_comment_prefixes=('#', ';'),
                            empty_lines_in_values=True,
                            strict=False)
    if mode == 'basic':
        spec = ConfigParser(interpolation=BasicInterpolation(),
                            allow_no_value=True,
                            delimiters=('=', ':'),
                            comment_prefixes=('#', ';'),
                            inline_comment_prefixes=('#', ';'),
                            empty_lines_in_values=True,
                            strict=False)
    spec.optionxform = str
    spec.read_dict(specobj)
    return spec
Exemplo n.º 3
0
 def load_conf(self):
     conf_file = self.confFile
     config = ConfigParser(interpolation=BasicInterpolation())
     config.read(conf_file,encoding = 'utf-8')
     conf_name = getFileName(conf_file)
     for dir_1 in self.dir_dict:
         self.dir_dict[dir_1] = config.get(conf_name, dir_1)
Exemplo n.º 4
0
    def __init__(
            self,
            profile_name=None,
            defaults=None,
            **kwargs):

        config_defaults = {
            'auto_authenticate': 'True',
            'development': 'False',
            'oauthlib_insecure_transport': 'False',
            'oauth_authorization_url': '%(hostname)s/oauth2/authorize',
            'oauth_client_id': 'farmos_api_client',
            'oauth_client_secret': '',
            'oauth_redirect_url': '%(hostname)s/api/authorized',
            'oauth_scope': 'user_access',
            'oauth_token_url': '%(hostname)s/oauth2/token',
        }

        # Merge additional default values if provided.
        if defaults is not None:
            config_defaults = {**config_defaults, **defaults}

        # Initialize the config object.
        super().__init__(defaults=config_defaults, interpolation=BasicInterpolation())

        # Add a section for the profile.
        if profile_name is not None:
            self.add_section(profile_name)
        else:
            profile_name = 'DEFAULT'

        # Load additional kwargs into the config.
        for key, value in kwargs.items():
            if value is not None:
                self.set(profile_name, key, value)
Exemplo n.º 5
0
def main():
    from configparser import ConfigParser, ExtendedInterpolation, BasicInterpolation

    parser = ConfigParser(interpolation=ExtendedInterpolation())
    # the default BasicInterpolation could be used as well
    #parser.read('test.ini') # this ignores missing files!
    parser.read_file(open('test-extended.ini'))

    print(parser['hashes']['shebang'])
    print(parser['hashes']['extensions'])
    print(parser['hashes']['interpolation not necessary'])
    print(parser['hashes']['even in multiline values'])
    print(parser['foo']['goo'])
    print(parser['foo'].get('goo'))
    print(parser.get('foo', 'goo'))

    parser = ConfigParser()
    # the default BasicInterpolation could be used as well
    #parser.read('test.ini')
    parser.read_file(open('test-extended.ini'))

    print(parser['hashes']['shebang'])
    print(parser['hashes']['extensions'])
    print(parser['hashes']['interpolation not necessary'])
    print(parser['hashes']['even in multiline values'])
    print(parser['foo']['goo'])
    print(parser['foo'].get('goo'))
    print(parser.get('foo', 'goo'))

    parser = ConfigParser(interpolation=BasicInterpolation())
    # the default BasicInterpolation could be used as well
    parser.read('test-basic.ini')
    print(parser['Paths']['my_pictures'])
    print(parser['Paths']['goo'])

    print(parser['foo']['goo'])
    print(parser['foo'].get('goo'))
    print(parser.get('foo', 'goo'))

    parser = ConfigParser()
    # the default BasicInterpolation could be used as well
    parser.read('test-basic.ini')
    print(parser['Paths']['my_pictures'])
    print(parser['Paths']['goo'])

    print(parser['foo']['goo'])
    print(parser['foo'].get('goo'))
    print(parser.get('foo', 'goo'))

    print_type_and_value(parser.getfloat('foo', 'midgap'))

    print_type_and_value(parser.get('foo', 'vec3_float'))
    print_type_and_value(str2list(parser.get('foo', 'vec3_float')))

    print_type_and_value(parser.get('foo', 'vec3_int'))
    print_type_and_value(parser.get('foo', 'vec3_int_v2'))
    print_type_and_value(str2list(parser.get('foo', 'vec3_int_v2')))

    return 0
Exemplo n.º 6
0
    def __init__(self, preferences_ini_path=PREFERENCES_INI_FULLPATH, preferences_ini_string=None):
        """  The repository for pylcg preferences: default (permanent), ini_file, and current. """
        self.preferences_ini_path = preferences_ini_path
        self.preferences_ini_string = preferences_ini_string

        # Default config is defined above in this module, and is IMMUTABLE (read-only):
        self.default_config = ConfigParser(allow_no_value=True, delimiters=CONFIG_DELIMITERS,
                                           interpolation=BasicInterpolation())
        self.default_config.read_string(DEFAULT_CONFIG_TEXT)

        # ini_file config will strictly track contents of preferences.ini file.
        self.ini_file_config = ConfigParser(allow_no_value=True, delimiters=CONFIG_DELIMITERS,
                                            interpolation=BasicInterpolation())
        self.load_ini_file()

        # Current config starts as a copy of ini_file config, later updated as needed by the user (via GUI).
        self.current_config = copy_config(self.ini_file_config)
Exemplo n.º 7
0
 def _load_conf(self):
     conf_file = self._confFile
     config = ConfigParser(interpolation=BasicInterpolation())
     config.optionxform = str  #disable the default changing to lowercase
     config.read(conf_file, encoding='utf-8')
     sections = config.sections()
     for sec in sections:
         self._dict[sec] = dict()
     for sec in sections:
         for item in config[sec]:
             self._dict[sec][item] = config.get(sec, item)
Exemplo n.º 8
0
class Config:
    """Interact with configuration variables."""

    configParser = ConfigParser(interpolation=BasicInterpolation())
    config_file_path = 'C:\\CDP_Selenium\\tests\\configuration\\config.ini'
    #
    @classmethod
    def initialize(cls):
        """Start config by reading config.ini."""
        cls.configParser.read(cls.config_file_path)

    @classmethod
    def credentials(cls, key):
        """Get credentials value from config.ini."""
        return cls.configParser.get('credentials', key, vars=os.environ)
Exemplo n.º 9
0
    def __init__(self, last_source=None, interpolation=None, kv_sep=' = ', indent_spaces=4, compact_form=False):
        """
        :param file/str last_source: Last config source file name. This source is read last when an attempt to read a
                                     config value is made (delayed reading, hence "last") if it exists.
                                     It is also the default target file location for :meth:`self.save`
                                     For file source, if the file does not exist, it is ignored.
                                     Defaults to ~/.config/<PROGRAM_NAME>

        :param Interpolation|bool|None interpolation: Support interpolation using the given :cls:`Interpolation`
                                                      instance, or if True, then defaults to :cls:`BasicInterpolation`
        :param str kv_sep: When serializing, separator used for key and value.
        :param int indent_spaces: When serializing, number of spaces to use when indenting a value spanning multiple
                                  lines.
        :param bool compact_form: Serialize in compact form, such as no new lines between each config key.
        """
        if not last_source and sys.argv and sys.argv[0] and not sys.argv[0].endswith('/pytest'):
            last_source = os.path.join('~', '.config', os.path.basename(sys.argv[0]))

        #: User config file name
        self._last_source = last_source and os.path.expanduser(last_source)

        #: Config sources
        self._sources = []

        #: Indicate if `self._sources` has been read
        self._sources_read = False

        #: Parser instance from ConfigParser that does the underlying config parsing
        interpolation = BasicInterpolation() if interpolation is True else interpolation
        self._parser = ConfigParser(interpolation=interpolation) if interpolation else ConfigParser(interpolation=None)

        #: A dict that maps (section, key) to its comment.
        self._comments = {}

        #: A dict that maps dot notation section.key to its actual (section, key)
        self._dot_keys = {}

        #: Seperator for key/value. Used for save only.
        self._kv_sep = kv_sep

        #: Number of spaces to use when indenting a value spanning multiple lines.
        self._indent_spaces = indent_spaces

        #: Save in compact form (no newline between keys)
        self._compact_form = compact_form

        #: Cache to avoid transforming value too many times
        self._value_cache = {}
Exemplo n.º 10
0
def main():

    config = ConfigParser(interpolation=BasicInterpolation())
    config.read('./config.ini')

    agent_conf = config["agent"]
    changbafeed_conf = config['changbafeed']

    command = changbafeed_conf["command"]
    result = subprocess.check_output(command, shell=True).decode('utf8')
    count = int(result.split()[0])

    result = [{
        "endpoint": agent_conf["endpoint"],
        "metric": "request_count",
        "timestamp": int(time.time()),
        "step": 60,
        "value": count,
        "counterType": "GAUGE",
        "tags": f"api={changbafeed_conf['api']}"
    }]
    result = requests.post(agent_conf["address"], data=json.dumps(result))
    print(result.text)
Exemplo n.º 11
0
class FinestrinoConfigParser(BaseParser, ConfigParser):
    NO_DEFAULT = object()
    enabled = True
    _instance = None
    _config_paths = [
        '/etc/finestrino/client.cfg',
        '/etc/finestrino/finestrino.cfg',
        'client.cfg',
        'finestrino.cfg',
    ]

    if hasattr(ConfigParser, "_interpolate"):
        # Override ConfigParser._interpolate (Python 2)
        def _interpolate(self, section, option, rawval, vars):
            value = ConfigParser._interpolate(self, section, option, eawval, vars)

            return EnvironmentInterpolation().before_get(
                parser=self, section=section, option=option, value=value, 
                defaults=None
            )
    else:
        # Override ConfigParser._DEFAULT_INTERPOLATION (Python 3)
        _DEFAULT_INTERPOLATION = CombinedInterpolation([
            BasicInterpolation(), EnvironmentInterpolation()])

    @classmethod
    def reload(cls):
        # Warn about deprecated old-style config paths.
        deprecated_paths = [p for p in cls._config_paths if 
            os.path.basename(p) == 'cleint.cfg' and os.path.exists(p)]
        if deprecated_paths:
            warnings.warn("Finestrino configuration files named 'client.cfg' \
                are deprecated and in favor of 'finestrino.cfg'.",
                DeprecationWarning)


        return cls.instance().read(cls._config_paths)

    def _get_with_default(self, 
        method, section, option, default, expected_type=None, **kwargs):
        """Gets the values of the section/option using method.

        Returns default if value is not found.

        Raises an exception if the default value is not None and doesn't 
        match the expected_type.
        
        Arguments:
            method {[type]} -- [description]
            section {[type]} -- [description]
            option {[type]} -- [description]
            default {[type]} -- [description]
        
        Keyword Arguments:
            expected_type {[type]} -- [description] (default: {None})
        """
        try:
            return method(self, section, option, **kwargs)
        except (NoOptionError, NoSectionError):
            if default is FinestrinoConfigParser.NO_DEFAULT:
                raise
            if expected_type is not None and default is not None and \
                not isinstance(default, expected_type):
                raise

            return default

    def get(self, section, option, default=NO_DEFAULT, **kwargs):
        return self._get_with_default(ConfigParser.get, section, option, default, **kwargs)

    def getboolean(self, section, option, default=NO_DEFAULT): 
        return self._get_with_default(ConfigParser.getboolean, section, option, default, bool)

    def getint(self, section, option, default=NO_DEFAULT):
        return self._get_with_default(ConfigParser.getint, section, option, default, int)

    def getfloat(self, section, option, default=NO_DEFAULT):
        return self._get_with_default(ConfigParser.getfloat, section, option, default, float)

    def getintdict(self, section):
        try:
            # Exclude keys from [DEFAULT]section because in general they do not hold int values
            return dict((key, int(value)) for key, value in self.items(section) 
                if key not in {k for k, _ in self.items('DEFAULT')})
        except NoSectionError:
            return {}

    def set(self, section, option, value=None):
        if not ConfigParser.has_section(self, section):
            ConfigParser.add_section(self, section)

        return ConfigParser.set(self, section, option, value                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    )             
Exemplo n.º 12
0
class LuigiConfigParser(BaseParser, ConfigParser):
    NO_DEFAULT = object()
    enabled = True
    _instance = None
    _config_paths = [
        '/etc/luigi/client.cfg',  # Deprecated old-style global luigi config
        '/etc/luigi/luigi.cfg',
        'client.cfg',  # Deprecated old-style local luigi config
        'luigi.cfg',
    ]
    if hasattr(ConfigParser, "_interpolate"):
        # Override ConfigParser._interpolate (Python 2)
        def _interpolate(self, section, option, rawval, vars):
            value = ConfigParser._interpolate(self, section, option, rawval, vars)
            return EnvironmentInterpolation().before_get(
                parser=self, section=section, option=option,
                value=value, defaults=None,
            )
    else:
        # Override ConfigParser._DEFAULT_INTERPOLATION (Python 3)
        _DEFAULT_INTERPOLATION = CombinedInterpolation([BasicInterpolation(), EnvironmentInterpolation()])

    @classmethod
    def reload(cls):
        # Warn about deprecated old-style config paths.
        deprecated_paths = [p for p in cls._config_paths if os.path.basename(p) == 'client.cfg' and os.path.exists(p)]
        if deprecated_paths:
            warnings.warn("Luigi configuration files named 'client.cfg' are deprecated if favor of 'luigi.cfg'. " +
                          "Found: {paths!r}".format(paths=deprecated_paths),
                          DeprecationWarning)

        return cls.instance().read(cls._config_paths)

    def _get_with_default(self, method, section, option, default, expected_type=None, **kwargs):
        """
        Gets the value of the section/option using method.

        Returns default if value is not found.

        Raises an exception if the default value is not None and doesn't match the expected_type.
        """
        try:
            try:
                # Underscore-style is the recommended configuration style
                option = option.replace('-', '_')
                return method(self, section, option, **kwargs)
            except (NoOptionError, NoSectionError):
                # Support dash-style option names (with deprecation warning).
                option_alias = option.replace('_', '-')
                value = method(self, section, option_alias, **kwargs)
                warn = 'Configuration [{s}] {o} (with dashes) should be avoided. Please use underscores: {u}.'.format(
                    s=section, o=option_alias, u=option)
                warnings.warn(warn, DeprecationWarning)
                return value
        except (NoOptionError, NoSectionError):
            if default is LuigiConfigParser.NO_DEFAULT:
                raise
            if expected_type is not None and default is not None and \
               not isinstance(default, expected_type):
                raise
            return default

    def get(self, section, option, default=NO_DEFAULT, **kwargs):
        return self._get_with_default(ConfigParser.get, section, option, default, **kwargs)

    def getboolean(self, section, option, default=NO_DEFAULT):
        return self._get_with_default(ConfigParser.getboolean, section, option, default, bool)

    def getint(self, section, option, default=NO_DEFAULT):
        return self._get_with_default(ConfigParser.getint, section, option, default, int)

    def getfloat(self, section, option, default=NO_DEFAULT):
        return self._get_with_default(ConfigParser.getfloat, section, option, default, float)

    def getintdict(self, section):
        try:
            # Exclude keys from [DEFAULT] section because in general they do not hold int values
            return dict((key, int(value)) for key, value in self.items(section)
                        if key not in {k for k, _ in self.items('DEFAULT')})
        except NoSectionError:
            return {}

    def set(self, section, option, value=None):
        if not ConfigParser.has_section(self, section):
            ConfigParser.add_section(self, section)

        return ConfigParser.set(self, section, option, value)
Exemplo n.º 13
0
import os
import sys
import logging

from configparser import ConfigParser, BasicInterpolation

config = ConfigParser(interpolation=BasicInterpolation())

try:
    config_file = os.environ.get('NUTTSSH_CONFIG_FILE', 'nuttssh.ini')
    config.read_file(open(config_file, 'r'))

    LISTEN_HOST = config.get('server', 'listen_host')
    LISTEN_PORT = config.getint('server', 'listen_port')
    SERVER_FQDN = config.get('server', 'server_fqdn')
    ENABLE_AUTH = config.getboolean('server', 'enable_auth')
    ALLOW_NEW_CLIENTS = config.getboolean('server', 'allow_new_clients')
    ALLOW_PTY = config.getboolean('server', 'allow_pty')
    ENABLE_SHELL = config.getboolean('server', 'enable_shell')

    AUTHORIZED_KEYS_FILE = config.get('keys', 'authorized_keys_file')
    HOST_KEY_FILE = []
    for item in config.items('keys'):
        if item[0] not in ('host_key_dir', 'authorized_keys_file'):
            HOST_KEY_FILE.append(item[1])
except FileNotFoundError:
    logging.error(f'Config file "{config_file}" not found')
    sys.exit(1)
except Exception as e:
    logging.error(f'Error parsing config file:\n{e}')
    sys.exit(1)
Exemplo n.º 14
0
    def update_from_file(
        self,
        file_path: Union[str, pathlib.Path],
        allow_new: bool = True,
        interpolate: bool = False,
        case_sensitive: bool = False,
    ) -> None:
        """Update the configuration from a configuration file

        The Python ConfigParser is used to read the file. The file format that is supported is described at
        https://docs.python.org/library/configparser.html

        Different profiles in a configuration file are denoted by double underscores in the sections names. For
        instance will the following configuration have a `foo` profile in the `spam` section (in addition to the
        default profile):

            [spam]
            ...

            [spam__foo]
            ...

        The file may contain a special section called `__replace__` which may contain key-value pairs which will
        replace format-style strings in keys and values in the rest of the file.

        Additionally, the file may contain a special section called `__vars__`. The key-value pairs from this section
        will be added to the `dictionary` of the configuration.

        If `interpolate` is set to True, ExtendedInterpolation of variables in the configuration file is used. This
        means that variables of the form ${key:section} can be used for references within the file. See
        https://docs.python.org/library/configparser.html#configparser.ExtendedInterpolation for details.

        Args:
            file_path:      Path to the configuration file.
            allow_new:      Whether to allow the creation of new sections and entries.
            interpolate:    Whether to interpolate variables in the configuration file.
            case_sensitive: Whether to read keys as case sensitive (or convert to lower case).
        """
        # Use ConfigParser to read from file
        cfg_parser_cls = CasedConfigParser if case_sensitive else ConfigParser
        cfg_parser = cfg_parser_cls(
            allow_no_value=True,
            delimiters=("=", ),
            interpolation=ExtendedInterpolation()
            if interpolate else BasicInterpolation(),
        )
        cfg_parser.read(file_path)

        # Read special __replace__
        replace_vars = {k: v
                        for k, v in cfg_parser["__replace__"].items()
                        } if "__replace__" in cfg_parser else {}

        # Add __vars__ to vars dictionary
        if "__vars__" in cfg_parser.sections():
            self.update_vars({k: v for k, v in cfg_parser["__vars__"].items()})

        # Add configuration entries
        for cfg_section in cfg_parser.sections():
            section, has_profile, profile = cfg_section.partition("__")
            if not section:  # Skip dunder sections
                continue
            for key, value in cfg_parser[cfg_section].items():
                # Handle meta-information
                if ":" in key:
                    continue
                meta = {
                    k.partition(":")[-1]: v
                    for k, v in cfg_parser[cfg_section].items()
                    if k.startswith(f"{key}:")
                }

                # Create a configuration entry
                self.update(
                    section,
                    _replace(key, replace_vars),
                    value if value is None else _replace(
                        value, replace_vars).replace("\n", " "),
                    profile=profile if has_profile else None,
                    source=str(file_path),
                    meta=meta,
                    allow_new=allow_new,
                    _update_sections=False,
                )

        self._set_sections_for_profiles()
Exemplo n.º 15
0
    def update_from_file(
        self,
        file_path: Union[str, pathlib.Path],
        allow_new: bool = True,
        interpolate: bool = False,
        case_sensitive: bool = False,
    ) -> None:
        """Update the configuration from a configuration file

        The Python ConfigParser is used to read the file. The file format that is supported is described at
        https://docs.python.org/library/configparser.html

        Different profiles in a configuration file is denoted by double underscores in the sections names. For instance
        does the following configuration have a `foo` profile in the `spam` section (in addition to the default
        profile):

            [spam]
            ...

            [spam__foo]
            ...

        If `interpolate` is set to True, ExtendedInterpolation of variables in the configuration file is used. See
        https://docs.python.org/library/configparser.html#configparser.ExtendedInterpolation for details.

        Args:
            file_path:      Path to the configuration file.
            allow_new:      Whether to allow the creation of new sections and entries.
            interpolate:    Whether to interpolate variables in the configuration file.
            case_sensitive: Whether to read keys as case sensitive (or convert to lower case).
        """
        # Use ConfigParser to read from file
        cfg_parser_cls = CasedConfigParser if case_sensitive else ConfigParser
        cfg_parser = cfg_parser_cls(
            allow_no_value=True,
            delimiters=("=", ),
            interpolation=ExtendedInterpolation()
            if interpolate else BasicInterpolation(),
        )
        cfg_parser.read(file_path)

        # Add configuration entries
        for cfg_section in cfg_parser.sections():
            section, has_profile, profile = cfg_section.partition("__")
            for key, value in cfg_parser[cfg_section].items():
                # Handle meta-information
                if ":" in key:
                    continue
                meta = {
                    k.partition(":")[-1]: v
                    for k, v in cfg_parser[cfg_section].items()
                    if k.startswith(f"{key}:")
                }

                # Create a configuration entry
                self.update(
                    section,
                    key,
                    value if value is None else value.replace("\n", " "),
                    profile=profile if has_profile else None,
                    source=str(file_path),
                    meta=meta,
                    allow_new=allow_new,
                    _update_sections=False,
                )

        self._set_sections_for_profiles()
Exemplo n.º 16
0
 def __init__(self, base_interpolation=None):
     self._base_interpolation = base_interpolation if base_interpolation is not None else BasicInterpolation(
     )