Skip to content

Commit 8c6d20d

Browse files
committed
finish up python io
1 parent ac29af6 commit 8c6d20d

3 files changed

Lines changed: 42 additions & 34 deletions

File tree

.config/dotnet-tools.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@
33
"isRoot": true,
44
"tools": {
55
"fable": {
6-
"version": "4.22.0",
6+
"version": "4.24.0",
77
"commands": [
88
"fable"
9-
]
9+
],
10+
"rollForward": false
1011
}
1112
}
1213
}

build/BasicTasks.fs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,8 @@ let clean = BuildTask.create "Clean" [] {
153153
++ "tests/**/bin"
154154
++ "tests/**/obj"
155155
++ "tests/TestingUtils/TestResults"
156+
++ "tests/**/py"
157+
++ "tests/**/js"
156158
++ "dist"
157159
++ ProjectInfo.netPkgDir
158160
|> Shell.cleanDirs

src/ARCtrl/ContractIO/FileSystemHelper.fs

Lines changed: 37 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,29 @@ open Fable
88
#if FABLE_COMPILER_JAVASCRIPT || FABLE_COMPILER_TYPESCRIPT
99
open Fable.Core.JsInterop
1010
open FsSpreadsheet.Js
11+
1112
#endif
1213
#if FABLE_COMPILER_PYTHON
1314
open FsSpreadsheet.Py
1415
open Fable.Core.PyInterop
16+
17+
importAll "shutil"
18+
importAll "os"
19+
import "Path" "pathlib"
20+
1521
#endif
1622

1723
#if !FABLE_COMPILER
1824
open FsSpreadsheet.Net
1925
#endif
2026

27+
2128
let directoryExistsAsync (path : string) : CrossAsync<bool> =
2229
#if FABLE_COMPILER_JAVASCRIPT || FABLE_COMPILER_TYPESCRIPT
23-
import "directoryExists" "./FileSystem.js"
30+
import "directoryExists" "${outdir}/FileSystem.js"
2431
#endif
2532
#if FABLE_COMPILER_PYTHON
26-
let f : string -> bool = import "directoryExists" "./file_system.py"
33+
let f (path : string) : bool = emitPyExpr (path) "Path(path).is_dir()"
2734
crossAsync {
2835
return f path
2936
}
@@ -39,7 +46,7 @@ let createDirectoryAsync (path : string) : CrossAsync<unit> =
3946
import "createDirectory" "./FileSystem.js"
4047
#endif
4148
#if FABLE_COMPILER_PYTHON
42-
let f : string -> unit =import "createDirectory" "./file_system.py"
49+
let f (path : string) : unit = emitPyExpr (path) "Path(path).mkdir(parents=True, exist_ok=True)"
4350
crossAsync {
4451
f path
4552
}
@@ -53,17 +60,10 @@ let createDirectoryAsync (path : string) : CrossAsync<unit> =
5360
let ensureDirectoryAsync (path : string) : CrossAsync<unit> =
5461
#if FABLE_COMPILER_JAVASCRIPT || FABLE_COMPILER_TYPESCRIPT
5562
import "ensureDirectory" "./FileSystem.js"
56-
#endif
57-
#if FABLE_COMPILER_PYTHON
58-
let f : string -> unit = import "ensureDirectory" "./file_system.py"
59-
crossAsync {
60-
f path
61-
}
62-
#endif
63-
#if !FABLE_COMPILER
63+
#else
6464
crossAsync {
6565
let! exists = directoryExistsAsync path
66-
if not <| exists then
66+
if not exists then
6767
return! createDirectoryAsync path
6868
}
6969
#endif
@@ -73,9 +73,9 @@ let ensureDirectoryOfFileAsync (filePath : string) : CrossAsync<unit> =
7373
import "ensureDirectoryOfFile" "./FileSystem.js"
7474
#endif
7575
#if FABLE_COMPILER_PYTHON
76-
let f : string -> unit = import "ensureDirectoryOfFile" "./file_system.py"
76+
let f (path : string) : string = emitPyExpr (path) "Path(file_path).parent"
7777
crossAsync {
78-
f filePath
78+
return! ensureDirectoryAsync(f filePath)
7979
}
8080
#endif
8181
#if !FABLE_COMPILER
@@ -90,7 +90,7 @@ let fileExistsAsync (path : string) : CrossAsync<bool> =
9090
import "fileExists" "./FileSystem.js"
9191
#endif
9292
#if FABLE_COMPILER_PYTHON
93-
let f : string -> bool = import "fileExists" "./file_system.py"
93+
let f (path : string) : bool = emitPyExpr (path) "Path(path).is_file()"
9494
crossAsync {
9595
return f path
9696
}
@@ -108,7 +108,7 @@ let readFileTextAsync (path : string) : CrossAsync<string> =
108108
import "readFileText" "./FileSystem.js"
109109
#endif
110110
#if FABLE_COMPILER_PYTHON
111-
let f : string -> string = import "readFileText" "./file_system.py"
111+
let f (path : string) : string = emitPyStatement (path) "with open(path, 'r', encoding='utf-8') as f: return f.read()"
112112
crossAsync {
113113
return f path
114114
}
@@ -124,7 +124,7 @@ let readFileBinaryAsync (path : string) : CrossAsync<byte []> =
124124
import "readFileBinary" "./FileSystem.js"
125125
#endif
126126
#if FABLE_COMPILER_PYTHON
127-
let f : string -> byte [] = import "readFileBinary" "./file_system.py"
127+
let f (path : string) : byte [] = emitPyStatement (path) "with open(path, 'rb') as f: return f.read()"
128128
crossAsync {
129129
return f path
130130
}
@@ -144,15 +144,16 @@ let readFileXlsxAsync (path : string) : CrossAsync<FsWorkbook> =
144144
}
145145
#endif
146146

