mirror of https://github.com/aewens/ircevents
First stable release, fixed various bugs in engine and updated example code to reflect current state of project
This commit is contained in:
parent
313bb99e94
commit
75e04130dc
|
@ -34,10 +34,14 @@ def _sent(source, state):
|
||||||
for line in send_lines:
|
for line in send_lines:
|
||||||
print(f"< {line.format()}")
|
print(f"< {line.format()}")
|
||||||
|
|
||||||
@events.when(_always=True)
|
@events.when(always_run=True)
|
||||||
def _display(line, state):
|
def _display(line, state):
|
||||||
print(f"> {line.format()}")
|
print(f"> {line.format()}")
|
||||||
|
|
||||||
|
@events.when(command="PING")
|
||||||
|
def _ping(line, state)
|
||||||
|
_send(f"PONG {line.params[0]}")
|
||||||
|
|
||||||
@events.when(command="001")
|
@events.when(command="001")
|
||||||
def _join(line, state):
|
def _join(line, state):
|
||||||
channels = state.get("channels", list())
|
channels = state.get("channels", list())
|
||||||
|
@ -45,6 +49,8 @@ def _join(line, state):
|
||||||
if channel not in server.channels:
|
if channel not in server.channels:
|
||||||
_send(f"JOIN {channel}")
|
_send(f"JOIN {channel}")
|
||||||
|
|
||||||
|
state.set("joined_channels", True)
|
||||||
|
|
||||||
# Load ircstates as a state mutation
|
# Load ircstates as a state mutation
|
||||||
events.use("ircstates", lambda raw: server.recv(raw))
|
events.use("ircstates", lambda raw: server.recv(raw))
|
||||||
|
|
||||||
|
|
|
@ -148,25 +148,25 @@ class Engine:
|
||||||
|
|
||||||
# Check if all required fields are found
|
# Check if all required fields are found
|
||||||
when_requires.remove(key)
|
when_requires.remove(key)
|
||||||
if len(whens_requires) > 0:
|
if len(when_requires) > 0:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
# If all requirements are found, stop checking this
|
# If all requirements are found, stop checking this
|
||||||
skip_whens.add(using_when)
|
skip_whens.add(using_when)
|
||||||
|
|
||||||
triggered = self._check_when(when)
|
triggered = self._check_when(when, mutation)
|
||||||
if not triggered:
|
if not triggered:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
state = self._states[namespace]
|
state = self._states[using.namespace]
|
||||||
func = self._whens_funcs.get(when_key)
|
func = self._whens_funcs.get(using_data)
|
||||||
if func is None:
|
if func is None:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
# Run callback using mutation data and state manager
|
# 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
|
Checks if mutation state will trigger callback
|
||||||
"""
|
"""
|
||||||
|
@ -174,21 +174,22 @@ class Engine:
|
||||||
trigger_when = True
|
trigger_when = True
|
||||||
|
|
||||||
# Use magic pair to always trigger callback
|
# 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
|
return trigger_when
|
||||||
|
|
||||||
# Check if conditions match mutation
|
# Check if conditions match mutation
|
||||||
for when_key, when_value in when.items():
|
for when_key in when._fields:
|
||||||
when_path = when_key.split("__")
|
when_path = when_key.split("__")
|
||||||
pointer = data
|
pointer = data
|
||||||
for wpath in when_path:
|
for wpath in when_path:
|
||||||
if not isinstance(pointer, dict):
|
if not hasattr(pointer, wpath):
|
||||||
eprint(f"Invalid path: {when_key}")
|
eprint(f"Invalid path at '{wpath}' in {when_key}")
|
||||||
break
|
break
|
||||||
|
|
||||||
pointer = pointer.get(wpath)
|
pointer = getattr(pointer, wpath, None)
|
||||||
|
|
||||||
when_status = False
|
when_status = False
|
||||||
|
when_value = getattr(when, when_key, None)
|
||||||
|
|
||||||
# Value can be a function complex checks
|
# Value can be a function complex checks
|
||||||
if callable(when_value):
|
if callable(when_value):
|
||||||
|
|
Loading…
Reference in New Issue