11from ..base_command import BaseCommand
2- from ....cio .edge import EdgePath , ListDirectory , RecursiveIterator , GetMetadata , Open , OpenMany , Upload , \
3- UploadFile , CreateDirectory , Copy , Move , Delete , Download , DownloadMany
2+ from ....cio .edge .commands import ListDirectory , RecursiveIterator , GetMetadata , Open , OpenMany , Upload , \
3+ CreateDirectory , Copy , Move , Delete , Download , DownloadMany , Rename , EnsureDirectory
4+ from ....lib .storage import commonfs
45from . import io
56
67
78class FileBrowser (BaseCommand ):
8- """ Edge Filer File Browser APIs """
9+ """Edge Filer File Browser API. """
910
1011 async def listdir (self , path ):
1112 """
12- List Directory
13+ List directory contents.
1314
14- :param str path: Path
15+ :param str path: Path.
16+ :returns: Directory contents.
17+ :rtype: AsyncIterator[cterasdk.cio.edge.types.EdgeResource]
18+ :raises cterasdk.exceptions.io.core.GetMetadataError: If the directory was not found.
19+ :raises cterasdk.exceptions.io.core.NotADirectoryException: If the target path is not a directory.
20+ :raises cterasdk.exceptions.io.edge.ListDirectoryError: Raised on error to fetch directory contents.
1521 """
16- for o in await ListDirectory (io .listdir , self ._edge , self .normalize (path )).a_execute ():
17- yield o
22+ async with EnsureDirectory (io .listdir , self ._edge , path ):
23+ for o in await ListDirectory (io .listdir , self ._edge , path ).a_execute ():
24+ yield o
1825
1926 async def walk (self , path = None ):
2027 """
21- Walk Directory Contents
28+ Walk directory contents.
2229
23- :param str, defaults to the root directory path: Path to walk
30+ :param str, optional path: Path to walk. Defaults to the root directory.
31+ :returns: A generator of file-system objects.
32+ :rtype: AsyncIterator[cterasdk.cio.edge.types.EdgeResource]
33+ :raises cterasdk.exceptions.io.core.GetMetadataError: If the directory was not found.
34+ :raises cterasdk.exceptions.io.core.NotADirectoryException: If the target path is not a directory.
2435 """
25- async for o in RecursiveIterator (io .listdir , self ._edge , self .normalize (path )).a_generate ():
26- yield o
36+ async with EnsureDirectory (io .listdir , self ._edge , path ):
37+ async for o in RecursiveIterator (io .listdir , self ._edge , path ).a_generate ():
38+ yield o
39+
40+ async def properties (self , path ):
41+ """
42+ Get object properties.
43+
44+ :param str path: Path.
45+ :returns: Object properties.
46+ :rtype: cterasdk.cio.edge.types.EdgeResource
47+ :raises cterasdk.exceptions.io.core.GetMetadataError: Raised on error to obtain object metadata.
48+ """
49+ async with GetMetadata (io .listdir , self ._edge , path , False ) as (_ , metadata ):
50+ return metadata
2751
2852 async def exists (self , path ):
2953 """
30- Check if item exists
54+ Check whether an item exists.
3155
32- :param str path: Path
56+ :param str path: Path.
57+ :returns: ``True`` if the item exists, ``False`` otherwise.
58+ :rtype: bool
3359 """
34- async with GetMetadata (io .listdir , self ._edge , self . normalize ( path ) , True ) as (exists , * _ ):
60+ async with GetMetadata (io .listdir , self ._edge , path , True ) as (exists , * _ ):
3561 return exists
3662
3763 async def handle (self , path ):
3864 """
39- Get File Handle .
65+ Get file handle .
4066
41- :param str path: Path to a file
67+ :param str path: Path to a file.
68+ :returns: File handle.
69+ :rtype: object
70+ :raises cterasdk.exceptions.io.edge.OpenError: Raised on error to obtain a file handle.
4271 """
43- return await Open (io .handle , self ._edge , self . normalize ( path ) ).a_execute ()
72+ return await Open (io .handle , self ._edge , path ).a_execute ()
4473
4574 async def handle_many (self , directory , * objects ):
4675 """
47- Get a Zip Archive File Handle .
76+ Get a ZIP archive file handle .
4877
49- :param str directory: Path to a folder
50- :param args objects: List of files and folders
78+ :param str directory: Path to a folder.
79+ :param args objects: Files and folders to include.
80+ :returns: File handle.
81+ :rtype: object
5182 """
5283 return await OpenMany (io .handle_many , self ._edge , directory , * objects ).a_execute ()
5384
5485 async def download (self , path , destination = None ):
5586 """
56- Download a file
87+ Download a file.
5788
58- :param str path: The file path on the Edge Filer
59- :param str,optional destination:
60- File destination, if it is a directory, the original filename will be kept, defaults to the default directory
89+ :param str path: The file path on the Edge Filer.
90+ :param str, optional destination:
91+ File destination. If a directory is provided, the original filename is preserved.
92+ Defaults to the default download directory.
93+ :returns: Path to the local file.
94+ :rtype: str
95+ :raises cterasdk.exceptions.io.edge.OpenError: Raised on error to obtain a file handle.
6196 """
62- return await Download (io .handle , self ._edge , self . normalize ( path ) , destination ).a_execute ()
97+ return await Download (io .handle , self ._edge , path , destination ).a_execute ()
6398
6499 async def download_many (self , target , objects , destination = None ):
65100 """
@@ -75,77 +110,105 @@ async def download_many(self, target, objects, destination=None):
75110 List of file and/or directory names to include in the download.
76111 :param str destination:
77112 Optional. Path to the destination file or directory. If a directory is provided,
78- the original filename will be preserved. Defaults to the default download directory.
113+ the original filename is preserved. Defaults to the default download directory.
114+ :returns: Path to the local file.
115+ :rtype: str
79116 """
80- return await DownloadMany (io .handle_many , self ._edge , self . normalize ( target ) , objects , destination ).a_execute ()
117+ return await DownloadMany (io .handle_many , self ._edge , target , objects , destination ).a_execute ()
81118
82- async def upload (self , name , destination , handle ):
119+ async def upload (self , destination , handle , name = None ):
83120 """
84- Upload from file handle.
121+ Upload from a file handle.
85122
86- :param str name: File name.
87- :param str destination: Path to remote directory.
88- :param object handle: Handle.
123+ :param str destination: Remote path.
124+ :param object handle: File-like handle.
125+ :param str, optional name: Filename to use if it cannot be derived from ``destination``
126+ :returns: Remote file path.
127+ :rtype: str
128+ :raises cterasdk.exceptions.io.edge.UploadError: Raised on upload failure.
89129 """
90- return await Upload (io .upload , self ._edge , io .listdir , name , self . normalize ( destination ) , handle ).a_execute ()
130+ return await Upload (io .upload , self ._edge , io .listdir , destination , handle , name ).a_execute ()
91131
92132 async def upload_file (self , path , destination ):
93133 """
94134 Upload a file.
95135
96- :param str path: Local path
97- :param str destination: Remote path
136+ :param str path: Local path.
137+ :param str destination: Remote path.
138+ :returns: Remote file path.
139+ :rtype: str
140+ :raises cterasdk.exceptions.io.edge.UploadError: Raised on upload failure.
98141 """
99- return await UploadFile (io .upload , self ._edge , io .listdir , path , self .normalize (destination )).a_execute ()
142+ _ , name = commonfs .split_file_directory (path )
143+ with open (path , 'rb' ) as handle :
144+ return await self .upload (destination , handle , name )
100145
101146 async def mkdir (self , path ):
102147 """
103- Create a new directory
148+ Create a new directory.
104149
105- :param str path: Directory path
150+ :param str path: Directory path.
151+ :returns: Remote directory path.
152+ :rtype: str
153+ :raises cterasdk.exceptions.io.edge.CreateDirectoryError: Raised on error to create a directory.
106154 """
107- return await CreateDirectory (io .mkdir , self ._edge , self . normalize ( path ) ).a_execute ()
155+ return await CreateDirectory (io .mkdir , self ._edge , path ).a_execute ()
108156
109157 async def makedirs (self , path ):
110158 """
111- Create a directory recursively
159+ Create a directory recursively.
112160
113- :param str path: Directory path
161+ :param str path: Directory path.
162+ :returns: Remote directory path.
163+ :rtype: str
164+ :raises cterasdk.exceptions.io.edge.CreateDirectoryError: Raised on error to create a directory.
114165 """
115- return await CreateDirectory (io .mkdir , self ._edge , self . normalize ( path ) , True ).a_execute ()
166+ return await CreateDirectory (io .mkdir , self ._edge , path , True ).a_execute ()
116167
117168 async def copy (self , path , destination = None , overwrite = False ):
118169 """
119- Copy a file or a folder
170+ Copy a file or directory.
120171
121- :param str path: Source file or folder path
122- :param str destination: Destination folder path
123- :param bool,optional overwrite: Overwrite on conflict, defaults to False
172+ :param str path: Source file or directory path.
173+ :param str destination: Destination directory path.
174+ :param bool, optional overwrite: Overwrite on conflict. Defaults to ``False``.
175+ :raises cterasdk.exceptions.io.edge.CopyError: Raised on error to copy a file or directory.
124176 """
125177 if destination is None :
126178 raise ValueError ('Copy destination was not specified.' )
127- return await Copy (io .copy , self ._edge , self . normalize ( path ), self . normalize ( destination ) , overwrite ).a_execute ()
179+ return await Copy (io .copy , self ._edge , io . listdir , path , destination , overwrite ).a_execute ()
128180
129181 async def move (self , path , destination = None , overwrite = False ):
130182 """
131- Move a file or a folder
183+ Move a file or directory.
132184
133- :param str path: Source file or folder path
134- :param str destination: Destination folder path
135- :param bool,optional overwrite: Overwrite on conflict, defaults to False
185+ :param str path: Source file or directory path.
186+ :param str destination: Destination directory path.
187+ :param bool, optional overwrite: Overwrite on conflict. Defaults to ``False``.
188+ :raises cterasdk.exceptions.io.edge.MoveError: Raised on error to move a file or directory.
136189 """
137190 if destination is None :
138191 raise ValueError ('Move destination was not specified.' )
139- return await Move (io .move , self ._edge , self . normalize ( path ), self . normalize ( destination ) , overwrite ).a_execute ()
192+ return await Move (io .move , self ._edge , io . listdir , path , destination , overwrite ).a_execute ()
140193
141- async def delete (self , path ):
194+ async def rename (self , path , name , overwrite = False ):
142195 """
143- Delete a file
196+ Rename a file or directory.
144197
145- :param str path: File path
198+ :param str path: Path of the file or directory to rename.
199+ :param str name: New name for the file or directory.
200+ :param bool, optional overwrite: Overwrite on conflict. Defaults to ``False``.
201+ :returns: Remote object path.
202+ :rtype: str
203+ :raises cterasdk.exceptions.io.edge.RenameError: Raised on error to rename a file or directory.
146204 """
147- return await Delete (io .delete , self ._edge , self . normalize ( path ) ).a_execute ()
205+ return await Rename (io .move , self ._edge , io . listdir , path , name , overwrite ).a_execute ()
148206
149- @staticmethod
150- def normalize (path ):
151- return EdgePath ('/' , path )
207+ async def delete (self , path ):
208+ """
209+ Delete a file or directory.
210+
211+ :param str path: File or directory path.
212+ :raises cterasdk.exceptions.io.edge.DeleteError: Raised on error to delete a file or directory.
213+ """
214+ return await Delete (io .delete , self ._edge , path ).a_execute ()
0 commit comments