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
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
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)
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
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)
def load_config(): """ Load the config file for the script, and make sure it is valid :return: the config object """ config = configparser.ConfigParser() config.read('kitsu2sonarr.ini') if 'kitsu.io' not in config: config['kitsu.io'] = {} config['kitsu.io']['client_id'] = None config['kitsu.io']['client_secret'] = None config['kitsu.io']['user_id'] = None with open('kitsu2sonarr.ini', 'w') as configfile: config.write(configfile) raise configparser.NoSectionError(section='kitsu.io') else: for config_key in config['kitsu.io']: if config['kitsu.io'][config_key] == '': raise Exception("Missing config items! {} is not defined!".format(config_key)) if 'sonarr' not in config: config['sonarr'] = {} config['sonarr']['url'] = None config['sonarr']['api_key'] = None with open('kitsu2sonarr.ini', 'w') as configfile: config.write(configfile) raise Exception("Missing config items!") else: for config_key in config['sonarr']: if config['sonarr'][config_key] == '': raise Exception("Missing config items! {} is not defined!".format(config_key)) return config
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
def read_mask_file(self): """ Reads the wafer maps from the 150mm section """ parser = configparser.RawConfigParser() parser.optionxform = str # Make keys Case-sensitive parser.read(self.mask_file) self._extract_mask_info(parser.items("Mask")) # Try all of the wafer diameters. I only want the biggest wafer size. # TODO: replace with douglib.utils.try_again args = ["150mm", "100mm", "50mm"] for arg in args: try: self._extract_maps(parser.items(arg)) self.dia = int(arg[:-2]) break except configparser.NoSectionError: continue else: # for loop never 'break', so there was always an error. Raise it. error_txt = "Why are there no sections?" raise configparser.NoSectionError(error_txt) self.devices = dict(parser.items("Devices")) self.device_names = sorted(self.devices.keys())
def load_config(config_filepath, config_sections=[]): ''' config_filepath is the path to a config/ini file config_sections is a list of names for sections in that file if None, then just return the [DEFAULT] section ''' config = configparser.ConfigParser() config.read(config_filepath) main_dict = {} for key in config[config.default_section]: main_dict[key] = config[config.default_section][key] d = {} for config_section in config_sections: if config_section in config: d1 = {} for key in config[config_section]: d1[key] = config[config_section][key] keys_intersection = set(d1.keys()).intersection(set(d.keys())) if ((len(keys_intersection)==0) or (set(main_dict.keys()) == keys_intersection)): d.update(d1) else: raise Exception('Config variable collision with variables %s. ' 'Check that the variables defined in section %s ' 'do not match any in other sections of the %s file' % (keys_intersection, config_section, config_filepath) ) else: raise configparser.NoSectionError() main_dict.update(d) return main_dict
def parse_config(self, database, config_file): """ param database: name of database object (str) param config_file: config file (str) retuns dictionary with config info """ stream = open(config_file, 'r') file_extenstion = config_file.split('.')[-1] if file_extenstion in ['cfg', 'cnf']: try: config = configparser.ConfigParser() if sys.version_info[0] == 3: config.read_file(stream) elif sys.version_info[0] == 2: config.readfp(stream) else: raise ValueError('Unsupported Python Version') return dict(config.items(database)) except Exception as e: raise configparser.NoSectionError(e) elif file_extenstion in ['yaml', 'yml']: options = yaml.safe_load(stream) try: return options[database] except Exception as e: raise KeyError(e) elif file_extenstion == 'json': options = json.load(stream) try: return options[database] except Exception as e: raise KeyError(e) else: raise ValueError('Unsupported config_file extension')
def _get_raw_section( self, section: ConfigSectionName ) -> Mapping[ConfigSectionName, Union[ConfigValue, _UnsupportedValue]]: options = self._sections.get(section) if options is None: raise configparser.NoSectionError(section) return options
def getConfig(self): section = 'RADIOD' option = 'loglevel' strLogLevel = 'INFO' # Get loglevel option config.read(ConfigFile) try: strLogLevel = config.get(section, option) except configparser.NoSectionError: msg = configparser.NoSectionError(section), 'in', ConfigFile self.message(msg, self.ERROR) if strLogLevel == "CRITICAL": loglevel = self.CRITICAL elif strLogLevel == "ERROR": loglevel = self.ERROR elif strLogLevel == "WARNING": loglevel = self.WARNING elif strLogLevel == "INFO": loglevel = self.INFO elif strLogLevel == "DEBUG": loglevel = self.DEBUG elif strLogLevel == "NONE": loglevel = self.NONE else: loglevel = self.INFO return loglevel
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
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
def options(self, section, no_defaults=True, **kwargs): if no_defaults: try: return list(self._sections[section].keys()) except KeyError: raise configparser.NoSectionError(section) else: return super().options(section, **kwargs)
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
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)
def _get_config(self, sectionname): config = configparser.ConfigParser() config.read(self.configfile) try: config = config[sectionname] retdict = dict(config.items()) except configparser.NoSectionError: raise configparser.NoSectionError(sectionname) return retdict
def options(self, section: str) -> list[str]: section_values = self.values.get(section) if section_values is None: raise configparser.NoSectionError(section) return [ *section_values.keys(), *(default_option for default_option in self.defaults if default_option not in section_values), ]
def _get_config_item(self): cfgkey = "Project {}".format(self.projectname) config = configparser.ConfigParser() config.read(self.cfgfile) try: config = config[cfgkey] curritem = dict(config.items()) except configparser.NoSectionError: raise configparser.NoSectionError(cfgkey) return curritem
def options(self, section: str) -> List[str]: section_values = self._find_section_values(section) if section_values is None: raise configparser.NoSectionError(section) result = [ option for option, option_value in section_values.items() if self._is_an_option(option_value) ] result.extend(default_option for default_option in self.defaults.keys() if default_option not in result) return result
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
def parse_config_files(self): """ Parse the possible config files and set appropriate values default values """ parser = self.config_parser # Fill in the built in values self.config = dict(self.__class__.defaults) config_files = self.get_config_files() try: repo = GitRepository(".", toplevel=False) except GitRepositoryError: repo = None # Read all config files for filename in config_files: self._read_config_file(repo, filename) # Update with the values from the defaults section. This is needed # in case the config file doesn't have a [<command>] section at all self.config.update(dict(parser.defaults())) # Make sure we read any legacy sections prior to the real subcommands # section i.e. read [gbp-pull] prior to [pull] if (self.command.startswith('gbp-') or self.command.startswith('git-')): cmd = self.command[4:] oldcmd = self.command if parser.has_section(oldcmd): self.config.update(dict(parser.items(oldcmd, raw=True))) self._warn_old_config_section(oldcmd, cmd) else: cmd = self.command for prefix in ['gbp', 'git']: oldcmd = '%s-%s' % (prefix, self.command) if parser.has_section(oldcmd): self.config.update(dict(parser.items(oldcmd, raw=True))) self._warn_old_config_section(oldcmd, cmd) # Update with command specific settings if parser.has_section(cmd): # Don't use items() until we got rid of the compat sections # since this pulls in the defaults again self.config.update(dict(parser._sections[cmd].items())) for section in self.sections: if parser.has_section(section): self.config.update(dict(parser._sections[section].items())) else: raise configparser.NoSectionError( "Mandatory section [%s] does not exist." % section) self.parse_lists()
def read_config(self): logger.info("Invoked PH Proxy") config = 'interface/ph_proxy/ph.cfg' global USERNAME global PASSWORD if os.path.exists(config): parser.read(config) else: raise IOError('Ph Proxy Config File does not exists') try: USERNAME = parser.get('PH_PROXY', 'username') PASSWORD = parser.get('PH_PROXY', 'password') except: raise configparser.NoSectionError('No such section in config file')
def add_comment(self, section, comment): """ Add a comment :param section: The section where to place the comment :param comment: The comment to add """ if not section or section == "DEFAULT": sectdict = self._defaults else: try: sectdict = self._sections[section] except KeyError: raise configparser.NoSectionError(section) sectdict['; %s' % (comment,)] = None
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
def load(self, file_path=None): if file_path is None: file_path = self.config_path if not os.path.isfile(file_path): self.config_path = "" return self.config_path = file_path raw_parser = configparser.RawConfigParser() raw_parser.read(file_path, encoding='utf8') for section in self.config.headers.keys(): if not raw_parser.has_section(section): raise configparser.NoSectionError(section) self.config.parse_file(raw_parser) return self.config
def get_absolute_path(self, header, key): path = self.base_agent_config.get(header, key) if path is None: raise configparser.NoSectionError( f"Could not find {header}: {key} in the provided configuration!" ) if os.path.isabs(path): return path if self.config_directory is None: raise ValueError( f"Can't locate {path} because it's a relative path and we don't know where to look!" ) joined = os.path.join(self.config_directory, path) return os.path.realpath(joined)
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]
def _read_station_sections(self): station_section_names = [ n for n in self.config.sections() if n != "General" ] if not len(station_section_names): raise configparser.NoSectionError( "No stations have been specified") for section_name in station_section_names: section = self.config[section_name] klassname = "MeteologgerStorage_" + section["storage_format"] if not hasattr(meteologgerstorage, klassname): raise UnsupportedFormat(section["storage_format"]) klass = getattr(meteologgerstorage, klassname) self.meteologger_storages.append( klass(section, logger=self.logging_system.logger))