How to use the configparser.DEFAULTSECT function in configparser

To help you get started, we’ve selected a few configparser examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github datadotworld / data.world-py / tests / datadotworld / test_config.py View on Github external
def default_config_file(config_file_path):
    config_parser = configparser.ConfigParser()
    config_parser.set(configparser.DEFAULTSECT, 'auth_token', 'file_token')
    config_parser.write(open(config_file_path, 'w'))
github ncqgm / gnumed / gnumed / gnumed / client / timelinelib / config / dotfile.py View on Github external
def _set_font(self, key, value):
        if self._toStr(value) is not None:
            self.config_parser.set(DEFAULTSECT, key, value)
            self._notify()
github laurentb / weboob / weboob / core / repositories.py View on Github external
def parse_index(self, fp):
        """
        Parse index of a repository

        :param fp: file descriptor to read
        :type fp: buffer
        """
        config = RawConfigParser()
        config.readfp(fp)

        # Read default parameters
        items = dict(config.items(DEFAULTSECT))
        try:
            self.name = items['name']
            self.update = int(items['update'])
            self.maintainer = items['maintainer']
            self.signed = bool(int(items.get('signed', '0')))
            self.key_update = int(items.get('key_update', '0'))
            self.obsolete = bool(int(items.get('obsolete', '0')))
        except KeyError as e:
            raise RepositoryUnavailable('Missing global parameters in repository: %s' % e)
        except ValueError as e:
            raise RepositoryUnavailable('Incorrect value in repository parameters: %s' % e)

        if len(self.name) == 0:
            raise RepositoryUnavailable('Name is empty')

        if 'url' in items:
github nuxeo / nuxeo-drive / nxdrive / commandline.py View on Github external
"""
        args = AbstractOSIntegration.get(None).get_system_configuration()
        if args:
            # This is the case on Windows only, values from the registry
            Options.update(args, setter="local", file="the Registry")

        for conf_file in {
            os.path.join(os.path.dirname(sys.executable), conf_name),
            os.path.join(Options.nxdrive_home, conf_name),
            conf_name,
        }:
            config = ConfigParser()
            config.read(conf_file)

            if config.has_option(DEFAULTSECT, "env"):
                env = config.get(DEFAULTSECT, "env")
                conf_args = {}

                for name, value in config.items(env):
                    if name == "env" or value == "":
                        continue

                    conf_args[name.replace("-", "_").lower()] = get_value(value)

                if conf_args:
                    file = os.path.abspath(conf_file)
                    Options.update(conf_args, setter="local", file=file, section=env)
                    args.update(**conf_args)

        if args:
            parser.set_defaults(**args)
github freeipa / freeipa / ipaserver / install / dogtaginstance.py View on Github external
def _get_default_config(self):
        """Load default config

        :return: config parser, immutable keys
        """
        defaults = self._mangle_values(self.defaults)
        # create a config template with interpolation support
        # read base config
        cfgtpl = ConfigParser(defaults=defaults)
        cfgtpl.optionxform = str
        with open(self.ipaca_default) as f:
            cfgtpl.read_file(f)

        # overwrite defaults with our defaults
        for key, value in defaults.items():
            cfgtpl.set(DEFAULTSECT, key, value)

        # all keys in default conf + known keys defined in code are
        # considered immutable.
        immutable_keys = set()
        immutable_keys.update(self._immutable_code_keys)
        for section_name in self.subsystems:
            for k, _v in cfgtpl.items(section_name, raw=True):
                immutable_keys.add(k)

        return cfgtpl, immutable_keys
github datadotworld / data.world-py / datadotworld / config.py View on Github external
os.makedirs(path.dirname(self._config_file_path))

        self._config_parser = (configparser.ConfigParser()
                               if six.PY3 else configparser.SafeConfigParser())

        if path.isfile(self._config_file_path):
            self._config_parser.read_file(open(self._config_file_path))
            if self.__migrate_invalid_defaults(self._config_parser) > 0:
                self.save()
        elif path.isfile(legacy_file_path):
            self._config_parser = self.__migrate_config(legacy_file_path)
            self.save()

        self._profile = profile
        self._section = (profile
                         if profile.lower() != configparser.DEFAULTSECT.lower()
                         else configparser.DEFAULTSECT)

        if not path.isdir(path.dirname(self.cache_dir)):
            os.makedirs(path.dirname(self.cache_dir))
github ncqgm / gnumed / gnumed / gnumed / client / timelinelib / config / dotfile.py View on Github external
def get_window_size(self):
        return (self.config_parser.getint(DEFAULTSECT, WINDOW_WIDTH),
                self.config_parser.getint(DEFAULTSECT, WINDOW_HEIGHT))
github Eugeny / reconfigure / reconfigure / parsers / iniparse / ini.py View on Github external
cur_option_name = cur_option.name
                if cur_section_name == DEFAULTSECT:
                    optobj = self._defaults
                else:
                    optobj = self._sections[cur_section_name]
                optobj._options[cur_option_name] = cur_option

            if isinstance(lineobj, SectionLine):
                self._data.extend(pending_lines)
                pending_lines = []
                pending_empty_lines = False
                cur_section = LineContainer(lineobj)
                self._data.add(cur_section)
                cur_option = None
                cur_option_name = None
                if cur_section.name == DEFAULTSECT:
                    self._defaults._lines.append(cur_section)
                    cur_section_name = DEFAULTSECT
                else:
                    if self._sectionxform:
                        cur_section_name = self._sectionxform(cur_section.name)
                    else:
                        cur_section_name = cur_section.name
                    if cur_section_name not in self._sections:
                        self._sections[cur_section_name] = \
                                INISection(cur_section, defaults=self._defaults,
                                           optionxformsource=self)
                    else:
                        self._sections[cur_section_name]._lines.append(cur_section)

            if isinstance(lineobj, (CommentLine, EmptyLine)):
                pending_lines.append(lineobj)
github redshodan / unsonic / unsonic / config.py View on Github external
def venv(self):
        return self.get(configparser.DEFAULTSECT, "venv")
github redshodan / unsonic / unsonic / config.py View on Github external
def __init__(self, filename, **kwargs):
        super().__init__(filename, **kwargs)
        global CONFIG
        CONFIG = self
        # %(install)s
        self.set(configparser.DEFAULTSECT, "install", INSTALL)
        # %(here)s
        if filename:
            here = os.path.dirname(os.path.abspath(filename))
            self.set(configparser.DEFAULTSECT, "here", here)
        else:
            self.set(configparser.DEFAULTSECT, "here", INSTALL)
        # %(venv)s
        if "VIRTUAL_ENV" in os.environ:
            self.set(configparser.DEFAULTSECT, "venv",
                     os.path.abspath(os.environ["VIRTUAL_ENV"]))
        else:
            self.set(configparser.DEFAULTSECT, "venv",
                     os.path.abspath("venv"))