/examples/cat.py

https://bitbucket.org/prologic/circuits/ · Python · 42 lines · 14 code · 15 blank · 13 comment · 0 complexity · df5a4d1e2f34b854be4c145836245b5a MD5 · raw file

  1. #!/usr/bin/env python
  2. """Clone of the standard UNIX "cat" 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 "cat" command.
  5. """
  6. import sys
  7. from circuits.io import stdout, File, write
  8. class Cat(File):
  9. # This adds the already instantiated stdout instnace
  10. stdout = stdout
  11. def read(self, data):
  12. """Read Event Handler
  13. This is fired by the File Component when there is data to be read
  14. from the underlying file that was opened.
  15. """
  16. self.fire(write(data), stdout)
  17. def eof(self):
  18. """End Of File Event
  19. This is fired by the File Component when the underlying input file
  20. has been exhcuasted.
  21. """
  22. raise SystemExit(0)
  23. # Start and "run" the system.
  24. Cat(sys.argv[1]).run()