100 lines
No EOL
3.7 KiB
Python
Executable file
100 lines
No EOL
3.7 KiB
Python
Executable file
#!/usr/bin/env python3
|
|
import os
|
|
import shutil
|
|
import argparse
|
|
from pathlib import Path
|
|
|
|
class FontFolderCleaner:
|
|
def __init__(self, font_location):
|
|
self.font_location = Path(font_location)
|
|
self.required_dirs = ['ttf', 'otf', 'web', 'other_files']
|
|
self.web_extensions = {'.woff', '.woff2', '.eot', '.svg'}
|
|
self.font_extensions = {'.ttf', '.otf'}
|
|
|
|
def create_required_directories(self, folder_path):
|
|
"""Create required subdirectories if they don't exist."""
|
|
for dir_name in self.required_dirs:
|
|
dir_path = folder_path / dir_name
|
|
if not dir_path.exists():
|
|
dir_path.mkdir(parents=True)
|
|
print(f"Created directory: {dir_path}")
|
|
|
|
def get_target_directory(self, file_path):
|
|
"""Determine the target directory based on file extension."""
|
|
ext = file_path.suffix.lower()
|
|
if ext == '.ttf':
|
|
return 'ttf'
|
|
elif ext == '.otf':
|
|
return 'otf'
|
|
elif ext in self.web_extensions:
|
|
return 'web'
|
|
else:
|
|
return 'other_files'
|
|
|
|
def move_file_to_correct_directory(self, file_path, folder_path):
|
|
"""Move file to the appropriate subdirectory."""
|
|
if file_path.name == '.DS_Store':
|
|
file_path.unlink()
|
|
print(f"Removed .DS_Store file: {file_path}")
|
|
return
|
|
|
|
target_dir = self.get_target_directory(file_path)
|
|
target_path = folder_path / target_dir / file_path.name
|
|
|
|
if file_path != target_path:
|
|
shutil.move(str(file_path), str(target_path))
|
|
print(f"Moved {file_path.name} to {target_dir}/")
|
|
|
|
def cleanup_folder(self, folder_path):
|
|
"""Clean up a single font folder."""
|
|
folder_path = Path(folder_path)
|
|
|
|
# Skip if not a font folder
|
|
if not folder_path.name.startswith('font-'):
|
|
print(f"Skipping non-font folder: {folder_path}")
|
|
return
|
|
|
|
print(f"\nProcessing folder: {folder_path}")
|
|
|
|
# Create required directories
|
|
self.create_required_directories(folder_path)
|
|
|
|
# Move files to appropriate directories
|
|
for item in folder_path.iterdir():
|
|
if item.is_file():
|
|
self.move_file_to_correct_directory(item, folder_path)
|
|
elif item.is_dir() and item.name not in self.required_dirs:
|
|
# Handle nested directories
|
|
for file in item.rglob('*'):
|
|
if file.is_file():
|
|
self.move_file_to_correct_directory(file, folder_path)
|
|
# Remove empty directories
|
|
if not any(item.iterdir()):
|
|
item.rmdir()
|
|
print(f"Removed empty directory: {item}")
|
|
|
|
def cleanup_all_folders(self):
|
|
"""Clean up all font folders in the font location."""
|
|
for item in self.font_location.iterdir():
|
|
if item.is_dir() and not item.name.startswith('.'):
|
|
self.cleanup_folder(item)
|
|
|
|
def main():
|
|
parser = argparse.ArgumentParser(description='Clean up font folders and organize files.')
|
|
parser.add_argument('--path', type=str, default='../font_files',
|
|
help='Path to the font files directory (default: ../font_files)')
|
|
args = parser.parse_args()
|
|
|
|
# Convert relative path to absolute path
|
|
font_location = Path(__file__).parent / args.path
|
|
font_location = font_location.resolve()
|
|
|
|
if not font_location.exists():
|
|
print(f"Error: Font location does not exist: {font_location}")
|
|
return
|
|
|
|
cleaner = FontFolderCleaner(font_location)
|
|
cleaner.cleanup_all_folders()
|
|
|
|
if __name__ == "__main__":
|
|
main() |