PageRenderTime 21ms CodeModel.GetById 15ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

/examples/tail.py

https://bitbucket.org/prologic/circuits/
Python | 42 lines | 40 code | 1 blank | 1 comment | 0 complexity | bac9a11cbb5d4e3ebadee559f5c83676 MD5 | raw file
 1#!/usr/bin/env python
 2
 3"""Clone of the standard UNIX "tail" command.
 4
 5This example shows how you can utilize some of the buitlin I/O components
 6in circuits to write a very simple clone of the standard UNIX "tail" command.
 7"""
 8
 9import sys
10from circuits import Component, Debugger
11from circuits.io import stdout, File, Write
12
13
14class Tail(Component):
15
16    # Shorthand for declaring a compoent to be a part of this component.
17    stdout = stdout
18
19    def init(self, filename):
20        """Initialize Tail Component
21
22        Using the convenience ``init`` method we simply register a ``File``
23        Component as part of our ``Tail`` Component and ask it to seek to
24        the end of the file.
25        """
26
27        File(filename, "r", autoclose=False).register(self).seek(0, 2)
28
29    def read(self, data):
30        """Read Event Handler
31
32        This event is triggered by the underlying ``File`` Component for
33        when there is data to be processed. Here we simply fire a ``Write``
34        event and target the ``stdout`` component instance that is a part of
35        our system -- thus writing the contents of the file we read out
36        to standard output.
37        """
38
39        self.fire(Write(data), self.stdout)
40
41# Setup and run the system.
42(Tail(sys.argv[1]) + Debugger()).run()