Skip to content

Commit

Permalink
Also remove old subdirectory as changing its name
Browse files Browse the repository at this point in the history
  • Loading branch information
thanhph111 committed Aug 31, 2021
1 parent ecc1176 commit 3d0d3f8
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 7 deletions.
18 changes: 17 additions & 1 deletion imagepaste/preferences.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,20 @@
from .tree import remove_empty_subdirectory


def get_subdirectory_name(self):
"""Get the subdirectory name."""
return self.get("subdirectory_name", ADDON_NAME)


def set_subdirectory_name(self, value):
"""Set the subdirectory name."""
from .tree import remove_empty_subdirectory

# Remove the old subdirectory before setting the new one
remove_empty_subdirectory(self.subdirectory_name)
self["subdirectory_name"] = value


class IMAGEPASTE_AddonPreferences(bpy.types.AddonPreferences):
"""Add-on preferences for ImagePaste"""

Expand Down Expand Up @@ -43,7 +57,9 @@ class IMAGEPASTE_AddonPreferences(bpy.types.AddonPreferences):
subdirectory_name: bpy.props.StringProperty(
name="Sub directory name",
description="A name for subdirectory",
default="ImagePaste",
default=ADDON_NAME,
get=get_subdirectory_name,
set=set_subdirectory_name,
)
image_filename_pattern: bpy.props.StringProperty(
name="Image filename",
Expand Down
19 changes: 13 additions & 6 deletions imagepaste/tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,13 @@ def populate_filename(filename_pattern: str) -> str:
return filename_pattern


def remove_empty_subdirectory() -> None:
"""Remove empty subdirectories."""
def remove_empty_subdirectory(subdirectory_name: str = None) -> None:
"""Remove empty subdirectories.
Args:
subdirectory_name (str, optional): a string representing a subdirectory name.
Defaults to None.
"""
from os import listdir
from os import rmdir
from os.path import (
Expand All @@ -210,11 +215,13 @@ def remove_empty_subdirectory() -> None:
from .report import Report
from .metadata import get_addon_preferences

preferences = get_addon_preferences()
if not preferences.subdirectory_name:
return
if not subdirectory_name:
preferences = get_addon_preferences()
if not preferences.subdirectory_name:
return
subdirectory_name = preferences.subdirectory_name
directory_path = dirname(bpy.data.filepath)
subdirectory_path = join(directory_path, preferences.subdirectory_name)
subdirectory_path = join(directory_path, subdirectory_name)
if not isdir(subdirectory_path):
return
if listdir(subdirectory_path):
Expand Down

0 comments on commit 3d0d3f8

Please sign in to comment.