Exemple #1
0
 def get_value(self, section: str, option: str) -> Optional[str]:
     section_values = self._find_section_values(section)
     if section_values is None:
         raise configparser.NoSectionError(section)
     stringify = partial(
         self._stringify_val,
         option=option,
         section=section,
         section_values=section_values,
     )
     if option not in section_values:
         if option not in self.defaults:
             raise configparser.NoOptionError(option, section)
         return stringify(raw_value=self.defaults[option])
     option_value = section_values[option]
     # Handle the special `my_list_option.add` and `my_list_option.remove` syntax.
     if isinstance(option_value, dict):
         has_add = "add" in option_value
         has_remove = "remove" in option_value
         if not has_add and not has_remove:
             raise configparser.NoOptionError(option, section)
         add_val = stringify(option_value["add"],
                             list_prefix="+") if has_add else None
         remove_val = stringify(option_value["remove"],
                                list_prefix="-") if has_remove else None
         if has_add and has_remove:
             return f"{add_val},{remove_val}"
         if has_add:
             return add_val
         return remove_val
     return stringify(option_value)
Exemple #2
0
    def load_plugin(self):

        try:
            plugin = self.gptconfig_settings("plugin")

            if not len(plugin):
                raise configparser.NoOptionError("plugin", "settings")
            path_plugin = "%s/plugins/%s.py" % (self.dirpath, plugin)
            if not os.path.isfile(path_plugin):
                raise configparser.NoOptionError("plugin", "settings")

            globals()[plugin] = __import__("plugins.%s" % plugin)
            pluginClass = getattr(getattr(globals()[plugin], plugin),
                                  plugin.title())
            __import__("plugins.%s" % plugin)
            self.plugin = pluginClass(self)

            return True
        except configparser.NoSectionError as e:
            self.logger.critical(
                "In the file .gp-tracking.conf no exists secction [settings].\nTry re-install for regenerate file config.\n"
            )
        except configparser.NoOptionError as e:
            self.logger.critical(
                "Exec the command gp-tracking --plugin NAME for set.\n")
        except ModuleNotFoundError as e:
            self.logger.critical(
                "Fail load the plugin module. Exec the command gp-tracking --plugin NAME for  replace.\n"
            )
        except Exception as e:
            self.logger.critical(e)

        return False
Exemple #3
0
def gsource_gdata(config, graphSource, graphData):
    """Create a graph from the config file GRAPH_SOURCE and GRAPH_DATA sections"""
    # make sure the config file has graph information in it
    graph_source_field = "gSource"
    save_graph_field = "save_json"
    required_graph_data_fields = ['id', 'pop', 'area', 'cd']

    if not config.has_section(graphData):
        raise configparser.NoSectionError(graphData)
    if not config.has_section(graphSource):
        raise configparser.NoSectionError(graphSource)

    configGraphData = config[graphData]
    configGraphSource = config[graphSource]

    missing = [
        x for x in required_graph_data_fields if x not in configGraphData
    ]

    if missing:
        missing_str = " ".join(missing)
        raise configparser.NoOptionError(missing_str, graphData)

    if graph_source_field not in configGraphSource:
        raise configparser.NoOptionError(graph_source_field, graphSource)

    ID = configGraphData['id']
    POP = configGraphData['pop']
    AREA = configGraphData['area']
    CD = configGraphData['cd']
    # create graph from data and load required data

    path = configGraphSource[graph_source_field]
    save_graph = False
    load_graph = False

    if save_graph_field in configGraphSource:
        save_graph = True
        if os.path.isfile(configGraphSource[save_graph_field]):
            print("trying to load graph from", path)
            path = configGraphSource[save_graph_field]
            save_graph = False
            load_graph = True

    type = "json" if load_graph else "fiona"
    graph = mgs.construct_graph(path,
                                ID,
                                pop_col=POP,
                                area_col=AREA,
                                district_col=CD,
                                cols_to_add=[POP, AREA, CD],
                                data_source_type=type)

    if save_graph:
        print("saving graph to", configGraphSource[save_graph_field])
        with open(configGraphSource[save_graph_field], "w") as f:
            json.dump(json_graph.adjacency_data(graph), f)

    return graph, POP, AREA, CD
