1- from typing import Optional , Union , List
1+ from contextlib import asynccontextmanager
2+ from typing import Optional , List
3+
24from .base import (
35 BaseSubAPI , lua_string ,
4- nil_return , opt_str_return , str_return , bool_return , int_return , list_return , opt_int_return ,
6+ nil_return , opt_str_return , str_return , bool_return ,
7+ int_return , list_return , dict_return ,
58)
6- from uuid import uuid4
79
810
9- class CCFile (BaseSubAPI ):
10- def __init__ (self , cc , path , mode ):
11+ class BaseHandle (BaseSubAPI ):
12+ def __init__ (self , cc , var ):
1113 super ().__init__ (cc )
12- self ._path = path
13- self ._mode = mode
14-
15- async def __aenter__ (self ):
16- self ._id = str (uuid4 ())
17- self ._API = 'temp[{}]' .format (lua_string (self ._id ))
18- await self ._cc ._send_cmd ('{} = fs.open({}, {})' .format (self ._API , * map (lua_string , [
19- self ._path , self ._mode
20- ])))
21- return self
14+ self ._API = var
2215
23- async def __aexit__ (self , exc_type , exc , tb ):
24- await self ._cc ._send_cmd ('{}.close(); {} = nil' .format (self ._API , self ._API ))
2516
26- async def read (self ) -> Optional [int ]:
27- return opt_int_return (await self ._send ('read' ))
17+ class ReadHandle (BaseHandle ):
18+ async def read (self , count : int ) -> Optional [str ]:
19+ return opt_str_return (await self ._send ('read' , count ))
2820
2921 async def readLine (self ) -> Optional [str ]:
3022 return opt_str_return (await self ._send ('readLine' ))
3123
3224 async def readAll (self ) -> str :
3325 return str_return (await self ._send ('readAll' ))
3426
35- async def write (self , data : Union [str , int ]):
36- return nil_return (await self ._send ('write' , data ))
37-
38- async def writeLine (self , data : str ):
39- return nil_return (await self ._send ('writeLine' , data ))
40-
41- async def flush (self ):
42- return nil_return (await self ._send ('flush' ))
43-
4427 def __aiter__ (self ):
4528 return self
4629
@@ -51,6 +34,17 @@ async def __anext__(self):
5134 return line
5235
5336
37+ class WriteHandle (BaseHandle ):
38+ async def write (self , text : str ):
39+ return nil_return (await self ._send ('write' , text ))
40+
41+ async def writeLine (self , text : str ):
42+ return nil_return (await self ._send ('writeLine' , text ))
43+
44+ async def flush (self ):
45+ return nil_return (await self ._send ('flush' ))
46+
47+
5448class FSAPI (BaseSubAPI ):
5549 _API = 'fs'
5650
@@ -66,9 +60,6 @@ async def isDir(self, path: str) -> bool:
6660 async def isReadOnly (self , path : str ) -> bool :
6761 return bool_return (await self ._send ('isReadOnly' , path ))
6862
69- async def getName (self , path : str ) -> str :
70- return str_return (await self ._send ('getName' , path ))
71-
7263 async def getDrive (self , path : str ) -> Optional [str ]:
7364 return opt_str_return (await self ._send ('getDrive' , path ))
7465
@@ -78,6 +69,9 @@ async def getSize(self, path: str) -> int:
7869 async def getFreeSpace (self , path : str ) -> int :
7970 return int_return (await self ._send ('getFreeSpace' , path ))
8071
72+ async def getCapacity (self , path : str ) -> int :
73+ return int_return (await self ._send ('getCapacity' , path ))
74+
8175 async def makeDir (self , path : str ):
8276 return nil_return (await self ._send ('makeDir' , path ))
8377
@@ -93,7 +87,8 @@ async def delete(self, path: str):
9387 async def combine (self , basePath : str , localPath : str ) -> str :
9488 return str_return (await self ._send ('combine' , basePath , localPath ))
9589
96- def open (self , path : str , mode : str ) -> CCFile :
90+ @asynccontextmanager
91+ async def open (self , path : str , mode : str ):
9792 '''
9893 Usage:
9994
@@ -104,15 +99,32 @@ def open(self, path: str, mode: str) -> CCFile:
10499 async for line in f:
105100 ...
106101 '''
107- return CCFile (self ._cc , path , mode )
102+ fid = self ._cc ._new_task_id ()
103+ var = 'temp[{}]' .format (lua_string (fid ))
104+ await self ._cc ._send_cmd ('{} = fs.open({}, {})' .format (
105+ var , * map (lua_string , [path , mode ])))
106+ try :
107+ yield (ReadHandle if mode == 'r' else WriteHandle )(self ._cc , var )
108+ finally :
109+ await self ._cc ._send_cmd ('{}.close(); {} = nil' .format (var , var ))
108110
109111 async def find (self , wildcard : str ) -> List [str ]:
110112 return list_return (await self ._send ('find' , wildcard ))
111113
112114 async def getDir (self , path : str ) -> str :
113115 return str_return (await self ._send ('getDir' , path ))
114116
117+ async def getName (self , path : str ) -> str :
118+ return str_return (await self ._send ('getName' , path ))
119+
120+ async def isDriveRoot (self , path : str ) -> bool :
121+ return bool_return (await self ._send ('isDriveRoot' , path ))
122+
115123 async def complete (
116- self , partialName : str , path : str , includeFiles : bool = None , includeSlashes : bool = None ,
124+ self , partialName : str , path : str , includeFiles : bool = None , includeDirs : bool = None ,
117125 ) -> List [str ]:
118- return list_return (await self ._send ('complete' , partialName , path , includeFiles , includeSlashes ))
126+ return list_return (await self ._send (
127+ 'complete' , partialName , path , includeFiles , includeDirs , omit_nulls = False ))
128+
129+ async def attributes (self , path : str ) -> dict :
130+ return dict_return (await self ._send ('attributes' , path ))
0 commit comments