From 95e74779b8cfa0de5e113abea638c4cd4ee668a8 Mon Sep 17 00:00:00 2001 From: hechth Date: Fri, 17 Nov 2023 17:49:23 +0100 Subject: [PATCH] implemented all necessary functions except setinfo --- fs_irods/iRODSFS.py | 66 ++++++++++++++++++++++++++++++++++++++++--- tests/test_iRODSFS.py | 4 --- 2 files changed, 62 insertions(+), 8 deletions(-) diff --git a/fs_irods/iRODSFS.py b/fs_irods/iRODSFS.py index 949b2d2..ee56e34 100644 --- a/fs_irods/iRODSFS.py +++ b/fs_irods/iRODSFS.py @@ -3,7 +3,7 @@ from fs.base import FS from fs.info import Info from fs.permissions import Permissions -from fs.errors import DirectoryExists, ResourceNotFound +from fs.errors import DirectoryExists, ResourceNotFound, RemoveRootError, DirectoryExpected, FileExpected from irods.session import iRODSSession from irods.collection import iRODSCollection @@ -42,7 +42,18 @@ def getinfo(self, path: str, namespaces: list|None = None) -> Info: Raises: ResourceNotFound: If the path does not exist. """ - raise NotImplementedError() + self._check_resource_exists(path) + + with self._session() as session: + info = Info({"basic": {"name": path}}) + if session.data_objects.exists(path): + info["basic"]["is_dir"] = False + info["details"] = {"type": "file"} + elif session.collections.exists(path): + info["basic"]["is_dir"] = True + info["details"] = {"type": "directory"} + + return info def listdir(self, path: str) -> list: """List a directory on the filesystem. @@ -111,7 +122,13 @@ def remove(self, path: str): ResourceNotFound: If the path does not exist. FileExpected: If the path is not a file. """ - raise NotImplementedError() + with self._session() as session: + if not session.data_objects.exists(path): + raise ResourceNotFound(path) + if not self.isfile(path): + raise FileExpected(path) + + session.data_objects.unlink(path) def removedir(self, path: str): """Remove a directory from the filesystem. @@ -120,8 +137,17 @@ def removedir(self, path: str): Raises: ResourceNotFound: If the path does not exist. DirectoryExpected: If the path is not a directory. + RemoveRootError: If the path is the root directory. """ - raise NotImplementedError() + with self._session() as session: + if not session.collections.exists(path): + raise ResourceNotFound(path) + if not self.isdir(path): + raise DirectoryExpected(path) + if path == "/": + raise RemoveRootError() + + session.collections.remove(path, recurse=False) def setinfo(self, path: str, info: dict) -> None: """Set information about a resource on the filesystem. @@ -131,4 +157,36 @@ def setinfo(self, path: str, info: dict) -> None: Raises: ResourceNotFound: If the path does not exist. """ + self._check_resource_exists(path) raise NotImplementedError() + + def _check_resource_exists(self, path): + """Check if a resource exists. + Args: + path (str): A path to a resource on the filesystem. + Raises: + ResourceNotFound: If the path does not exist. + """ + with self._session() as session: + if not session.data_objects.exists(path) and not session.collections.exists(path): + raise ResourceNotFound(path) + + def isfile(self, path: str) -> bool: + """Check if a path is a file. + Args: + path (str): A path to a resource on the filesystem. + Returns: + bool: True if the path is a file, False otherwise. + """ + with self._session() as session: + return session.data_objects.exists(path) + + def isdir(self, path: str) -> bool: + """Check if a path is a directory. + Args: + path (str): A path to a resource on the filesystem. + Returns: + bool: True if the path is a directory, False otherwise. + """ + with self._session() as session: + return session.collections.exists(path) diff --git a/tests/test_iRODSFS.py b/tests/test_iRODSFS.py index 1d8bb3d..9322c94 100644 --- a/tests/test_iRODSFS.py +++ b/tests/test_iRODSFS.py @@ -2,10 +2,6 @@ from unittest.mock import patch from tests.builder_iRODSFS import iRODSFSBuilder -def test_default(): - sut = iRODSFS() - assert sut is not None - @patch("fs_irods.iRODSFS.iRODSSession") def test_enters_session(mocksession):