-
Notifications
You must be signed in to change notification settings - Fork 3
/
ProjectFilePathNode.py
78 lines (66 loc) · 2.6 KB
/
ProjectFilePathNode.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
import os
class ProjectFilePathNode:
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"root": ("STRING", {"default": "output"}),
"project_name": ("STRING", {"default": "MyProject"}),
"subfolder": ("STRING", {"default": "images"}),
"filename": ("STRING", {"default": "image"}),
},
"optional": {
"separator": (["auto", "forward_slash", "backslash"], {"default": "auto"}),
}
}
RETURN_TYPES = ("STRING",)
FUNCTION = "generate_file_path"
CATEGORY = "file_management"
def generate_file_path(self, root, project_name, subfolder, filename, separator="auto"):
# Sanitize inputs
root = self.sanitize_path_component(root)
project_name = self.sanitize_path_component(project_name)
subfolder = self.sanitize_path_component(subfolder)
filename = self.sanitize_filename(filename)
# Determine the separator
if separator == "auto":
sep = os.path.sep
elif separator == "forward_slash":
sep = "/"
else:
sep = "\\"
# Construct path
path_components = [root, project_name, subfolder, filename]
full_path = sep.join(filter(bool, path_components))
# Normalize the path
full_path = os.path.normpath(full_path)
return (full_path,)
@staticmethod
def sanitize_path_component(component):
# Remove any characters that are unsafe for file paths
unsafe_chars = ['<', '>', ':', '"', '|', '?', '*']
for char in unsafe_chars:
component = component.replace(char, '')
# Convert spaces to underscores
component = component.replace(' ', '_')
# Remove leading/trailing dots and slashes
return component.strip('./\\')
@staticmethod
def sanitize_filename(filename):
# Remove any characters that are unsafe for filenames
unsafe_chars = ['<', '>', ':', '"', '/', '\\', '|', '?', '*']
for char in unsafe_chars:
filename = filename.replace(char, '')
# Remove any file extension
filename = os.path.splitext(filename)[0]
return filename
@classmethod
def IS_CHANGED(cls, **kwargs):
# This ensures the node updates when any input changes
return float("nan")
NODE_CLASS_MAPPINGS = {
"ProjectFilePathNode": ProjectFilePathNode
}
NODE_DISPLAY_NAME_MAPPINGS = {
"ProjectFilePathNode": "Project File Path Generator"
}