Esempio n. 1
0
def build_keyboard(cls, i, ex, args, meta_args):
    """Listen for keyboard event"""
    if meta_args.mode == 'single key':
        ex.on_event = hive.socket(cls.add_single_listener, identifier="event.add_handler")

        args.key = hive.parameter("str.keycode", "w")
        i.key = hive.property(cls, "key", "str.keycode", args.key)

        i.push_key = hive.push_in(i.key)
        ex.key = hive.antenna(i.push_key)

        i.on_key_changed = hive.triggerable(cls.change_listener_keys)
        hive.trigger(i.push_key, i.on_key_changed)

        i.on_pressed = hive.triggerfunc()
        ex.on_pressed = hive.hook(i.on_pressed)

        i.on_released = hive.triggerfunc()
        ex.on_released = hive.hook(i.on_released)

        i.is_pressed = hive.property(cls, "is_pressed", "bool")
        i.pull_is_pressed = hive.pull_out(i.is_pressed)
        ex.is_pressed = hive.output(i.pull_is_pressed)

    else:
        ex.on_event = hive.socket(cls.add_any_listener, identifier="event.add_handler")

        i.key_pressed = hive.property(cls, 'key_pressed', data_type='str.keycode')
        i.pull_key_pressed = hive.push_out(i.key_pressed)
        ex.key_pressed = hive.output(i.pull_key_pressed)

        i.key_released = hive.property(cls, 'key_released', data_type='str.keycode')
        i.pull_key_released = hive.push_out(i.key_released)
        ex.key_released = hive.output(i.pull_key_released)
Esempio n. 2
0
def build_server(cls, i, ex, args):
    i.local_address = hive.property(cls, "local_address", "tuple")
    i.push_bind_address = hive.push_in(i.local_address)
    ex.bind_to = hive.antenna(i.push_bind_address)

    i.do_bind = hive.triggerable(cls.do_bind)
    hive.trigger(i.push_bind_address, i.do_bind)

    # Receiving connection
    i.connected_address = hive.property(cls, "connected_address", "tuple")
    i.push_connected_address = hive.push_out(i.connected_address)
    ex.on_client_connected = hive.output(i.push_connected_address)

    # Receiving connection
    i.disconnected_address = hive.property(cls, "disconnected_address",
                                           "tuple")
    i.push_disconnected_address = hive.push_out(i.disconnected_address)
    ex.on_client_disconnected = hive.output(i.push_disconnected_address)

    # Receiving
    i.from_address = hive.property(cls, "from_address", "tuple")
    i.pull_from_address = hive.pull_out(i.from_address)
    ex.from_address = hive.output(i.pull_from_address)

    i.received_data = hive.property(cls, "received_data", "bytes")
    i.push_received = hive.push_out(i.received_data)
    ex.on_received = hive.output(i.push_received)

    # Hive callbacks
    i.on_disconnected = hive.triggerfunc()
    i.on_connected = hive.triggerfunc()
    i.on_received = hive.triggerfunc()

    hive.trigger(i.on_received, i.push_received)
    hive.trigger(i.on_connected, i.push_connected_address)
    hive.trigger(i.on_disconnected, i.push_disconnected_address)

    # Sending
    i.to_address = hive.property(cls, "to_address", "tuple")
    i.pull_to_address = hive.pull_in(i.to_address)
    ex.to_address = hive.antenna(i.pull_to_address)

    i.outgoing_data = hive.property(cls, "outgoing_data", "bytes")
    i.push_outgoing_data = hive.push_in(i.outgoing_data)
    ex.send = hive.antenna(i.push_outgoing_data)

    i.do_send_data = hive.triggerable(cls.do_send)
    hive.trigger(i.push_outgoing_data, i.pull_to_address, pretrigger=True)
    hive.trigger(i.push_outgoing_data, i.do_send_data)

    i.synchronise_data = hive.triggerable(cls.synchronise)
    i.on_tick = OnTick()
    hive.connect(i.on_tick.on_tick, i.synchronise_data)
