blob: 3c20d689243e79e366e31efc9a78f061f2ca77d8 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
|
#!/usr/bin/env python
#
# ===- csv2json.py - Static Analyzer test helper ---*- python -*-===#
#
# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
# See https://llvm.org/LICENSE.txt for license information.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#
# ===------------------------------------------------------------------------===#
r"""
Clang Static Analyzer test helper
=================================
This script converts a CSV file to a JSON file with a specific structure.
The JSON file contains a single dictionary. The keys of this dictionary
are taken from the first column of the CSV. The values are dictionaries
themselves, mapping the CSV header names (except the first column) to
the corresponding row values.
Usage:
csv2json.py <source-file>
Example:
// RUN: %csv2json.py %t | FileCheck %s
"""
import csv
import sys
import json
def csv_to_json_dict(csv_filepath):
"""
Args:
csv_filepath: The path to the input CSV file.
Raises:
FileNotFoundError: If the CSV file does not exist.
csv.Error: If there is an error parsing the CSV file.
Exception: For any other unexpected errors.
"""
try:
with open(csv_filepath, "r", encoding="utf-8") as csvfile:
reader = csv.reader(csvfile)
# Read the header row (column names)
try:
header = next(reader)
except StopIteration: # Handle empty CSV file
json.dumps({}, indent=2) # write an empty dict
return
# handle a csv file that contains no rows, not even a header row.
if not header:
json.dumps({}, indent=2)
return
other_column_names = [name.strip() for name in header[1:]]
data_dict = {}
for row in reader:
if len(row) != len(header):
raise csv.Error("Inconsistent CSV file")
exit(1)
key = row[0]
value_map = {}
for i, col_name in enumerate(other_column_names):
# +1 to skip the first column
value_map[col_name] = row[i + 1].strip()
data_dict[key] = value_map
return json.dumps(data_dict, indent=2)
except FileNotFoundError:
raise FileNotFoundError(f"Error: CSV file not found at {csv_filepath}")
except csv.Error as e:
raise csv.Error(f"Error parsing CSV file: {e}")
except Exception as e:
raise Exception(f"An unexpected error occurred: {e}")
def main():
"""Example usage with error handling."""
csv_file = sys.argv[1]
try:
print(csv_to_json_dict(csv_file))
except (FileNotFoundError, csv.Error, Exception) as e:
print(str(e))
except:
print("An error occured")
if __name__ == "__main__":
main()
|