diff --git a/__init__.py b/__init__.py index b8866d4..9d18654 100644 --- a/__init__.py +++ b/__init__.py @@ -21,15 +21,18 @@ from .classes.DRMN_TXTFileSaver import N_CLASS_MAPPINGS as TXTFileSaverMappings, N_DISPLAY_NAME_MAPPINGS as TXTFileSaverNameMappings from .classes.DRMN_TagManipulatorByImageNames import N_CLASS_MAPPINGS as TagManipulatorByImageNamesMappings, N_DISPLAY_NAME_MAPPINGS as TagManipulatorByImageNamesNameMappings from .classes.DRMN_CaptionVisualizer import N_CLASS_MAPPINGS as CaptionVisualizerMappings, N_DISPLAY_NAME_MAPPINGS as CaptionVisualizerNameMappings +from .classes.DRMN_SearchAndReplace import N_CLASS_MAPPINGS as SearchAndReplaceMappings, N_DISPLAY_NAME_MAPPINGS as SearchAndReplaceNameMappings NODE_CLASS_MAPPINGS = {} NODE_CLASS_MAPPINGS.update(TXTFileSaverMappings) NODE_CLASS_MAPPINGS.update(TagManipulatorByImageNamesMappings) NODE_CLASS_MAPPINGS.update(CaptionVisualizerMappings) +NODE_CLASS_MAPPINGS.update(SearchAndReplaceMappings) NODE_DISPLAY_NAME_MAPPINGS = {} NODE_DISPLAY_NAME_MAPPINGS.update(TXTFileSaverNameMappings) NODE_DISPLAY_NAME_MAPPINGS.update(TagManipulatorByImageNamesNameMappings) NODE_DISPLAY_NAME_MAPPINGS.update(CaptionVisualizerNameMappings) +NODE_DISPLAY_NAME_MAPPINGS.update(SearchAndReplaceNameMappings) WEB_DIRECTORY = "./web" \ No newline at end of file diff --git a/classes/DRMN_SearchAndReplace.py b/classes/DRMN_SearchAndReplace.py new file mode 100644 index 0000000..a9ef33f --- /dev/null +++ b/classes/DRMN_SearchAndReplace.py @@ -0,0 +1,78 @@ +import os + + +def read_text_file(file_path): + try: + with open(file_path, 'r') as file: + text = file.read() + return text + except FileNotFoundError: + print(f"Error: File '{file_path}' not found.") + return None + except Exception as e: + print(f"Error: {e}") + return None + + +def save_file(file_path, content): + with open(file_path, 'w') as file: + file.write(content) + print(f"File saved successfully at {file_path}") + + +class DRMN_SearchAndReplace: + + def __init__(self): + pass + + @classmethod + def INPUT_TYPES(s): + return { + "required": { + "TextFilePathList": ("STRING", {"forceInput": True}), + "SearchFor": ("STRING", {"multiline": True, "default": "concept"}), + "ReplaceWith": ("STRING", {"multiline": True, "default": "concept"}) + }, + } + + INPUT_IS_LIST = True + RETURN_TYPES = () + RETURN_NAMES = () + FUNCTION = "SaR" + OUTPUT_NODE = True + CATEGORY = "JDCN Dataset Tools" + + def SaR(self, TextFilePathList, SearchFor, ReplaceWith): + + try: + + directory_path = os.path.dirname(TextFilePathList[0]) + backup_path = os.path.join(directory_path, "sr_backup") + os.makedirs(backup_path, exist_ok=True) + + for file_path in TextFilePathList: + + base_name = os.path.basename(file_path) + save_path = os.path.join(backup_path, base_name) + + content = read_text_file(file_path) + save_file(save_path, content) + + content = content.replace(SearchFor[0], ReplaceWith[0]) + save_file(file_path, content) + + return () + + except Exception as e: + print(f"Error saving: {e}") + + return () + + +N_CLASS_MAPPINGS = { + "DRMN_SearchAndReplace": DRMN_SearchAndReplace, +} + +N_DISPLAY_NAME_MAPPINGS = { + "DRMN_SearchAndReplace": "DRMN_SearchAndReplace", +}