From 3d0d3f84dcf8bcf4285ce7b2c0888ff8b5e89e8f Mon Sep 17 00:00:00 2001 From: Thanh Phan Date: Tue, 31 Aug 2021 12:51:39 +0700 Subject: [PATCH] Also remove old subdirectory as changing its name --- imagepaste/preferences.py | 18 +++++++++++++++++- imagepaste/tree.py | 19 +++++++++++++------ 2 files changed, 30 insertions(+), 7 deletions(-) diff --git a/imagepaste/preferences.py b/imagepaste/preferences.py index a81721c..d28a704 100644 --- a/imagepaste/preferences.py +++ b/imagepaste/preferences.py @@ -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""" @@ -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", diff --git a/imagepaste/tree.py b/imagepaste/tree.py index 778e04a..fdbc3b2 100644 --- a/imagepaste/tree.py +++ b/imagepaste/tree.py @@ -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 ( @@ -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):