示例#1
0
    def _configure_mediator_server(self, context, config):
        '''
        Create / Set-Up Mediator Server according to config data.
        '''

        self.dotted_server = config.get('server',
                                        'mediator',
                                        'type')
        # Get log_level if we have it, if not: convert null to none.
        initial_log_level = null_to_none(ConfigContext.log_level(context))
        # Use SystemManager's DebugFlag setting?
        debug_flags = self._manager.system.debug
        # Grab ut flag from background?
        ut_flagged = background.testing.get_unit_testing()

        # ...And get ready for running our sub-proc.
        self.server = multiproc.set_up(
            proc_name=self.dotted_server,
            config=config,
            context=context,
            entry_fn=_start_server,
            initial_log_level=initial_log_level,
            debug_flags=debug_flags,
            unit_testing=ut_flagged)
示例#2
0
    def _search(self, place: Dict[str, Any], dotted: Optional[label.DotStr],
                data: EncodedEither,
                data_type: Type[Encodable]) -> Optional[Encodable]:
        '''
        Provide `self._registry` as starting point of search.

        Searches the registry.

        If 'data_type' is supplied, will restrict search for registered
        Encodable to use to just that or subclasses.

        If `dotted` is provided, walks that keypath and
        returns whatever is registered to that.

        Otherwise, recursively walk our registry dict to find something that
        will claim() `data`.

        Returns registree or None.
        '''
        # Set data_type to any Encodable if not supplied.
        if data_type is None:
            data_type = Encodable

        # ---
        # Provided with a dotted key. Use that for explicit search.
        # ---
        # More like 'get' than search...
        if dotted and label.is_dotstr(dotted):
            keys = label.regularize(dotted)
            # Path shouldn't be long. Just let Null-pattern pretend to be a
            # dict if we hit a 'Does Not Exist'.
            for key in keys:
                place = place.get(key, Null())
            # Done, return either the registree or None.
            place = null_to_none(place)
            # Place must be:
            #   - An Encodable.
            #   - The same class or a subclass of data_type.
            if (not place or (type(place) != data_type
                              and not issubclass(place, data_type))):
                # Don't return some random halfway point in the registry tree.
                place = None
            return place

        # ---
        # Search for a claimer...
        # ---
        for key in place:
            node = place[key]
            # If we got a sub-tree/branch, recurse into it.
            if isinstance(node, dict):
                result = self._search(node, dotted, data, data_type)
                # Did that find it?
                if result:
                    # Yes; return decoded result.
                    return result
                else:
                    # No; done with this - go to next node.
                    continue

            # If we got a leaf node, check it.
            if not issubclass(node, Encodable):
                self._log_warning(
                    "Unexpected node in registry... expect either "
                    f"strings or Encodables, got: {node}")
                continue

            # Do they claim this?
            claiming, _, _ = node.claim(data)
            # claiming, claim, reason = node.claim(data)
            if claiming:
                # Yes; return decoded result.
                return node

            # Else, not this; continue looking.

        # ---
        # Nothing found.
        # ---
        return None
示例#3
0
def event_math(event: MathOutputEvent) -> Tuple[str, str]:
    '''
    Gives the math tree of the event.
    '''
    return ('output',
            null_to_none(MathOutputTree.to_map(event.output)))
示例#4
0
def event_type(event: OutputEvent) -> Tuple[str, str]:
    '''
    Gives the dotted type of the event.
    '''
    return ('type', null_to_none(event.dotted))
示例#5
0
def event_id(event: OutputEvent) -> Tuple[str, str]:
    '''
    Gives the input id of the event.
    '''
    return ('id', null_to_none(event.serial_id.serialize(None)))
示例#6
0
def event_input(event: OutputEvent) -> Tuple[str, str]:
    '''
    Gives the input of the event.
    '''
    return ('input', null_to_none(InputContext.input(event.context)))