def directory_exists(cls, config, value): if not os.path.exists(value): raise OptionCheckError("Directory {} does not exists".format(value), option=cls.__name__) if not os.path.isdir(value): raise OptionCheckError("{} is not a directory".format(value), option=cls.__name__)
def file_exists(cls, config, value): if not os.path.exists(value): raise OptionCheckError("File {} does not exists".format(value), option=cls.__name__) if not os.path.isfile(value): raise OptionCheckError("{} is not a file".format(value), option=cls.__name__)
def network_ip(option, config, value): """Must not be network or broadcast address (except if /31)""" if value.ip == value.value: raise OptionCheckError("The host part of {} is the network address of " "the subnet. Must be an IP of the subnet." .format(value), option=option.__name__) # Prefix length 31 is special, see RFC 3021 if value.prefixlen != 31 and value.ip == value.broadcast: raise OptionCheckError("The host part of {} is the broadcast address " "of the subnet. Must be an IP of the subnet." .format(value), option=option.__name__)
def f(cls, config, value): obj = value checked = [] for key in keys: if not isinstance(obj, collections.Mapping): path = cls.__name__ + ''.join(map('[{!r}]'.format, checked)) raise OptionCheckError("must be a mapping type like dict", option=path) checked.append(key) try: obj = obj[key] except KeyError: path = cls.__name__ + ''.join(map('[{!r}]'.format, checked)) raise OptionCheckError("Missing key", option=path) from None
def group_exists(option, config, value): """Must be a valid UNIX group""" try: return grp.getgrnam(value) except KeyError: raise OptionCheckError("Group {} does not exists".format(value), option=option.__name__)
def user_exists(option, config, value): """Must be a valid UNIX user""" try: return pwd.getpwnam(value) except KeyError: raise OptionCheckError("User {} does not exists".format(value), option=option.__name__)
def interface_exists(option, config, value): """Network interface must exists""" try: socket.if_nametoindex(value) except OSError: raise OptionCheckError("Interface {} not found".format(value), option=option.__name__)
def __call__(self, config, value): if not isinstance(value, self.types): types = ", ".join([type_.__qualname__ for type_ in self.types]) raise OptionCheckError( "Must be an instance of {}".format(types), option=self.option.__name__, )
def __call__(self, config, value): obj = value checked = [] for key in self.keys: if not isinstance(obj, collections.abc.Mapping): path = self.option.name + "".join(map("[{!r}]".format, checked)) raise OptionCheckError( "must be a mapping type like dict", option=path, ) checked.append(key) try: obj = obj[key] except KeyError: path = self.option.name + "".join(map("[{!r}]".format, checked)) raise OptionCheckError("Missing key", option=path) from None
def __call__(self, config, value): for i, v in enumerate(value): try: self.element_check(config, v) except ConfigOptionError as e: raise OptionCheckError("Error at index {:d}: {}" .format(i, e.args[0]), option=self.option.__name__)
def f(cls, config, value): for i, v in enumerate(value): try: element_check.__get__(None, cls)(config, v) except ConfigError as e: raise OptionCheckError("Error at index {:d}: {}".format( i, e.args[0]), option=cls.__name__)
def checker(cls, config, value): networks = config[other_option] first = netaddr.IPAddress(value.first) last = netaddr.IPAddress(value.last) contained = any(first in network and last in network for network in networks) if not contained: raise OptionCheckError("Range not contained in any of the " "networks {}".format(', '.join(networks)), option=cls.__name__)
def __call__(self, config, value): for k, v in value.items(): try: if self.key_check is not None: self.key_check(config, k) if self.value_check is not None: self.value_check(config, v) except ConfigOptionError as e: raise OptionCheckError("Error in key {}: {}" .format(k, e.args[0]), option=self.option.__name__)
def address_exists(cls, config, value): ip = IPRoute() if value.version == 4: family = socket.AF_INET elif value.version == 6: family = socket.AF_INET6 else: raise AssertionError("Unknown version {}".format(value.version)) if ip.get_addr(family=family, address=value.ip, prefixlen=value.prefixlen): raise OptionCheckError("No such address {}".format(value), option=cls.__name__)
def f(cls, config, value): for k, v in value.items(): try: if key_check is not None: key_check.__get__(None, cls)(config, k) if value_check is not None: value_check.__get__(None, cls)(config, v) except ConfigError as e: raise OptionCheckError("Error in key {}: {}".format( k, e.args[0]), option=cls.__name__)
def __call__(self, config, value): if 'PUBLIC' not in value and self.user_name not in value: raise OptionCheckError("No mapping for user {}" .format(self.user_name), option=self.option.__name__)
def checker(cls, config, value): if not (low <= value <= high): raise OptionCheckError( "Must be between {} and {} inclusively".format(low, high), option=cls.__name__)
def __call__(self, config, value): if not self.expr.match(value): raise OptionCheckError("Does not match regular expression {!r}" .format(self.expr.pattern), option=self.option.name)
def not_empty(option, config, value): """Must not be empty""" if len(value) <= 0: raise OptionCheckError("Must not be empty", option=option.__name__)
def checker(cls, config, value): if 'PUBLIC' not in value and user_name not in value: raise OptionCheckError("No mapping for user {}".format(user_name), option=cls.__name__)
def user_exists(cls, config, value): try: return pwd.getpwnam(value) except KeyError: raise OptionCheckError("User {} does not exists".format(value), option=cls.__name__)
def group_exists(cls, config, value): try: return grp.getgrnam(value) except KeyError: raise OptionCheckError("Group {} does not exists".format(value), option=cls.__name__)
def f(cls, config, value): if not isinstance(value, types): raise OptionCheckError("Must be an instance of {}".format( ', '.join(types)), option=cls.__name__)
def __call__(self, config, value): if not (self.low <= value <= self.high): raise OptionCheckError("Must be between {!r} and {!r} inclusively" .format(self.low, self.high), option=self.option.__name__)
def checker(cls, config, value): if value <= threshold: raise OptionCheckError("Must be greater than {}".format(threshold), option=cls.__name__)
def interface_exists(cls, config, value): try: socket.if_nametoindex(value) except OSError: raise OptionCheckError("Interface {} not found".format(value), option=cls.__name__)
def __call__(self, config, value): if value <= self.threshold: raise OptionCheckError("Must be greater than {!r}" .format(self.threshold), option=self.option.__name__)
def not_empty(cls, config, value): if len(value) <= 0: raise OptionCheckError("Must not be empty", option=cls.__name__)
def static_check(cls, config, value): if not re.match(r'\A[a-z][a-z0-9-]*\Z', value, re.ASCII): raise OptionCheckError("not a valid node ID", option=cls)