def test_create_csv_file(self): csv_file_path = os.path.join(self.tmp_dir, 'metrics-file.csv') header = ['Name', 'Value'] lines = [{ 'Name': 'line1', 'Value': 'line1' }, { 'Name': 'line2', 'Value': 'line2' }, { 'Name': 'line2' }] self.assertFalse(os.path.exists(csv_file_path)) create_csv_file(csv_file_path, header, lines) self.assertTrue(os.path.exists(csv_file_path)) with open(csv_file_path) as csv_file: csv_reader = csv.reader(csv_file, delimiter=',') line_count = 0 for row in csv_reader: if line_count == 0: self.assertEqual(header, row) line_count += 1 else: line_index = line_count - 1 if line_count == 3: row_values = [lines[line_index]['Name'], ''] else: row_values = [ lines[line_index]['Name'], lines[line_index]['Value'] ] self.assertEqual(row_values, row) line_count += 1 self.assertEqual(4, line_count)
def create_relationships_csv_file(csv_header, file_name, formatted_data, dir, export_path=False): file_path = os.path.join(dir, file_name) create_csv_file(file_path, csv_header, formatted_data) if export_path: log.info('A CSV file was created with the relationships information in {}'.format(file_path)) with open(file_path) as csv_file: return io.StringIO(csv_file.read())
def export_relationships_to_csv(entities, relationships, export_path): csv_header = ['from_tag', 'from_name', 'from_version', 'from_type', 'to_tag', 'to_name', 'to_version', 'to_type'] formatted_data = [] for entity in entities: formatted_data = __format_relationships_to_csv_data(entity.name, entity.type, relationships, formatted_data) file_name = __get_file_name(entities, FileType.CSV.value) if export_path is None: with tempfile.TemporaryDirectory() as tempdir: file_path = os.path.join(tempdir, file_name) create_csv_file(file_path, csv_header, formatted_data) with open(file_path) as csv_file: return io.StringIO(csv_file.read()) else: file_path = os.path.join(export_path, file_name) create_csv_file(file_path, csv_header, formatted_data) log.info('A CSV file was created with the relationship information in {}'.format(file_path)) with open(file_path) as csv_file: return io.StringIO(csv_file.read())
def _export_metrics_to_csv(self, file_path, tags_info): csv_header, data_formatted = self._format_data_for_csv(tags_info) file_path += '.' + FileType.CSV.value create_csv_file(file_path, csv_header, data_formatted) with open(file_path) as csv_file: return io.StringIO(csv_file.read()), file_path