]> ToastFreeware Gitweb - philipp/winterrodeln/wrpylib.git/blob - wrpylib/lib_sledrun_wikitext_to_json.py
Create function optional_set.
[philipp/winterrodeln/wrpylib.git] / wrpylib / lib_sledrun_wikitext_to_json.py
1 from itertools import takewhile, dropwhile
2 from typing import Optional, Any
3
4 from mwparserfromhell.nodes import Template, Text, Tag
5 from mwparserfromhell.wikicode import Wikicode
6
7
8 def optional_set(haystack: dict, key: str, value: Optional[Any]):
9     """Sets the key to value in haystack only if value is not None"""
10     if value is not None:
11         haystack[key] = value
12
13
14 def get_sledrun_description(sledrun: Wikicode) -> Optional[str]:
15     """Get description from sledrun"""
16     for v in sledrun.get_sections(levels=[2], matches='Allgemeines', include_headings=False):
17         dw = dropwhile(lambda n: isinstance(n, Template) or (isinstance(n, Text) and not str(n).strip()), v.nodes)
18         tw = takewhile(lambda n: not (isinstance(n, Tag) and str(n) == '*'), dw)
19         result = str(Wikicode(tw)).strip()
20         if result:
21             return result