def csv_files_to_file(csv_rows_consumer, out_prefix, out_dir, out_file_name, file_path_names): """ Consume the file at file_path_name as a csv file, passing it through csv_rows_consumer, writing the results as a csv file into out_dir as the same name, lowered, and prefixed. :param csv_rows_consumer: consumes data_items yielding collection for each :param out_prefix: prefix out_file_name :type out_prefix: str :param out_dir: Directory to write output file to. :type out_dir: str :param out_file_name: Output file base name. :type out_file_name: str :param file_path_names: tuple of paths and basenames to input csv files :type file_path_names: str, str """ ensure_dir(out_dir) out_filename = os.path.join( out_dir, '{}{}'.format( out_prefix, out_file_name.lower())) in_urls_func = partial(yield_nth_of, 0, file_path_names) multi_in_single_out(i_get_csv_data, write_as_csv, csv_rows_consumer, out_filename, in_urls_func)
def test_ensure_dir_makesdirs_with_none(self, mock_makedirs, mock_exists): """ When a directory doesn't exist according to os.path.exists call makedirs with it. """ mock_exists.return_value = False directory = "a/directory" ensure_dir(directory) mock_exists.assert_called_once_with(directory) mock_makedirs.assert_called_once_with(directory)
def test_ensure_dir_not_makesdirs_when_exists(self, mock_makedirs, mock_exists): """ When a directory exists according to os.path.exists do not call makedirs with it. """ mock_exists.return_value = True directory = "a/directory" ensure_dir(directory) mock_exists.assert_called_once_with(directory) self.assertFalse(mock_makedirs.call_count)
def csv_file_to_file(csv_rows_consumer, out_prefix, out_dir, file_path_name): """ Consume the file at file_path_name as a csv file, passing it through csv_rows_consumer, writing the results as a csv file into out_dir as the same name, lowered, and prefixed. :param csv_rows_consumer: consumes data_items yielding collection for each :type csv_rows_consumer: callable :param out_prefix: prefix out_file_name :type out_prefix: str :param out_dir: directory to write output file to :type out_dir: str :param file_path_name: path to input csv file :type file_path_name: str, str """ data_path, data_file_name = file_path_name data_items = i_get_csv_data(data_path) ensure_dir(out_dir) out_filename = os.path.join( out_dir, '{}{}'.format( out_prefix, data_file_name.lower())) write_as_csv(csv_rows_consumer(data_items), out_filename)