Exemple #4
0
    def get(self, section, option, default=NoDefault):
        """Get an option from the specified section."""
        if not self.has_section(section):
            if default is NoDefault:
                raise cp.NoSectionError(section)
            else:
                self.add_section(section)

        if not self.has_option(section, option):
            if default is NoDefault:
                raise cp.NoOptionError(option, section)
            else:
                self.set(section, option, default)
                return default

        value = cp.ConfigParser.get(self, section, option, raw=self.raw)

        # Use type of default_value to parse value correctly
        default_value = self.get_default(section, option)
        if not isinstance(default_value, str):
            try:
                value = ast.literal_eval(value)
            except (SyntaxError, ValueError):
                pass
        return value
Exemple #5
0
def read_parser(parser):
    """Read configuration from config file parser."""

    return_dict = {}

    for section in default_config:
        if section not in parser:
            parser.add_section(section)

        for key in default_config[section]:
            def_val = default_config[section][key]
            if key in parser[section]:
                if def_val[3] == bool:
                    val = parser[section].getboolean(key)
                    return_dict[key] = val
                else:
                    val = parser[section][key]
                    if val == "None":
                        return_dict[key] = None
                    else:
                        return_dict[key] = def_val[3](val)
            else:
                if def_val[0] is True:
                    raise configparser.NoOptionError(key, section)
                else:
                    return_dict[key] = def_val[1]

    return return_dict
Exemple #6
0
def _validate_input(config, group, name, variable_type, required):
    """Validates and returns the correspoinding value defined in the config.

    Keyword arguments:
    config - the instance of the configparser
    group - the name of the group containing the property
    name - the name of the property to get that value for
    variable_type - the type of property, 'path', 'mapping' 'bool', otherwise return the raw string
    required - if the option is required and none is found than raise an exception
    """
    try:
        value = config.get(group, name)
        if value == '':
            raise configparser.NoOptionError(name, group)

        if variable_type == 'path':
            return os.path.normpath(value)
        elif variable_type == 'mapping':
            return list(v.split(',') for v in value.split(';'))
        elif variable_type == 'bool':
            return value.lower() == 'true'
        else:
            return value
    except (configparser.NoSectionError, configparser.NoOptionError):
        if required:
            raise
        elif variable_type == 'bool':
            return False
        else:
            return None
Exemple #7
0
    def del_option(self,
                   section_name: str,
                   option_name: str,
                   clear_disc: bool = False):
        '''
        Удаление опции, включая все файлы в ней!

        :param section_name: имя секции
        :param option_name: имя опции
        :param clear_disc: очистить ли жёсткий диск?
        :return:
        '''
        check_option = self.check_option(
            section_name=section_name,
            option_name=option_name)  # Проверим наличие секции
        if check_option is False:  # Если опция есть, а секции нет
            raise configparser.NoOptionError(f'{option_name}',
                                             section=section_name)

        elif check_option is True:  # Если опция есть
            if clear_disc:  # Если диск зачищать нужно
                # Делаем удаление
                self.__drop_directory(directory=self.get_option_path(
                    section_name=section_name, option_name=option_name))
                # если каталога секции не было или он удалён успешно
                self.__config.remove_option(section=section_name,
                                            option=option_name)

            else:  # Если не нужно чистить диск
                self.__config.remove_option(section=section_name,
                                            option=option_name)

        return
Exemple #8
0
    def getjson(self, section, option, fallback=None):
        """Load JSON object from value.

        Args:
            section (str): section name.
            option (str): option name.

        Kwargs:
            fallback: Dict or List.

        Returns dict or list.
        """
        try:
            val = self.get(section, option)
            if val.strip() == '' and fallback is not None:
                return fallback
            elif val.strip() == '':
                raise configparser.NoSectionError(section) from None
        except configparser.NoSectionError as e:
            if fallback is not None:
                return fallback
            else:
                raise configparser.NoSectionError(section) from None
        except configparser.NoOptionError as e:
            if fallback is not None:
                return fallback
            else:
                raise configparser.NoOptionError(section, option) from None

        try:
            return js.loads(val)
        except json.decoder.JSONDecodeError as e:
            raise configparser.ParsingError("section '%s'" % section +
                                            " option '%s'" % option +
                                            " (JSON %s)" % e) from None