Esempio n. 3
0
def build_buffer(i, ex, args, meta_args):
    """Store the input value and output saved value.

    In pull mode, the trigger is used to update the internal value.
    In push mode, the trigger is used to output the internal value.

    Can be used to cache changing values
    """
    args.start_value = hive.parameter(meta_args.data_type, None)
    i.cached_value = hive.attribute(meta_args.data_type, args.start_value)

    if meta_args.mode == "push":
        i.push_value = hive.push_in(i.cached_value)
        ex.value = hive.antenna(i.push_value)

        i.push_cached_value = hive.push_out(i.cached_value)
        ex.cached_value = hive.output(i.push_cached_value)

        ex.output = hive.entry(i.push_cached_value)

    elif meta_args.mode == "pull":
        i.pull_value = hive.pull_in(i.cached_value)
        ex.value = hive.antenna(i.pull_value)

        i.pull_cached_value = hive.pull_out(i.cached_value)
        ex.cached_value = hive.output(i.pull_cached_value)

        ex.update_cache = hive.entry(i.pull_value)
Esempio n. 4
0
def build_foreach(i, ex, args, meta_args):
    """Iterate over iterable object"""
    # Set iterable
    i.iterable = hive.variable("$iterable[int]")
    i.pull_iterable = hive.pull_in(i.iterable)
    ex.iterable = hive.antenna(i.pull_iterable)

    i.do_trig = hive.triggerfunc()
    i.trig_in = hive.triggerable(i.do_trig)
    ex.start = hive.entry(i.trig_in)

    i.break_ = hive.variable('bool', False)

    i.item = hive.variable(meta_args.data_type)
    i.push_item = hive.push_out(i.item)
    ex.item = hive.output(i.push_item)

    i.index = hive.variable('int', 0)
    i.pull_index = hive.pull_out(i.index)
    ex.index = hive.output(i.pull_index)

    i.finished = hive.triggerfunc()

    i.do_break = hive.modifier(do_break)
    ex.break_ = hive.entry(i.do_break)
    ex.finished = hive.hook(i.finished)

    i.iter = hive.modifier(do_iter)
    hive.trigger(i.do_trig, i.pull_iterable)
    hive.trigger(i.do_trig, i.iter)
Esempio n. 5
0
def build_input_handler(cls, i, ex, args):
    i.update = hive.triggerable(cls.update)
    ex.update = hive.entry(i.update)

    i.event = hive.property(cls, "event", "tuple")
    i.push_event = hive.push_out(i.event)
    ex.event = hive.output(i.push_event)
Esempio n. 6
0
def build_convert(i, ex, args, meta_args):
    i.value_in = hive.variable(meta_args.from_data_type)
    i.value_out = hive.variable(meta_args.to_data_type)

    # For push in, push out
    if meta_args.mode == "push":
        i.ppin = hive.push_in(i.value_in)
        i.ppout = hive.push_out(i.value_out)

        hive.trigger(i.ppin, i.ppout)

    else:
        i.ppin = hive.pull_in(i.value_in)
        i.ppout = hive.pull_out(i.value_out)

        hive.trigger(i.ppout, i.ppin, pretrigger=True)

    ex.value_in = hive.antenna(i.ppin)
    ex.value_out = hive.output(i.ppout)

    # For casting (explicit conversion)
    if meta_args.conversion == "cast":
        to_base_type_name = hive.get_base_data_type(meta_args.to_data_type)
        value_cls = _type_map[to_base_type_name]

        def converter(self):
            self._value_out = value_cls(self._value_in)

        i.do_conversion = hive.modifier(converter)
        hive.trigger(i.ppout, i.do_conversion, pretrigger=True)

    # For duck typing, move value through
    else:
        i.move_value = hive.modifier(move_value)
        hive.trigger(i.ppin, i.move_value)
