From dc951f727ecfbb6a455fbc576e2aa915eac129ca Mon Sep 17 00:00:00 2001 From: Thanh Phan Date: Fri, 6 Aug 2021 23:05:35 +0700 Subject: [PATCH] Add a preferences option to custom image filenames Also add required mock functions supporting the new option. --- imagepaste/clipboard/clipboard.py | 18 +++++++++++ imagepaste/helper.py | 54 +++++++++++++++++++++++++++++++ imagepaste/preferences.py | 27 ++++++++++++++++ 3 files changed, 99 insertions(+) diff --git a/imagepaste/clipboard/clipboard.py b/imagepaste/clipboard/clipboard.py index 33b7046..483c152 100644 --- a/imagepaste/clipboard/clipboard.py +++ b/imagepaste/clipboard/clipboard.py @@ -62,8 +62,26 @@ def get_timestamp_filename() -> str: Returns: str: a string representing the current time in the file name format. """ + + def populate_filename(pattern: str) -> str: + """Populate a filename with a pattern. + + Args: + pattern (str): a string representing a pattern. + + Returns: + str: a string representing a filename. + """ + return pattern + from time import strftime + from ..helper import get_addon_preferences + from ..helper import is_valid_filename + preferences = get_addon_preferences() + filename = populate_filename(preferences.image_filename_pattern) + ".png" + if is_valid_filename(filename): + return filename return f"ImagePaste-{strftime('%y%m%d-%H%M%S')}.png" def __repr__(self) -> str: diff --git a/imagepaste/helper.py b/imagepaste/helper.py index 8108e26..0f467cf 100644 --- a/imagepaste/helper.py +++ b/imagepaste/helper.py @@ -45,3 +45,57 @@ def get_save_directory() -> str: if not exists(subdirectory_path): makedirs(subdirectory_path) return subdirectory_path + + +def is_valid_filename(filename: str) -> bool: + """Check if the filename is valid. + + Args: + filename (str): a string representing the file name. + + Returns: + bool: True if the filename is valid, False otherwise. + """ + + def is_windows_valid_filename(filename: str) -> bool: + """Check if the filename is valid on Windows. + + Args: + filename (str): a string representing a filename. + + Returns: + bool: True if the filename is valid on Windows, False otherwise. + """ + pass + + def is_linux_valid_filename(filename: str) -> bool: + """Check if the filename is valid on Linux. + + Args: + filename (str): a string representing a filename. + + Returns: + bool: True if the filename is valid on Linux, False otherwise. + """ + pass + + def is_darwin_valid_filename(filename: str) -> bool: + """Check if the filename is valid on macOS. + + Args: + filename (str): a string representing a filename. + + Returns: + bool: True if the filename is valid on macOS, False otherwise. + """ + pass + + import sys + + if sys.platform == "win32": + return is_windows_valid_filename(filename) + elif sys.platform == "linux": + return is_linux_valid_filename(filename) + elif sys.platform == "darwin": + return is_darwin_valid_filename(filename) + return False diff --git a/imagepaste/preferences.py b/imagepaste/preferences.py index 3b3e306..492c342 100644 --- a/imagepaste/preferences.py +++ b/imagepaste/preferences.py @@ -44,6 +44,15 @@ class IMAGEPASTE_AddonPreferences(bpy.types.AddonPreferences): description="A name for subdirectory", default="ImagePaste", ) + image_filename_pattern: bpy.props.StringProperty( + name="Image file name", + description=( + "A name for pasted images\n" + "%y: Year, %m: Month, %d: Day\n" + "%H: Hour, %M: Minute, %S: Second" + ), + default="ImagePaste-%y%m%d-%H%M%S", + ) image_type_to_move: bpy.props.EnumProperty( name="Image type to move", description="Which type of image will be moved", @@ -61,6 +70,8 @@ class IMAGEPASTE_AddonPreferences(bpy.types.AddonPreferences): ) def draw(self, _context): + from .helper import is_valid_filename + split_ratio = 0.3 layout = self.layout @@ -115,6 +126,22 @@ def draw(self, _context): column_2_sub.active = self.is_use_subdirectory column_2_sub.prop(self, "subdirectory_name", text="") + # New box + box = layout.box().column() + box.label(text="Custom image file name") + + # New property + prop = box.row(align=True) + split = prop.split(factor=split_ratio) + # First column + column_1 = split.column() + column_1.alignment = "RIGHT" + column_1.label(text="Image file name") + # Second column + column_2 = split.column().row(align=True) + column_2.prop(self, "image_filename_pattern", text="") + column_2.alert = is_valid_filename(self.image_filename_pattern) + # New box box = layout.box().column() box.label(