-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpaternoster.py
executable file
·155 lines (117 loc) · 4.86 KB
/
paternoster.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#!/usr/bin/env python3
#encoding: utf-8
# Licensed under the GPL version 3
# 2012 (c) nojhan <[email protected]>
import re
import sys
import argparse
import subprocess
import queue
def run( cmd, text, nowait=False ):
# If there is a string formatting mark
try:
# put the matching text in it
behest = cmd % tuple(text)
except TypeError:
# else, do not
behest = cmd
with open("/dev/null") as dev_null:
if nowait:
subprocess.Popen( behest, stdout=sys.stdout, stderr=dev_null, shell=True )
else:
subprocess.call( behest, stdout=sys.stdout, stderr=dev_null, shell=True )
def call( cmd, text, nowait=False, queue=None ):
if queue:
queue.put( (cmd, text, nowait) )
else:
run( cmd, text, nowait )
def parse( text, pattern, cmd=[""], nowait=False, queue=None, capture_groups = False ):
regex = re.compile(pattern)
for match in regex.finditer(text):
# If no groups are specified
if not match.groups():
call( cmd[0], text, nowait, queue )
else:
nb_groups = len(match.groups())
if capture_groups:
# For each group index.
# Note that match.groups returns a tuple (thus being indexed in [0,n[),
# but that match.start(0) refers to the whole match, the groups being indexed in [1,n].
# Thus, we need to range in [1,n+1[.
texts = []
for group in range(1, nb_groups+1):
# If a group didn't match, there's nothing to do
if match.group(group) is not None:
texts.append( text[match.start(group):match.end(group)] )
# Finally, call a single command with all the captured groups
call( cmd[0], texts, nowait, queue )
else:
# Build a list of commands that match the number of groups,
# If there is not enough commands, duplicate the last one.
group_cmds = cmd + [cmd[-1]] * (nb_groups - len(cmd))
for group in range(1, nb_groups+1):
# If a group didn't match, there's nothing to do
if match.group(group) is not None:
call( group_cmds[group-1], text[match.start(group):match.end(group)], nowait, queue )
def write( text, stream = sys.stdout):
"""
Write "txt" on sys.stdout, then flush.
"""
try:
stream.write(text)
stream.flush()
# Silently handle broken pipes
except IOError:
try:
stream.close()
except IOError:
pass
def read_parse( stream_in, stream_out, pattern, cmd, nowait = False, at_end = False, capture_groups = False ):
"""
Read the given file-like object as a non-blocking stream
and call the function on each item (line),
with the given extra arguments.
"""
if at_end:
events = queue.Queue(0)
else:
events = None
while True:
try:
item = stream_in.readline()
except UnicodeDecodeError:
continue
except KeyboardInterrupt:
break
if not item:
break
# Write the line being processed
write( item, stream_out )
# Then do something
parse(item, pattern, cmd, nowait, events, capture_groups)
if at_end:
while events.not_empty:
try:
run( *events.get_nowait() )
except queue.Empty:
break
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Call a command for each input matching a pattern.")
parser.add_argument("pattern", metavar="REGEX", type=str, nargs=1,
help="A regular expression")
parser.add_argument("commands", metavar="CMD", type=str, nargs="+",
help="The command to run if REGEX match. \
If CMD contains a string formating placefolder (like \"%%s\"), \
it will be replaced by the matching text. \
Multiple commands may be specified as a list of space-separated strings. \
Each command will then be called for each corresponding matching group within REGEX.")
parser.add_argument("-w", "--no-wait", action="store_true",
help="Don't wait for the end of the current command before calling the next one.")
parser.add_argument("-e", "--end", action="store_true",
help="Run commands at the end of the input stream.")
parser.add_argument("-g", "--groups", action="store_true",
help="Capture groups as arguments for the CMD, instead of running CMD for each group.\
CMD must have as many formating placeholder as captured groups.")
args = parser.parse_args()
read_parse( sys.stdin, sys.stdout, args.pattern[0], args.commands, args.no_wait, args.end, args.groups )