Coverage for src/wiktextract/wxr_context.py: 94%

28 statements  

« prev     ^ index     » next       coverage.py v7.6.4, created at 2024-10-25 10:11 +0000

1# Wiktextract context object 

2import sqlite3 

3 

4from wikitextprocessor import Wtp 

5 

6from .config import WiktionaryConfig 

7 

8 

9class WiktextractContext: 

10 __slots__ = ( 

11 "wtp", 

12 "config", 

13 "lang", 

14 "word", 

15 "pos", 

16 "thesaurus_db_path", 

17 "thesaurus_db_conn", 

18 ) 

19 

20 def __init__(self, wtp: Wtp, config: WiktionaryConfig): 

21 from .thesaurus import init_thesaurus_db 

22 

23 self.config = config 

24 self.wtp = wtp 

25 self.lang = None 

26 self.word = None 

27 self.pos = None 

28 self.thesaurus_db_path = wtp.db_path.with_stem( # type: ignore[union-attr] 

29 f"{wtp.db_path.stem}_thesaurus" # type: ignore[union-attr] 

30 ) 

31 self.thesaurus_db_conn = ( 

32 init_thesaurus_db(self.thesaurus_db_path) 

33 if config.extract_thesaurus_pages 

34 else None 

35 ) 

36 

37 def reconnect_databases(self, check_same_thread: bool = True) -> None: 

38 # `multiprocessing.pool.Pool.imap()` runs in another thread, if the db 

39 # connection is used to create iterable data for `imap`, 

40 # `check_same_thread` must be `False`. 

41 if self.config.extract_thesaurus_pages: 41 ↛ 45line 41 didn't jump to line 45 because the condition on line 41 was always true

42 self.thesaurus_db_conn = sqlite3.connect( 

43 self.thesaurus_db_path, check_same_thread=check_same_thread 

44 ) 

45 self.wtp.db_conn = sqlite3.connect( 

46 self.wtp.db_path, check_same_thread=check_same_thread # type: ignore[arg-type] 

47 ) 

48 

49 def remove_unpicklable_objects(self) -> None: 

50 # remove these variables before passing the `WiktextractContext` object 

51 # to worker processes 

52 if self.config.extract_thesaurus_pages: 52 ↛ 54line 52 didn't jump to line 54 because the condition on line 52 was always true

53 self.thesaurus_db_conn.close() # type: ignore[union-attr] 

54 self.thesaurus_db_conn = None 

55 self.wtp.db_conn.close() 

56 self.wtp.db_conn = None # type: ignore[assignment] 

57 self.wtp.lua = None 

58 self.wtp.lua_invoke = None 

59 self.wtp.lua_reset_env = None 

60 self.wtp.lua_clear_loaddata_cache = None