| |
| |
| |
| |
|
|
| import json |
| import os |
|
|
| |
| def read_json_file(file_path): |
| """ |
| Reads a JSON file's content and returns it. |
| Ensures content matches the expected format. |
| """ |
| if not os.access(file_path, os.R_OK): |
| print(f"Warning: No read permissions for file {file_path}") |
| return None |
|
|
| try: |
| with open(file_path, 'r', encoding='utf-8') as file: |
| content = json.load(file) |
| |
| if not all(['name' in item and 'prompt' in item and 'negative_prompt' in item for item in content]): |
| print(f"Warning: Invalid content in file {file_path}") |
| return None |
| return content |
| except Exception as e: |
| print(f"An error occurred while reading {file_path}: {str(e)}") |
| return None |
|
|
| |
| def load_styles_from_directory(directory): |
| """ |
| Loads styles from all JSON files in the directory. |
| Renames duplicate style names by appending a suffix. |
| """ |
| |
| json_files = [os.path.join(directory, file) for file in os.listdir(directory) if file.endswith('.json') and os.path.isfile(os.path.join(directory, file))] |
| combined_data = [] |
| seen = set() |
|
|
| for json_file in json_files: |
| json_data = read_json_file(json_file) |
| if json_data: |
| for item in json_data: |
| original_style = item['name'] |
| style = original_style |
| suffix = 1 |
| while style in seen: |
| style = f"{original_style}_{suffix}" |
| suffix += 1 |
| item['name'] = style |
| seen.add(style) |
| combined_data.append(item) |
|
|
| unique_style_names = [item['name'] for item in combined_data if isinstance(item, dict) and 'name' in item] |
| |
| return combined_data, unique_style_names |
|
|
| |
|
|