Coverage for mindsdb / integrations / handlers / mendeley_handler / mendeley_tables.py: 0%

63 statements  

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

1from mindsdb_sql_parser import ast 

2import pandas as pd 

3from mindsdb.integrations.libs.api_handler import APITable 

4from mindsdb.integrations.utilities.sql_utils import extract_comparison_conditions 

5 

6 

7class CatalogSearchTable(APITable): 

8 

9 def select(self, query: ast.Select) -> pd.DataFrame: 

10 """The select method implements the mappings from the ast.Select and calls the actual API through the call_mendeley_api. 

11 Firstly, it is used to put the parameters specified in the query in a dictionary, which is then used when calling the method call_mendeley_api. 

12 If no conditions are specified, an error is raised since the search cannot be conducted. 

13 

14 Args: 

15 query (ast.Select): query used to specify the wanted results 

16 Returns: 

17 result (DataFrame): the result of the query 

18 """ 

19 

20 conditions = extract_comparison_conditions(query.where) 

21 

22 params = {} 

23 

24 # Since there are three different types of search, and each of them takes different parameters, we use the parameters that lead 

25 # to the most specific results. For example, in the case of the user specifying the title and the doi of a document, priority is given to 

26 # the doi. 

27 

28 if query.limit is not None: 

29 params['limit'] = query.limit.value 

30 else: 

31 params['limit'] = 30 

32 

33 for op, arg1, arg2 in conditions: 

34 

35 if arg1 in ['arxiv', 'doi', 'isbn', 'issn', 'pmid', 'scopus', 'filehash']: 

36 

37 if op != '=': 

38 raise NotImplementedError 

39 params[arg1] = arg2 

40 

41 result = self.handler.call_mendeley_api( 

42 method_name='identifier_search', 

43 params=params) 

44 

45 break 

46 

47 elif arg1 == 'id': 

48 if op != '=': 

49 raise NotImplementedError 

50 params['id'] = arg2 

51 

52 result = self.handler.call_mendeley_api( 

53 method_name='get', 

54 params=params) 

55 

56 break 

57 

58 elif "title" or "author" or "source" or "abstract" or "min_year" or "max_year" or "open_access" or "view" in conditions: 

59 

60 if arg1 == 'title': 

61 if op != '=': 

62 raise NotImplementedError 

63 params['title'] = arg2 

64 

65 elif arg1 == 'author': 

66 if op != '=': 

67 raise NotImplementedError 

68 params['author'] = arg2 

69 

70 elif arg1 == 'source': 

71 if op != '=': 

72 raise NotImplementedError 

73 params['source'] = arg2 

74 

75 elif arg1 == 'abstract': 

76 if op != '=': 

77 raise NotImplementedError 

78 params['abstract'] = arg2 

79 

80 elif arg1 == 'min_year': 

81 params['min_year'] = arg2 

82 

83 elif arg1 == 'max_year': 

84 params['max_year'] = arg2 

85 

86 elif arg1 == 'open_access': 

87 if op != '=': 

88 raise NotImplementedError 

89 params['open_access'] = arg2 

90 

91 result = self.handler.call_mendeley_api( 

92 method_name='advanced_search', 

93 params=params) 

94 

95 if conditions == []: 

96 raise ValueError('Please give input for the search to be conducted.') 

97 

98 columns = [] 

99 

100 for target in query.targets: 

101 if isinstance(target, ast.Star): 

102 columns = self.get_columns() 

103 break 

104 elif isinstance(target, ast.Identifier): 

105 columns.append(target.parts[-1]) 

106 else: 

107 raise ValueError(f"Unknown query target {type(target)}") 

108 

109 return result[columns] 

110 

111 def get_columns(self): 

112 """ get_columns method returns the columns returned by the API""" 

113 return [ 

114 

115 'title', 

116 'type', 

117 'source', 

118 'year', 

119 'pmid', 

120 'sgr', 

121 'issn', 

122 'scopus', 

123 'doi', 

124 'pui', 

125 'authors', 

126 'keywords', 

127 'link', 

128 'id' 

129 ]