Skip to content

Commit 334e800

Browse files
committed
feat: add write
1 parent f211f8b commit 334e800

File tree

4 files changed

+206
-83
lines changed

4 files changed

+206
-83
lines changed

src/stdlib/Builtins.fs

Lines changed: 183 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -8,104 +8,222 @@ open Fable.Core
88
// fsharplint:disable MemberNames,InterfaceNames
99

1010
type TextIOBase =
11-
abstract read : unit -> string
12-
abstract read : __size: int -> string
11+
abstract read: unit -> string
12+
abstract read: __size: int -> string
13+
abstract write: __s: string -> int
14+
abstract writelines: __lines: string seq -> unit
15+
abstract readline: __size: int -> string
16+
abstract readlines: __hint: int -> string list
17+
abstract tell: unit -> int
1318

1419
type TextIOWrapper =
1520
inherit IDisposable
1621
inherit TextIOBase
1722

1823
module OpenTextMode =
19-
let [<Literal>] ReadUpdate = "r+"
20-
let [<Literal>] UpdateRead = "+r"
21-
let [<Literal>] ReadTextUpdate = "rt+"
22-
let [<Literal>] ReadUpdateText = "r+t"
23-
let [<Literal>] UpdateReadText = "+rt"
24-
let [<Literal>] TextReadUpdate = "tr+"
25-
let [<Literal>] TextUpdateRead = "t+r"
26-
let [<Literal>] UpdateTextRead = "+tr"
27-
let [<Literal>] WriteUpdate = "w+"
28-
let [<Literal>] UpdateWrite = "+w"
29-
let [<Literal>] WriteTextUpdate = "wt+"
30-
let [<Literal>] WriteUpdateText = "w+t"
31-
let [<Literal>] UpdateWriteText = "+wt"
32-
let [<Literal>] TextWriteUpdate = "tw+"
33-
let [<Literal>] TextUpdateWrite = "t+w"
34-
let [<Literal>] UpdateTextWrite = "+tw"
35-
let [<Literal>] AppendUpdate = "a+"
36-
let [<Literal>] UpdateAppend = "+a"
37-
let [<Literal>] AppendTextUpdate = "at+"
38-
let [<Literal>] AppendUpdateText = "a+t"
39-
let [<Literal>] UpdateAppendText = "+at"
40-
let [<Literal>] TextAppendUpdate = "ta+"
41-
let [<Literal>] TextUpdateAppend = "t+a"
42-
let [<Literal>] UpdateTextAppend = "+ta"
43-
let [<Literal>] CreateUpdate = "x+"
44-
let [<Literal>] UpdateCreate = "+x"
45-
let [<Literal>] CreateTextUpdate = "xt+"
46-
let [<Literal>] CreateUpdateText = "x+t"
47-
let [<Literal>] UpdateCreateText = "+xt"
48-
let [<Literal>] TextCreateUpdate = "tx+"
49-
let [<Literal>] TextUpdateCreate = "t+x"
50-
let [<Literal>] UpdateTextCreate = "+tx"
51-
52-
let [<Literal>] Read = "rt"
53-
let [<Literal>] ReadText = "rt"
54-
let [<Literal>] TextRead = "tr"
55-
56-
let [<Literal>] Write = "w"
57-
let [<Literal>] WriteText = "wt"
58-
let [<Literal>] TextWrite = "tw"
59-
let [<Literal>] Append = "a"
60-
let [<Literal>] AppendText = "at"
61-
let [<Literal>] TextAppend = "ta"
62-
let [<Literal>] Create = "x"
63-
let [<Literal>] CreateText = "xt"
64-
let [<Literal>] TextCreate = "tx"
24+
[<Literal>]
25+
let ReadUpdate = "r+"
26+
27+
[<Literal>]
28+
let UpdateRead = "+r"
29+
30+
[<Literal>]
31+
let ReadTextUpdate = "rt+"
32+
33+
[<Literal>]
34+
let ReadUpdateText = "r+t"
35+
36+
[<Literal>]
37+
let UpdateReadText = "+rt"
38+
39+
[<Literal>]
40+
let TextReadUpdate = "tr+"
41+
42+
[<Literal>]
43+
let TextUpdateRead = "t+r"
44+
45+
[<Literal>]
46+
let UpdateTextRead = "+tr"
47+
48+
[<Literal>]
49+
let WriteUpdate = "w+"
50+
51+
[<Literal>]
52+
let UpdateWrite = "+w"
53+
54+
[<Literal>]
55+
let WriteTextUpdate = "wt+"
56+
57+
[<Literal>]
58+
let WriteUpdateText = "w+t"
59+
60+
[<Literal>]
61+
let UpdateWriteText = "+wt"
62+
63+
[<Literal>]
64+
let TextWriteUpdate = "tw+"
65+
66+
[<Literal>]
67+
let TextUpdateWrite = "t+w"
68+
69+
[<Literal>]
70+
let UpdateTextWrite = "+tw"
71+
72+
[<Literal>]
73+
let AppendUpdate = "a+"
74+
75+
[<Literal>]
76+
let UpdateAppend = "+a"
77+
78+
[<Literal>]
79+
let AppendTextUpdate = "at+"
80+
81+
[<Literal>]
82+
let AppendUpdateText = "a+t"
83+
84+
[<Literal>]
85+
let UpdateAppendText = "+at"
86+
87+
[<Literal>]
88+
let TextAppendUpdate = "ta+"
89+
90+
[<Literal>]
91+
let TextUpdateAppend = "t+a"
92+
93+
[<Literal>]
94+
let UpdateTextAppend = "+ta"
95+
96+
[<Literal>]
97+
let CreateUpdate = "x+"
98+
99+
[<Literal>]
100+
let UpdateCreate = "+x"
101+
102+
[<Literal>]
103+
let CreateTextUpdate = "xt+"
104+
105+
[<Literal>]
106+
let CreateUpdateText = "x+t"
107+
108+
[<Literal>]
109+
let UpdateCreateText = "+xt"
110+
111+
[<Literal>]
112+
let TextCreateUpdate = "tx+"
113+
114+
[<Literal>]
115+
let TextUpdateCreate = "t+x"
116+
117+
[<Literal>]
118+
let UpdateTextCreate = "+tx"
119+
120+
[<Literal>]
121+
let Read = "rt"
122+
123+
[<Literal>]
124+
let ReadText = "rt"
125+
126+
[<Literal>]
127+
let TextRead = "tr"
128+
129+
[<Literal>]
130+
let Write = "w"
131+
132+
[<Literal>]
133+
let WriteText = "wt"
134+
135+
[<Literal>]
136+
let TextWrite = "tw"
137+
138+
[<Literal>]
139+
let Append = "a"
140+
141+
[<Literal>]
142+
let AppendText = "at"
143+
144+
[<Literal>]
145+
let TextAppend = "ta"
146+
147+
[<Literal>]
148+
let Create = "x"
149+
150+
[<Literal>]
151+
let CreateText = "xt"
152+
153+
[<Literal>]
154+
let TextCreate = "tx"
65155

