First stable release, fixed various bugs in engine and updated example code to reflect current state of project

This commit is contained in:
aewens 2020-03-16 16:18:28 -05:00
parent 313bb99e94
commit 75e04130dc
3 changed files with 20 additions and 13 deletions

View File

@ -34,10 +34,14 @@ def _sent(source, state):
for line in send_lines:
print(f"< {line.format()}")
@events.when(_always=True)
@events.when(always_run=True)
def _display(line, state):
print(f"> {line.format()}")
@events.when(command="PING")
def _ping(line, state)
_send(f"PONG {line.params[0]}")
@events.when(command="001")
def _join(line, state):
channels = state.get("channels", list())
@ -45,6 +49,8 @@ def _join(line, state):
if channel not in server.channels:
_send(f"JOIN {channel}")
state.set("joined_channels", True)
# Load ircstates as a state mutation
events.use("ircstates", lambda raw: server.recv(raw))

View File

@ -1 +1 @@
0.1.11
0.2.0

View File

@ -148,25 +148,25 @@ class Engine:
# Check if all required fields are found
when_requires.remove(key)
if len(whens_requires) > 0:
if len(when_requires) > 0:
continue
# If all requirements are found, stop checking this
skip_whens.add(using_when)
triggered = self._check_when(when)
triggered = self._check_when(when, mutation)
if not triggered:
continue
state = self._states[namespace]
func = self._whens_funcs.get(when_key)
state = self._states[using.namespace]
func = self._whens_funcs.get(using_data)
if func is None:
return None
# Run callback using mutation data and state manager
func(data, state)
func(mutation, state)
def _check_when(self, when):
def _check_when(self, when, data):
"""
Checks if mutation state will trigger callback
"""
@ -174,21 +174,22 @@ class Engine:
trigger_when = True
# Use magic pair to always trigger callback
if when.get("always_run") is True:
if getattr(when, "always_run", None) is True:
return trigger_when
# Check if conditions match mutation
for when_key, when_value in when.items():
for when_key in when._fields:
when_path = when_key.split("__")
pointer = data
for wpath in when_path:
if not isinstance(pointer, dict):
eprint(f"Invalid path: {when_key}")
if not hasattr(pointer, wpath):
eprint(f"Invalid path at '{wpath}' in {when_key}")
break
pointer = pointer.get(wpath)
pointer = getattr(pointer, wpath, None)
when_status = False
when_value = getattr(when, when_key, None)
# Value can be a function complex checks
if callable(when_value):