-
-
Notifications
You must be signed in to change notification settings - Fork 98
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
316: Move things, break loops r=pathunstrom a=astronouth7303 Grab a u-haul! 🚚 Generally moving things and reducing imports to improve the import loop situation * Break up "the machinery implementing a thing" and "a pile of things that use the thing" * Prefer module imports to individual imports, so any unavoidable loops don't wreck everything. Depends on #306 because I didn't feel like dealing with the merge conflict. No docs changes because this doesn't touch anything publicly documented. Co-authored-by: Jamie Bliss <jamie@ivyleav.es>
- Loading branch information
Showing
16 changed files
with
273 additions
and
250 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
""" | ||
The event machinery | ||
""" | ||
|
||
import logging | ||
import re | ||
|
||
__all__ = ( | ||
'EventMixin', 'BadEventHandlerException', | ||
) | ||
|
||
boundaries_finder = re.compile('(.)([A-Z][a-z]+)') | ||
boundaries_finder_2 = re.compile('([a-z0-9])([A-Z])') | ||
|
||
|
||
def camel_to_snake(txt): | ||
s1 = boundaries_finder.sub(r'\1_\2', txt) | ||
return boundaries_finder_2.sub(r'\1_\2', s1).lower() | ||
|
||
|
||
class BadEventHandlerException(TypeError): | ||
|
||
def __init__(self, instance, method, event): | ||
object_type = type(instance) | ||
event_type = type(event) | ||
o_name = object_type.__name__ | ||
e_name = event_type.__name__ | ||
article = ['a', 'an'][int(e_name.lower()[0] in "aeiou")] | ||
|
||
message = f""" | ||
{o_name}.{method}() signature incorrect, it should accept {article} {e_name} object and a signal function. | ||
{e_name} is a dataclass that represents an event. Its attributes | ||
tell you about the event. | ||
The signal function is a function you can call that accepts an event instance | ||
as its only parameter. Call it to add an event to the queue. You don't have to | ||
use it, but it is a mandatory argument provided by ppb. | ||
It should look like this: | ||
def {method}({e_name.lower()}_event: {e_name}, signal_function): | ||
(Your code goes here.) | ||
""" | ||
super().__init__(message) | ||
|
||
|
||
class EventMixin: | ||
def __event__(self, bag, fire_event): | ||
elog = logging.getLogger('game.events') | ||
|
||
name = camel_to_snake(type(bag).__name__) | ||
meth_name = 'on_' + name | ||
meth = getattr(self, meth_name, None) | ||
if callable(meth): | ||
try: | ||
elog.debug(f"Calling handler {meth} for {name}") | ||
meth(bag, fire_event) | ||
except TypeError as ex: | ||
from inspect import signature | ||
sig = signature(meth) | ||
try: | ||
sig.bind(bag, fire_event) | ||
except TypeError: | ||
raise BadEventHandlerException(self, meth_name, bag) from ex | ||
else: | ||
raise |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.