Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added simple example scripts using Drainer.

  • Loading branch information...
commit f61f38d0361510f9571d16920f80bfb35e209934 1 parent ec28381
@nvie authored
View
15 examples/simple_annotate_date.py
@@ -0,0 +1,15 @@
+"""
+Simply call "ls -a", no special stuff.
+"""
+import sys
+import datetime
+import drainers
+
+def annotate(line, is_err):
+ sys.stdout.write('[%s] ' % datetime.datetime.now())
+ if is_err:
+ sys.stdout.write('ERROR: ')
+ sys.stdout.write(line)
+
+d = drainers.Drainer(sys.argv[1:], read_event_cb=annotate)
+d.start()
View
48 examples/simple_find.py
@@ -0,0 +1,48 @@
+"""
+Simple finder. Search for a file called "foobar" for a maximum of 10
+seconds, then abort. Present results of found files and errors.
+"""
+import sys
+import drainers
+import threading
+
+class Finder(object):
+ def __init__(self):
+ self.errors = []
+ self.found = []
+ self.timed_out = False
+
+ def time_out(self): # will be invoked by threading.Timer() after 10 secs
+ self.timed_out = True
+
+ def has_timed_out(self):
+ return self.timed_out
+
+ def handle_line(self, line, is_err):
+ if is_err:
+ self.errors.append(line)
+ else:
+ self.found.append(line)
+
+ def main(self, search_for):
+ d = drainers.Drainer(['find', '.', '-name', search_for],
+ read_event_cb=self.handle_line,
+ should_abort_cb=self.has_timed_out)
+
+ t = threading.Timer(10.0, self.time_out)
+ t.daemon = True
+ t.start()
+
+ print 'Finding files called "%s" for 10 seconds...' % search_for
+ exitcode = d.start()
+
+ print 'Return code was: %d' % exitcode
+ print ''
+ print 'Found:'
+ print ''.join(self.found)
+ print ''
+ print 'Errors:'
+ print ''.join(self.errors)
+
+finder = Finder()
+finder.main(sys.argv[1])
View
18 examples/simple_ls.py
@@ -0,0 +1,18 @@
+"""
+Simply call "ls -a", no special stuff.
+"""
+import sys
+import drainers
+
+counter = 0
+
+def just_echo(line, is_err):
+ global counter
+ if is_err:
+ sys.stdout.write('ERROR: ')
+ sys.stdout.write(line)
+ counter += 1
+
+d = drainers.Drainer(['ls', '-a'], read_event_cb=just_echo)
+d.start()
+print '%d lines read' % counter
Please sign in to comment.
Something went wrong with that request. Please try again.