mirror of
https://github.com/github/awesome-copilot.git
synced 2026-02-22 11:25:13 +00:00
fix: Revised error messages and filename sanitization processing
- Improved error messages within the `find_library_file` function to include directory names - Organized comments within the `sanitize_filename` function - Improved error messages within the `split_library` function to include directory names
This commit is contained in:
@@ -26,121 +26,121 @@ from pathlib import Path
|
|||||||
def sanitize_filename(name: str) -> str:
|
def sanitize_filename(name: str) -> str:
|
||||||
"""
|
"""
|
||||||
Sanitize icon name to create a valid filename.
|
Sanitize icon name to create a valid filename.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
name: Original icon name
|
name: Original icon name
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
Sanitized filename safe for all platforms
|
Sanitized filename safe for all platforms
|
||||||
"""
|
"""
|
||||||
# Replace spaces with hyphens
|
# Replace spaces with hyphens
|
||||||
filename = name.replace(' ', '-')
|
filename = name.replace(' ', '-')
|
||||||
|
|
||||||
# Remove or replace special characters
|
# Remove or replace special characters
|
||||||
filename = re.sub(r'[^\w\-.]', '', filename)
|
filename = re.sub(r'[^\w\-.]', '', filename)
|
||||||
|
|
||||||
# Remove multiple consecutive hyphens
|
# Remove multiple consecutive hyphens
|
||||||
filename = re.sub(r'-+', '-', filename)
|
filename = re.sub(r'-+', '-', filename)
|
||||||
|
|
||||||
# Remove leading/trailing hyphens
|
# Remove leading/trailing hyphens
|
||||||
filename = filename.strip('-')
|
filename = filename.strip('-')
|
||||||
|
|
||||||
return filename
|
return filename
|
||||||
|
|
||||||
|
|
||||||
def find_library_file(directory: Path) -> Path:
|
def find_library_file(directory: Path) -> Path:
|
||||||
"""
|
"""
|
||||||
Find the .excalidrawlib file in the given directory.
|
Find the .excalidrawlib file in the given directory.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
directory: Directory to search
|
directory: Directory to search
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
Path to the library file
|
Path to the library file
|
||||||
|
|
||||||
Raises:
|
Raises:
|
||||||
SystemExit: If no library file or multiple library files found
|
SystemExit: If no library file or multiple library files found
|
||||||
"""
|
"""
|
||||||
library_files = list(directory.glob('*.excalidrawlib'))
|
library_files = list(directory.glob('*.excalidrawlib'))
|
||||||
|
|
||||||
if len(library_files) == 0:
|
if len(library_files) == 0:
|
||||||
print(f"Error: No .excalidrawlib file found in {directory}")
|
print(f"Error: No .excalidrawlib file found in {directory}")
|
||||||
print(f"Please place a .excalidrawlib file in this directory first.")
|
print(f"Please place a .excalidrawlib file in {directory} first.")
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
if len(library_files) > 1:
|
if len(library_files) > 1:
|
||||||
print(f"Error: Multiple .excalidrawlib files found in {directory}")
|
print(f"Error: Multiple .excalidrawlib files found in {directory}")
|
||||||
print(f"Please keep only one library file in the directory.")
|
print(f"Please keep only one library file in {directory}.")
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
return library_files[0]
|
return library_files[0]
|
||||||
|
|
||||||
|
|
||||||
def split_library(library_dir: str) -> None:
|
def split_library(library_dir: str) -> None:
|
||||||
"""
|
"""
|
||||||
Split an Excalidraw library file into individual icon files.
|
Split an Excalidraw library file into individual icon files.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
library_dir: Path to the directory containing the .excalidrawlib file
|
library_dir: Path to the directory containing the .excalidrawlib file
|
||||||
"""
|
"""
|
||||||
library_dir = Path(library_dir)
|
library_dir = Path(library_dir)
|
||||||
|
|
||||||
if not library_dir.exists():
|
if not library_dir.exists():
|
||||||
print(f"Error: Directory not found: {library_dir}")
|
print(f"Error: Directory not found: {library_dir}")
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
if not library_dir.is_dir():
|
if not library_dir.is_dir():
|
||||||
print(f"Error: Path is not a directory: {library_dir}")
|
print(f"Error: Path is not a directory: {library_dir}")
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
# Find the library file
|
# Find the library file
|
||||||
library_path = find_library_file(library_dir)
|
library_path = find_library_file(library_dir)
|
||||||
print(f"Found library: {library_path.name}")
|
print(f"Found library: {library_path.name}")
|
||||||
|
|
||||||
# Load library file
|
# Load library file
|
||||||
print(f"Loading library data...")
|
print(f"Loading library data...")
|
||||||
with open(library_path, 'r', encoding='utf-8') as f:
|
with open(library_path, 'r', encoding='utf-8') as f:
|
||||||
library_data = json.load(f)
|
library_data = json.load(f)
|
||||||
|
|
||||||
# Validate library structure
|
# Validate library structure
|
||||||
if 'libraryItems' not in library_data:
|
if 'libraryItems' not in library_data:
|
||||||
print("Error: Invalid library file format (missing 'libraryItems')")
|
print("Error: Invalid library file format (missing 'libraryItems')")
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
# Create icons directory
|
# Create icons directory
|
||||||
icons_dir = library_dir / 'icons'
|
icons_dir = library_dir / 'icons'
|
||||||
icons_dir.mkdir(exist_ok=True)
|
icons_dir.mkdir(exist_ok=True)
|
||||||
print(f"Output directory: {library_dir}")
|
print(f"Output directory: {library_dir}")
|
||||||
|
|
||||||
# Process each library item (icon)
|
# Process each library item (icon)
|
||||||
library_items = library_data['libraryItems']
|
library_items = library_data['libraryItems']
|
||||||
icon_list = []
|
icon_list = []
|
||||||
|
|
||||||
print(f"Processing {len(library_items)} icons...")
|
print(f"Processing {len(library_items)} icons...")
|
||||||
|
|
||||||
for item in library_items:
|
for item in library_items:
|
||||||
# Get icon name
|
# Get icon name
|
||||||
icon_name = item.get('name', 'Unnamed')
|
icon_name = item.get('name', 'Unnamed')
|
||||||
|
|
||||||
# Create sanitized filename
|
# Create sanitized filename
|
||||||
filename = sanitize_filename(icon_name) + '.json'
|
filename = sanitize_filename(icon_name) + '.json'
|
||||||
|
|
||||||
# Save icon data
|
# Save icon data
|
||||||
icon_path = icons_dir / filename
|
icon_path = icons_dir / filename
|
||||||
with open(icon_path, 'w', encoding='utf-8') as f:
|
with open(icon_path, 'w', encoding='utf-8') as f:
|
||||||
json.dump(item, f, ensure_ascii=False, indent=2)
|
json.dump(item, f, ensure_ascii=False, indent=2)
|
||||||
|
|
||||||
# Add to reference list
|
# Add to reference list
|
||||||
icon_list.append({
|
icon_list.append({
|
||||||
'name': icon_name,
|
'name': icon_name,
|
||||||
'filename': filename
|
'filename': filename
|
||||||
})
|
})
|
||||||
|
|
||||||
print(f" ✓ {icon_name} → {filename}")
|
print(f" ✓ {icon_name} → {filename}")
|
||||||
|
|
||||||
# Sort icon list by name
|
# Sort icon list by name
|
||||||
icon_list.sort(key=lambda x: x['name'])
|
icon_list.sort(key=lambda x: x['name'])
|
||||||
|
|
||||||
# Generate reference.md
|
# Generate reference.md
|
||||||
library_name = library_path.stem
|
library_name = library_path.stem
|
||||||
reference_path = library_dir / 'reference.md'
|
reference_path = library_dir / 'reference.md'
|
||||||
@@ -150,14 +150,14 @@ def split_library(library_dir: str) -> None:
|
|||||||
f.write("## Available Icons\n\n")
|
f.write("## Available Icons\n\n")
|
||||||
f.write("| Icon Name | Filename |\n")
|
f.write("| Icon Name | Filename |\n")
|
||||||
f.write("|-----------|----------|\n")
|
f.write("|-----------|----------|\n")
|
||||||
|
|
||||||
for icon in icon_list:
|
for icon in icon_list:
|
||||||
f.write(f"| {icon['name']} | `icons/{icon['filename']}` |\n")
|
f.write(f"| {icon['name']} | `icons/{icon['filename']}` |\n")
|
||||||
|
|
||||||
f.write("\n## Usage\n\n")
|
f.write("\n## Usage\n\n")
|
||||||
f.write("Each icon JSON file contains the complete `elements` array needed to render that icon in Excalidraw.\n")
|
f.write("Each icon JSON file contains the complete `elements` array needed to render that icon in Excalidraw.\n")
|
||||||
f.write("You can copy the elements from these files into your Excalidraw diagrams.\n")
|
f.write("You can copy the elements from these files into your Excalidraw diagrams.\n")
|
||||||
|
|
||||||
print(f"\n✅ Successfully split library into {len(icon_list)} icons")
|
print(f"\n✅ Successfully split library into {len(icon_list)} icons")
|
||||||
print(f"📄 Reference file created: {reference_path}")
|
print(f"📄 Reference file created: {reference_path}")
|
||||||
print(f"📁 Icons directory: {icons_dir}")
|
print(f"📁 Icons directory: {icons_dir}")
|
||||||
@@ -174,7 +174,7 @@ def main():
|
|||||||
print(" python split-excalidraw-library.py skills/excalidraw-diagram-generator/libraries/aws-architecture-icons/")
|
print(" python split-excalidraw-library.py skills/excalidraw-diagram-generator/libraries/aws-architecture-icons/")
|
||||||
print("\nNote: The directory should contain a .excalidrawlib file.")
|
print("\nNote: The directory should contain a .excalidrawlib file.")
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
library_dir = sys.argv[1]
|
library_dir = sys.argv[1]
|
||||||
split_library(library_dir)
|
split_library(library_dir)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user