1 #!/usr/bin/env python 2 3 """ 4 Input/output-related functions. 5 6 Copyright (C) 2015, 2016 Paul Boddie <paul@boddie.org.uk> 7 8 This program is free software; you can redistribute it and/or modify it under 9 the terms of the GNU General Public License as published by the Free Software 10 Foundation; either version 3 of the License, or (at your option) any later 11 version. 12 13 This program is distributed in the hope that it will be useful, but WITHOUT 14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 15 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 16 details. 17 18 You should have received a copy of the GNU General Public License along with 19 this program. If not, see <http://www.gnu.org/licenses/>. 20 """ 21 22 from native import _read, _write 23 from sys import stdout 24 25 class sysfile: 26 27 "A system-level file object." 28 29 def __init__(self, fd): 30 31 "Initialise the file with the given 'fd'." 32 33 self.fd = fd 34 35 def read(self, n): 36 37 "Read 'n' bytes from the file." 38 39 return _read(self.fd, n) 40 41 def write(self, s): 42 43 "Write 's' to the file." 44 45 _write(self.fd, str(s)) 46 47 def open(name, mode=None, buffering=None): 48 49 """ 50 Open the file with the given 'name', using the given 'mode' and applying the 51 indicated 'buffering'. 52 """ 53 54 return file(name, mode, buffering) 55 56 def raw_input(prompt=None): pass 57 58 def print_(dest, args, nl): 59 60 """ 61 Write to 'dest' the string representation of 'args', adding a newline if 62 'nl' is given as a true value. 63 """ 64 65 # Write to standard output if dest is not specified. 66 67 dest = dest or stdout 68 69 first = True 70 71 for arg in args: 72 73 # Insert spaces between arguments. 74 75 if first: 76 first = False 77 else: 78 dest.write(" ") 79 80 dest.write(str(arg)) 81 82 # Add a newline if specified. 83 84 if nl: 85 dest.write("\n") 86 87 # vim: tabstop=4 expandtab shiftwidth=4