Fix logic for Notifier.listeners_for

Add tests for proper logic around notifier
This commit is contained in:
Ian Cordasco 2015-12-29 23:28:20 -06:00
parent 222be9ac49
commit 37b92cd4b4
3 changed files with 34 additions and 23 deletions

View file

@ -77,6 +77,18 @@ See https://gitlab.com/pycqa/flake8/issues/84
.. note:: Will probably need a Trie implementation for this .. note:: Will probably need a Trie implementation for this
What we *might* want is for a autofix plugin to register something like
::
'flake8.autofix_extension': [
'E1 = my_fixer.E1Listener',
'E2 = my_fixer.E2Listener',
]
This means that the notifer would need to take an error code like ``E111`` and
then notify anything listening for ``E111``, ``E11``, ``E1``, and ``E``.
.. _reporter-plugins: .. _reporter-plugins:
Support for Plugins that Format Output Support for Plugins that Format Output

View file

@ -9,12 +9,12 @@ class Notifier(object):
def listeners_for(self, error_code): def listeners_for(self, error_code):
"""Retrieve listeners for an error_code. """Retrieve listeners for an error_code.
The error code does not need to be a specific error code. For example, There may be listeners registered for E1, E100, E101, E110, E112, and
There may be listeners registered for E100, E101, E110, E112, and E126. To get all the listeners for one of E100, E101, E110, E112, or
E126. If you wanted to get all listeners starting with 'E1' then you E126 you would also need to incorporate the listeners for E1 (since
would pass 'E1' as the error code here. they're all in the same class).
Example usage Example usage:
.. code-block:: python .. code-block:: python
@ -22,20 +22,15 @@ class Notifier(object):
n = notifier.Notifier() n = notifier.Notifier()
# register listeners # register listeners
for listener in n.listeners_for('E1'):
listener.notify(...)
for listener in n.listeners_for('W102'): for listener in n.listeners_for('W102'):
listener.notify(...) listener.notify(...)
""" """
node = self.listeners.find(error_code) path = error_code
if node is None: while path:
return node = self.listeners.find(path)
for listener in node.data: for listener in node.data:
yield listener
for child in node.traverse():
for listener in child.data:
yield listener yield listener
path = path[:-1]
def notify(self, error_code, *args, **kwargs): def notify(self, error_code, *args, **kwargs):
"""Notify all listeners for the specified error code.""" """Notify all listeners for the specified error code."""

View file

@ -8,7 +8,7 @@ class _Listener(object):
self.was_notified = False self.was_notified = False
def notify(self, error_code, *args, **kwargs): def notify(self, error_code, *args, **kwargs):
assert self.error_code == error_code assert error_code.startswith(self.error_code)
self.was_notified = True self.was_notified = True
@ -18,14 +18,18 @@ class TestNotifier(object):
self.notifier = notifier.Notifier() self.notifier = notifier.Notifier()
self.listener_map = {} self.listener_map = {}
for i in range(10): def add_listener(error_code):
for j in range(30): listener = _Listener(error_code)
error_code = 'E{0}{1:02d}'.format(i, j) self.listener_map[error_code] = listener
listener = _Listener(error_code) self.notifier.register_listener(error_code, listener)
self.listener_map[error_code] = listener
self.notifier.register_listener(error_code, listener)
def test_notify_a_single_error_code(self): for i in range(10):
add_listener('E{0}'.format(i))
for j in range(30):
add_listener('E{0}{1:02d}'.format(i, j))
def test_notify(self):
"""Show that we notify a specific error code.""" """Show that we notify a specific error code."""
self.notifier.notify('E111', 'extra', 'args') self.notifier.notify('E111', 'extra', 'args')
assert self.listener_map['E111'].was_notified is True assert self.listener_map['E111'].was_notified is True
assert self.listener_map['E1'].was_notified is True