def _run_checks(self): allowed_name_chars = string.ascii_letters + string.digits + ',.-_' for attr in self._mandatory_attributes: if not getattr(self, 'get_{}'.format(attr), lambda: '')() and not getattr(self, attr, ''): raise ListenerModuleConfigurationError( 'Missing or empty {!r} attribute in configuration.'.format( attr)) if set(self.get_name()) - set(allowed_name_chars): raise ListenerModuleConfigurationError( 'The "name" of a listener module may only contain the following characters: {!r}' .format(allowed_name_chars)) if not inspect.isclass(self.get_listener_module_class()): raise ListenerModuleConfigurationError( 'Attribute "listener_module_class" must be a class.')
def _get_configuration(cls): """ Load configuration, optionally converting a plain Python class to a ListenerModuleConfiguration object. Set cls._configuration_class to a subclass of ListenerModuleConfiguration to change the returned object type. :return: configuration object :rtype: ListenerModuleConfiguration """ try: conf_class = cls.Configuration except AttributeError: raise ListenerModuleConfigurationError( 'Class {!r} missing inner "Configuration" class.'.format( cls.__name__)) if not inspect.isclass(conf_class): raise ListenerModuleConfigurationError( '{!s}.Configuration must be a class.'.format(cls.__name__)) if conf_class is ListenerModuleHandler.Configuration: raise ListenerModuleConfigurationError( 'Missing {!s}.Configuration class.'.format(cls.__name__)) if issubclass(cls.Configuration, cls._configuration_class): cls.Configuration.listener_module_class = cls return cls.Configuration() else: conf_obj = cls.Configuration() attrs = cls._configuration_class.get_configuration_keys() kwargs = dict(listener_module_class=cls) for attr in attrs: try: get_method = getattr(conf_obj, 'get_{}'.format(attr)) if not callable(get_method): raise ListenerModuleConfigurationError( 'Attribute {!r} of configuration class {!r} is not callable.' .format(get_method, conf_obj.__class__)) kwargs[attr] = get_method() continue except AttributeError: pass try: kwargs[attr] = getattr(conf_obj, attr) except AttributeError: pass # Checking for required attributes is done in ListenerModuleConfiguration(). return cls._configuration_class(**kwargs)
def __init__(self, *args, **kwargs): """ When subclassing, in __init__() first call must be: super(.., self).__init__(*args, **kwargs) self.config will be set by the metaclass. """ if not self.config: raise ListenerModuleConfigurationError( '{}.config was not set by meta class.'.format( self.__class__.__name__)) self.logger = get_logger(self.config.get_name()) self.ucr.load() self._lo = None self._po = None self._ldap_credentials = None self.logger.debug('Starting with configuration: %r', self.config)
def get_configuration(self): """ Get the configuration of a listener module. :return: dict """ res = dict() for key in self.get_configuration_keys(): getter = getattr(self, 'get_{}'.format(key), None) if getter and callable(getter): value = getter() else: if hasattr(self, key): self.logger.warn( "No 'get_%s' method found, using value of attribute %r directly.", key, key) value = getattr(self, key) else: raise ListenerModuleConfigurationError( 'Neither "get_{0}" method nor class attribute found for configuration key {0!r}.' .format(key)) res[key] = value return res