forked from b-init/ImagePaste
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper.py
164 lines (134 loc) · 4.74 KB
/
helper.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import bpy
ADDON_NAME = __package__.split(".")[0]
def get_addon_preferences() -> bpy.types.AddonPreferences:
"""Get the addon preferences.
Returns:
bpy.types.AddonPreferences: The addon preferences.
"""
return bpy.context.preferences.addons[ADDON_NAME].preferences
def get_save_directory() -> str:
"""Get the path to the directory where the images are saved.
Returns:
str: The path to the directory where the images are saved.
"""
from os import makedirs
from os.path import exists
from os.path import dirname
from os.path import join
preferences = get_addon_preferences()
if not bpy.data.filepath:
if preferences.is_use_another_directory and preferences.another_directory:
return preferences.another_directory
return bpy.app.tempdir
if (
preferences.is_use_another_directory
and preferences.is_force_use_another_directory
and preferences.another_directory
):
return preferences.another_directory
directory_path = dirname(bpy.data.filepath)
if not preferences.is_use_subdirectory or not preferences.subdirectory_name:
return directory_path
subdirectory_path = join(directory_path, preferences.subdirectory_name)
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_general_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.
"""
if not filename:
return False
if len(filename) > 255:
return False
if filename in (".", ".."):
return False
return True
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.
"""
# Check if the filename does not end with a dot or a space
if filename[-1] in (".", " "):
return False
# Check if the filename is a reserved name
windows_reserved_filenames = (
"CON",
"PRN",
"AUX",
"NUL",
"COM1",
"COM2",
"COM3",
"COM4",
"COM5",
"COM6",
"COM7",
"COM8",
"COM9",
"LPT1",
"LPT2",
"LPT3",
"LPT4",
"LPT5",
"LPT6",
"LPT7",
"LPT8",
"LPT9",
)
if filename.upper() in windows_reserved_filenames:
return False
# Check if the filename contains any invalid characters
windows_invalid_characters = ("/", "\\", ":", "*", "?", '"', "<", ">", "|")
for character in windows_invalid_characters:
if character in filename:
return False
# Check if the filename contains any unprintable characters
for index in range(31):
if chr(index) in filename:
return False
return True
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.
"""
# The "\" character is valid in Linux but Blender does not support it
invalid_characters = ["/", "\\"]
for invalid_character in invalid_characters:
if invalid_character in filename:
return False
return True
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.
"""
return is_linux_valid_filename(filename)
import sys
if not is_general_valid_filename(filename):
return False
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