66156

67157
type _Opener = Tuple<string, int> -> int
68158

69159
type IExports =
70160
/// Return the absolute value of the argument.
71-
abstract abs : int -> int
161+
abstract abs: int -> int
72162
/// Return the absolute value of the argument.
73-
abstract abs : float -> float
163+
abstract abs: float -> float
74164
/// Return a Unicode string of one character with ordinal i; 0 <= i <= 0x10ffff.
75-
abstract chr : int -> char
165+
abstract chr: int -> char
76166
/// Return the names in the current scope.
77-
abstract dir : unit -> string list
167+
abstract dir: unit -> string list
168+
78169
/// Return an alphabetized list of names comprising (some of) the
79170
/// attributes of the given object, and of attributes reachable from
80171
/// it
81-
abstract dir : obj -> string list
172+
abstract dir: obj -> string list
173+
82174
/// Return the identity of an object.
83-
abstract id : obj -> int
175+
abstract id: obj -> int
176+
84177
///Return the length (the number of items) of an object. The argument may
85178
///be a sequence (such as a string, bytes, tuple, list, or range) or a
86179
///collection (such as a dictionary, set, or frozen set).
87-
abstract len : obj -> int
180+
abstract len: obj -> int
181+
88182
/// Make an iterator that computes the function using arguments from
89183
/// the iterable. Stops when iterable is exhausted.
90-
abstract map : ('T1 -> 'T2) * IEnumerable<'T1> -> IEnumerable<'T2>
184+
abstract map: ('T1 -> 'T2) * IEnumerable<'T1> -> IEnumerable<'T2>
185+
91186
/// Make an iterator that computes the function using arguments from each
92187
/// of the iterables. Stops when the shortest iterable is exhausted.
93-
abstract map : ('T1 * 'T2 -> 'T3) * IEnumerable<'T1> * IEnumerable<'T2> -> IEnumerable<'T3>
188+
abstract map: ('T1 * 'T2 -> 'T3) * IEnumerable<'T1> * IEnumerable<'T2> -> IEnumerable<'T3>
189+
94190
/// Make an iterator that computes the function using arguments from each
95191
/// of the iterables. Stops when the shortest iterable is exhausted.
96-
abstract map : ('T1 * 'T2 * 'T3 -> 'T4) * IEnumerable<'T1> * IEnumerable<'T2> * IEnumerable<'T3> -> IEnumerable<'T4>
192+
abstract map: ('T1 * 'T2 * 'T3 -> 'T4) * IEnumerable<'T1> * IEnumerable<'T2> * IEnumerable<'T3> -> IEnumerable<'T4>
193+
97194
/// Return the Unicode code point for a one-character string.
98-
abstract ord : char -> int
195+
abstract ord: char -> int
99196
/// Object to string
100-
abstract str : obj -> string
197+
abstract str: obj -> string
101198
/// Object to int
102-
abstract int : obj -> int
199+
abstract int: obj -> int
103200
/// Object to float
104-
abstract float : obj -> float
105-
abstract print : obj: obj -> unit
201+
abstract float: obj -> float
202+
abstract print: obj: obj -> unit
203+
204+
[<NamedParams(fromIndex = 1)>]
205+
abstract ``open``:
206+
file: int *
207+
?mode: string *
208+
?buffering: int *
209+
?encoding: string *
210+
?errors: string *
211+
?newline: string *
212+
?closefd: bool *
213+
?opener: _Opener ->
214+
TextIOWrapper
106215

107-
[<NamedParams(fromIndex=1)>] abstract ``open`` : file: int * ?mode: string * ?buffering: int * ?encoding: string * ?errors: string * ?newline: string * ?closefd: bool * ?opener: _Opener -> TextIOWrapper
108-
[<NamedParams(fromIndex=1)>] abstract ``open`` : file: string * ?mode: string * ?buffering: int * ?encoding: string * ?errors: string * ?newline: string * ?closefd: bool * ?opener: _Opener -> TextIOWrapper
216+
[<NamedParams(fromIndex = 1)>]
217+
abstract ``open``:
218+
file: string *
219+
?mode: string *
220+
?buffering: int *
221+
?encoding: string *
222+
?errors: string *
223+
?newline: string *
224+
?closefd: bool *
225+
?opener: _Opener ->
226+
TextIOWrapper
109227

110228
[<ImportAll("builtins")>]
111229
let builtins: IExports = nativeOnly

src/stdlib/Html.fs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
module Fable.Python.Html
2-
3-
open Fable.Core
4-
5-
// fsharplint:disable MemberNames
6-
7-
type IExports =
8-
abstract escape : string -> string
9-
abstract escape : string * bool -> string
10-
abstract unescape : string -> string
11-
12-
/// Miscellaneous operating system interfaces
13-
[<ImportAll("html")>]
14-
let html: IExports = nativeOnly
1+
module Fable.Python.Html
2+
3+
open Fable.Core
4+
5+
// fsharplint:disable MemberNames
6+
7+
type IExports =
8+
abstract escape: string -> string
9+
abstract escape: string * bool -> string
10+
abstract unescape: string -> string
11+
12+
/// Miscellaneous operating system interfaces
13+
[<ImportAll("html")>]
14+
let html: IExports = nativeOnly

src/stdlib/Sys.fs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,20 @@ type ByteOrder =
1010
| Big
1111

1212
type VersionInfo =
13-
abstract major : int
13+
abstract major: int
1414
abstract minor: int
1515
abstract micro: int
1616
abstract releaselevel: string
1717
abstract serial: int
1818

1919
type IExports =
20-
abstract argv : string array
20+
abstract argv: string array
2121
abstract byteorder: ByteOrder
2222
abstract hexversion: int
2323
abstract maxsize: int
2424
abstract maxunicode: int
25-
abstract path : string array
26-
abstract platform : string
25+
abstract path: string array
26+
abstract platform: string
2727
abstract prefix: string
2828
abstract version: string
2929
abstract version_info: VersionInfo

test/TestBuiltins.fs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,8 @@ let ``test print works`` () =
99
result |> equal ()
1010

1111
let ``test __name__ works`` () = __name__ |> equal "test_builtins"
12+
13+
[<Fact>]
14+
let ``test write works`` () =
15+
let result = builtins.``open``("test.txt", OpenTextMode.Write)
16+
result.write "ABC" |> equal ()

0 commit comments

Comments
 (0)