Exemple #9
0
def readValueFromConfigurationFile(configurationFile,
                                   sectionName,
                                   keyName,
                                   default=None):
    config = configparser.SafeConfigParser()
    config.readfp(open(configurationFile))
    if config.has_section(sectionName):
        if config.has_option(sectionName, keyName):
            value = config.get(sectionName, keyName)
            return value
        else:
            logging.error(
                "Configuration file (%s) does not have this option in section %s: %s",
                configurationFile, keyName, sectionName)
            if default == None:
                raise configparser.NoOptionError(keyName, sectionName)
            else:
                return default
    else:
        logging.error("Configuration file (%s) does not have this section: %s",
                      configurationFile, sectionName)
        if default == None:
            raise configparser.NoSectionError(sectionName)
        else:
            return default
Exemple #10
0
    def get(self, section: str, key: str) -> str:
        section = str(section).lower()
        key = str(key).lower()
        common_kws = {}
        ans = None

        # first check overrides
        if self._overrides.has_option(section, key):
            ans = self._overrides.get(section, key, **common_kws)
        else:
            # ...then environment
            env_key = _env_var_name(section, key)
            if env_key in os.environ:
                ans = os.environ[env_key]
                self._used_env.add(env_key)
            # ...then the config file
            elif self._options.has_option(section, key):
                ans = self._options.get(section, key, **common_kws)
            # ...then the default config
            elif self._defaults.has_option(section, key):
                ans = self._defaults.get(section, key, **common_kws)

        if ans is None:
            if not self.has_section(section):
                raise configparser.NoSectionError(section)
            raise configparser.NoOptionError(key, section)

        self._used.add((section, key))
        return _expand_env_var(ans)
Exemple #11
0
def read_value_from_configuration_file(configuration_file, section_name, key_name, default=None):
    """
    Read a value from an entry in a section from a configuration file.

    :param str configuration_file: The file path of the configuration file
    :param str section_name: Name of the section
    :param str key_name: Name of the entry to read
    :param default: Default value of the entry if not found
    :return: The value read or default value
    :rtype: str
    """
    config = configparser.SafeConfigParser()
    config.readfp(open(configuration_file))
    if config.has_section(section_name):
        if config.has_option(section_name, key_name):
            value = config.get(section_name, key_name)
            return value
        else:
            logging.error("Configuration file (%s) does not have this option in section %s: %s", configuration_file, key_name, section_name)
            if default == None:
                raise configparser.NoOptionError(key_name, section_name)
            else:
                return default
    else:
        logging.error("Configuration file (%s) does not have this section: %s", configuration_file, section_name)
        if default == None:
            raise configparser.NoSectionError(section_name)
        else:
            return default
Exemple #12
0
def load_config():
    """Load a keyring using the config file in the config root."""

    filename = "keyringrc.cfg"

    keyring_cfg = os.path.join(platform.config_root(), filename)

    if not os.path.exists(keyring_cfg):
        return

    config = configparser.RawConfigParser()
    config.read(keyring_cfg)
    _load_keyring_path(config)

    # load the keyring class name, and then load this keyring
    try:
        if config.has_section("backend"):
            keyring_name = config.get("backend", "default-keyring").strip()
        else:
            raise configparser.NoOptionError("backend", "default-keyring")

    except (configparser.NoOptionError, ImportError):
        logger = logging.getLogger("keyring")
        logger.warning("Keyring config file contains incorrect values.\n" +
                       "Config file: %s" % keyring_cfg)
        return

    return load_keyring(keyring_name)
Exemple #13
0
 def test_08_read_item_config_bad(self):
     """ Read from a config file and get what we expect """
     with mock.patch.object(self.library.configfile, 'get',
                            side_effect=configparser.NoOptionError('option', 'section')):
         result = self.library.read_item_from_config(section='testing', key='normal_user',
                                                     default='foo')
     self.assertEqual(result, 'foo', 'Could not retrieve a default value from a configfile')
