Coverage for nlp_manager/cloud_run_info.py: 80%

40 statements  

« prev     ^ index     » next       coverage.py v7.8.0, created at 2025-08-27 10:34 -0500

1""" 

2crate_anon/nlp_manager/cloud_run_info.py 

3 

4=============================================================================== 

5 

6 Copyright (C) 2015, University of Cambridge, Department of Psychiatry. 

7 Created by Rudolf Cardinal (rnc1001@cam.ac.uk). 

8 

9 This file is part of CRATE. 

10 

11 CRATE is free software: you can redistribute it and/or modify 

12 it under the terms of the GNU General Public License as published by 

13 the Free Software Foundation, either version 3 of the License, or 

14 (at your option) any later version. 

15 

16 CRATE is distributed in the hope that it will be useful, 

17 but WITHOUT ANY WARRANTY; without even the implied warranty of 

18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

19 GNU General Public License for more details. 

20 

21 You should have received a copy of the GNU General Public License 

22 along with CRATE. If not, see <https://www.gnu.org/licenses/>. 

23 

24=============================================================================== 

25 

26Information class describing an NLPRP remote server; particularly, which 

27NLP processors it offers. 

28 

29""" 

30 

31import logging 

32from typing import List, Optional, Tuple 

33 

34from crate_anon.nlp_manager.cloud_parser import Cloud 

35from crate_anon.nlp_manager.cloud_request import CloudRequestListProcessors 

36from crate_anon.nlp_manager.input_field_config import InputFieldConfig 

37from crate_anon.nlp_manager.nlp_definition import NlpDefinition 

38from crate_anon.nlp_webserver.server_processor import ServerProcessor 

39 

40log = logging.getLogger(__name__) 

41 

42 

43class CloudRunInfo: 

44 """ 

45 Represents session-wide information about an NLP cloud run, including 

46 details of the server and our chosen processors and config. 

47 """ 

48 

49 def __init__( 

50 self, 

51 nlpdef: NlpDefinition, 

52 debug_post_request: bool = False, 

53 debug_post_response: bool = False, 

54 ) -> None: 

55 """ 

56 Args: 

57 nlpdef: 

58 a :class:`crate_anon.nlp_manager.nlp_definition.NlpDefinition` 

59 """ 

60 self.nlpdef = nlpdef 

61 # Convenience member for our users: 

62 self.cloudcfg = nlpdef.get_cloud_config_or_raise() 

63 self._remote_processors = None # type: Optional[List[ServerProcessor]] 

64 self._local_processors = None # type: Optional[List[Cloud]] 

65 self._debug_post_request = debug_post_request 

66 self._debug_post_response = debug_post_response 

67 

68 self._configure_local_processors() 

69 

70 def get_remote_processors(self) -> List[ServerProcessor]: 

71 """ 

72 Returns processors offered by the remote server. 

73 """ 

74 if self._remote_processors is None: 

75 # Fetch from server 

76 req = CloudRequestListProcessors( 

77 nlpdef=self.nlpdef, 

78 debug_post_request=self._debug_post_request, 

79 debug_post_response=self._debug_post_response, 

80 ) 

81 self._remote_processors = req.get_remote_processors() 

82 return self._remote_processors 

83 

84 def get_local_processors(self) -> List[Cloud]: 

85 """ 

86 Returns instances of local processors (which know about the local 

87 database structure, etc.). 

88 """ 

89 if self._local_processors is None: 

90 self._local_processors = [ 

91 p for p in self.nlpdef.processors if isinstance(p, Cloud) 

92 ] 

93 return self._local_processors 

94 

95 def _configure_local_processors(self) -> None: 

96 for lp in self.get_local_processors(): 

97 for rp in self.get_remote_processors(): 

98 lp.set_procinfo_if_correct(rp) 

99 

100 def get_requested_processors(self) -> List[Tuple[str, str]]: 

101 """ 

102 Returns the processors we wish the server to use. 

103 

104 Returns: 

105 a list of tuples: each ``procname, procversion``. 

106 """ 

107 requested = [] # type: List[Tuple[str, str]] 

108 for lp in self.get_local_processors(): 

109 if lp.available_remotely: 

110 name_version = lp.procname, lp.procversion 

111 requested.append(name_version) 

112 return requested 

113 

114 def delete_dest_records( 

115 self, 

116 ifconfig: InputFieldConfig, 

117 pkval: int, 

118 pkstr: Optional[str], 

119 commit: bool = True, 

120 ): 

121 """ 

122 Used for incremental updates. Deletes old destination records. 

123 """ 

124 for processor in self.get_local_processors(): 

125 processor.delete_dest_record(ifconfig, pkval, pkstr, commit=commit)