@@ -8,22 +8,29 @@ open Fable
88#if FABLE_ COMPILER_ JAVASCRIPT || FABLE_ COMPILER_ TYPESCRIPT
99open Fable.Core .JsInterop
1010open FsSpreadsheet.Js
11+
1112#endif
1213#if FABLE_ COMPILER_ PYTHON
1314open FsSpreadsheet.Py
1415open Fable.Core .PyInterop
16+
17+ importAll " shutil"
18+ importAll " os"
19+ import " Path" " pathlib"
20+
1521#endif
1622
1723#if ! FABLE_ COMPILER
1824open FsSpreadsheet.Net
1925#endif
2026
27+
2128let 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> =
5360let 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-
148147let 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