Exemple #14
0
    def get(self,
            section,
            option,
            default: Any = NoDefault) -> Any:  # type: ignore
        """
        Get an option.

        Parameters
        ----------
        section: str
            Section name. If `None` is provide use the default section name.
        option: str
            Option name for `section`.
        default:
            Default value (if not specified, an exception will be raised if
            option doesn't exist).
        """
        section = self._check_section_option(section, option)

        if not self.has_section(section):
            if default is NoDefault:
                raise cp.NoSectionError(section)
            else:
                self.add_section(section)

        if not self.has_option(section, option):
            if default is NoDefault:
                raise cp.NoOptionError(option, section)
            else:
                self.set(section, option, default)
                return default

        raw_value: str = super(UserConfig, self).get(section, option, raw=True)
        default_value = self.get_default(section, option)
        value: Any

        if isinstance(default_value, str):
            value = raw_value
        elif isinstance(default_value, bool):
            value = ast.literal_eval(raw_value)
        elif isinstance(default_value, float):
            value = float(raw_value)
        elif isinstance(default_value, int):
            value = int(raw_value)
        else:
            try:
                # Lists, tuples, None, ...
                value = ast.literal_eval(raw_value)
            except (SyntaxError, ValueError):
                value = raw_value

        if default_value is not NoDefault and type(default_value) is not type(
                value):
            logger.error(
                f"Inconsistent config type for [{section}][{option}]. "
                f"Expected {default_value.__class__.__name__} but "
                f"got {value.__class__.__name__}.")

        return value
Exemple #15
0
 def get_value(self, section: str, option: str) -> Optional[str]:
     for cfg in self._configs:
         try:
             return cfg.get_value(section, option)
         except (configparser.NoSectionError, configparser.NoOptionError):
             pass
     if not self.has_section(section):
         raise configparser.NoSectionError(section)
     raise configparser.NoOptionError(option, section)
Exemple #16
0
    def from_config_section(cls, section: configparser.SectionProxy):
        domain_names = section.get('domain-names')
        if domain_names is None:
            raise configparser.NoOptionError('domain-names', section.name)
        domain_names = re.split('[,\t ]+', domain_names)

        option = cls(search_list=domain_names)
        option.validate()
        return option
Exemple #17
0
 def _get(self, section, option):
     """Like .get, but returns the real section name and the value."""
     name, data = self._get_section(section)
     if data is None:
         raise configparser.NoSectionError(section)
     try:
         return name, data[option]
     except KeyError as exc:
         raise configparser.NoOptionError(option, name) from exc
Exemple #18
0
 def test_info(self, *_):
     with patch('skill_sdk.routes.request', new=self.request):
         result = loads(info())
         self.assertEqual(result, {"skillId": "testingskill", "skillVersion": f"1 {__version__}", "skillSpiVersion": __spi_version__, 'supportedLocales': ['de']})
         with patch.object(skill_sdk.routes.config, 'get', side_effect=configparser.NoOptionError('skill', 'version')):
             result = info()
             self.assertEqual(result.status_code, 500)
             data = loads(result.body)
             self.assertEqual(data['code'], 999)
             self.assertEqual(data['text'], 'internal error')
Exemple #19
0
 def getstr(self, section, option):
     """
     Get an option from a section of configuration file
     """
     try:
         return self.parser.get(section,
                                option).strip('\'"').replace('\n', '')
     except configparser.NoOptionError:
         default = self.get_default(option)
         if default is None:
             raise configparser.NoOptionError(option, section)
         return default
Exemple #20
0
 def get(self, section, option, *args, **kwargs):
     """
     Return a configuration value as a string.
     """
     try:
         value = configparser.ConfigParser.get(self, section, option, **kwargs)
         if value is None:
             return ""
         return value
     except configparser.NoSectionError:
         # plugins are used to only catch NoOptionError
         raise configparser.NoOptionError(option, section)
