def getstringlist(self, section, option, default=_DEFAULT): """getstringlist(section, option[, default]) -> list If default is not given or set, raises Error in case of an error. Gets a list of strings, using CSV to parse and delimit. """ try: value = self._config.get(section, option) parser = csv.reader( [value], lineterminator='\n', quoting=csv.QUOTE_MINIMAL) try: if PY2: vals = [v.decode('utf-8') for v in next(parser)] else: vals = next(parser) except (csv.Error, ValueError) as e: raise Error(e) return vals except Error as e: if default is _DEFAULT: if self.defaults is not None: try: return self.defaults.getstringlist(section, option) except Error: pass raise Error(e) return default
def get_version(self): """Get the version of the loaded config file (for testing only) Raises Error if no file was loaded or versioning is disabled. """ if self._version is None: raise Error("Versioning disabled") if self._loaded_version is None: raise Error("No file loaded") return self._loaded_version
def getstringlist(self, *args): """getstringlist(section, option[, default]) -> list If default is not given, raises Error in case of an error. Gets a list of strings, using CSV to parse and delimit. """ if len(args) == 3: if not isinstance(args[-1], list): raise ValueError try: value = self._config.get(*args[:2]) except Error: return args[-1] else: value = self._config.get(*args) parser = csv.reader([value], lineterminator='\n', quoting=csv.QUOTE_MINIMAL) try: vals = [v.decode('utf-8') for v in parser.next()] except (csv.Error, ValueError) as e: raise Error(e) return vals
def getstringlist(self, section, option, default=_DEFAULT): """getstringlist(section, option[, default]) -> list If default is not given, raises Error in case of an error. Gets a list of strings, using CSV to parse and delimit. """ try: value = self._config.get(section, option) except Error: if default is _DEFAULT: raise if not isinstance(default, list): raise ValueError return default parser = csv.reader([value], lineterminator='\n', quoting=csv.QUOTE_MINIMAL) try: vals = [v.decode('utf-8') for v in parser.next()] except (csv.Error, ValueError) as e: raise Error(e) return vals
def __getitem__(self, key): try: return self.get(key, "url") except Error: if key.startswith('http://') or key.startswith('https://'): return key else: raise Error(key)
def getlist(self, section, option, separator_re): """Parses option as the list of values separated by the given regexp.""" try: val = self.get(section, option).strip() return re.split(separator_re, val) except re.error as e: six.reraise(Error("Bad split regexp '%s': %s" % \ (separator_re, e)), None, exc_info()[2])
def getlist(self, section, option, default=_DEFAULT, sep=","): """Returns a str list saved with setlist()""" try: value = self._config.get(section, option) return split_escape(value, sep) except (Error, ValueError) as e: if default is _DEFAULT: if self.defaults is not None: try: return self.defaults.getlist(section, option, sep=sep) except Error: pass raise Error(e) return default
def getbytes(self, section, option, default=_DEFAULT): try: value = self._config.get(section, option) if PY3: value = value.encode("utf-8", "surrogateescape") return value except (Error, ValueError) as e: if default is _DEFAULT: if self.defaults is not None: try: return self.defaults.getbytes(section, option) except Error: pass raise Error(e) return default
def getfloat(self, section, option, default=_DEFAULT): """getfloat(section, option[, default]) -> float If default is not give or set, raises Error in case of an error """ try: return self._config.getfloat(section, option) except (Error, ValueError) as e: if default is _DEFAULT: if self.defaults is not None: try: return self.defaults.getfloat(section, option) except Error: pass raise Error(e) return default
def register_upgrade_function(self, function): """Register an upgrade function that gets called at each read() if the current config version and the loaded version don't match. Can also be registered after read was called. function(config, old_version: int, new_version: int) -> None """ if self._version is None: raise Error("Versioning disabled") self._upgrade_funcs.append(function) # after read(), so upgrade now if self._loaded_version is not None: self._do_upgrade(function) return function
def load_config(config_file): split = lambda args: csv.reader([args], skipinitialspace=True).next() cfg = SafeConfigParser() files_read = cfg.read(config_file) if files_read != [config_file]: raise Error('Could not open config file %r' % (config_file,)) components = dict(cfg.items('Telescope')) telescope = {} for comp_name, comp_type in components.items(): telescope[comp_name] = {'class' : comp_type, 'attrs' : {}, 'sensors' : []} sections = [':'.join((comp_type, name, item)) for name in ['*', comp_name] for item in ['attrs', 'sensors']] for section in sections: try: items = cfg.items(section) except NoSectionError: continue if section.endswith('attrs'): attr_items = [(name, eval(value, {})) for name, value in items] telescope[comp_name]['attrs'].update(attr_items) else: sensor_items = [[name] + split(args) for name, args in items] telescope[comp_name]['sensors'].extend(sensor_items) return telescope
def __init__(self, varname): Error.__init__(self, 'Variable not found %r' % varname) self.varname = varname
from ConfigParser import ConfigParser, Error import os p2_config = ConfigParser() p2_config.read(os.path.join(os.environ["HOME"], ".distributed_jobman.rc")) default_values = dict(cache_timeout=str(60 * 5)) keys = ["username", "password", "address", "name", "cache_timeout"] database = dict() for key in keys: value = p2_config.get("database", key, vars=default_values) if value is None: raise ValueError("Option %s must be set in configuration file " "~/.distributed_jobman.rc") database[key] = value database["cache_timeout"] = float(database["cache_timeout"]) scheduler_types = ["multi-gpu", "cluster"] scheduler = dict(type=p2_config.get("scheduler", "type")) if scheduler["type"] not in scheduler_types: raise Error("Invalid scheduler type: %s" % scheduler["type"]) config = dict(database=database, scheduler=scheduler)
def from_config(cls, cp, section, variable_args): """Returns a distribution based on a configuration file. The section must have the names of the polar and azimuthal angles in the tag part of the section header. For example: .. code-block:: ini [prior-theta+phi] name = uniform_solidangle If nothing else is provided, the default names and bounds of the polar and azimuthal angles will be used. To specify a different name for each angle, set the `polar-angle` and `azimuthal-angle` attributes. For example: .. code-block:: ini [prior-foo+bar] name = uniform_solidangle polar-angle = foo azimuthal-angle = bar Note that the names of the variable args in the tag part of the section name must match the names of the polar and azimuthal angles. Bounds may also be specified for each angle, as factors of pi. For example: .. code-block:: ini [prior-theta+phi] polar-angle = theta azimuthal-angle = phi min-theta = 0 max-theta = 0.5 This will return a distribution that is uniform in the upper hemisphere. By default, the domain of the azimuthal angle is `[0, 2pi)`. To make this domain cyclic, add `azimuthal_cyclic_domain =`. Parameters ---------- cp : ConfigParser instance The config file. section : str The name of the section. variable_args : str The names of the parameters for this distribution, separated by ``VARARGS_DELIM``. These must appear in the "tag" part of the section header. Returns ------- UniformSolidAngle A distribution instance from the pycbc.inference.prior module. """ tag = variable_args variable_args = variable_args.split(VARARGS_DELIM) # get the variables that correspond to the polar/azimuthal angles try: polar_angle = cp.get_opt_tag(section, 'polar-angle', tag) except Error: polar_angle = cls._default_polar_angle try: azimuthal_angle = cp.get_opt_tag(section, 'azimuthal-angle', tag) except Error: azimuthal_angle = cls._default_azimuthal_angle if polar_angle not in variable_args: raise Error("polar-angle %s is not one of the variable args (%s)" % (polar_angle, ', '.join(variable_args))) if azimuthal_angle not in variable_args: raise Error("azimuthal-angle %s is not one of the variable args " % (azimuthal_angle) + "(%s)" % (', '.join(variable_args))) # get the bounds, if provided polar_bounds = bounded.get_param_bounds_from_config( cp, section, tag, polar_angle) azimuthal_bounds = bounded.get_param_bounds_from_config( cp, section, tag, azimuthal_angle) # see if the a cyclic domain is desired for the azimuthal angle azimuthal_cyclic_domain = cp.has_option_tag(section, 'azimuthal_cyclic_domain', tag) return cls(polar_angle=polar_angle, azimuthal_angle=azimuthal_angle, polar_bounds=polar_bounds, azimuthal_bounds=azimuthal_bounds, azimuthal_cyclic_domain=azimuthal_cyclic_domain)