def _get_section_option(self, section_option): """splits ``section_option`` (section.option) in two parts and returns them as list ``[section, option]``, if: * it likes the format of ``section_option`` * the ``section`` is known * the ``option`` is known Else one of the following exceptions will be thrown: * `BadOptionError` * `NoSectionError` * `NoOptionError` """ sect_opt = section_option.lower().split(".") # TODO: cache it if len(sect_opt) != 2 or not sect_opt[0] or not sect_opt[1]: raise BadOptionError( _("Bad format: '%s' - expected: " "section.option") % get_unicode(section_option)) if not sect_opt[0] in self._cfg: raise NoSectionError(sect_opt[0]) if not sect_opt[1] in self._cfg[sect_opt[0]]: raise NoOptionError(sect_opt[1], sect_opt[0]) return sect_opt
def _doveadmpw(password, scheme, encoding): """Communicates with Dovecot's doveadm and returns the hashed password: {scheme[.encoding]}hash """ if encoding: scheme = ".".join((scheme, encoding)) cmd_args = [ cfg_dget("bin.doveadm"), "pw", "-s", scheme, "-p", get_unicode(password), ] process = Popen(cmd_args, stdout=PIPE, stderr=PIPE) stdout, stderr = process.communicate() if process.returncode: raise VMMError(stderr.strip().decode(ENCODING), VMM_ERROR) hashed = stdout.strip().decode(ENCODING) if not hashed.startswith("{%s}" % scheme): raise VMMError( "Unexpected result from %s: %s" % (cfg_dget("bin.doveadm"), hashed), VMM_ERROR, ) return hashed
def is_dir(path): """Check if the expanded path is a directory. When the expanded path is a directory the expanded path will be returned. Otherwise a ConfigValueError will be raised. """ path = expand_path(path) if lisdir(path): return path raise ConfigValueError(_("No such directory: %s") % get_unicode(path))
def check_size_value(value): """Check if the size value *value* has the proper format, e.g.: 1024k. Returns the validated value string if it has the expected format. Otherwise a `ConfigValueError` will be raised.""" try: size_in_bytes(value) except (TypeError, ValueError) as err: raise ConfigValueError( _("Not a valid size value: '%s'") % get_unicode(value)) from err return value
def check_mailbox_format(format): """ Check if the mailbox format *format* is supported. When the *format* is supported it will be returned, otherwise a `ConfigValueError` will be raised. """ format = format.lower() if known_format(format): return format raise ConfigValueError( _("Unsupported mailbox format: '%s'") % get_unicode(format))
def check_dovecot_version(version_string): """Check if the *version_string* has the proper format, e.g.: '2.0.0', and if the configured version is >= MIN_DOVECOT_VERSION. Returns the validated version string if it has the expected format. Otherwise a `ConfigValueError` will be raised. """ if not VERSION_RE.match(version_string): raise ConfigValueError( _("Not a valid Dovecot version: '%s'") % get_unicode(version_string)) if version_hex(version_string) < MIN_DOVECOT_VERSION: raise ConfigValueError( _("vmm requires Dovecot >= %s") % version_str(MIN_DOVECOT_VERSION)) return version_string
def bool_new(self, value): """Converts the string `value` into a `bool` and returns it. | '1', 'on', 'yes' and 'true' will become `True` | '0', 'off', 'no' and 'false' will become `False` Throws a `ConfigValueError` for all other values, except bools. """ if isinstance(value, bool): return value if value.lower() in self.BOOLEAN_STATES: return self.BOOLEAN_STATES[value.lower()] else: raise ConfigValueError( _("Not a boolean: '%s'") % get_unicode(value))
def check_db_ssl_mode(ssl_mode): """Check if the *ssl_mode* is one of the SSL modes, known by pgsql.""" if ssl_mode in DB_SSL_MODES: return ssl_mode raise ConfigValueError( _("Unknown pgsql SSL mode: '%s'") % get_unicode(ssl_mode))
def str(self, section, option): """Returns the value of the `option` from `section`, converted to Unicode.""" return get_unicode(self.get(section, option))