Exemple #21
0
    def get(self, section, option, default=NoDefault):
        """
        Get an option
        section=None: attribute a default section name
        default: default value (if not specified, an exception
        will be raised if option doesn't exist)
        """
        section = self._check_section_option(section, option)

        if not self.has_section(section):
            if default is NoDefault:
                raise cp.NoSectionError(section)
            else:
                self.add_section(section)

        if not self.has_option(section, option):
            if default is NoDefault:
                raise cp.NoOptionError(option, section)
            else:
                self.set(section, option, default)
                return default

        value = cp.ConfigParser.get(self, section, option, raw=self.raw)
        # Use type of default_value to parse value correctly
        default_value = self.get_default(section, option)
        if isinstance(default_value, bool):
            value = ast.literal_eval(value)
        elif isinstance(default_value, float):
            value = float(value)
        elif isinstance(default_value, int):
            value = int(value)
        elif is_text_string(default_value):
            if PY2:
                try:
                    value = value.decode("utf-8")
                    try:
                        # Some str config values expect to be eval after
                        # decoding
                        new_value = ast.literal_eval(value)
                        if is_text_string(new_value):
                            value = new_value
                    except (SyntaxError, ValueError):
                        pass
                except (UnicodeEncodeError, UnicodeDecodeError):
                    pass
        else:
            try:
                # lists, tuples, ...
                value = ast.literal_eval(value)
            except (SyntaxError, ValueError):
                pass
        return value
Exemple #22
0
    def get(self,
            section: str,
            option: str,
            default: Any = NoDefault) -> Any:  # type: ignore
        """
        Get an option.

        :param section: Config section to search in.
        :param option: Config option to get.
        :param default: Default value to fall back to if not present.
        :returns: Config value.
        :raises cp.NoSectionError: if the section does not exist.
        :raises cp.NoOptionError: if the option does not exist and no default is given.
        """

        with self._lock:

            if not self.has_section(section):
                if default is NoDefault:
                    raise cp.NoSectionError(section)
                else:
                    self.add_section(section)

            if not self.has_option(section, option):
                if default is NoDefault:
                    raise cp.NoOptionError(option, section)
                else:
                    self.set(section, option, default)
                    return default

            raw_value: str = super().get(section, option, raw=True)
            default_value = self.get_default(section, option)
            value: Any

            if isinstance(default_value, str):
                value = raw_value
            else:
                try:
                    value = ast.literal_eval(raw_value)
                except (SyntaxError, ValueError):
                    value = raw_value

            if default_value is not NoDefault:
                if type(default_value) is not type(value):
                    logger.error(
                        f"Inconsistent config type for [{section}][{option}]. "
                        f"Expected {default_value.__class__.__name__} but "
                        f"got {value.__class__.__name__}.")

            return value
Exemple #23
0
    def getint(self, section, option):
        """
        Get an integer option from a section of configuration file 
        """
        if not isinstance(self.defaults[option], int):
            raise TypeError('"{0}" not an integer option!!'.format(option))

        try:
            return self.parser.getint(section, option)
        except configparser.NoOptionError:
            default = self.get_default(option)
            if default is None:
                raise configparser.NoOptionError(option, section)
            return default
 def _addon_data_path(self):
     try:
         addon_data_file_path = self.config["AddOn"]["SaveFilePath"]
         if not addon_data_file_path:
             raise configparser.NoOptionError("Addon", "SaveFilePath")
         if not os.path.exists(addon_data_file_path):
             self.logger.error("""Addon save file not found: {0}.
                   Ensure options.ini is correct and you've run the AddOn at least once.""".format(addon_data_file_path))
             input()
             exit()
         return addon_data_file_path
     except (KeyError, configparser.NoSectionError, configparser.NoOptionError) as e:
         self.logger.error("Missing addon path in options.ini. See example_options for help.")
         input()
         exit()
Exemple #25
0
def verify_required_options(section, option_keys):
    """
    Verifies that section exists, and that it has option_keys defined
    :param section: Section in the config
    :param option_keys: list of required options
    :type section: str
    :type option_keys: list
    :return: SectionProxy
    """
    if section not in config:
        raise configparser.NoSectionError(section)
    for option in option_keys:
        if option not in config[section]:
            raise configparser.NoOptionError(option, section)
    return config[section]
