Coverage for downtify/library_cache_keys.py: 80%

20 statements  

« prev     ^ index     » next       coverage.py v7.17.4, created at 2026-11-21 22:18 +0000

1"""Stable cache keys for library files (survive path / folder changes).""" 

2 

3from __future__ import annotations 

4 

5import hashlib 

6from pathlib import Path 

7 

8 

9def file_content_key(full_path: Path) -> str | None: 

10 """Hash of basename + file size — same identity after a folder move.""" 

11 

12 if not full_path.is_file(): 

13 return None 

14 try: 

15 size = int(full_path.stat().st_size) 

16 except OSError: 

17 return None 

18 base = full_path.name.casefold() 

19 if not base: 

20 return None 

21 payload = f'{base}:{size}'.encode() 

22 return hashlib.sha256(payload).hexdigest() 

23 

24 

25def file_content_key_from_name_and_size( 

26 name: str, file_size: int 

27) -> str | None: 

28 """Rebuild the content key when only basename and size are known.""" 

29 

30 base = Path(str(name or '').replace('\\', '/')).name.casefold() 

31 if not base or file_size < 0: 

32 return None 

33 return hashlib.sha256(f'{base}:{int(file_size)}'.encode()).hexdigest()