Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from rhizolab/add-extension-interface
Added an extension interface object
- Loading branch information
Showing
5 changed files
with
45 additions
and
9 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,34 @@ | ||
from flask import render_template_string | ||
|
||
from collections import defaultdict, OrderedDict | ||
|
||
# a super-class to be sub-classes by server extension packages | ||
class Extension(object): | ||
|
||
# create a new extension object | ||
def __init__(self): | ||
def __init__(self, extension_interface=None): | ||
self.name = None # set in app.py | ||
self.path = None # set in app.py | ||
self.extension_interface = extension_interface | ||
|
||
# render a template from the extension's templates folder | ||
def render_template(self, name, **kwargs): | ||
template_file_name = self.path + '/templates/' + name | ||
template = open(template_file_name).read() | ||
return render_template_string(template, **kwargs) | ||
|
||
class ExtensionInterface(): | ||
|
||
def __init__(self): | ||
self._listeners = defaultdict(OrderedDict) | ||
|
||
def on(self, eventName, listener): | ||
self._listeners[eventName][listener] = listener | ||
|
||
def off(self, eventName, listener): | ||
self._listeners[eventName].pop(listener) | ||
|
||
def emit(self, eventName, *args, **kwargs): | ||
for listener in list(self._listeners[eventName].values()): | ||
listener(*args, **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