mirror of
https://github.com/arsenetar/dupeguru.git
synced 2026-01-22 14:41:39 +00:00
Format files with black
- Format all files with black - Update tox.ini flake8 arguments to be compatible - Add black to requirements-extra.txt - Reduce ignored flake8 rules and fix a few violations
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
# Copyright 2015 Hardcoded Software (http://www.hardcoded.net)
|
||||
|
||||
# This software is licensed under the "GPLv3" License as described in the "LICENSE" file,
|
||||
# which should be included with this package. The terms are also available at
|
||||
# This software is licensed under the "GPLv3" License as described in the "LICENSE" file,
|
||||
# which should be included with this package. The terms are also available at
|
||||
# http://www.gnu.org/licenses/gpl-3.0.html
|
||||
|
||||
"""Very simple inter-object notification system.
|
||||
@@ -14,55 +14,58 @@ the method with the same name as the broadcasted message is called on the listen
|
||||
|
||||
from collections import defaultdict
|
||||
|
||||
|
||||
class Broadcaster:
|
||||
"""Broadcasts messages that are received by all listeners.
|
||||
"""
|
||||
|
||||
def __init__(self):
|
||||
self.listeners = set()
|
||||
|
||||
|
||||
def add_listener(self, listener):
|
||||
self.listeners.add(listener)
|
||||
|
||||
|
||||
def notify(self, msg):
|
||||
"""Notify all connected listeners of ``msg``.
|
||||
|
||||
|
||||
That means that each listeners will have their method with the same name as ``msg`` called.
|
||||
"""
|
||||
for listener in self.listeners.copy(): # listeners can change during iteration
|
||||
if listener in self.listeners: # disconnected during notification
|
||||
for listener in self.listeners.copy(): # listeners can change during iteration
|
||||
if listener in self.listeners: # disconnected during notification
|
||||
listener.dispatch(msg)
|
||||
|
||||
|
||||
def remove_listener(self, listener):
|
||||
self.listeners.discard(listener)
|
||||
|
||||
|
||||
|
||||
class Listener:
|
||||
"""A listener is initialized with the broadcaster it's going to listen to. Initially, it is not connected.
|
||||
"""
|
||||
|
||||
def __init__(self, broadcaster):
|
||||
self.broadcaster = broadcaster
|
||||
self._bound_notifications = defaultdict(list)
|
||||
|
||||
|
||||
def bind_messages(self, messages, func):
|
||||
"""Binds multiple message to the same function.
|
||||
|
||||
|
||||
Often, we perform the same thing on multiple messages. Instead of having the same function
|
||||
repeated again and agin in our class, we can use this method to bind multiple messages to
|
||||
the same function.
|
||||
"""
|
||||
for message in messages:
|
||||
self._bound_notifications[message].append(func)
|
||||
|
||||
|
||||
def connect(self):
|
||||
"""Connects the listener to its broadcaster.
|
||||
"""
|
||||
self.broadcaster.add_listener(self)
|
||||
|
||||
|
||||
def disconnect(self):
|
||||
"""Disconnects the listener from its broadcaster.
|
||||
"""
|
||||
self.broadcaster.remove_listener(self)
|
||||
|
||||
|
||||
def dispatch(self, msg):
|
||||
if msg in self._bound_notifications:
|
||||
for func in self._bound_notifications[msg]:
|
||||
@@ -70,20 +73,19 @@ class Listener:
|
||||
if hasattr(self, msg):
|
||||
method = getattr(self, msg)
|
||||
method()
|
||||
|
||||
|
||||
|
||||
class Repeater(Broadcaster, Listener):
|
||||
REPEATED_NOTIFICATIONS = None
|
||||
|
||||
|
||||
def __init__(self, broadcaster):
|
||||
Broadcaster.__init__(self)
|
||||
Listener.__init__(self, broadcaster)
|
||||
|
||||
|
||||
def _repeat_message(self, msg):
|
||||
if not self.REPEATED_NOTIFICATIONS or msg in self.REPEATED_NOTIFICATIONS:
|
||||
self.notify(msg)
|
||||
|
||||
|
||||
def dispatch(self, msg):
|
||||
Listener.dispatch(self, msg)
|
||||
self._repeat_message(msg)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user