-
Notifications
You must be signed in to change notification settings - Fork 0
/
csv2loom.py
108 lines (92 loc) · 3.13 KB
/
csv2loom.py
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
103
104
105
106
107
108
import csv
import json
import uuid
import time
import argparse
import os
def csv_to_json(csv_file):
print(f"Reading CSV file: {csv_file}")
with open(csv_file, 'r') as f:
reader = csv.reader(f)
columns = next(reader)
data = list(reader)
print("Translating to LOOM format...")
model = {
"columns": [],
"headerRows": [{"id": str(uuid.uuid4())}],
"bodyRows": [],
"headerCells": [],
"bodyCells": [],
"footerRows": [{"id": str(uuid.uuid4()) for _ in range(2)}],
"footerCells": [],
"filterRules": []
}
for i, column in enumerate(columns):
column_id = str(uuid.uuid4())
model["columns"].append({
"id": column_id,
"sortDir": "default",
"isVisible": True,
"width": "140px",
"type": "text",
"currencyType": "USD",
"dateFormat": "mm/dd/yyyy",
"shouldWrapOverflow": True,
"tags": [],
"calculationType": "none",
"aspectRatio": "unset",
"horizontalPadding": "unset",
"verticalPadding": "unset"
})
model["headerCells"].append({
"id": str(uuid.uuid4()),
"columnId": column_id,
"rowId": model["headerRows"][0]["id"],
"markdown": column
})
for i, row in enumerate(data):
row_id = str(uuid.uuid4())
model["bodyRows"].append({
"id": row_id,
"index": i,
"creationTime": int(time.time() * 1000),
"lastEditedTime": int(time.time() * 1000)
})
for j, cell in enumerate(row):
model["bodyCells"].append({
"id": str(uuid.uuid4()),
"isExternalLink": False,
"columnId": model["columns"][j]["id"],
"rowId": row_id,
"dateTime": None,
"markdown": cell,
"tagIds": []
})
for i, row_id in enumerate(model["footerRows"]):
for column in model["columns"]:
model["footerCells"].append({
"id": str(uuid.uuid4()),
"columnId": column["id"],
"rowId": row_id["id"]
})
print("Translation complete.")
return {"model": model, "pluginVersion": "8.0.0"}
def main():
parser = argparse.ArgumentParser(description='Convert a CSV file to a LOOM file.')
parser.add_argument('-i', '--input', type=str, required=True, help='Input CSV file')
parser.add_argument('-o', '--output', type=str, required=True, help='Output JSON file')
args = parser.parse_args()
json_data = csv_to_json(args.input)
# Check if output file has extension
filename, file_extension = os.path.splitext(args.output)
# If not, add .loom extension
if file_extension == "":
filename += ".loom"
else:
filename += file_extension
print(f"Writing to output file: {filename}")
with open(filename, 'w') as json_file:
json.dump(json_data, json_file, indent=4)
print("Operation completed successfully.")
if __name__ == "__main__":
main()