Best Python code snippet using localstack_python
descriptors.py
Source:descriptors.py
1# Copyright 2020, Chris PeBenito <pebenito@ieee.org>2#3# SPDX-License-Identifier: LGPL-2.1-only4#5import re6from typing import Callable, Union7from ..exception import InvalidCheckValue8from ..descriptors import CriteriaDescriptor, CriteriaPermissionSetDescriptor9class ConfigDescriptor(CriteriaDescriptor):10 """11 Single item configuration option descriptor.12 Parameter:13 lookup_function The name of the SELinuxPolicy lookup function,14 e.g. lookup_type or lookup_boolean.15 Read-only instance attribute use (obj parameter):16 checkname The name of the check.17 policy The instance of SELinuxPolicy18 """19 def __init__(self, lookup_function: Union[Callable, str]) -> None:20 super().__init__(lookup_function=lookup_function)21 def __set__(self, obj, value):22 if not value:23 self.instances[obj] = None24 else:25 try:26 super().__set__(obj, value.strip())27 except ValueError as ex:28 raise InvalidCheckValue("{}: Invalid {} setting: {}".format(29 obj.checkname, self.name, ex)) from ex30class ConfigSetDescriptor(CriteriaDescriptor):31 """32 Descriptor for a configuration option set.33 Parameter:34 lookup_function The name of the SELinuxPolicy lookup function,35 e.g. lookup_type or lookup_boolean.36 Keyword Parameters:37 strict (Bool) If True, all objects must exist in the policy38 when setting the value. If False, any objects that39 fail the policy lookup will be dropped instead of raising40 an exception. The default is True.41 expand (Bool) If True, each object will be expanded. Default42 is False.43 Read-only instance attribute use (obj parameter):44 checkname The name of the check.45 log A logger instance.46 policy The instance of SELinuxPolicy47 """48 def __init__(self, lookup_function: Union[Callable, str], strict: bool = True,49 expand: bool = False) -> None:50 super().__init__(lookup_function=lookup_function, default_value=frozenset())51 self.strict = strict52 self.expand = expand53 def __set__(self, obj, value):54 if not value:55 self.instances[obj] = frozenset()56 else:57 log = obj.log58 if callable(self.lookup_function):59 lookup = self.lookup_function60 else:61 lookup = getattr(obj.policy, self.lookup_function)62 ret = set()63 for item in (i for i in re.split(r"\s", value) if i):64 try:65 o = lookup(item)66 if self.expand:67 ret.update(o.expand())68 else:69 ret.add(o)70 except ValueError as e:71 if self.strict:72 log.error("Invalid {} item: {}".format(self.name, e))73 log.debug("Traceback:", exc_info=e)74 raise InvalidCheckValue("{}: Invalid {} item: {}".format(75 obj.checkname, self.name, e)) from e76 log.info("{}: Invalid {} item: {}".format(77 obj.checkname, self.name, e))78 self.instances[obj] = frozenset(ret)79class ConfigPermissionSetDescriptor(CriteriaPermissionSetDescriptor):80 """81 Descriptor for a configuration permissions set.82 Read-only instance attribute use (obj parameter):83 checkname The name of the check.84 policy The instance of SELinuxPolicy85 tclass If it exists, it will be used to validate the86 permissions. See validate_perms_any()87 """88 def __init__(self) -> None:89 super().__init__(default_value=frozenset())90 def __set__(self, obj, value):91 if not value:92 self.instances[obj] = frozenset()93 else:94 try:95 super().__set__(obj, (v for v in value.split(" ") if v))96 except ValueError as ex:97 raise InvalidCheckValue("{}: Invalid {} setting: {}".format(...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!