Coverage for gws-app/gws/spec/generator/strings.py: 91%

23 statements  

« prev     ^ index     » next       coverage.py v7.16.2, created at 2026-10-05 13:35 +0200

1"""Collect documentation strings from docstrings and ``strings.ini`` files.""" 

2 

3import re 

4 

5from . import base, util 

6 

7 

8def collect(gen: base.Generator): 

9 """Collect the documentation strings. 

10 

11 English strings come from the docstrings of the server types and enum 

12 members. Then all ``strings.ini`` and ``strings.<suffix>.ini`` files under 

13 the root directory are read; ini sections are language codes. A string 

14 starting with ``[xx]`` is stored under the language ``xx``. 

15 

16 Args: 

17 gen: Generator state. 

18 

19 Returns: 

20 A dict of strings, keyed by language code and type uid. 

21 """ 

22 

23 strings_dct = {} 

24 

25 for typ in gen.serverTypes: 

26 _add_string(strings_dct, 'en', typ.name, typ.doc) 

27 if typ.enumDocs: 

28 for k, v in typ.enumDocs.items(): 

29 _add_string(strings_dct, 'en', typ.name + '.' + k, v) 

30 

31 for path in util.find_files(gen.rootDir, pattern=r'/strings(\..+)?\.ini$', deep=True): 

32 base.log.debug(f'parsing strings from {path!r}') 

33 d = util.parse_ini(util.read_file(path)) 

34 for lang, strs in d.items(): 

35 for uid, text in strs.items(): 

36 _add_string(strings_dct, lang, uid, text) 

37 

38 return strings_dct 

39 

40 

41def _add_string(strings_dct, lang, uid, text): 

42 """Add a string to the dict, a leading ``[xx]`` overrides the language.""" 

43 

44 text = (text or '').strip() 

45 m = re.match(r'^\[(\w\w)](.+)$', text) 

46 if m: 

47 lang = m.group(1) 

48 text = m.group(2).strip() 

49 strings_dct.setdefault(lang, {})[uid] = text.replace('\\n', '\n')