def get_setup_handlers(self) -> List[Handler]:
        """
        Build a list of setup handlers and cache it

        :return: The list of handlers
        """
        handlers = []
        """:type: [Handler]"""

        if self.allow_rapid_commit:
            # Rapid commit happens as the first thing in the post() stage
            handlers.append(RapidCommitHandler(self.rapid_commit_rejections))

        # These are mandatory
        handlers.append(ServerIdHandler(duid=self.server_id))
        handlers.append(ClientIdHandler())
        handlers.append(InterfaceIdOptionHandler())

        # Add setup handlers from extensions
        for extension_name, extension in server_extension_registry.items():
            create_setup_handlers = getattr(extension, 'create_setup_handlers', None)
            if create_setup_handlers:
                setup_handlers = create_setup_handlers()
                for setup_handler in setup_handlers:
                    logger.log(DEBUG_HANDLING, "Extension {} added {} to setup phase".format(
                        extension_name, setup_handler.__class__.__name__
                    ))
                handlers += setup_handlers

        return handlers
    def get_cleanup_handlers() -> List[Handler]:
        """
        Build a list of cleanup handlers and cache it

        :return: The list of handlers
        """
        handlers = []
        """:type: [Handler]"""

        # Reject unicast requests unless they have been explicitly permitted
        handlers.append(RejectUnwantedUnicastHandler())

        # Add cleanup handlers so they run last in the handling phase
        handlers.append(UnansweredIAOptionHandler())

        # Add cleanup handlers from extensions
        for extension_name, extension in server_extension_registry.items():
            create_cleanup_handlers = getattr(extension, 'create_cleanup_handlers', None)
            if create_cleanup_handlers:
                cleanup_handlers = create_cleanup_handlers()
                for cleanup_handler in cleanup_handlers:
                    logger.log(DEBUG_HANDLING, "Extension {} added {} to cleanup phase".format(
                        extension_name, cleanup_handler.__class__.__name__
                    ))
                handlers += cleanup_handlers

        # Confirm/Release/Decline messages always need a status
        handlers.append(AddMissingStatusOptionHandler())

        return handlers
Example #3
0
    def get_cleanup_handlers() -> List[Handler]:
        """
        Build a list of cleanup handlers and cache it

        :return: The list of handlers
        """
        handlers = []
        """:type: [Handler]"""

        # Reject unicast requests unless they have been explicitly permitted
        handlers.append(RejectUnwantedUnicastHandler())

        # Add cleanup handlers so they run last in the handling phase
        handlers.append(UnansweredIAOptionHandler())

        # Add cleanup handlers from extensions
        for extension_name, extension in server_extension_registry.items():
            create_cleanup_handlers = getattr(extension, 'create_cleanup_handlers', None)
            if create_cleanup_handlers:
                cleanup_handlers = create_cleanup_handlers()
                for cleanup_handler in cleanup_handlers:
                    logger.log(DEBUG_HANDLING, "Extension {} added {} to cleanup phase".format(
                        extension_name, cleanup_handler.__class__.__name__
                    ))
                handlers += cleanup_handlers

        # Confirm/Release/Decline messages always need a status
        handlers.append(AddMissingStatusOptionHandler())

        return handlers
Example #4
0
    def get_setup_handlers(self) -> List[Handler]:
        """
        Build a list of setup handlers and cache it

        :return: The list of handlers
        """
        handlers = []
        """:type: [Handler]"""

        if self.allow_rapid_commit:
            # Rapid commit happens as the first thing in the post() stage
            handlers.append(RapidCommitHandler(self.rapid_commit_rejections))

        # These are mandatory
        handlers.append(ServerIdHandler(duid=self.server_id))
        handlers.append(ClientIdHandler())
        handlers.append(InterfaceIdOptionHandler())

        # Add setup handlers from extensions
        for extension_name, extension in server_extension_registry.items():
            create_setup_handlers = getattr(extension, 'create_setup_handlers',
                                            None)
            if create_setup_handlers:
                setup_handlers = create_setup_handlers()
                for setup_handler in setup_handlers:
                    logger.log(
                        DEBUG_HANDLING,
                        "Extension {} added {} to setup phase".format(
                            extension_name, setup_handler.__class__.__name__))
                handlers += setup_handlers

        return handlers
Example #5
0
def get_config_loader() -> ConfigLoader:
    """
    Get the config loader with all extensions

    :return: The fully extended config loader
    """

    # Patch the parser because otherwise it will reject the example section in the schema

    # Construct the paths to all necessary files
    schema_filename = os.path.abspath(os.path.join(os.path.dirname(__file__), "config_schema.xml"))

    # Patch the parser if we have an old version of ZConfig
    # noinspection PyProtectedMember
    if 'schema' not in BaseParser._allowed_parents['example']:
        # noinspection PyProtectedMember
        BaseParser._allowed_parents['example'] += ['schema', 'sectiontype']

        # Add some properties
        ZConfig.info.SchemaType.example = None
        ZConfig.info.SectionType.example = None

        # Patch a method by saving the old method and replacing the original with a patched version
        ZConfig.info.oldCreateDerivedSchema = ZConfig.info.createDerivedSchema

        # noinspection PyUnresolvedReferences,PyPep8Naming
        def patchedCreateDerivedSchema(base: ZConfig.info.SchemaType) -> ZConfig.info.SchemaType:
            """
            Also copy the example section.

            :param base: The original
            :return: The copy
            """
            new = ZConfig.info.oldCreateDerivedSchema(base)
            new.example = base.example
            return new

        ZConfig.info.createDerivedSchema = patchedCreateDerivedSchema

    # Load the schema
    schema_loader = SchemaLoader()
    schema = schema_loader.loadURL(url=schema_filename)

    # Build the config loader based on the schema, extended with the schemas of option handlers
    config_loader = ConfigLoader(schema=schema)

    # Iterate over all server extensions
    for extension_name, extension in server_extension_registry.items():
        # Option handlers that refer to packages contain components
        if inspect.ismodule(extension) and hasattr(extension, '__path__'):
            # It's a package! Try to import
            try:
                config_loader.importSchemaComponent(extension.__name__)
                logger.debug("Configuration extension {} loaded".format(extension_name))
            except SchemaResourceError:
                # Component missing, assume it's a package without a config component
                pass

    return config_loader