mirror of
https://github.com/PyCQA/flake8.git
synced 2026-04-03 11:56:52 +00:00
Bare bones of a notification system
This commit is contained in:
parent
f013698072
commit
7b2a1c157b
9 changed files with 209 additions and 0 deletions
1
flake8/__init__.py
Normal file
1
flake8/__init__.py
Normal file
|
|
@ -0,0 +1 @@
|
|||
__version__ = '3.0.0a1'
|
||||
74
flake8/_trie.py
Normal file
74
flake8/_trie.py
Normal file
|
|
@ -0,0 +1,74 @@
|
|||
"""Independent implementation of a Trie tree."""
|
||||
|
||||
__all__ = ('Trie', 'TrieNode')
|
||||
|
||||
|
||||
def _iterate_stringlike_objects(string):
|
||||
for i in range(len(string)):
|
||||
yield string[i:i+1]
|
||||
|
||||
|
||||
class Trie(object):
|
||||
"""The object that manages the trie nodes."""
|
||||
|
||||
def __init__(self):
|
||||
"""Initialize an empty trie."""
|
||||
self.root = TrieNode(None, None)
|
||||
|
||||
def add(self, path, node_data):
|
||||
"""Add the node data to the path described."""
|
||||
node = self.root
|
||||
for prefix in _iterate_stringlike_objects(path):
|
||||
child = node.find_prefix(prefix)
|
||||
if child is None:
|
||||
child = node.add_child(prefix, [])
|
||||
node = child
|
||||
node.data.append(node_data)
|
||||
|
||||
def find(self, path):
|
||||
"""Find a node based on the path provided."""
|
||||
node = self.root
|
||||
for prefix in _iterate_stringlike_objects(path):
|
||||
child = node.find_prefix(prefix)
|
||||
if child is None:
|
||||
return None
|
||||
node = child
|
||||
return node
|
||||
|
||||
|
||||
class TrieNode(object):
|
||||
"""The majority of the implementation details of a Trie."""
|
||||
|
||||
def __init__(self, prefix, data, children=None):
|
||||
"""Initialize a TrieNode with data and children."""
|
||||
self.children = children or {}
|
||||
self.data = data
|
||||
self.prefix = prefix
|
||||
|
||||
def __repr__(self):
|
||||
"""Generate an easy to read representation of the node."""
|
||||
return 'TrieNode(prefix={0}, data={1}, children={2})'.format(
|
||||
self.prefix, self.data, dict(self.children)
|
||||
)
|
||||
|
||||
def find_prefix(self, prefix):
|
||||
"""Find the prefix in the children of this node.
|
||||
|
||||
:returns: A child matching the prefix or None.
|
||||
:rtype: :class:`~TrieNode` or None
|
||||
"""
|
||||
return self.children.get(prefix, None)
|
||||
|
||||
def add_child(self, prefix, data, children=None):
|
||||
"""Create and add a new child node.
|
||||
|
||||
:returns: The newly created node
|
||||
:rtype: :class:`~TrieNode`
|
||||
"""
|
||||
new_node = TrieNode(prefix, data, children)
|
||||
self.children[prefix] = new_node
|
||||
return new_node
|
||||
|
||||
def traverse(self):
|
||||
"""Traverse children of this node in order."""
|
||||
raise NotImplementedError()
|
||||
22
flake8/notifier.py
Normal file
22
flake8/notifier.py
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
from flake8 import _trie
|
||||
|
||||
|
||||
class Notifier(object):
|
||||
def __init__(self):
|
||||
self.listeners = _trie.Trie()
|
||||
|
||||
def listeners_for(self, error_code):
|
||||
node = self.listeners.find(error_code)
|
||||
for listener in node.data:
|
||||
yield listener
|
||||
if node.children:
|
||||
for child in node.traverse():
|
||||
for listener in child.data:
|
||||
yield listener
|
||||
|
||||
def notify(self, error_code, *args, **kwargs):
|
||||
for listener in self.listeners_for(error_code):
|
||||
listener.notify(*args, **kwargs)
|
||||
|
||||
def register_listener(self, error_code, listener):
|
||||
self.listeners.add(error_code, listener)
|
||||
0
flake8/style_guide.py
Normal file
0
flake8/style_guide.py
Normal file
Loading…
Add table
Add a link
Reference in a new issue