Coverage for rdgai/gui/display.py: 96.30%

27 statements  

« prev     ^ index     » next       coverage.py v7.17.3, created at 2026-11-24 23:53 +0000

1""" 

2Display details for the GUI's header, kept in a sidecar YAML file next to the TEI (``<file>.display.yaml``): 

3a title shown instead of the file name, and each annotator's short name (shown in the circle). The annotator's 

4full name is not kept here: it is in the TEI (``<respStmt xml:id="..."><name>``). 

5""" 

6import os 

7import tempfile 

8from pathlib import Path 

9 

10import yaml 

11 

12 

13def display_path(tei_path:Path) -> Path: 

14 return Path(tei_path).with_name(f"{Path(tei_path).stem}.display.yaml") 

15 

16 

17def load_display(path:Path) -> dict: 

18 try: 

19 return yaml.safe_load(Path(path).read_text(encoding="utf-8")) or {} 

20 except FileNotFoundError: 

21 return {} 

22 

23 

24def save_display(data:dict, path:Path): 

25 path = Path(path) 

26 fd, tmp = tempfile.mkstemp(dir=path.parent, prefix=f".{path.name}.", suffix=".tmp") 

27 try: 

28 with os.fdopen(fd, "w", encoding="utf-8") as f: 

29 yaml.safe_dump(data, f, sort_keys=False, allow_unicode=True) 

30 os.replace(tmp, path) 

31 finally: 

32 if os.path.exists(tmp): 

33 os.remove(tmp) 

34 

35 

36def initials(name:str|None) -> str|None: 

37 """ 'Robert Turnbull' -> 'RT'; a single word gives its first two letters. """ 

38 words = [word for word in (name or "").replace("-", " ").split() if word[:1].isalpha()] 

39 if not words: 

40 return None 

41 if len(words) == 1: 

42 return words[0][:2].upper() 

43 return (words[0][0] + words[-1][0]).upper()