Esempio n. 7
0
def build_spawn(cls, i, ex, args, meta_args):
    """Spawn an entity into the scene"""
    ex.get_spawn_entity = hive.socket(cls.set_spawn_entity, "entity.spawn")

    # Associate entity with this hive so it is safely destroyed
    ex.get_register_destructor = hive.socket(cls.set_register_destructor,
                                             "entity.register_destructor")
    ex.on_entity_process_instantiated = hive.plugin(
        cls.on_entity_process_created, "bind.on_created")

    i.entity_class_id = hive.property(cls, "entity_class_id",
                                      "str.entity_class_id")
    i.pull_class_id = hive.pull_in(i.entity_class_id)
    ex.entity_class_id = hive.antenna(i.pull_class_id)

    i.entity_last_created_id = hive.property(cls, "entity_last_created_id",
                                             "int.entity_id")

    if meta_args.spawn_hive:
        i.pull_entity_id = hive.pull_out(i.entity_last_created_id)
        ex.entity_last_created_id = hive.output(i.pull_entity_id)

    else:
        i.push_entity_id = hive.push_out(i.entity_last_created_id)
        ex.created_entity_id = hive.output(i.push_entity_id)

    i.do_spawn = hive.triggerable(cls.do_spawn_entity)
    i.trigger = hive.triggerfunc(i.do_spawn)
    i.on_triggered = hive.triggerable(i.trigger)

    hive.trigger(i.trigger, i.pull_class_id, pretrigger=True)

    # Process instantiator
    if meta_args.spawn_hive:
        i.instantiator = Instantiator(forward_events='all',
                                      bind_process='child')

        # Pull entity to instantiator
        hive.connect(i.pull_entity_id, i.instantiator.entity_id)

        # Get last created
        ex.hive_class = hive.antenna(i.instantiator.hive_class)
        ex.last_process_id = hive.output(i.instantiator.last_process_id)
        ex.stop_process = hive.antenna(i.instantiator.stop_process)
        ex.pause_events = hive.antenna(i.instantiator.pause_events)
        ex.resume_events = hive.antenna(i.instantiator.resume_events)

        # Instantiate
        hive.trigger(i.trigger, i.instantiator.create)

    else:
        # Finally push out entity
        hive.trigger(i.trigger, i.push_entity_id)

    ex.spawn = hive.entry(i.on_triggered)
Esempio n. 8
0
def build_transistor(i, ex, args, meta_args):
    """Convert a pull output into a push output, using a trigger input"""
    i.value = hive.variable(meta_args.data_type)
    i.pull_value = hive.pull_in(i.value)
    ex.value = hive.antenna(i.pull_value)

    i.push_value = hive.push_out(i.value)
    ex.result = hive.output(i.push_value)

    ex.trigger = hive.entry(i.pull_value)

    hive.trigger(i.pull_value, i.push_value)
Esempio n. 9
0
def build_struct(cls, i, ex, args):
    """Interface to Struct class"""
    i.tuple = hive.variable("tuple")
    i.pack_in = hive.push_in(i.tuple)
    ex.pack = hive.antenna(i.pack_in)

    i.bytes = hive.variable("bytes")
    i.unpack_out = hive.push_out(i.bytes)
    ex.unpack = hive.output(i.unpack_out)

    i.size_out = hive.pull_out(cls.size)
    ex.size = hive.output(i.size_out)
Esempio n. 10
0
def build_count(i, ex, args):
    """Simple integer counter"""
    args.start_value = hive.parameter("int", 0)
    ex.count = hive.attribute("int", args.start_value)

    i.do_count_up = hive.modifier(do_count_up)
    ex.increment = hive.entry(i.do_count_up)

    i.do_count_down = hive.modifier(do_count_down)
    ex.decrement = hive.entry(i.do_count_down)

    i.count_out = hive.push_out(ex.count)
    ex.count_out = hive.output(i.count_out)
