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: 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 tiff_base(self): section = self["FILE SYSTEM"] dir_path = section.get("tiff_base") if not dir_path: raise Error("Missing tiff_base in configuration.") path = Path(dir_path).expanduser() return path
async def test_can_add_stack_trace(mocker): # arrange expected_key = fake.word() expected_details = fake.word() expected_error_str = fake.word() logger, sink, env = get_logger_and_sink(mocker) from configparser import Error # Just some non-builtin exception # act try: raise Error(expected_error_str) except Error: logger.add_stack_trace(expected_key, expected_details) await logger.flush() # assert context = get_flushed_context(sink) value = context.properties[expected_key] assert isinstance(value, dict) assert value["details"] == expected_details assert value["error_type"] == "configparser.Error" assert value["error_str"] == expected_error_str assert value["traceback"].split("\n")[-2] == " raise Error(expected_error_str)"
def __init__(self, name): if name == "Carrier": self.name = name self.life = 5 elif name == "Battleship": self.name = name self.life = 4 elif name == "Cruiser": self.name = name self.life = 3 elif name == "Submarine": self.name = name self.life = 3 elif name == "Destroyer": self.name = name self.life = 2 else: raise Error( "Error while trying to create submarine. Submarine name can only be one of the following:" "Carrier, Battleship, Cruiser, Submarine, Destroyer") # self.name = name # self.life = life self.locations = None
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: raise Error("Bad split regexp '%s': %s" % (separator_re, e), exc_info()[2])
def getbytes(self, section, option, default=_DEFAULT): try: value = self._config.get(section, option) 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 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 init_config(config_path, config_set): """Initialize config""" parser = RawConfigParser() if not parser.read(config_path): raise Error('couldn\'t read ' + config_path) try: config = parser[config_set] except KeyError: raise NoSectionError('couldn\'t find ' + config_set + ' in ' + config_path) validate_config(config) return config
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 validate_config(config): """Raise error if config doesn't pass validation""" required = { 'date_format', 'start_date', 'end_date', 'data_path', 'variable_group', 'variable', 'shape_file', 'resolution', 'min_lat', 'max_lat', 'min_lon', 'max_lon', 'config_prefix' } if required - set(config.keys()) != set(): raise Error('missing options. required: {}'.format(required)) try: start_date = datetime.strptime(config['start_date'], config['date_format']) except Exception: raise ValueError('start_date do not correspond to date format') try: end_date = datetime.strptime(config['end_date'], config['date_format']) except Exception: raise ValueError('end_date do not correspond to date format') if end_date < start_date: raise ValueError( 'wrong time slice. end_date should be later than start_date') try: float(config['resolution']) float(config['min_lat']) float(config['max_lat']) float(config['min_lon']) float(config['max_lon']) except ValueError: raise ValueError( 'wrong value for resolution or bounds. expected int or float(dot format)' ) if not os.path.isfile(config['shape_file']): raise FileNotFoundError('shape file not found: ' + config['shape_file'] + ' does not exist') if not os.path.isdir(config['data_path']): raise FileNotFoundError('data path not found: ' + config['data_path'] + ' does not exist')
def _get_main(self, config): """Get the configuration of the main section""" self.test_environment = config.getboolean('main', 'TEST_ENVIRONMENT') install_dir = dirname(dirname(abspath(__file__))) default_base_data_dir = join(install_dir, 'qiita_db', 'support_files', 'test_data') self.base_data_dir = config.get('main', 'BASE_DATA_DIR') or \ default_base_data_dir try: log_path = config.get('main', 'LOG_PATH') if log_path: raise Error('The option LOG_PATH in the main section is no ' 'longer supported, use LOG_DIR instead.') except NoOptionError: pass self.log_dir = config.get('main', 'LOG_DIR') if self.log_dir: # if the option is a directory, it will exist if not isdir(self.log_dir): raise ValueError("The LOG_DIR (%s) option is required to be a " "directory." % self.log_dir) self.base_url = config.get('main', 'BASE_URL') if not isdir(self.base_data_dir): raise ValueError("The BASE_DATA_DIR (%s) folder doesn't exist" % self.base_data_dir) self.working_dir = config.get('main', 'WORKING_DIR') if not isdir(self.working_dir): raise ValueError("The WORKING_DIR (%s) folder doesn't exist" % self.working_dir) self.max_upload_size = config.getint('main', 'MAX_UPLOAD_SIZE') self.require_approval = config.getboolean('main', 'REQUIRE_APPROVAL')
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)
def __init__(self, section, option): Error.__init__( self, "Option %r in section %r has no default value" % (option, section))
def __init__(self, message, filename): Error.__init__(self, message) self.filename = filename
def read_config(self): if not self._conf.read(os.path.expanduser('~/charon.cfg')): raise IOError if not self.self_check(): raise Error('Config is incomplete')
def _get_main(self, config): """Get the configuration of the main section""" self.test_environment = config.getboolean('main', 'TEST_ENVIRONMENT') install_dir = dirname(dirname(abspath(__file__))) default_base_data_dir = join(install_dir, 'qiita_db', 'support_files', 'test_data') self.base_data_dir = config.get('main', 'BASE_DATA_DIR') or \ default_base_data_dir try: log_path = config.get('main', 'LOG_PATH') if log_path: raise Error('The option LOG_PATH in the main section is no ' 'longer supported, use LOG_DIR instead.') except NoOptionError: pass self.log_dir = config.get('main', 'LOG_DIR') if self.log_dir: # if the option is a directory, it will exist if not isdir(self.log_dir): raise ValueError("The LOG_DIR (%s) option is required to be a " "directory." % self.log_dir) self.base_url = config.get('main', 'BASE_URL') if not isdir(self.base_data_dir): raise ValueError("The BASE_DATA_DIR (%s) folder doesn't exist" % self.base_data_dir) self.working_dir = config.get('main', 'WORKING_DIR') if not isdir(self.working_dir): raise ValueError("The WORKING_DIR (%s) folder doesn't exist" % self.working_dir) self.max_upload_size = config.getint('main', 'MAX_UPLOAD_SIZE') self.require_approval = config.getboolean('main', 'REQUIRE_APPROVAL') self.qiita_env = config.get('main', 'QIITA_ENV') if not self.qiita_env: self.qiita_env = "" self.private_launcher = config.get('main', 'PRIVATE_LAUNCHER') self.plugin_launcher = config.get('main', 'PLUGIN_LAUNCHER') self.plugin_dir = config.get('main', 'PLUGIN_DIR') if not self.plugin_dir: self.plugin_dir = join(expanduser('~'), '.qiita_plugins') if not exists(self.plugin_dir): mkdir(self.plugin_dir) elif not isdir(self.plugin_dir): raise ValueError("The PLUGIN_DIR (%s) folder doesn't exist" % self.plugin_dir) self.valid_upload_extension = [ ve.strip() for ve in config.get('main', 'VALID_UPLOAD_EXTENSION').split(',') ] if (not self.valid_upload_extension or self.valid_upload_extension == ['']): self.valid_upload_extension = [] raise ValueError('No files will be allowed to be uploaded.') self.certificate_file = config.get('main', 'CERTIFICATE_FILE') if not self.certificate_file: self.certificate_file = join(install_dir, 'qiita_core', 'support_files', 'server.crt') self.cookie_secret = config.get('main', 'COOKIE_SECRET') if not self.cookie_secret: self.cookie_secret = b64encode(uuid4().bytes + uuid4().bytes) warnings.warn("Random cookie secret generated.") self.key_file = config.get('main', 'KEY_FILE') if not self.key_file: self.key_file = join(install_dir, 'qiita_core', 'support_files', 'server.key')
def calib_base(self): dir_path = self.get("FILE SYSTEM", "calib_base") if not dir_path: raise Error("Missing calib_base in configuration.") path = Path(dir_path).expanduser() return path
def __init__(self, option): Error.__init__(self, "No option %r" % (option)) self.option = option self.args = (option)