Coverage for app/venv/lib/python3.14/site-packages/weblate/utils/icons.py: 77%

20 statements  

« prev     ^ index     » next       coverage.py v7.15.2, created at 2026-10-07 07:15 +0000

1# Copyright © Michal Čihař <michal@weblate.org> 

2# 

3# SPDX-License-Identifier: GPL-3.0-or-later 

4 

5from __future__ import annotations 

6 

7from functools import lru_cache 

8 

9from django.contrib.staticfiles import finders 

10 

11 

12@lru_cache(maxsize=512) 

13def find_static_file(name: str) -> str: 

14 """Return cached static file finder result.""" 

15 filename = finders.find(name) 

16 if not filename: 16 ↛ 17line 16 didn't jump to line 17 because the condition on line 16 was never true

17 error = f"Could not load find static file: {name}" 

18 raise ValueError(error) 

19 return filename 

20 

21 

22@lru_cache(maxsize=1024) 

23def load_icon(name: str, *, auto_prefix: bool = True) -> bytes: 

24 """Load an icon from static files.""" 

25 if not name: 25 ↛ 26line 25 didn't jump to line 26 because the condition on line 25 was never true

26 msg = "Empty icon name" 

27 raise ValueError(msg) 

28 

29 if auto_prefix and "/" not in name: 

30 name = f"icons/{name}" 

31 

32 filename = find_static_file(name) 

33 

34 with open(filename, "rb") as handle: 

35 return handle.read()