Exemple #26
0
    def get_option_path(self, section_name: str, option_name: str) -> str:
        '''
        Функция отдаёт каталог опции.

        :param section_name: имя секции
        :param option_name: имя опции
        :return: строка с полным путём каталога или None, если нет секции или опции.
        '''
        check_result = self.check_option(section_name=section_name,
                                         option_name=option_name)
        if check_result is True:  # Проверим опцию
            path = os.path.join(
                self.get_section_path(section_name=section_name),
                self.__config.get(section=section_name, option=option_name))
            return path
        else:  # Если он False
            raise configparser.NoOptionError(f'{option_name}',
                                             section=section_name)
Exemple #27
0
    def getlist(self, section, option, fallback=None):
        """Get list from option value.

        Example:

        .. code::

            [Bar]
            files_to_check =
                /path/to/file1,
                /path/to/file2,
                /path/to/another file with space in the name

        Args:
            section (str): section name.
            option (str): option name.

        Kwargs:
            fallback (list): List of default values.

        Returns list.
        """
        try:
            val = self.get(section, option)
            if val.strip() == '':
                return []
            val = val.replace('\n', '').replace('\r', '').split(',')
        except configparser.NoSectionError as e:
            if fallback is not None:
                val = fallback
            else:
                raise configparser.NoSectionError(section) from None
        except configparser.NoOptionError as e:
            if fallback is not None:
                val = fallback
            else:
                raise configparser.NoOptionError(section, option) from None

        if isinstance(val, list):
            return val
        else:
            raise configparser.ParsingError("section '%s'" % section +
                                            " option '%s'" % option +
                                            " expected list") from None
Exemple #28
0
def load_config():
    """Load a keyring using the config file.

    The config file can be in the current working directory, or in the user's
    home directory.
    """
    keyring = None

    # search from current working directory and the home folder
    keyring_cfg_list = [os.path.join(os.getcwd(), "keyringrc.cfg"),
                        os.path.join(os.path.expanduser("~"), "keyringrc.cfg")]

    # initialize the keyring_config with the first detected config file
    keyring_cfg = None
    for path in keyring_cfg_list:
        keyring_cfg = path
        if os.path.exists(path):
            break

    if os.path.exists(keyring_cfg):
        config = config_parser.RawConfigParser()
        config.read(keyring_cfg)
        # load the keyring-path option
        try:
            if config.has_section("backend"):
                keyring_path = config.get("backend", "keyring-path").strip()
            else:
                keyring_path = None
        except config_parser.NoOptionError:
            keyring_path = None

        # load the keyring class name, and then load this keyring
        try:
            if config.has_section("backend"):
                keyring_name = config.get("backend", "default-keyring").strip()
            else:
                raise config_parser.NoOptionError('backend', 'default-keyring')

            keyring = load_keyring(keyring_path, keyring_name)
        except (config_parser.NoOptionError, ImportError):
            logger.warning("Keyring config file contains incorrect values.\n" +
                           "Config file: %s" % keyring_cfg)

    return keyring
Exemple #29
0
 def get(self, section, setting=None, dummy=False):
     """
     Return a configuration value as a string.
     :param section: The configuration file section.
     :param setting: The configuration file setting.
     :return basestring
     """
     if setting is None:
         # parse as xpath
         return self._xml.findall(section)
     else:
         try:
             data = self._settings[section][setting]
             if data is None:
                 return ''
             else:
                 return data
         except KeyError:
             raise configparser.NoOptionError(setting, section)
Exemple #30
0
    def get(self, section, option, *args, **kwargs):
        """Get a value, replacing environment variables also.

        The arguments are the same as `RawConfigParser.get`, but in the found
        value, ``$WORD`` or ``${WORD}`` are replaced by the value of the
        environment variable ``WORD``.

        Returns the finished value.

        """
        for section_prefix in self.section_prefixes:
            real_section = section_prefix + section
            if configparser.RawConfigParser.has_option(self, real_section, option):
                break
        else:
            raise configparser.NoOptionError(option, section)

        v = configparser.RawConfigParser.get(self, real_section, option, *args, **kwargs)
        v = substitute_variables(v, os.environ)
        return v