Coverage for mindsdb / integrations / handlers / mediawiki_handler / mediawiki_handler.py: 0%

35 statements  

« prev     ^ index     » next       coverage.py v7.13.1, created at 2026-01-21 00:36 +0000

1from mediawikiapi import MediaWikiAPI 

2 

3from mindsdb.integrations.handlers.mediawiki_handler.mediawiki_tables import PagesTable 

4from mindsdb.integrations.libs.api_handler import APIHandler 

5from mindsdb.integrations.libs.response import ( 

6 HandlerStatusResponse as StatusResponse, 

7) 

8 

9from mindsdb.utilities import log 

10from mindsdb_sql_parser import parse_sql 

11 

12logger = log.getLogger(__name__) 

13 

14 

15class MediaWikiHandler(APIHandler): 

16 """ 

17 The MediaWiki handler implementation. 

18 """ 

19 

20 name = 'mediawiki' 

21 

22 def __init__(self, name: str, **kwargs): 

23 """ 

24 Initialize the handler. 

25 Args: 

26 name (str): name of particular handler instance 

27 **kwargs: arbitrary keyword arguments. 

28 """ 

29 super().__init__(name) 

30 

31 self.kwargs = kwargs 

32 

33 self.connection = None 

34 self.is_connected = False 

35 

36 pages_data = PagesTable(self) 

37 self._register_table("pages", pages_data) 

38 

39 def connect(self): 

40 """ 

41 Set up the connection required by the handler. 

42 Returns 

43 ------- 

44 StatusResponse 

45 connection object 

46 """ 

47 if self.is_connected is True: 

48 return self.connection 

49 

50 self.connection = MediaWikiAPI() 

51 

52 self.is_connected = True 

53 

54 return self.connection 

55 

56 def check_connection(self) -> StatusResponse: 

57 """ 

58 Check connection to the handler. 

59 Returns: 

60 HandlerStatusResponse 

61 """ 

62 

63 response = StatusResponse(False) 

64 

65 try: 

66 self.connect() 

67 response.success = True 

68 except Exception as e: 

69 logger.error('Error connecting to MediaWiki!') 

70 response.error_message = str(e) 

71 

72 self.is_connected = response.success 

73 

74 return response 

75 

76 def native_query(self, query: str) -> StatusResponse: 

77 """Receive and process a raw query. 

78 Parameters 

79 ---------- 

80 query : str 

81 query in a native format 

82 Returns 

83 ------- 

84 StatusResponse 

85 Request status 

86 """ 

87 ast = parse_sql(query) 

88 return self.query(ast)