Skip to content

Commit

Permalink
style: Improve docstrings
Browse files Browse the repository at this point in the history
Signed-off-by: loonghao <[email protected]>
  • Loading branch information
loonghao committed May 16, 2024
1 parent 453059c commit 02b1944
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 11 deletions.
58 changes: 49 additions & 9 deletions maya_umbrella/filesystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,32 @@


def this_root():
"""Return the absolute path of the current file's directory."""
"""Return the absolute path of the current file's directory.
Returns:
str: The absolute path of the current file's directory.
"""
return os.path.abspath(os.path.dirname(__file__))


def safe_remove_file(file_path):
"""Remove the file at the given path without raising an error if the file does not exist."""
"""Remove the file at the given path without raising an error if the file does not exist.
Args:
file_path (str): Path to the file to remove.
"""
try:
os.remove(file_path)
except (OSError, IOError): # noqa: UP024
pass


def safe_rmtree(path):
"""Remove the directory at the given path without raising an error if the directory does not exist."""
"""Remove the directory at the given path without raising an error if the directory does not exist.
Args:
path (str): Path to the directory to remove.
"""
try:
shutil.rmtree(path)
except (OSError, IOError): # noqa: UP024
Expand All @@ -52,7 +64,14 @@ def read_file(path):


def read_json(path):
"""Read the content of the file at the given path."""
"""Read the content of a JSON file at the given path.
Args:
path (str): Path to the JSON file.
Returns:
dict: The content of the JSON file as a dictionary, or an empty dictionary if the file could not be read.
"""
options = {"encoding": "utf-8"} if six.PY3 else {}
with open(path, **options) as file_:
try:
Expand All @@ -63,18 +82,28 @@ def read_json(path):


def write_file(path, content):
"""Write the given content to the file at the given path."""
"""Write the given content to the file at the given path.
Args:
path (str): Path to the file to write.
content (str): Content to write to the file.
"""
root = os.path.dirname(path)
if not os.path.exists(root):
os.makedirs(root)
with atomic_write(path, mode="wb", overwrite=True) as file_:
file_.write(six.ensure_binary(content))


def load_hook(hook_file):
"""Load the Python module from the given hook file.
Args:
hook_file (str): Path to the Python file to load.
def load_hook(hook_file):
"""Load the Python module from the given hook file."""
Returns:
module: The loaded Python module.
"""
hook_name = os.path.basename(hook_file).split(".py")[0]
if hasattr(importlib, "machinery"):
# Python 3
Expand All @@ -95,7 +124,11 @@ def load_hook(hook_file):


def get_hooks():
"""Return a list of paths to all hook files in the 'hooks' directory."""
"""Return a list of paths to all hook files in the 'hooks' directory.
Returns:
list: A list of paths to all hook files in the 'hooks' directory.
"""
pattern = os.path.join(this_root(), "hooks", "*.py")
return [hook for hook in glob.glob(pattern) if "__init__" not in hook]

Expand Down Expand Up @@ -237,7 +270,14 @@ def get_backup_path(path, root_path=None):


def get_maya_install_root(maya_version):
"""Get the Maya install root path."""
"""Get the Maya install root path for the specified version.
Args:
maya_version (str): The version of Maya to find the install root for.
Returns:
str: The Maya install root path, or None if not found.
"""
logger = logging.getLogger(__name__)
maya_location = os.getenv("MAYA_LOCATION")
try:
Expand Down
2 changes: 0 additions & 2 deletions tests/test_filesystem.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# Import third-party modules

# Import third-party modules
import pytest

Expand Down

0 comments on commit 02b1944

Please sign in to comment.