Skip to content

Commit

Permalink
👌 IMPROVE: Specify abstract methods on Transport (#5242)
Browse files Browse the repository at this point in the history
Replace `raise NotImplementedError` with proper `@abc.abstractmethod` decorators
  • Loading branch information
chrisjsewell authored Dec 6, 2021
1 parent eb73c7d commit e3651e0
Showing 1 changed file with 31 additions and 32 deletions.
63 changes: 31 additions & 32 deletions aiida/transports/transport.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,17 +140,17 @@ def __exit__(self, type_, value, traceback):
def is_open(self):
return self._is_open

@abc.abstractmethod
def open(self):
"""
Opens a local transport channel
"""
raise NotImplementedError

@abc.abstractmethod
def close(self):
"""
Closes the local transport channel
"""
raise NotImplementedError

def __repr__(self):
return f'<{self.__class__.__name__}: {str(self)}>'
Expand Down Expand Up @@ -257,6 +257,7 @@ def get_safe_open_interval(self):
"""
return self._safe_open_interval

@abc.abstractmethod
def chdir(self, path):
"""
Change directory to 'path'
Expand All @@ -266,17 +267,16 @@ def chdir(self, path):
:rtype: str
"""

raise NotImplementedError

@abc.abstractmethod
def chmod(self, path, mode):
"""
Change permissions of a path.
:param str path: path to file
:param int mode: new permissions
"""
raise NotImplementedError

@abc.abstractmethod
def chown(self, path, uid, gid):
"""
Change the owner (uid) and group (gid) of a file.
Expand All @@ -288,8 +288,8 @@ def chown(self, path, uid, gid):
:param int uid: new owner's uid
:param int gid: new group id
"""
raise NotImplementedError

@abc.abstractmethod
def copy(self, remotesource, remotedestination, dereference=False, recursive=True):
"""
Copy a file or a directory from remote source to remote destination
Expand All @@ -304,8 +304,8 @@ def copy(self, remotesource, remotedestination, dereference=False, recursive=Tru
:raises: IOError, if one of src or dst does not exist
"""
raise NotImplementedError

@abc.abstractmethod
def copyfile(self, remotesource, remotedestination, dereference=False):
"""
Copy a file from remote source to remote destination
Expand All @@ -318,8 +318,8 @@ def copyfile(self, remotesource, remotedestination, dereference=False):
:raises IOError: if one of src or dst does not exist
"""
raise NotImplementedError

@abc.abstractmethod
def copytree(self, remotesource, remotedestination, dereference=False):
"""
Copy a folder from remote source to remote destination
Expand All @@ -332,7 +332,6 @@ def copytree(self, remotesource, remotedestination, dereference=False):
:raise IOError: if one of src or dst does not exist
"""
raise NotImplementedError

def copy_from_remote_to_remote(self, transportdestination, remotesource, remotedestination, **kwargs):
"""
Expand Down Expand Up @@ -384,6 +383,7 @@ def copy_from_remote_to_remote(self, transportdestination, remotesource, remoted
for filename in sandbox.get_content_list():
transportdestination.put(os.path.join(sandbox.abspath, filename), remotedestination, **kwargs_put)

@abc.abstractmethod
def _exec_command_internal(self, command, **kwargs):
"""
Execute the command on the shell, similarly to os.system.
Expand All @@ -398,8 +398,8 @@ def _exec_command_internal(self, command, **kwargs):
:return: stdin, stdout, stderr and the session, when this exists \
(can be None).
"""
raise NotImplementedError

@abc.abstractmethod
def exec_command_wait_bytes(self, command, stdin=None, **kwargs):
"""
Execute the command on the shell, waits for it to finish,
Expand All @@ -413,7 +413,6 @@ def exec_command_wait_bytes(self, command, stdin=None, **kwargs):
:param stdin: (optional,default=None) can be a string or a file-like object.
:return: a tuple: the retcode (int), stdout (bytes) and stderr (bytes).
"""
raise NotImplementedError

def exec_command_wait(self, command, stdin=None, encoding='utf-8', **kwargs):
"""
Expand All @@ -439,6 +438,7 @@ def exec_command_wait(self, command, stdin=None, encoding='utf-8', **kwargs):
# Return the decoded strings
return (retval, stdout_bytes.decode(encoding), stderr_bytes.decode(encoding))

@abc.abstractmethod
def get(self, remotepath, localpath, *args, **kwargs):
"""
Retrieve a file or folder from remote source to local destination
Expand All @@ -447,8 +447,8 @@ def get(self, remotepath, localpath, *args, **kwargs):
:param remotepath: (str) remote_folder_path
:param localpath: (str) local_folder_path
"""
raise NotImplementedError

@abc.abstractmethod
def getfile(self, remotepath, localpath, *args, **kwargs):
"""
Retrieve a file from remote source to local destination
Expand All @@ -457,8 +457,8 @@ def getfile(self, remotepath, localpath, *args, **kwargs):
:param str remotepath: remote_folder_path
:param str localpath: local_folder_path
"""
raise NotImplementedError

@abc.abstractmethod
def gettree(self, remotepath, localpath, *args, **kwargs):
"""
Retrieve a folder recursively from remote source to local destination
Expand All @@ -467,16 +467,16 @@ def gettree(self, remotepath, localpath, *args, **kwargs):
:param str remotepath: remote_folder_path
:param str localpath: local_folder_path
"""
raise NotImplementedError

@abc.abstractmethod
def getcwd(self):
"""
Get working directory
:return: a string identifying the current working directory
"""
raise NotImplementedError

@abc.abstractmethod
def get_attribute(self, path):
"""
Return an object FixedFieldsAttributeDict for file in a given path,
Expand All @@ -498,7 +498,6 @@ def get_attribute(self, path):
:param str path: path to file
:return: object FixedFieldsAttributeDict
"""
raise NotImplementedError

def get_mode(self, path):
"""
Expand All @@ -511,24 +510,25 @@ def get_mode(self, path):

return stat.S_IMODE(self.get_attribute(path).st_mode)

@abc.abstractmethod
def isdir(self, path):
"""
True if path is an existing directory.
:param str path: path to directory
:return: boolean
"""
raise NotImplementedError

@abc.abstractmethod
def isfile(self, path):
"""
Return True if path is an existing file.
:param str path: path to file
:return: boolean
"""
raise NotImplementedError

@abc.abstractmethod
def listdir(self, path='.', pattern=None):
"""
Return a list of the names of the entries in the given path.
Expand All @@ -540,7 +540,6 @@ def listdir(self, path='.', pattern=None):
filters in Unix style. Unix only.
:return: a list of strings
"""
raise NotImplementedError

def listdir_withattributes(self, path='.', pattern=None): # pylint: disable=unused-argument
"""
Expand Down Expand Up @@ -573,6 +572,7 @@ def listdir_withattributes(self, path='.', pattern=None): # pylint: disable=unu
retlist.append({'name': file_name, 'attributes': attributes, 'isdir': self.isdir(filepath)})
return retlist

@abc.abstractmethod
def makedirs(self, path, ignore_existing=False):
"""
Super-mkdir; create a leaf directory and all intermediate ones.
Expand All @@ -585,8 +585,8 @@ def makedirs(self, path, ignore_existing=False):
:raises: OSError, if directory at path already exists
"""
raise NotImplementedError

@abc.abstractmethod
def mkdir(self, path, ignore_existing=False):
"""
Create a folder (directory) named path.
Expand All @@ -597,8 +597,8 @@ def mkdir(self, path, ignore_existing=False):
:raises: OSError, if directory at path already exists
"""
raise NotImplementedError

@abc.abstractmethod
def normalize(self, path='.'):
"""
Return the normalized path (on the server) of a given path.
Expand All @@ -609,8 +609,8 @@ def normalize(self, path='.'):
:raise IOError: if the path can't be resolved on the server
"""
raise NotImplementedError

@abc.abstractmethod
def put(self, localpath, remotepath, *args, **kwargs):
"""
Put a file or a directory from local src to remote dst.
Expand All @@ -620,8 +620,8 @@ def put(self, localpath, remotepath, *args, **kwargs):
:param str localpath: absolute path to local source
:param str remotepath: path to remote destination
"""
raise NotImplementedError

@abc.abstractmethod
def putfile(self, localpath, remotepath, *args, **kwargs):
"""
Put a file from local src to remote dst.
Expand All @@ -630,8 +630,8 @@ def putfile(self, localpath, remotepath, *args, **kwargs):
:param str localpath: absolute path to local file
:param str remotepath: path to remote file
"""
raise NotImplementedError

@abc.abstractmethod
def puttree(self, localpath, remotepath, *args, **kwargs):
"""
Put a folder recursively from local src to remote dst.
Expand All @@ -640,8 +640,8 @@ def puttree(self, localpath, remotepath, *args, **kwargs):
:param str localpath: absolute path to local folder
:param str remotepath: path to remote folder
"""
raise NotImplementedError

@abc.abstractmethod
def remove(self, path):
"""
Remove the file at the given path. This only works on files;
Expand All @@ -651,8 +651,8 @@ def remove(self, path):
:raise IOError: if the path is a directory
"""
raise NotImplementedError

@abc.abstractmethod
def rename(self, oldpath, newpath):
"""
Rename a file or folder from oldpath to newpath.
Expand All @@ -663,25 +663,25 @@ def rename(self, oldpath, newpath):
:raises IOError: if oldpath/newpath is not found
:raises ValueError: if oldpath/newpath is not a valid string
"""
raise NotImplementedError

@abc.abstractmethod
def rmdir(self, path):
"""
Remove the folder named path.
This works only for empty folders. For recursive remove, use rmtree.
:param str path: absolute path to the folder to remove
"""
raise NotImplementedError

@abc.abstractmethod
def rmtree(self, path):
"""
Remove recursively the content at path
:param str path: absolute path to remove
"""
raise NotImplementedError

@abc.abstractmethod
def gotocomputer_command(self, remotedir):
"""
Return a string to be run using os.system in order to connect
Expand All @@ -695,8 +695,8 @@ def gotocomputer_command(self, remotedir):
:param str remotedir: the full path of the remote directory
"""
raise NotImplementedError

@abc.abstractmethod
def symlink(self, remotesource, remotedestination):
"""
Create a symbolic link between the remote source and the remote
Expand All @@ -705,7 +705,6 @@ def symlink(self, remotesource, remotedestination):
:param remotesource: remote source
:param remotedestination: remote destination
"""
raise NotImplementedError

def whoami(self):
"""
Expand All @@ -728,11 +727,11 @@ def whoami(self):
self.logger.error(f"Problem executing whoami. Exit code: {retval}, stdout: '{username}', stderr: '{stderr}'")
raise IOError(f'Error while executing whoami. Exit code: {retval}')

@abc.abstractmethod
def path_exists(self, path):
"""
Returns True if path exists, False otherwise.
"""
raise NotImplementedError

# The following definitions are almost copied and pasted
# from the python module glob.
Expand Down

0 comments on commit e3651e0

Please sign in to comment.