/
tools.py
94 lines (79 loc) · 2.57 KB
/
tools.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import re
from .qpipe import Pipe
from subprocess import check_output
class Iter(Pipe):
"""Takes an iterable in its constructor and emits each item.
Examples:
Iter(range(10))
Iter([1, 5, 10])
"""
def setup(self, arg_iterable):
for arg in arg_iterable:
self.emit(arg)
class Fn(Pipe):
"""Takes a function and creates a pipe that calls it with each input and emits the results.
Examples:
def square(x):
return x*x
Fn(square)
Fn(lambda x: x*x)
"""
def setup(self, fn):
self.fn = fn
def do(self, arg):
self.emit(self.fn(arg))
class Print(Pipe):
"""Prints each incoming element using python's native print function.
"""
def do(self, arg):
print(arg)
class Open(Pipe):
"""Emits lines from a file
Can be used in two ways:
- Construct with a filename: `Open("myfile.txt")`
- Feed with files: `Iter(["myfile1.txt", "myfile2.txt"]).into(Open())`
In either case will emit lines one by one from given files
"""
def setup(self, filename=None):
self.emitfile(filename)
def do(self, filename):
self.emitfile(filename)
def emitfile(self, filename):
if not filename:
return
with open(filename) as f:
for line in f:
self.emit(line)
class Exec(Pipe):
"""Executes a command and emits the output of the command as returned by subprocess.check_output
Can take a command as constructor or multiple commands from an upstream pipe:
Exec("uname -a")
Iter(["echo 'hi there'", "hostname"]).into(Exec())
"""
def setup(self, command=None):
if command:
self.emit(check_output(command))
def do(self, command):
self.emit(check_output(command))
class Grep(Pipe):
"""Given a regular expression string as an argument, receives elements and only emits the ones matching the regex.
Example:
Iter(["hello", "goodbye", "helloww"]).into(Grep("hello")) # Emits "hello" and "helloww"
"""
def setup(self, grepstring):
self.regex = re.compile(grepstring)
def do(self, text):
if self.regex.search(text):
self.emit(text)
class Reverse(Pipe):
"""Receives elements until none are left, then emits them in reverse order.
Example:
Iter([1, 2, 3]).into(Reverse()) # Emits 3, 2, 1
"""
def setup(self):
self.data = []
def do(self, value):
self.data.append(value)
def teardown(self):
for val in reversed(self.data):
self.emit(val)