def get_plot_file(self): """ Searches for and retrieves a plot file from CEPH. Might find multiple files (e.g. if more than one plot_type is specified), but will only copy over one. :return: (str) local path to downloaded files OR None if no files found """ _existing_plot_files = self._get_plot_files_locally() if _existing_plot_files: return _existing_plot_files _existing_plot_files = self._check_for_plot_files() local_plot_paths = [] if _existing_plot_files: client = SFTPClient() for plot_file in _existing_plot_files: # Generate paths to data on server and destination on local machine _server_path = self.server_dir + plot_file _local_path = os.path.join(self.static_graph_dir, plot_file) try: client.retrieve(server_file_path=_server_path, local_file_path=_local_path, override=True) LOGGER.info('File \'%s\' found and saved to %s', _server_path, _local_path) except RuntimeError: LOGGER.error("File \'%s\' does not exist", _server_path) return None local_plot_paths.append( f'/static/graphs/{plot_file}') # shortcut to static dir return local_plot_paths # No files found return None
def test_invalid_connection(self): """ Test: A ConnectionException raised When: _test_connection is called without a valid connection """ client = SFTPClient() # client initialised but no connection made with self.assertRaises(ConnectionException): client._test_connection()
def create_mocked_connection_client(self): """ Creates a client with a mocked connection which returns True when exists(arg) is called if 'arg' is recognised as valid :return: The client with a mocked connection """ client = SFTPClient() client._connection = MagicMock() client._connection.exists.side_effect = self.is_argument_valid return client
def test_invalid_init(self): """ Test: A TypeError is raised When: SFTPClient is initialised with invalid credentials """ with self.assertRaises(TypeError): SFTPClient("invalid")
def test_default_init(self): """ Test: Class variables are created and set When: SFTPClient is initialised with default credentials """ client = SFTPClient() self.assertIsNotNone(client.credentials) self.assertIsNone(client._connection)
def _check_for_plot_files(self): """ Searches the server directory for existing plot files using the directory specified. :return: (list) files on the server path that match regex """ # start sftpclient client = SFTPClient() # initialise list to store names of existing files matching the search _found_files = [] # regular expression for plot file name(s) file_regex = self._generate_file_name_regex() if file_regex: # Add files that match regex to the list of files found _found_files.extend( client.get_filenames(server_dir_path=self.server_dir, regex=file_regex)) else: return None return _found_files