diff options
| author | David Pursehouse <david.pursehouse@sonymobile.com> | 2012-09-06 14:16:15 +0900 |
|---|---|---|
| committer | David Pursehouse <david.pursehouse@sonymobile.com> | 2012-09-12 11:42:08 +0900 |
| commit | 894bc5d6d219509e69c7761afcd4f399036306e7 (patch) | |
| tree | 5a20197a757f779265300fe1da7797d21cecd085 /pygerrit | |
| parent | d8db31b4e99afef93534c84254e49036b348b905 (diff) | |
| download | pygerrit-894bc5d6d219509e69c7761afcd4f399036306e7.tar.gz | |
Fix event registration from other module
The purpose of the `register` decorator in the event factory is
to allow client scripts to define their own event classes and
have them dispatched when monitoring the event stream.
The current implementation does not work when an event class is
registered from another module, because it uses globals() which
only returns the symbols defined in the event module, so trying
to instantiate the object causes an exception.
Change the implementation to also store the module name when
registering, and then use that when instantiating the event
object.
Add unit tests to cover the event registration functionality.
Change-Id: I1af901621b07fc9236bc95a81956269507994871
Diffstat (limited to 'pygerrit')
| -rw-r--r-- | pygerrit/events.py | 10 |
1 files changed, 7 insertions, 3 deletions
diff --git a/pygerrit/events.py b/pygerrit/events.py index 49ce472..72a92ed 100644 --- a/pygerrit/events.py +++ b/pygerrit/events.py @@ -21,7 +21,7 @@ class GerritEventFactory(object): def decorate(klazz): if name in cls._events: raise GerritError("Duplicate event: %s" % name) - cls._events[name] = klazz.__name__ + cls._events[name] = [klazz.__module__, klazz.__name__] klazz.name = name return klazz return decorate @@ -40,8 +40,12 @@ class GerritEventFactory(object): name = json_data["type"] if not name in cls._events: raise GerritError("Unknown event: %s" % name) - classname = cls._events[name] - return globals()[classname](json_data) + event = cls._events[name] + module_name = event[0] + class_name = event[1] + module = __import__(module_name, fromlist=[module_name]) + klazz = getattr(module, class_name) + return klazz(json_data) class GerritEvent(object): |
