Coverage for website/builder/assets.py: 98%

48 statements  

« prev     ^ index     » next       coverage.py v7.10.6, created at 2025-09-08 06:03 +0000

1""" 

2Asset Management - Static File Handling and Asset Operations. 

3 

4This module handles asset copying, static file management, 

5and asset-related operations for the website builder. 

6""" 

7 

8import shutil 

9from pathlib import Path 

10 

11 

12class AssetManager: 

13 """Handles asset management and static file operations.""" 

14 

15 def __init__(self, output_dir: str = "site"): 

16 """Initialize asset manager.""" 

17 self.output_dir = Path(output_dir) 

18 

19 def copy_assets(self) -> None: 

20 """Copy all website assets to output directory.""" 

21 assets_source = Path("website/assets") 

22 assets_dest = self.output_dir / "assets" 

23 

24 if assets_source.exists(): 

25 if assets_dest.exists(): 

26 shutil.rmtree(assets_dest) 

27 

28 # Define ignore patterns for files we don't want to copy 

29 def ignore_patterns(dir, files): 

30 ignored = [] 

31 for file in files: 

32 # Ignore Python files and other development files 

33 if file.endswith((".py", ".pyc", "__pycache__")): 

34 ignored.append(file) 

35 return ignored 

36 

37 shutil.copytree(assets_source, assets_dest, ignore=ignore_patterns) 

38 print(f"📁 Assets copied to {assets_dest}") 

39 else: 

40 print(f"⚠️ Assets directory not found: {assets_source}") 

41 

42 def copy_static_file(self, source_path: str, dest_path: str) -> None: 

43 """Copy a single static file.""" 

44 source = Path(source_path) 

45 dest = self.output_dir / dest_path 

46 

47 if source.exists(): 

48 dest.parent.mkdir(parents=True, exist_ok=True) 

49 shutil.copy2(source, dest) 

50 print(f"📄 Copied {source} -> {dest}") 

51 else: 

52 print(f"⚠️ Static file not found: {source}") 

53 

54 def ensure_output_directory(self) -> None: 

55 """Ensure output directory exists.""" 

56 self.output_dir.mkdir(parents=True, exist_ok=True) 

57 

58 def copy_static_files(self, static_files: list[str]) -> None: 

59 """Copy multiple static files.""" 

60 for file_path in static_files: 

61 source_path = Path(file_path) 

62 

63 if ":" in file_path: 

64 source, dest = file_path.split(":", 1) 

65 source_path = Path(source) 

66 dest_path = self.output_dir / dest 

67 else: 

68 # Copy to same relative path 

69 dest_path = self.output_dir / source_path.name 

70 

71 # Handle directories and files differently 

72 if source_path.exists(): 

73 if source_path.is_dir(): 

74 # Copy directory 

75 if dest_path.exists(): 

76 shutil.rmtree(dest_path) 

77 shutil.copytree(source_path, dest_path) 

78 print(f"📁 Directory copied: {source_path} -> {dest_path}") 

79 else: 

80 # Copy file 

81 dest_path.parent.mkdir(parents=True, exist_ok=True) 

82 shutil.copy2(source_path, dest_path) 

83 print(f"📄 File copied: {source_path} -> {dest_path}") 

84 else: 

85 print(f"⚠️ Static file/directory not found: {source_path}")