Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions cterasdk/asynchronous/core/files/browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,14 @@ async def listdir(self, path, depth=None, include_deleted=False):
"""
return await io.listdir(self._core, self.normalize(path), depth=depth, include_deleted=include_deleted)

async def exists(self, path):
"""
Check if item exists

:param str path: Path
"""
return await io.exists(self._core, self.normalize(path))

async def versions(self, path):
"""
List snapshots of a file or directory
Expand Down
14 changes: 11 additions & 3 deletions cterasdk/asynchronous/core/files/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,18 @@ async def listdir(core, path, depth=None, include_deleted=False, search_criteria
return await core.v1.api.execute('', 'fetchResources', param)


async def root(core, path):
async def exists(core, path):
try:
await metadata(core, path)
return True
except exceptions.ResourceNotFoundError:
return False


async def metadata(core, path):
response = await listdir(core, path, 0)
if response.root is None:
raise exceptions.RemoteStorageException(path.absolute)
raise exceptions.ResourceNotFoundError(path.absolute)
return response.root


Expand Down Expand Up @@ -81,7 +89,7 @@ async def move(core, *paths, destination=None):


async def retrieve_remote_dir(core, directory):
resource = await root(core, directory)
resource = await metadata(core, directory)
if not resource.isFolder:
raise exceptions.RemoteStorageException('The destination path is not a directory', None, path=directory.absolute)
return str(resource.cloudFolderInfo.uid)
Expand Down
4 changes: 2 additions & 2 deletions cterasdk/asynchronous/core/notifications.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from .base_command import BaseCommand
from ...common import Object
from ...lib import CursorResponse
from ...exceptions import ClientResponseException, NotificationsError
from ...exceptions import HTTPError, NotificationsError


class Notifications(BaseCommand):
Expand Down Expand Up @@ -81,7 +81,7 @@ async def ancestors(self, descendant):
logging.getLogger('cterasdk.metadata.connector').debug('Getting ancestors. %s', {'guid': param.guid, 'folder_id': param.folder_id})
try:
return await self._core.v2.api.post('/metadata/ancestors', param)
except ClientResponseException:
except HTTPError:
logging.getLogger('cterasdk.metadata.connector').error('Could not retrieve ancestors. %s',
{'folder_id': param.folder_id, 'guid': param.guid})
raise
Expand Down
18 changes: 17 additions & 1 deletion cterasdk/asynchronous/edge/files/browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,23 @@ async def listdir(self, path):

:param str path: Path
"""
return await io.listdir(self._edge, path)
return await io.listdir(self._edge, self.normalize(path))

async def walk(self, path):
"""
Walk Directory Contents

:param str path: Path to walk
"""
return io.walk(self._edge, path)

async def exists(self, path):
"""
Check if item exists

:param str path: Path
"""
return await io.exists(self._edge, self.normalize(path))

async def handle(self, path):
"""
Expand Down
23 changes: 21 additions & 2 deletions cterasdk/asynchronous/edge/files/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,33 @@
from ....cio.common import encode_request_parameter
from ....cio import edge as fs
from ....cio import exceptions
from ....exceptions import HTTPError


logger = logging.getLogger('cterasdk.edge')


async def listdir(edge, path):
with fs.listdir(path) as param:
return await edge.api.execute('/status/fileManager', 'listPhysicalFolders', param)
return fs.format_listdir_response(path.reference.as_posix(), await edge.io.propfind(path.absolute, 1))


async def walk(edge, path):
paths = [fs.EdgePath.instance('/', path)]
while len(paths) > 0:
path = paths.pop(0)
entries = await listdir(edge, path)
for e in entries:
if e.is_dir:
paths.append(fs.EdgePath.instance('/', e))
yield e


async def exists(edge, path):
try:
await edge.io.propfind(path.absolute, 0)
return True
except HTTPError:
return False


async def mkdir(edge, path):
Expand Down
47 changes: 41 additions & 6 deletions cterasdk/cio/edge.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import logging
from datetime import datetime
from contextlib import contextmanager
from pathlib import Path
from ..common import Object
from ..objects.uri import unquote
from . import common, exceptions


Expand All @@ -10,17 +13,49 @@
class EdgePath(common.BasePath):
"""Path for CTERA Edge Filer"""

def __init__(self, scope, reference):
"""
Initialize a CTERA Edge Filer Path.

:param str scope: Scope.
:param str reference: Reference.
"""
if isinstance(reference, Object):
super().__init__(scope, reference.path)
elif isinstance(reference, str):
super().__init__(scope, reference)
else:
message = 'Path validation failed: ensure the path exists and is correctly formatted.'
logger.error(message)
raise ValueError(message)

@staticmethod
def instance(scope, reference):
return EdgePath(scope, reference)


@contextmanager
def listdir(path):
param = Object()
param.path = path
logger.info('Listing directory: %s', path)
yield param
def fetch_reference(href):
namespace = 'localFiles/'
return unquote(href[href.index(namespace)+len(namespace):])


def format_listdir_response(parent, response):
entries = []
for e in response:
path = fetch_reference(e.href)
if parent != path:
is_dir = e.getcontenttype == 'httpd/unix-directory'
param = Object(
path=path,
name=Path(path).name,
is_dir=is_dir,
is_file=not is_dir,
created_at=e.creationdate,
last_modified=datetime.strptime(e.getlastmodified, "%a, %d %b %Y %H:%M:%S GMT").isoformat(),
size=e.getcontentlength
)
entries.append(param)
return entries


@contextmanager
Expand Down
12 changes: 7 additions & 5 deletions cterasdk/clients/base.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import logging
import threading
from . import async_requests
from . import async_requests, errors
from .settings import ClientSessionSettings, TraceSettings
from ..common import utils

Expand Down Expand Up @@ -103,11 +103,13 @@ def join_headers(self, request):
def baseurl(self):
return self._builder()

def request(self, request, *, on_response=None):
return self._request(request, on_response=on_response)
def request(self, request, *, on_response=None, on_error=None):
on_error = on_error if on_error else errors.DefaultHandler()
return self._request(request, on_response=on_response, on_error=on_error)

async def async_request(self, request, *, on_response=None):
return await self._request(request, on_response=on_response)
async def a_request(self, request, *, on_response=None, on_error=None):
on_error = on_error if on_error else errors.DefaultHandler()
return await self._request(request, on_response=on_response, on_error=on_error)

async def close(self):
await self._session.close()
Expand Down
Loading