Coverage for src/wiktextract/extractor/pt/etymology.py: 96%

47 statements  

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

1from wikitextprocessor.parser import ( 

2 LEVEL_KIND_FLAGS, 

3 LevelNode, 

4 NodeKind, 

5 TemplateNode, 

6 WikiNode, 

7) 

8 

9from ...page import clean_node 

10from ...wxr_context import WiktextractContext 

11from .models import Attestation, WordEntry 

12 

13 

14def extract_etymology_section( 

15 wxr: WiktextractContext, 

16 page_data: list[WordEntry], 

17 level_node: LevelNode, 

18) -> None: 

19 cats = {} 

20 e_nodes = [] 

21 e_texts = [] 

22 etymology_links = [] 

23 attestations = [] 

24 for node in level_node.children: 

25 if isinstance(node, WikiNode) and node.kind in LEVEL_KIND_FLAGS: 

26 break 

27 elif isinstance(node, WikiNode) and node.kind == NodeKind.LIST: 

28 links: list[tuple[str, str]] = [] 

29 e_text = clean_node( 

30 wxr, cats, e_nodes, link_collector=links 

31 ).lstrip(": ") 

32 if e_text != "": 

33 e_texts.append(e_text) 

34 etymology_links.extend(links) 

35 e_nodes.clear() 

36 for list_item in node.find_child(NodeKind.LIST_ITEM): 

37 links = [] 

38 e_text = clean_node( 

39 wxr, cats, list_item.children, link_collector=links 

40 ) 

41 if e_text != "": 41 ↛ 36line 41 didn't jump to line 36 because the condition on line 41 was always true

42 e_texts.append(e_text) 

43 etymology_links.extend(links) 

44 elif isinstance(node, TemplateNode) and node.template_name == "datação": 

45 attestations = extract_defdate_template(wxr, cats, node) 

46 else: 

47 e_nodes.append(node) 

48 

49 if len(e_nodes) > 0: 

50 links = [] 

51 e_text = clean_node(wxr, cats, e_nodes, link_collector=links).lstrip( 

52 ": " 

53 ) 

54 if e_text != "": 54 ↛ 57line 54 didn't jump to line 57 because the condition on line 54 was always true

55 e_texts.append(e_text) 

56 etymology_links.extend(links) 

57 for data in page_data: 

58 if data.lang_code == page_data[-1].lang_code: 

59 data.etymology_texts.extend(e_texts) 

60 data.etymology_links.extend(etymology_links) 

61 data.categories.extend(cats.get("categories", [])) 

62 data.attestations.extend(attestations) 

63 

64 

65def extract_defdate_template( 

66 wxr: WiktextractContext, cats: dict[str, list[str]], t_node: TemplateNode 

67) -> list[Attestation]: 

68 attestations = [] 

69 date = ( 

70 clean_node(wxr, cats, t_node) 

71 .removeprefix("(Datação:") 

72 .removesuffix(")") 

73 .strip() 

74 ) 

75 if date != "": 75 ↛ 77line 75 didn't jump to line 77 because the condition on line 75 was always true

76 attestations.append(Attestation(date=date)) 

77 return attestations