def get(self, section, option, vars=None): if not self.has_section(section): raise NoSectionError(section) if vars is not None and option in vars: value = vars[option] sec = self.data[section] if option in sec: return sec._compat_get(option) else: raise NoOptionError(option, section)
def get(self, section, option): """ Get method that raises NoOptionError if the value was unset. This differs from the SafeConfigParser which may also raise a NoSectionError. """ try: ret = super(ExactOnlineConfig, self).get(section, option) except NoSectionError: raise NoOptionError(option, section) return ret
def get_merge(self, sections, key): """Get a value from a merge of specified sections. The order of the passed sections denote their priority with the first having the highest priority. :param sections: The sections to look in for the value :param key: The key of the setting :return: The desired settings value """ for section in sections: if self.settings.has_option(section, key): return self.settings.get(section, key) print("Setting {} does not exist in any of the given sections".format(key)) raise NoOptionError(sections[-1], key)
def includeme(config): global use_https global archive_service_baseurl datasource_settings = config.registry.datasource_settings try: archive_service_baseurl = datasource_settings.datasource.depatisconnect.api_uri except: raise NoOptionError('api_uri', 'datasource_depatisconnect') if archive_service_baseurl.startswith('https'): use_https = True
def spec(cls, content): """ Parse and validate a reasoner configuration file (.spec) content. :type content: str :rtype: ReasonerSpec """ spec = ReasonerSpec() spec.read_string(content) for key in ('id', 'name', 'version'): if not spec.has_option('reasoner', key): raise NoOptionError('reasoner', key) return spec
def get(self, section, name, default=None): """Get the value of a option. Section of the config file and the option name. You can pass a default value if the option doesn't exist. """ if not self.parser.has_section(section): raise NoSectionError("No section: %s" % section) elif not self.parser.has_option(section, name): if not default: raise NoOptionError("No option: %s" % name) else: return default return self.parser.get(section, name)
def getbytes(self, key, default=None): """Reads a size value in human format, if not set then default. Examples: 1, 2 B, 3K, 4 MB """ if not self.cf.has_option(self.main_section, key): if default is None: raise NoOptionError(key, self.main_section) s = default else: s = self.cf.get(self.main_section, key) return skytools.hsize_to_bytes(s)
def get(self, section, option): if option in self._buffer: parsed = self._buffer[option] if isinstance(parsed, dict): return parsed elif isinstance(parsed, list): return ",".join([str(p) for p in parsed]) else: return str(parsed) else: raise NoOptionError(section, option) ###############################################################################################################################################
def _mock_get(self, section, option, fallback): if section not in self.values: if fallback: return fallback raise NoSectionError(section) section_values = self.values[section] if option not in section_values: if fallback: return fallback raise NoOptionError(option, section) return section_values[option]
def getlist(self, key, default=None): """Reads comma-separated list from key.""" if not self.cf.has_option(self.main_section, key): if default is None: raise NoOptionError(key, self.main_section) return default s = self.get(key).strip() res = [] if not s: return res for v in s.split(","): res.append(v.strip()) return res
def get_db_config(self): db_config = ConfigParser() db_config.read(PathConstant.TI_CONFIG) if not db_config.has_section(self.TI_DB_SECTION): log.error("There is not TiDB configuration in %s. section: %s" % ( PathConstant.TI_CONFIG, self.TI_DB_SECTION )) raise NoSectionError(self.TI_DB_SECTION) for db_option in self.ti_db_options: if not db_config.has_option(self.TI_DB_SECTION, db_option): log.error("There is not option[%s] of TiDB configuration" % ( db_option )) raise NoOptionError(self.TI_DB_SECTION, db_option) self.ti_db_options[db_option] = \ db_config.get(self.TI_DB_SECTION, db_option) return copy.deepcopy(self.ti_db_options)
def _get(self, config_func, option): superclasses = [ superclass.__name__ for superclass in self.__class__.__mro__ ] superclasses[-1] = 'DEFAULT' for i, superclass in enumerate(superclasses): superclasses[i] = superclass = re.sub('\B([A-Z][a-z])', r' \1', superclass) superclasses[i] = superclass = re.sub('([a-z])([A-Z])', r'\1 \2', superclass) if self._config.has_section( superclass) and self._config.has_option( superclass, option): return None if self._config.get( superclass, option) == 'None' else config_func( superclass, option) raise NoOptionError(option, superclasses)
def __init__(self, loop, config): self.loop = loop self.config = config if config.has_option("global", "key"): self.fernet = Fernet(config.get("global", "key")) else: log(__name__).error( "You need to add a key to section [global], e.g.\n" "key: %s", Fernet.generate_key().decode("ascii")) raise NoOptionError("key", "global") # create data dirs, if needed basedir = config.get("global", "basedir") for section in config.sections(): if section.startswith("dir:"): dir_name = os.path.join(basedir, section[4:]) os.makedirs(dir_name, exist_ok=True) self.app = web.Application() router = self.app.router router.add_route('GET', "/favicon.ico", self.favicon) router.add_route('POST', "/login", self.login) router.add_route('GET', "/.well-known/acme-challenge/{token}", self.serve_letsencrypt) router.add_route('GET', "/.well-known/acme-challenge/upload/{token}/{thumb}", self.upload_letsencrypt) router.add_route('GET', "/privacy/", self.privacy) router.add_route('GET', "/{token}/auth/privacy/{path:.*}", self.privacy) router.add_route('GET', "/{token}/auth/{path:.*}", self.handle) router.add_route('POST', "/{token}/auth/{path:.*}", self.handle) router.add_route('GET', "/{path:.*}", self.handle, name='handle-get') router.add_route('POST', "/{path:.*}", self.handle, name='handle-post') self.runner = AppRunner(self.app, access_log_format=config.get( "logging", "access_log_format")) self.loop.run_until_complete(self.runner.setup()) self.cert_watcher = None loop.run_until_complete(self.init_site("http_site")) loop.run_until_complete(self.init_site("https_site")) self.letsencrypt_data = dict()
def get(self, section, option, **kwargs): """ Get method that raises NoOptionError if the value was unset. This differs from the SafeConfigParser which may also raise a NoSectionError. We take extra **kwargs because the Python 3.5 configparser extends the get method signature and it calls self with those parameters. def get(self, section, option, *, raw=False, vars=None, fallback=_UNSET): """ try: ret = super(ExactOnlineConfig, self).get(section, option, **kwargs) except NoSectionError: raise NoOptionError(option, section) return ret
def get(self, section, option, *, raw=False, vars=None, fallback=_UNSET): """Get an option value for a given section. If `vars' is provided, it must be a dictionary. The option is looked up in `vars' (if provided), `section', and in `DEFAULTSECT' in that order. If the key is not found and `fallback' is provided, it is used as a fallback value. `None' can be provided as a `fallback' value. If interpolation is enabled and the optional argument `raw' is False, all interpolations are expanded in the return values. Arguments `raw', `vars', and `fallback' are keyword only. The section DEFAULT is special. """ try: d = self._unify_values(section, vars) except NoSectionError: if fallback is _UNSET: raise else: return fallback option = self.optionxform(option) try: value = d[option] except KeyError: if fallback is _UNSET: if section == "Report" and \ (str(option).startswith("league") or str(option).startswith("report") or str(option).startswith("team")): logger.warning( "MISSING CONFIGURATION VALUE: \"{0}: {1}\"! Setting to default value of \"False\". To include " "this section, update \"config.ini\" and try again.".format(section, option)) return "False" else: raise NoOptionError(option, section) else: return fallback if raw or value is None: return value else: return self._interpolation.before_get(self, section, option, value, d)
def _get_raw_option(self, section: str, option: str, api_name: str) -> str: """ Designed to get raw option data and return it to the calling function, but will check if the section exists. :param section: (str) Section name :param option: (str) Option name under section :param api_name: (str) Parser API (get, set, etc.) :return: (str) Raw returned value from API """ if self.has_section(section=section): if self.has_option(section=section, option=option): api = getattr(self, api_name) return api(section=section, option=option) raise NoOptionError(section, option) raise NoSectionError(section)
def __load_config(self, config): if not os.path.exists(config): raise Exception("ti server configuration file not exist. " "path: %s" % config) log.info("ti-server config path: %s" % config) ti_config = ConfigParser() ti_config.read(config) if not ti_config.has_section(self.TI_SERVER_SECTION): log.info("there is not ti server configuration in config " "file: %s" % config) raise NoSectionError(self.TI_SERVER_SECTION) for option in self.ti_server_info: if not ti_config.has_option(self.TI_SERVER_SECTION, option): log.info("there is not ti server %s in config " "file: %s" % (option, config)) raise NoOptionError(option, self.TI_SERVER_SECTION) self.ti_server_info[option] = \ ti_config.get(self.TI_SERVER_SECTION, option)
def getarray(self, section, option, check_default=True, sep=None, conv=None, **kws): def parsearray(s): spt = s.split(sep) if callable(conv): tmp = [] for s in spt: tmp.append(conv(s)) spt = tmp return spt if self.has_option(section, option, False): return parsearray(ConfigParser.get(self, section, option, **kws)) elif check_default and self.has_default(option): return parsearray(self.get_default(option, **kws)) elif not self.has_section(section): raise NoSectionError(section) else: raise NoOptionError(option, section)
def get(self, section, option, **kw): """ return default values instead of breaking this is a drop-in replacement for standard get from ConfigParser """ try: return ConfigParser.get(self, section, option, raw=True) except NoOptionError: try: default_value = DEFAULT_CONF[section][option] except KeyError: raise NoOptionError(option, section) else: return default_value except NoSectionError: self.add_section(section) return self.get(section, option)
def get_wildcard(self, key, values=(), default=None): """Reads a wildcard property from conf and returns its string value, if not set then default.""" orig_key = key keys = [key] for wild in values: key = key.replace('*', wild, 1) keys.append(key) keys.reverse() for k in keys: if self.cf.has_option(self.main_section, k): return self.cf.get(self.main_section, k) if default is None: raise NoOptionError(orig_key, self.main_section) return default
def get(self, section, key): try: value = self.conf_items.get(section, key) except NoSectionError: raise NoSectionError("Section '{}' not found".format(section)) except NoOptionError: raise NoOptionError( "Section '{}' does not contain a key '{}'".format( section, key)) if value == "None": return None if value == "False": return False if value == "True": return True if value.startswith("~"): return os.path.expanduser(value) return value
def get(self, section, option): if hasattr(self._buffer, option): parsed = getattr(self._buffer, option) if type(parsed) in [list, np.ndarray]: return ",".join([str(p) for p in parsed]) elif type(parsed) == u.quantity.Quantity: try: return ",".join([str(p) for p in parsed.value]) except TypeError: return parsed.value elif isinstance(parsed, u.core.UnitBase): return parsed.to_string() return parsed else: raise NoOptionError(section, option)
def __init__(self, path=None, esm='esm', *args): """ Initializes the ESMConfig. Credentials for the ESM(s) live in an .ini file that should be located in a user directory with properly restrictive permissions. This class will check for environemental variables for a home dir and check there and then check in the local dir. Args: path (str): path to a mfesaw ini file. esm (str): section name of the esm settings in the ini file. default is 'default'. """ self.data = {} config = configparser.ConfigParser() if path: config.read(path) else: home_vars = ['HOME', 'APPDATA', 'XDG_HOME'] ini_paths = _find_envs(home_vars) ini_paths.append('.') ini_names = ['.mfe_saw.ini', '.mfesaw.ini', '.mfesaw2.ini'] ini_files = _find_files(ini_names, ini_paths) config.read(ini_files) if not config: raise FileNotFoundError('.mfesaw2.ini file not found.') if not config.has_section(esm): raise NoSectionError( 'Section not found in INI file: {}'.format(esm)) options = ['esmhost', 'esmuser', 'esmpass'] for opt in options: if not config.has_option(esm, opt): raise NoOptionError('Missing opt in ini file.'.format(opt)) self.__dict__[opt] = config[esm][opt] self[opt] = config[esm][opt]
def get(self, section, option, *, _conv=None): """Get an option value for a given section.""" d = self._get_section(section) option = option.lower() try: value = d[option] except KeyError: raise NoOptionError(option, section) if isinstance(value, dict): raise TypeError( f"Expected {section}.{option} to be an option, not a section.") # toml should convert types automatically # don't manually convert, just check, that the type is correct if _conv is not None and not isinstance(value, _conv): raise TypeError( f"The type of {section}.{option} should be {_conv}") return value
def get(self, section: str, option: str): # type: ignore """Gets an option value for a given section. :param section: section name :param option: option name :returns: :class:`Option`: Option object holding key/value pair """ if section not in self: raise NoSectionError(section) from None section_ = self[section] option = self.optionxform(option) try: value = section_[option] except KeyError: raise NoOptionError(option, str(section_)) return value
def get(self, section, option, fallback=_UNSET): # noqa """Gets an option value for a given section. Warning: Please notice this method works differently from what is expected of :meth:`MutableMapping.get` (or :meth:`dict.get`). Similarly to :meth:`configparser.ConfigParser.get`, will take least 2 arguments, and the second argument does not correspond to a default value. This happens because this function is not designed to return a :obj:`Section` of the :obj:`ConfigUpdater` document, but instead a nested :obj:`Option`. See :meth:`get_section`, if instead, you want to retrieve a :obj:`Section`. Args: section (str): section name option (str): option name fallback: if the key is not found and fallback is provided, it will be returned. ``None`` is a valid fallback value. Raises: :class:`NoSectionError`: if ``section`` cannot be found :class:`NoOptionError`: if the option cannot be found and no ``fallback`` was given Returns: :class:`Option`: Option object holding key/value pair """ section_obj = self.get_section(section, _UNSET) if section_obj is _UNSET: raise NoSectionError(section) from None option = self.optionxform(option) value = section_obj.get(option, fallback) # ^ we checked section_obj against _UNSET, so we are sure about its type if value is _UNSET: raise NoOptionError(option, section) return value
def get(self, section, option): """Gets an option value for a given section. Args: section (str): section name option (str): option name Returns: :class:`Option`: Option object holding key/value pair """ if not self.has_section(section): raise NoSectionError(section) from None section = self.__getitem__(section) option = self.optionxform(option) try: value = section[option] except KeyError: raise NoOptionError(option, section) return value
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 NoSectionError(section) else: self.add_section(section) if not self.has_option(section, option): if default is NoDefault: raise NoOptionError(option, section) else: self.set(section, option, default) return default value = 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 (isinstance(default_value, str) or isinstance(default_value, list) or isinstance(default_value, tuple)): try: # lists, tuples, ... value = ast.literal_eval(value) except (SyntaxError, ValueError): pass return value
def get(self, section, option, fallback=_UNSET): """Gets an option value for a given section. Args: section (str): section name option (str): option name fallback: if the key is not found and fallback is provided, it will be returned. ``None`` is a valid fallback value. Returns: :class:`Option`: Option object holding key/value pair """ if not self.has_section(section): raise NoSectionError(section) from None section = self.__getitem__(section) option = self.optionxform(option) try: return section[option] except KeyError: if fallback is _UNSET: raise NoOptionError(option, section) return fallback
def _get_value(self, config_func, obj, option): """""" if '_' + option in obj.__dict__: return obj.__dict__['_' + option] cls = obj.__class__ superclasses = [superclass.__name__ for superclass in cls.__mro__] for superclass in superclasses: if self.has_section(superclass) and \ self.has_option(superclass, option): try: value = config_func(superclass, option) except ValueError: if self.get(superclass, option) == 'None': value = None else: raise break else: raise NoOptionError(option, superclasses) if value == 'None': value = None obj.__dict__['_' + option] = value return value