Package osh :: Package command :: Module cat
[frames] | no frames]

Source Code for Module osh.command.cat

 1  # osh 
 2  # Copyright (C) Jack Orenstein <jao@geophile.com> 
 3  # 
 4  # This program is free software; you can redistribute it and/or modify 
 5  # it under the terms of the GNU General Public License as published by 
 6  # the Free Software Foundation; either version 2 of the License, or 
 7  # (at your option) any later version. 
 8  # 
 9  # This program is distributed in the hope that it will be useful, 
10  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
11  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
12  # GNU General Public License for more details. 
13  # 
14  # You should have received a copy of the GNU General Public License 
15  # along with this program; if not, write to the Free Software 
16  # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 
17   
18  r"""C{cat FILENAME} 
19   
20  Each line of the file named by C{FILENAME} is written to output. 
21  Newline characters (\n) are omitted. The C{cat} command takes no 
22  input. 
23  """ 
24   
25  import sys 
26   
27  import osh.core 
28   
29  # CLI 
30 -def _cat():
31 return _Cat()
32 33 # API
34 -def cat(filename):
35 r"""Each line of the file named by C{filename} is written to output. 36 Newline characters (\n) are omitted. C{cat} takes no input. 37 """ 38 return _Cat().process_args(filename)
39
40 -class _Cat(osh.core.Generator):
41 42 _filename = None 43 44 45 # object interface 46
47 - def __init__(self):
48 osh.core.Generator.__init__(self, '', (1, 1))
49 50 51 # OshCommand interface 52
53 - def doc(self):
54 return __doc__
55
56 - def setup(self):
57 args = self.args() 58 self._filename = args.next_string() 59 if not self._filename: 60 self.usage()
61 62 63 # Generator interface 64
65 - def execute(self):
66 file = open(self._filename, 'r') 67 try: 68 eof = False 69 while not eof: 70 line = file.readline() 71 if line: 72 if line.endswith('\n'): 73 line = line[:-1] 74 self.send(line) 75 else: 76 eof = True 77 finally: 78 file.close()
79