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
« 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."""
3import re
5from . import base, util
8def collect(gen: base.Generator):
9 """Collect the documentation strings.
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``.
16 Args:
17 gen: Generator state.
19 Returns:
20 A dict of strings, keyed by language code and type uid.
21 """
23 strings_dct = {}
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)
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)
38 return strings_dct
41def _add_string(strings_dct, lang, uid, text):
42 """Add a string to the dict, a leading ``[xx]`` overrides the language."""
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')