-
Notifications
You must be signed in to change notification settings - Fork 1
/
concat_klinter.py
95 lines (68 loc) · 3.29 KB
/
concat_klinter.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
class concat_klinter:
"""Class for concatenating strings with optional additional string, following klinter guidelines."""
@classmethod
def INPUT_TYPES(cls):
"""Defines the input types for the concatenation operation."""
return {
"required": {
"string_a": ("STRING", {"forceInput": True, "default": "", "multiline": True}),
"string_b": ("STRING", {"forceInput": True, "default": "", "multiline": True}),
# Assuming string_c remains optional as indicated
"string_c": ("STRING", {"default": "", "multiline": True}),
}
}
RETURN_TYPES = ("STRING",)
FUNCTION = "concat"
CATEGORY = "klinter"
def concat(self, string_a, string_b, string_c=""):
"""Concatenates up to three strings, adding a space between non-empty strings."""
# Ensure each non-empty string ends with a space for proper concatenation
string_a_with_space = f"{string_a} " if string_a else ""
string_b_with_space = f"{string_b} " if string_b else ""
string_c_with_space = f"{string_c} " if string_c else ""
# Concatenate the strings and remove any trailing space
concatenated_string = (string_a_with_space + string_b_with_space + string_c_with_space).rstrip()
return (concatenated_string,)
class whitelist:
"""Class for checking if a given string appears in a list of strings and returning a filter word if true."""
@classmethod
def INPUT_TYPES(cls):
"""Defines the input types for the operation."""
return {
"required": {
"in_question": ("STRING", {"forceInput": True, "default": "", "multiline": True}),
"wordlist": ("STRING", {"forceInput": True, "default": "", "multiline": True}),
"safeword": ("STRING", {"forceInput": True, "default": "", "multiline": True}),
}
}
RETURN_TYPES = ("STRING",)
FUNCTION = "whitelist"
CATEGORY = "klinter"
def whitelist(self, in_question, wordlist, safeword):
"""
Checks if 'in_question' exists within 'wordlist'. If it does, returns 'safeword',
otherwise returns 'in_question'.
Args:
in_question (str): The string to search for.
wordlist (str): A string containing a list of words, assumed to be space-separated.
safeword (str): The word to return if 'in_question' is found in 'wordlist'.
Returns:
str: 'safeword' if 'in_question' is found in 'wordlist', else 'in_question'.
"""
# Convert the 'wordlist' string into a list of words
words = wordlist.split()
# Check if 'in_question' is in the list of words
if in_question in words:
return (safeword,)
else:
return (in_question,)
# Mapping of this node class for easy reference or extension
NODE_CLASS_MAPPINGS = {
"whitelist": whitelist,
"concat_klinter": concat_klinter,
}
# Mapping of node identifier to its display name for UI or logging purposes
NODE_DISPLAY_NAME_MAPPINGS = {
"whitelist": "whitelist (Klinter)",
"concat_klinter": "Concat String (Klinter)",
}