# -*- coding: utf-8 -*-
"""File system helper."""
import abc
import os
[docs]
class FileSystemHelper(object):
"""File system helper interface."""
[docs]
@abc.abstractmethod
def BasenamePath(self, path):
"""Determines the basename of the path.
Args:
path (str): path.
Returns:
str: basename of the path.
"""
[docs]
@abc.abstractmethod
def CheckFileExistsByPath(self, path):
"""Checks if a specific file exists.
Args:
path (str): path of the file.
Returns:
bool: True if the file exists, False otherwise.
"""
[docs]
@abc.abstractmethod
def DirnamePath(self, path):
"""Determines the directory name of the path.
Args:
path (str): path.
Returns:
str: directory name of the path or None.
"""
[docs]
@abc.abstractmethod
def GetFileSizeByPath(self, path):
"""Retrieves the size of a specific file.
Args:
path (str): path of the file.
Returns:
int: size of the file in bytes.
"""
[docs]
@abc.abstractmethod
def JoinPath(self, path_segments):
"""Joins the path segments into a path.
Args:
path_segments (list[str]): path segments.
Returns:
str: joined path segments prefixed with the path separator.
"""
[docs]
@abc.abstractmethod
def ListDirectory(self, path):
"""Lists the entries in a directory.
Args:
path (str): path of the directory.
Yields:
str: name of a directory entry.
"""
[docs]
@abc.abstractmethod
def OpenFileByPath(self, path):
"""Opens a specific file.
Args:
path (str): path of the file.
Returns:
file: file-like object of the file.
"""
[docs]
@abc.abstractmethod
def SplitPath(self, path):
"""Splits the path into path segments.
Args:
path (str): path.
Returns:
list[str]: path segments without the root path segment, which is
an empty string.
"""
[docs]
class NativeFileSystemHelper(object):
"""Python native system helper."""
[docs]
def BasenamePath(self, path):
"""Determines the basename of the path.
Args:
path (str): path.
Returns:
str: basename of the path.
"""
return os.path.basename(path)
[docs]
def CheckFileExistsByPath(self, path):
"""Checks if a specific file exists.
Args:
path (str): path of the file.
Returns:
bool: True if the file exists, False otherwise.
"""
return os.path.exists(path)
[docs]
def DirnamePath(self, path):
"""Determines the directory name of the path.
Args:
path (str): path.
Returns:
str: directory name of the path or None.
"""
return os.path.dirname(path)
[docs]
def GetFileSizeByPath(self, path):
"""Retrieves the size of a specific file.
Args:
path (str): path of the file.
Returns:
int: size of the file in bytes.
"""
stat_object = os.stat(path)
return stat_object.st_size
[docs]
def JoinPath(self, path_segments):
"""Joins the path segments into a path.
Args:
path_segments (list[str]): path segments.
Returns:
str: joined path segments prefixed with the path separator.
"""
return ''.join([os.path.sep, os.path.sep.join(path_segments)])
[docs]
def ListDirectory(self, path):
"""Lists the entries in a directory.
Args:
path (str): path of the directory.
Yields:
str: name of a directory entry.
"""
yield from os.listdir(path)
[docs]
def OpenFileByPath(self, path):
"""Opens a specific file.
Args:
path (str): path of the file.
Returns:
file: file-like object of the file.
"""
return open(path, 'rb') # pylint: disable=consider-using-with
[docs]
def SplitPath(self, path):
"""Splits the path into path segments.
Args:
path (str): path.
Returns:
list[str]: path segments without the root path segment, which is
an empty string.
"""
# Split the path with the path separator and remove empty path segments.
return list(filter(None, path.split(os.path.sep)))