|
10 | 10 | package py |
11 | 11 |
|
12 | 12 | import ( |
| 13 | + "io" |
| 14 | + "io/ioutil" |
13 | 15 | "os" |
14 | 16 | ) |
15 | 17 |
|
16 | | -var FileType = NewTypeX("file", `represents an open file`, |
17 | | - nil, nil) |
18 | | - |
19 | 18 | type File os.File |
20 | 19 |
|
| 20 | +var FileType = NewType("file", `represents an open file`) |
| 21 | + |
| 22 | +func init() { |
| 23 | + FileType.Dict["write"] = MustNewMethod("write", func(self Object, value Object) (Object, error) { |
| 24 | + return self.(*File).Write(value) |
| 25 | + }, 0, "write(arg) -> writes the contents of arg to the file, returning the number of characters written.") |
| 26 | + |
| 27 | + FileType.Dict["read"] = MustNewMethod("read", func(self Object, args Tuple, kwargs StringDict) (Object, error) { |
| 28 | + return self.(*File).Read(args, kwargs) |
| 29 | + }, 0, "read([size]) -> read at most size bytes, returned as a string.\n\nIf the size argument is negative or omitted, read until EOF is reached.\nNotice that when in non-blocking mode, less data than what was requested\nmay be returned, even if no size parameter was given.") |
| 30 | + FileType.Dict["close"] = MustNewMethod("close", func(self Object) (Object, error) { |
| 31 | + return self.(*File).Close() |
| 32 | + }, 0, "close() -> None or (perhaps) an integer. Close the file.\n\nSets data attribute .closed to True. A closed file cannot be used for\nfurther I/O operations. close() may be called more than once without\nerror. Some kinds of file objects (for example, opened by popen())\nmay return an exit status upon closing.") |
| 33 | +} |
| 34 | + |
21 | 35 | // Type of this object |
22 | 36 | func (o *File) Type() *Type { |
23 | 37 | return FileType |
24 | 38 | } |
25 | 39 |
|
| 40 | +func (o *File) Write(value Object) (Object, error) { |
| 41 | + var b []byte |
| 42 | + |
| 43 | + switch v := value.(type) { |
| 44 | + // FIXME Bytearray |
| 45 | + case Bytes: |
| 46 | + b = v |
| 47 | + |
| 48 | + case String: |
| 49 | + b = []byte(v) |
| 50 | + |
| 51 | + default: |
| 52 | + return nil, ExceptionNewf(TypeError, "expected a string or other character buffer object") |
| 53 | + } |
| 54 | + |
| 55 | + n, err := (*os.File)(o).Write(b) |
| 56 | + return Int(n), err |
| 57 | +} |
| 58 | + |
| 59 | +func (o *File) readResult(b []byte) (Object, error) { |
| 60 | + var asBytes = false // default mode is "as string" - also move this in File struct |
| 61 | + |
| 62 | + if b == nil { |
| 63 | + if asBytes { |
| 64 | + return Bytes{}, nil |
| 65 | + } else { |
| 66 | + return String(""), nil |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + if asBytes { |
| 71 | + return Bytes(b), nil |
| 72 | + } else { |
| 73 | + return String(b), nil |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +func (o *File) Read(args Tuple, kwargs StringDict) (Object, error) { |
| 78 | + var arg Object = None |
| 79 | + |
| 80 | + err := UnpackTuple(args, kwargs, "read", 0, 1, &arg) |
| 81 | + if err != nil { |
| 82 | + return nil, err |
| 83 | + } |
| 84 | + |
| 85 | + var r io.Reader = (*os.File)(o) |
| 86 | + |
| 87 | + switch pyN, ok := arg.(Int); { |
| 88 | + case arg == None: |
| 89 | + // read all |
| 90 | + |
| 91 | + case ok: |
| 92 | + // number of bytes to read |
| 93 | + // 0: read nothing |
| 94 | + // < 0: read all |
| 95 | + // > 0: read n |
| 96 | + n, _ := pyN.GoInt64() |
| 97 | + if n == 0 { |
| 98 | + return o.readResult(nil) |
| 99 | + } |
| 100 | + if n > 0 { |
| 101 | + r = io.LimitReader(r, n) |
| 102 | + } |
| 103 | + |
| 104 | + default: |
| 105 | + // invalid type |
| 106 | + return nil, ExceptionNewf(TypeError, "read() argument 1 must be int, not %s", arg.Type().Name) |
| 107 | + } |
| 108 | + |
| 109 | + b, err := ioutil.ReadAll(r) |
| 110 | + if err == io.EOF { |
| 111 | + return o.readResult(nil) |
| 112 | + } |
| 113 | + if err != nil { |
| 114 | + return nil, err |
| 115 | + } |
| 116 | + |
| 117 | + return o.readResult(b) |
| 118 | +} |
| 119 | + |
| 120 | +func (o *File) Close() (Object, error) { |
| 121 | + _ = (*os.File)(o).Close() |
| 122 | + return None, nil |
| 123 | +} |
| 124 | + |
| 125 | +func OpenFile(filename string) (Object, error) { |
| 126 | + f, err := os.Open(filename) |
| 127 | + if err != nil { |
| 128 | + // XXX: should check for different types of errors |
| 129 | + return nil, ExceptionNewf(FileNotFoundError, err.Error()) |
| 130 | + } |
| 131 | + |
| 132 | + return (*File)(f), nil |
| 133 | +} |
| 134 | + |
26 | 135 | // Check interface is satisfied |
0 commit comments