Esempio n. 11
0
def build_func(i, ex, args, meta_args):
    """Define callable object from expression"""
    # Get AST and parse ags
    ast_node = ast.parse(meta_args.definition, mode='exec')
    assert isinstance(ast_node, ast.Module)
    assert len(ast_node.body) == 1
    func_def_ast = ast_node.body[0]
    assert isinstance(func_def_ast, ast.FunctionDef)

    visitor = FunctionDefinitionVisitor()
    visitor.visit(ast_node)

    # Build the function itself
    user_namespace = {}
    exec(meta_args.definition, user_namespace)
    user_func = user_namespace[func_def_ast.name]

    has_return = visitor.output_type_name is not function_no_return

    # Define modifier source code (here, we will lookup the "user_func" name later)
    function_call_str = "user_func({})".format(", ".join(
        ["self._{}".format(a) for a in visitor.antennae]))
    result_body = result_str if has_return else ""
    modifier_decl = modifier_str.format(function_call_str=function_call_str,
                                        result_body=result_body)

    # Build modifier function
    namespace = {'user_func': user_func}
    exec(modifier_decl, namespace)
    modifier_func = namespace['modifier']

    # Create modifier bees
    i.modifier = hive.modifier(modifier_func)
    ex.trigger = hive.entry(i.modifier)

    i.pull_all_inputs = hive.triggerfunc()

    # Create IO pins
    for arg, (type_name, default) in visitor.antennae.items():
        attr = hive.variable(type_name, start_value=default)
        setattr(i, arg, attr)
        pull_in = hive.pull_in(attr)
        setattr(ex, arg, hive.antenna(pull_in))
        hive.trigger(i.pull_all_inputs, pull_in, pretrigger=True)

    if has_return:
        result_name = 'result'
        attr = hive.variable(visitor.output_type_name)
        setattr(i, result_name, attr)
        push_out = hive.push_out(attr)
        setattr(ex, result_name, hive.output(push_out))
Esempio n. 12
0
def build_commandline(cls, i, ex, args):
    i.start = h.triggerable(cls.start)
    i.stop = h.triggerable(cls.stop)
    i.flush = h.triggerable(cls.flush)
    prop_command = h.property(cls, "command", "str")
    i.push_command = h.push_out(prop_command)

    ex.prop_command = prop_command
    ex.command = h.output(i.push_command)
    ex.send_command = cls.send_command
    ex.start = h.entry(i.start)
    ex.flush = h.entry(i.flush)
    ex.stop = h.entry(i.stop)
    ex.listen = h.socket(cls.add_listener)
Esempio n. 13
0
def build_myhive(cls, i, ex, args):
    ex.a_ = hive.property(cls, "a", "int")
    ex.b_ = hive.property(cls, "b", "int")

    i.a_in = hive.push_in(ex.a_)
    i.b_in = hive.push_in(ex.b_)

    ex.a = hive.antenna(i.a_in)
    ex.b = hive.antenna(i.b_in)

    ex.c_ = hive.attribute()
    i.c_out = hive.push_out(ex.c_)
    ex.c = hive.output(i.c_out)

    # On triggered
    def on_triggered(this):
        this.c_ = this.a_ + this.b_
        this.c.push()

    i.on_triggered = hive.modifier(lambda this: setattr(this, 'c_', (this.a_ + this.b_)))
    ex.trigger = hive.entry(i.on_triggered)
Esempio n. 14
0
def build_server(cls, i, ex, args):
    i.connect_address = hive.property(cls, "server_address", "tuple")
    i.push_connect = hive.push_in(i.connect_address)
    ex.connect_to = hive.antenna(i.push_connect)

    i.do_connect = hive.triggerable(cls.do_connect)
    hive.trigger(i.push_connect, i.do_connect)

    # Hive callbacks
    i.on_disconnected = hive.triggerfunc()
    i.on_connected = hive.triggerfunc()
    i.on_received = hive.triggerfunc()

    # Receiving connection
    ex.on_connected = hive.hook(i.on_connected)

    # Lost connection
    ex.on_disconnected = hive.hook(i.on_disconnected)

    # Receiving
    i.received_data = hive.property(cls, "received_data", "bytes")
    i.push_received = hive.push_out(i.received_data)
    ex.on_received = hive.output(i.push_received)

    hive.trigger(i.on_received, i.push_received)

    # Sending
    i.outgoing_data = hive.property(cls, "outgoing_data", "bytes")
    i.push_outgoing_data = hive.push_in(i.outgoing_data)
    ex.send = hive.antenna(i.push_outgoing_data)

    i.do_send_data = hive.triggerable(cls.do_send)
    hive.trigger(i.push_outgoing_data, i.do_send_data)

    i.synchronise_data = hive.triggerable(cls.synchronise)
    i.on_tick = OnTick()
    hive.connect(i.on_tick.on_tick, i.synchronise_data)