147-
148147
let moveFileAsync oldPath newPath =
149148
#if FABLE_COMPILER_JAVASCRIPT || FABLE_COMPILER_TYPESCRIPT
150149
import "moveFile" "./FileSystem.js"
151150
#endif
152151
#if FABLE_COMPILER_PYTHON
153-
let f : string * string -> unit = import "moveFile" "./file_system.py"
152+
let f (oldPath : string) (newPath : string) : unit =
153+
// import "moveFile" "${outDir}/src/ARCtrl/ContractIO/file_system.py"
154+
emitPyStatement (oldPath,newPath) "shutil.move($0, $1)"
154155
crossAsync {
155-
f (oldPath,newPath)
156+
f oldPath newPath
156157
}
157158
#endif
158159
#if !FABLE_COMPILER
@@ -166,10 +167,7 @@ let moveDirectoryAsync oldPath newPath =
166167
import "moveDirectory" "./FileSystem.js"
167168
#endif
168169
#if FABLE_COMPILER_PYTHON
169-
let f : string * string -> unit = import "moveDirectory" "./file_system.py"
170-
crossAsync {
171-
f (oldPath,newPath)
172-
}
170+
moveFileAsync oldPath newPath
173171
#endif
174172
#if !FABLE_COMPILER
175173
crossAsync {
@@ -182,7 +180,10 @@ let deleteFileAsync path =
182180
import "deleteFile" "./FileSystem.js"
183181
#endif
184182
#if FABLE_COMPILER_PYTHON
185-
let f : string -> unit = import "deleteFile" "./file_system.py"
183+
let f (path : string) : unit = emitPyStatement (path) """try:
184+
os.remove(path)
185+
except FileNotFoundError:
186+
pass"""
186187
crossAsync {
187188
f path
188189
}
@@ -198,7 +199,7 @@ let deleteDirectoryAsync path =
198199
import "deleteDirectory" "./FileSystem.js"
199200
#endif
200201
#if FABLE_COMPILER_PYTHON
201-
let f : string -> unit =import "deleteDirectory" "./file_system.py"
202+
let f (path : string) : unit = emitPyStatement (path) "shutil.rmtree(path, ignore_errors=True)"
202203
crossAsync {
203204
f path
204205
}
@@ -215,9 +216,10 @@ let writeFileTextAsync path text =
215216
import "writeFileText" "./FileSystem.js"
216217
#endif
217218
#if FABLE_COMPILER_PYTHON
218-
let f : string * string -> unit = import "writeFileText" "./file_system.py"
219+
let f (path :string) (text : string) : unit =
220+
emitPyStatement (path,text) "with open($0, 'w') as f: f.write($1)"
219221
crossAsync {
220-
f(path,text)
222+
f path text
221223
}
222224
#endif
223225
#if !FABLE_COMPILER
@@ -231,9 +233,11 @@ let writeFileBinaryAsync path (bytes : byte []) =
231233
import "writeFileBinary" "./FileSystem.js"
232234
#endif
233235
#if FABLE_COMPILER_PYTHON
234-
let f : string * byte [] -> unit = import "writeFileBinary" "./file_system.py"
236+
let f (path :string) (bytes : byte []) : unit =
237+
// let f : (string * byte []) -> unit = import "writeFileBinary" "${outDir}/src/ARCtrl/ContractIO/file_system.py"
238+
emitPyStatement (path,bytes) "with open($0, 'wb') as f: f.write($1)"
235239
crossAsync {
236-
f (path,bytes)
240+
f path bytes
237241
}
238242
#endif
239243
#if !FABLE_COMPILER
@@ -275,7 +279,8 @@ let getSubDirectoriesAsync (path : string) : CrossAsync<string []> =
275279
}
276280
#endif
277281
#if FABLE_COMPILER_PYTHON
278-
let f : string -> string [] = import "getSubDirectories" "./file_system.py"
282+
283+
let f (path : string) : string [] = emitPyExpr (path) "[str(entry) for entry in Path(path).iterdir() if entry.is_dir()]"
279284
crossAsync {
280285
let paths = f path
281286
return paths |> Array.map standardizeSlashes
@@ -297,7 +302,7 @@ let getSubFilesAsync (path : string) : CrossAsync<string []> =
297302
}
298303
#endif
299304
#if FABLE_COMPILER_PYTHON
300-
let f : string -> string [] = import "getSubFiles" "./file_system.py"
305+
let f (path : string) : string [] = emitPyExpr (path) "[str(entry) for entry in Path(path).iterdir() if entry.is_file()]"
301306
crossAsync {
302307
let paths = f path
303308
return paths |> Array.map standardizeSlashes

0 commit comments

Comments
 (0)