Lichen

Annotated lib/posix/io.py

337:d6b253d49e99
2016-12-06 Paul Boddie Moved stdin, stdout, stderr to posix.io.
paul@6 1
#!/usr/bin/env python
paul@6 2
paul@328 3
"""
paul@328 4
POSIX input/output functions.
paul@328 5
paul@328 6
Copyright (C) 2016 Paul Boddie <paul@boddie.org.uk>
paul@328 7
paul@328 8
This program is free software; you can redistribute it and/or modify it under
paul@328 9
the terms of the GNU General Public License as published by the Free Software
paul@328 10
Foundation; either version 3 of the License, or (at your option) any later
paul@328 11
version.
paul@328 12
paul@328 13
This program is distributed in the hope that it will be useful, but WITHOUT
paul@328 14
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
paul@328 15
FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
paul@328 16
details.
paul@328 17
paul@328 18
You should have received a copy of the GNU General Public License along with
paul@328 19
this program.  If not, see <http://www.gnu.org/licenses/>.
paul@328 20
"""
paul@328 21
paul@328 22
import native
paul@328 23
paul@336 24
# Abstractions for system-level files and streams.
paul@336 25
paul@332 26
class sysfile:
paul@332 27
paul@332 28
    "A system-level file object."
paul@332 29
paul@332 30
    def __init__(self, fd):
paul@332 31
paul@332 32
        "Initialise the file with the given 'fd'."
paul@332 33
paul@332 34
        self.fd = fd
paul@332 35
paul@332 36
    def read(self, n):
paul@332 37
paul@336 38
        "Read 'n' bytes from the file, returning a string."
paul@332 39
paul@336 40
        _check_int(n)
paul@332 41
        return read(self.fd, n)
paul@332 42
paul@332 43
    def write(self, s):
paul@332 44
paul@336 45
        "Write string 's' to the file."
paul@336 46
paul@336 47
        _check_string(s)
paul@336 48
        write(self.fd, s)
paul@336 49
paul@336 50
class sysstream:
paul@336 51
paul@336 52
    "A system-level stream object."
paul@336 53
paul@336 54
    def __init__(self, fd, mode="r"):
paul@336 55
paul@336 56
        "Initialise the stream with the given 'fd' and 'mode'."
paul@336 57
paul@336 58
        self.__data__ = fdopen(fd, mode)
paul@332 59
paul@336 60
    def read(self, n):
paul@336 61
paul@336 62
        "Read 'n' bytes from the stream."
paul@336 63
paul@336 64
        _check_int(n)
paul@336 65
        return native._fread(self.__data__, n)
paul@336 66
paul@336 67
    def write(self, s):
paul@336 68
paul@336 69
        "Write string 's' to the stream."
paul@336 70
paul@336 71
        _check_string(s)
paul@336 72
        native._fwrite(self.__data__, s)
paul@336 73
paul@337 74
# Standard streams.
paul@337 75
paul@337 76
stdin = sysstream(0)
paul@337 77
stdout = sysstream(1, "w")
paul@337 78
stderr = sysstream(2, "w")
paul@337 79
paul@336 80
# Input/output functions.
paul@332 81
paul@6 82
def close(fd): pass
paul@6 83
def closerange(fd_low, fd_high): pass
paul@6 84
def dup(fd): pass
paul@6 85
def dup2(old_fd, new_fd): pass
paul@328 86
def fchdir(fd): pass
paul@6 87
def fchmod(fd, mode): pass
paul@6 88
def fchown(fd, uid, gid): pass
paul@328 89
def fdatasync(fd): pass
paul@328 90
paul@328 91
def fdopen(fd, mode="r"):
paul@328 92
paul@328 93
    """
paul@328 94
    Open a stream for the given file descriptor 'fd', operating in the given
paul@328 95
    'mode'.
paul@328 96
    """
paul@328 97
paul@328 98
    _check_fd(fd)
paul@328 99
    _check_string(mode)
paul@328 100
    return native._fdopen(fd, mode)
paul@328 101
paul@6 102
def fpathconf(fd, name): pass
paul@6 103
def fstat(fd): pass
paul@6 104
def fstatvfs(fd): pass
paul@328 105
def fsync(fd): pass
paul@6 106
def ftruncate(fd, length): pass
paul@6 107
def isatty(fd): pass
paul@328 108
paul@328 109
SEEK_CUR = 1
paul@328 110
SEEK_END = 2
paul@328 111
SEEK_SET = 0
paul@328 112
paul@6 113
def lseek(fd, pos, how): pass
paul@6 114
def open(filename, flag, mode=0777): pass
paul@6 115
def openpty(): pass
paul@6 116
def pipe(): pass
paul@6 117
def putenv(key, value): pass
paul@328 118
paul@328 119
def read(fd, n):
paul@328 120
paul@328 121
    """
paul@328 122
    Read using the low-level file descriptor 'fd' the given number of bytes 'n'.
paul@328 123
    """
paul@328 124
paul@328 125
    _check_fd(fd)
paul@328 126
    _check_int(n)
paul@328 127
    return native._read(fd, n)
paul@328 128
paul@6 129
def times(): pass
paul@6 130
def ttyname(fd): pass
paul@6 131
def umask(new_mask): pass
paul@6 132
def uname(): pass
paul@6 133
def unsetenv(key): pass
paul@328 134
paul@328 135
def write(fd, s):
paul@328 136
paul@328 137
    "Write using the low-level file descriptor 'fd' the given string 's'."
paul@328 138
paul@328 139
    _check_fd(fd)
paul@328 140
    _check_string(s)
paul@328 141
    native._write(fd, s)
paul@6 142
paul@336 143
# Constants.
paul@336 144
paul@6 145
O_APPEND = 1024
paul@6 146
O_ASYNC = 8192
paul@6 147
O_CREAT = 64
paul@6 148
O_DIRECT = 16384
paul@6 149
O_DIRECTORY = 65536
paul@6 150
O_DSYNC = 4096
paul@6 151
O_EXCL = 128
paul@6 152
O_LARGEFILE = 32768
paul@6 153
O_NDELAY = 2048
paul@6 154
O_NOATIME = 262144
paul@6 155
O_NOCTTY = 256
paul@6 156
O_NOFOLLOW = 131072
paul@6 157
O_NONBLOCK = 2048
paul@6 158
O_RDONLY = 0
paul@6 159
O_RDWR = 2
paul@6 160
O_RSYNC = 1052672
paul@6 161
O_SYNC = 1052672
paul@6 162
O_TRUNC = 512
paul@6 163
O_WRONLY = 1
paul@6 164
paul@336 165
# Type validation functions.
paul@336 166
paul@328 167
def _check_fd(fd):
paul@328 168
paul@328 169
    "Check the given low-level file descriptor 'fd'."
paul@328 170
paul@328 171
    if not native._isinstance(fd, int):
paul@328 172
        raise ValueError(fd)
paul@328 173
paul@328 174
def _check_int(i):
paul@328 175
paul@328 176
    "Check the given int 'i'."
paul@328 177
paul@328 178
    if not native._isinstance(i, int):
paul@328 179
        raise ValueError(i)
paul@328 180
paul@328 181
def _check_string(s):
paul@328 182
paul@328 183
    "Check the given string 's'."
paul@328 184
paul@328 185
    if not native._isinstance(s, string):
paul@328 186
        raise ValueError(s)
paul@6 187
paul@6 188
# vim: tabstop=4 expandtab shiftwidth=4