Attempt to refactor a bit of context, new count transform that counts the calls, new bonobo.structs package with basic data structures, removal of blessings.

This commit is contained in:
Romain Dorgueil
2017-04-24 23:44:29 +02:00
parent cff32c1612
commit ef2c881075
37 changed files with 405 additions and 145 deletions

View File

@ -3,21 +3,22 @@
import functools
from pprint import pprint as _pprint
from .tokens import NOT_MODIFIED
from colorama import Fore, Style
import colorama as _colorama
_colorama.init()
import blessings as _blessings
terminal = _blessings.Terminal()
from bonobo.constants import NOT_MODIFIED
from bonobo.context.processors import contextual
from bonobo.structs.bags import Bag
from bonobo.util.objects import ValueHolder
from bonobo.util.term import CLEAR_EOL
__all__ = [
'Limit',
'NOT_MODIFIED',
'PrettyPrint',
'Tee',
'count',
'noop',
'pprint',
'terminal',
]
@ -34,7 +35,7 @@ def Limit(n=10):
if i <= n:
yield NOT_MODIFIED
_limit.__name__ = 'limit({})'.format(n)
_limit.__name__ = 'Limit({})'.format(n)
return _limit
@ -48,25 +49,52 @@ def Tee(f):
return wrapped
@contextual
def count(counter, *args, **kwargs):
counter += 1
@count.add_context_processor
def _count_counter(self, context):
counter = ValueHolder(0)
yield counter
context.send(Bag(counter.value))
pprint = Tee(_pprint)
def PrettyPrint(title_keys=('title', 'name', 'id'), print_values=True, sort=True):
def _pprint(*args, **kwargs):
global terminal
nonlocal title_keys, sort, print_values
row = args[0]
for key in title_keys:
if key in row:
print(term.bold(row.get(key)))
print(
Style.BRIGHT,
row.get(key),
Style.RESET_ALL,
sep=''
)
break
if print_values:
for k in sorted(row) if sort else row:
print(
'{t.blue}{k}{t.normal} : {t.black}({tp}){t.normal} {v}{t.clear_eol}'.
format(k=k, v=repr(row[k]), t=term, tp=type(row[k]).__name__)
'',
Fore.BLUE,
k,
Style.RESET_ALL,
' : ',
Fore.BLACK,
'(',
type(row[k]).__name__,
')',
Style.RESET_ALL,
' ',
repr(row[k]),
CLEAR_EOL,
)
yield NOT_MODIFIED
@ -76,41 +104,5 @@ def PrettyPrint(title_keys=('title', 'name', 'id'), print_values=True, sort=True
return _pprint
'''
Old code from rdc.etl
def writehr(self, label=None):
width = t.width or 80
if label:
label = str(label)
sys.stderr.write(t.black('·' * 4) + shade('{') + label + shade('}') + t.black('·' * (width - (6+len(label)) - 1)) + '\n')
else:
sys.stderr.write(t.black('·' * (width-1) + '\n'))
def writeln(self, s):
"""Output method."""
sys.stderr.write(self.format(s) + '\n')
def initialize(self):
self.lineno = 0
def transform(self, hash, channel=STDIN):
"""Actual transformation."""
self.lineno += 1
if not self.condition or self.condition(hash):
hash = hash.copy()
hash = hash if not isinstance(self.field_filter, collections.Callable) else hash.restrict(self.field_filter)
if self.clean:
hash = hash.restrict(lambda k: len(k) and k[0] != '_')
self.writehr(self.lineno)
self.writeln(hash)
self.writehr()
sys.stderr.write('\n')
yield hash
'''
def noop(*args, **kwargs): # pylint: disable=unused-argument
return NOT_MODIFIED

View File

@ -20,3 +20,81 @@ class ValueHolder:
def __init__(self, value, *, type=None):
self.value = value
self.type = type
def __lt__(self, other):
return self.value < other
def __le__(self, other):
return self.value <= other
def __eq__(self, other):
return self.value == other
def __ne__(self, other):
return self.value != other
def __gt__(self, other):
return self.value > other
def __ge__(self, other):
return self.value >= other
def __add__(self, other):
return self.value + other
def __radd__(self, other):
return other + self.value
def __iadd__(self, other):
self.value += other
def __sub__(self, other):
return self.value - other
def __rsub__(self, other):
return other - self.value
def __isub__(self, other):
self.value -= other
def __mul__(self, other):
return self.value * other
def __rmul__(self, other):
return other * self.value
def __imul__(self, other):
self.value *= other
def __matmul__(self, other):
return self.value @ other
def __rmatmul__(self, other):
return other @ self.value
def __imatmul__(self, other):
self.value @= other
def __truediv__(self, other):
return self.value / other
def __rtruediv__(self, other):
return other / self.value
def __itruediv__(self, other):
self.value /= other
"""
object.__matmul__(self, other)
object.__truediv__(self, other)
object.__floordiv__(self, other)
object.__mod__(self, other)
object.__divmod__(self, other)
object.__pow__(self, other[, modulo])
object.__lshift__(self, other)
object.__rshift__(self, other)
object.__and__(self, other)
object.__xor__(self, other)
object.__or__(self, other)
"""

2
bonobo/util/term.py Normal file
View File

@ -0,0 +1,2 @@
CLEAR_EOL = '\033[0K'
MOVE_CURSOR_UP = lambda n: '\033[{}A'.format(n)

View File

@ -1,14 +0,0 @@
class Token:
"""Factory for signal oriented queue messages or other token types."""
def __init__(self, name):
self.__name__ = name
def __repr__(self):
return '<{}>'.format(self.__name__)
BEGIN = Token('Begin')
END = Token('End')
NOT_MODIFIED = Token('NotModified')