-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathbrowser.py
More file actions
214 lines (179 loc) · 8.54 KB
/
browser.py
File metadata and controls
214 lines (179 loc) · 8.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
from ..base_command import BaseCommand
from ....cio.edge.commands import ListDirectory, RecursiveIterator, GetMetadata, Open, OpenMany, Upload, \
CreateDirectory, Copy, Move, Delete, Download, DownloadMany, Rename, EnsureDirectory
from ....lib.storage import commonfs
from . import io
class FileBrowser(BaseCommand):
"""Edge Filer File Browser API."""
async def listdir(self, path):
"""
List directory contents.
:param str path: Path.
:returns: Directory contents.
:rtype: AsyncIterator[cterasdk.cio.edge.types.EdgeResource]
:raises cterasdk.exceptions.io.core.GetMetadataError: If the directory was not found.
:raises cterasdk.exceptions.io.core.NotADirectoryException: If the target path is not a directory.
:raises cterasdk.exceptions.io.edge.ListDirectoryError: Raised on error to fetch directory contents.
"""
async with EnsureDirectory(io.listdir, self._edge, path):
for o in await ListDirectory(io.listdir, self._edge, path).a_execute():
yield o
async def walk(self, path=None):
"""
Walk directory contents.
:param str, optional path: Path to walk. Defaults to the root directory.
:returns: A generator of file-system objects.
:rtype: AsyncIterator[cterasdk.cio.edge.types.EdgeResource]
:raises cterasdk.exceptions.io.core.GetMetadataError: If the directory was not found.
:raises cterasdk.exceptions.io.core.NotADirectoryException: If the target path is not a directory.
"""
async with EnsureDirectory(io.listdir, self._edge, path):
async for o in RecursiveIterator(io.listdir, self._edge, path).a_generate():
yield o
async def properties(self, path):
"""
Get object properties.
:param str path: Path.
:returns: Object properties.
:rtype: cterasdk.cio.edge.types.EdgeResource
:raises cterasdk.exceptions.io.core.GetMetadataError: Raised on error to obtain object metadata.
"""
async with GetMetadata(io.listdir, self._edge, path, False) as (_, metadata):
return metadata
async def exists(self, path):
"""
Check whether an item exists.
:param str path: Path.
:returns: ``True`` if the item exists, ``False`` otherwise.
:rtype: bool
"""
async with GetMetadata(io.listdir, self._edge, path, True) as (exists, *_):
return exists
async def handle(self, path):
"""
Get file handle.
:param str path: Path to a file.
:returns: File handle.
:rtype: object
:raises cterasdk.exceptions.io.edge.OpenError: Raised on error to obtain a file handle.
"""
return await Open(io.handle, self._edge, path).a_execute()
async def handle_many(self, directory, *objects):
"""
Get a ZIP archive file handle.
:param str directory: Path to a folder.
:param args objects: Files and folders to include.
:returns: File handle.
:rtype: object
"""
return await OpenMany(io.handle_many, self._edge, directory, *objects).a_execute()
async def download(self, path, destination=None):
"""
Download a file.
:param str path: The file path on the Edge Filer.
:param str, optional destination:
File destination. If a directory is provided, the original filename is preserved.
Defaults to the default download directory.
:returns: Path to the local file.
:rtype: str
:raises cterasdk.exceptions.io.edge.OpenError: Raised on error to obtain a file handle.
"""
return await Download(io.handle, self._edge, path, destination).a_execute()
async def download_many(self, target, objects, destination=None):
"""
Download selected files and/or directories as a ZIP archive.
.. warning::
The provided list of objects is not validated. Only existing files and directories
will be included in the resulting ZIP file.
:param str target:
Path to the cloud folder containing the files and directories to download.
:param list[str] objects:
List of file and/or directory names to include in the download.
:param str destination:
Optional. Path to the destination file or directory. If a directory is provided,
the original filename is preserved. Defaults to the default download directory.
:returns: Path to the local file.
:rtype: str
"""
return await DownloadMany(io.handle_many, self._edge, target, objects, destination).a_execute()
async def upload(self, destination, handle, name=None):
"""
Upload from a file handle.
:param str destination: Remote path.
:param object handle: File-like handle.
:param str, optional name: Filename to use if it cannot be derived from ``destination``
:returns: Remote file path.
:rtype: str
:raises cterasdk.exceptions.io.edge.UploadError: Raised on upload failure.
"""
return await Upload(io.upload, self._edge, io.listdir, destination, handle, name).a_execute()
async def upload_file(self, path, destination):
"""
Upload a file.
:param str path: Local path.
:param str destination: Remote path.
:returns: Remote file path.
:rtype: str
:raises cterasdk.exceptions.io.edge.UploadError: Raised on upload failure.
"""
_, name = commonfs.split_file_directory(path)
with open(path, 'rb') as handle:
return await self.upload(destination, handle, name)
async def mkdir(self, path):
"""
Create a new directory.
:param str path: Directory path.
:returns: Remote directory path.
:rtype: str
:raises cterasdk.exceptions.io.edge.CreateDirectoryError: Raised on error to create a directory.
"""
return await CreateDirectory(io.mkdir, self._edge, path).a_execute()
async def makedirs(self, path):
"""
Create a directory recursively.
:param str path: Directory path.
:returns: Remote directory path.
:rtype: str
:raises cterasdk.exceptions.io.edge.CreateDirectoryError: Raised on error to create a directory.
"""
return await CreateDirectory(io.mkdir, self._edge, path, True).a_execute()
async def copy(self, path, destination=None, overwrite=False):
"""
Copy a file or directory.
:param str path: Source file or directory path.
:param str destination: Destination directory path.
:param bool, optional overwrite: Overwrite on conflict. Defaults to ``False``.
:raises cterasdk.exceptions.io.edge.CopyError: Raised on error to copy a file or directory.
"""
if destination is None:
raise ValueError('Copy destination was not specified.')
return await Copy(io.copy, self._edge, io.listdir, path, destination, overwrite).a_execute()
async def move(self, path, destination=None, overwrite=False):
"""
Move a file or directory.
:param str path: Source file or directory path.
:param str destination: Destination directory path.
:param bool, optional overwrite: Overwrite on conflict. Defaults to ``False``.
:raises cterasdk.exceptions.io.edge.MoveError: Raised on error to move a file or directory.
"""
if destination is None:
raise ValueError('Move destination was not specified.')
return await Move(io.move, self._edge, io.listdir, path, destination, overwrite).a_execute()
async def rename(self, path, name, overwrite=False):
"""
Rename a file or directory.
:param str path: Path of the file or directory to rename.
:param str name: New name for the file or directory.
:param bool, optional overwrite: Overwrite on conflict. Defaults to ``False``.
:returns: Remote object path.
:rtype: str
:raises cterasdk.exceptions.io.edge.RenameError: Raised on error to rename a file or directory.
"""
return await Rename(io.move, self._edge, io.listdir, path, name, overwrite).a_execute()
async def delete(self, path):
"""
Delete a file or directory.
:param str path: File or directory path.
:raises cterasdk.exceptions.io.edge.DeleteError: Raised on error to delete a file or directory.
"""
return await Delete(io.delete, self._edge, path).a_execute()