-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
boke0
committed
Mar 10, 2021
1 parent
90debe4
commit 01629ca
Showing
3 changed files
with
96 additions
and
30 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,58 @@ | ||
class Event: | ||
def __init__(self, doc=None): | ||
class EventManager: | ||
def __init__(self, events=[], doc=None): | ||
self.__doc__ = doc | ||
self.events = {} | ||
for event in events: | ||
self.events[event] = Event() | ||
|
||
def __get__(self, obj, objtype=None): | ||
if obj is None: | ||
return Self | ||
return EventHandler(self, obj) | ||
return self | ||
return EventManagerInstance(self, obj) | ||
|
||
def __set__(self, obj, objtype=None): | ||
pass | ||
|
||
class EventHandler: | ||
def __init__(self, event, obj): | ||
self.event = event | ||
self.obj = obj | ||
def _getfunctionlist(self): | ||
try: | ||
eventhandler = self.obj.__eventhandler__ | ||
except AttributeError: | ||
eventhandler = self.obj.__eventhandler__ = {} | ||
return eventhandler.setdefault(self.event, []) | ||
def __getitem__(self, key): | ||
return self.events[key] | ||
|
||
def __setitem__(self, key, value): | ||
self.events[key] = value | ||
|
||
def listen(self, event): | ||
self.events[event] = Event() | ||
|
||
|
||
class EventManagerInstance: | ||
def __init__(self, event_manager, obj): | ||
self.event_manager = event_manager | ||
self.object = obj | ||
|
||
def __getitem__(self, key): | ||
return self.event_manager.events[key].handler(self.object) | ||
|
||
|
||
class Event: | ||
def __init__(self): | ||
self._funcs = [] | ||
|
||
def __iadd__(self, func): | ||
self._getfunctionlist().append(func) | ||
self._funcs.append(func) | ||
return self | ||
|
||
def __isub__(self, func): | ||
self._getfunctionlist().remove(func) | ||
self._funcs.remove(func) | ||
return self | ||
def __call__(self,earg=None): | ||
for func in self._getfunctionlist(): | ||
func(self.obj, earg) | ||
|
||
def handler(self, obj): | ||
return EventHandler(self, obj) | ||
|
||
|
||
class EventHandler: | ||
def __init__(self, event, obj): | ||
self.event = event | ||
self.object = obj | ||
|
||
def __call__(self, **kwargs): | ||
for f in self.event._funcs: | ||
f(self.object, **kwargs) |
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,41 @@ | ||
import unittest | ||
|
||
from mitama.db import DatabaseManager, BaseDatabase | ||
from mitama.db.types import Column, String | ||
|
||
DatabaseManager.test() | ||
|
||
|
||
class Database(BaseDatabase): | ||
pass | ||
|
||
|
||
db = Database() | ||
|
||
|
||
class ModelC(db.Model): | ||
name = Column(String) | ||
|
||
|
||
def addhoge(modelc): | ||
modelc.name = "huga" | ||
modelc.update() | ||
|
||
|
||
ModelC.listen("hoge") | ||
ModelC.event["hoge"] += addhoge | ||
|
||
|
||
db.create_all() | ||
|
||
|
||
class TestEvent(unittest.TestCase): | ||
def test_event(self): | ||
DatabaseManager.start_session() | ||
hoge = ModelC() | ||
hoge.name = "hoge" | ||
hoge.create() | ||
hoge.event["hoge"]() | ||
self.assertEqual(hoge.name, "huga") | ||
DatabaseManager.close_session() | ||
pass |