Skip to content

Commit

Permalink
Add a preferences option to custom image filenames
Browse files Browse the repository at this point in the history
Also add required mock functions supporting the new option.
  • Loading branch information
thanhph111 committed Aug 12, 2021
1 parent bb5c1cf commit dc951f7
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 0 deletions.
18 changes: 18 additions & 0 deletions imagepaste/clipboard/clipboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
54 changes: 54 additions & 0 deletions imagepaste/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
27 changes: 27 additions & 0 deletions imagepaste/preferences.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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

Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit dc951f7

Please sign in to comment.