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 posix.io import fdopen, read, write 23 24 class sysfile: 25 26 "A system-level file object." 27 28 def __init__(self, fd): 29 30 "Initialise the file with the given 'fd'." 31 32 self.fd = fd 33 34 def read(self, n): 35 36 "Read 'n' bytes from the file." 37 38 return read(self.fd, n) 39 40 def write(self, s): 41 42 "Write 's' to the file." 43 44 write(self.fd, str(s)) 45 46 def open(name, mode=None, buffering=None): 47 48 """ 49 Open the file with the given 'name', using the given 'mode' and applying the 50 indicated 'buffering'. 51 """ 52 53 return file(name, mode, buffering) 54 55 def raw_input(prompt=None): pass 56 57 def print_(dest, args, nl): 58 59 """ 60 Write to 'dest' the string representation of 'args', adding a newline if 61 'nl' is given as a true value. 62 """ 63 64 # If imported at the module level, the sys module must be set up first, 65 # which should be ensured by the module ordering activity, and a module 66 # attribute will be employed to hold a reference to sys.stdout. 67 68 from sys import stdout 69 70 # Write to standard output if dest is not specified. 71 72 dest = dest or stdout 73 74 first = True 75 76 for arg in args: 77 78 # Insert spaces between arguments. 79 80 if first: 81 first = False 82 else: 83 dest.write(" ") 84 85 dest.write(str(arg)) 86 87 # Add a newline if specified. 88 89 if nl: 90 dest.write("\n") 91 92 # vim: tabstop=4 expandtab shiftwidth=4