/examples/tail.py

https://bitbucket.org/prologic/circuits/ · Python · 42 lines · 15 code · 10 blank · 17 comment · 0 complexity · bac9a11cbb5d4e3ebadee559f5c83676 MD5 · raw file

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