You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

local_file_system.py 4.1 kB

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. # Copyright 2019 Huawei Technologies Co., Ltd
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. # ============================================================================
  15. """Local File System."""
  16. import io
  17. import os
  18. from mindinsight.datavisual.common import exceptions
  19. from mindinsight.datavisual.utils.tools import to_str
  20. from mindinsight.datavisual.data_access.base_file_system import BaseFileSystem
  21. from mindinsight.datavisual.data_access.base_file_system import StatInfo
  22. from mindinsight.utils.exceptions import PathNotExistError
  23. class LocalFileSystem(BaseFileSystem):
  24. """Local file system."""
  25. def list_dir(self, path):
  26. """
  27. List directories by path.
  28. Args:
  29. path (str): Directory path or file path.
  30. Returns:
  31. list[str], directories.
  32. """
  33. path = to_str(path)
  34. if not self.is_dir(path):
  35. raise exceptions.PathNotDirectoryError("Path is %s." % path)
  36. return os.listdir(path)
  37. def is_dir(self, path):
  38. """
  39. Determine if it is a directory.
  40. Args:
  41. path (str): Directory path or file path.
  42. Returns:
  43. bool, if it is a directory path, return True.
  44. """
  45. return os.path.isdir(to_str(path))
  46. def is_file(self, path):
  47. """
  48. Determine if it is a file.
  49. Args:
  50. path (str): Directory path or file path.
  51. Returns:
  52. bool, if it is a file path, return True.
  53. """
  54. return os.path.isfile(to_str(path))
  55. def exists(self, path):
  56. """
  57. Determine if it exists.
  58. Args:
  59. path (str): Directory path or file path.
  60. Returns:
  61. bool, if it exists, return True.
  62. """
  63. return os.path.exists(to_str(path))
  64. def file_stat(self, file_path):
  65. """
  66. Get file stat information.
  67. Args:
  68. file_path (str): File path.
  69. Returns:
  70. Nametuple, the (size, mtime) of file.
  71. """
  72. try:
  73. file_info = os.stat(to_str(file_path))
  74. except OSError:
  75. raise PathNotExistError("File %s is not exist." % file_path)
  76. return StatInfo(size=file_info.st_size, mtime=file_info.st_mtime)
  77. @staticmethod
  78. def read_access(file_path):
  79. """
  80. Determine if it has read permission.
  81. Args:
  82. file_path (str): File path.
  83. Returns:
  84. bool, if it has read permission, return True.
  85. """
  86. return os.access(to_str(file_path), os.R_OK)
  87. def join(self, path, *paths):
  88. """
  89. Join paths.
  90. Args:
  91. path (str): Directory path.
  92. paths (str): Path or paths.
  93. Returns:
  94. str, the joined path.
  95. """
  96. return os.path.join(path, *paths)
  97. @staticmethod
  98. def read(file_path, binary_mode=False, size=None, offset=None):
  99. """
  100. Read file.
  101. Args:
  102. file_path (str): File path.
  103. binary_mode (bool): If true, mode will be 'rb'. Else, 'r'.
  104. size (int): Size of bytes to read.
  105. offset (int): Offset of file to read.
  106. Returns:
  107. bytes, the content read.
  108. """
  109. mode = "rb" if binary_mode else "r"
  110. encoding = None if binary_mode else "utf8"
  111. with io.open(file_path, mode, encoding=encoding) as file:
  112. if offset is not None:
  113. file.seek(offset)
  114. if size is not None:
  115. return file.read(size)
  116. return file.read()