Skip to content

Commit 56b5c35

Browse files
authored
Saimon/version module initializer (#329)
1 parent fb10924 commit 56b5c35

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+3496
-1903
lines changed

cterasdk/asynchronous/core/files/browser.py

Lines changed: 192 additions & 130 deletions
Large diffs are not rendered by default.

cterasdk/asynchronous/core/files/io.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from ....cio import core as fs
1+
from ....cio.core import commands as fs
22

33

44
async def listdir(core, param):
@@ -33,9 +33,8 @@ async def handle(core, param):
3333
return await core.io.download(param)
3434

3535

36-
async def handle_many(core, param, directory):
37-
async with fs.EnsureDirectory(listdir, core, directory) as (_, resource):
38-
return await core.io.download_zip(str(resource.cloudFolderInfo.uid), param)
36+
async def handle_many(core, cloudfolder, param):
37+
return await core.io.download_zip(cloudfolder, param)
3938

4039

4140
async def upload(core, cloudfolder, param):
Lines changed: 122 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,100 @@
11
from ..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
45
from . import io
56

67

78
class 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()

cterasdk/cio/common.py

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
import urllib.parse
12
from pathlib import PurePosixPath
2-
from ..objects.uri import quote
3+
from ..common import Object
34
from ..common.utils import utf8_decode
45
from ..convert.serializers import toxmlstr
56

@@ -33,40 +34,77 @@ def reference(self):
3334
def relative(self):
3435
return self._reference.as_posix()
3536

37+
@property
38+
def relative_encode(self):
39+
return urllib.parse.quote(self._reference.as_posix())
40+
3641
@property
3742
def name(self):
3843
return self._reference.name
3944

4045
@property
4146
def parent(self):
42-
return self.__class__(self._scope.as_posix(), self._reference.parent.as_posix())
47+
return self.__class__(self._reference.parent.as_posix()) # pylint: disable=no-value-for-parameter
4348

4449
@property
4550
def absolute(self):
4651
return f'/{self.scope.joinpath(self.reference).as_posix()}'
4752

4853
@property
4954
def absolute_encode(self):
50-
return f'/{self.scope.joinpath(quote(self.reference.as_posix())).as_posix()}'
55+
return f'/{self.scope.joinpath(urllib.parse.quote(self.reference.as_posix())).as_posix()}'
5156

5257
@property
5358
def absolute_parent(self):
5459
return self.parent.as_posix()
5560

61+
@property
62+
def extension(self):
63+
return self.reference.suffix
64+
5665
def join(self, p):
5766
"""
5867
Join Path.
5968
6069
:param str p: Path.
6170
"""
62-
return self.__class__(self._scope.as_posix(), self.reference.joinpath(p).as_posix())
71+
return self.__class__(self.reference.joinpath(p).as_posix()) # pylint: disable=no-value-for-parameter
6372

6473
@property
6574
def parts(self):
6675
return self.reference.parts
6776

77+
def __eq__(self, p):
78+
return self.absolute == p.absolute
79+
80+
def __str__(self):
81+
return self.relative
82+
83+
84+
class BaseResource(Object):
85+
"""
86+
Class for a Filesystem Resource.
87+
88+
:ivar str name: Resource name
89+
:ivar cterasdk.cio.common.BasePath path: Path Object
90+
:ivar bool is_dir: ``True`` if directory, ``False`` otherwise
91+
:ivar int size: Size
92+
:ivar datetime.datetime last_modified: Last Modified
93+
:ivar str extension: Extension
94+
"""
95+
def __init__(self, name, path, is_dir, size, last_modified):
96+
super().__init__(name=name, path=path, is_dir=is_dir, size=size, last_modified=last_modified, extension=path.extension)
97+
98+
def __repr__(self):
99+
return (
100+
f"{self.__class__.__name__}("
101+
f"'is_dir': {self.is_dir}, "
102+
f"'size': {self.size}, "
103+
f"'path': {self.path}}})"
104+
)
105+
68106
def __str__(self):
69-
return self.absolute
107+
return str(self.path)
70108

71109

72110
def encode_stream(fd, size):

cterasdk/cio/core/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)