-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInternalCalls.py
More file actions
93 lines (75 loc) · 3.01 KB
/
Copy pathInternalCalls.py
File metadata and controls
93 lines (75 loc) · 3.01 KB
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
import csv
import os
import sys
def normalize_address(address):
if not address:
return ''
return address.strip().rstrip('/').lower()
def process_csv_file(file_path, output_dir):
rows = []
with open(file_path, newline='', encoding='utf-8') as f:
reader = csv.reader(f)
headers = next(reader)
# Ensure UID is the first column
if headers[0] != "UID":
raise ValueError("Expected first column to be 'UID'.")
# Add new columns if not already present
if "CallsIflow" not in headers:
headers.append("CallsIflow")
if "IsCalledByIflow" not in headers:
headers.append("IsCalledByIflow")
for row in reader:
while len(row) < len(headers):
row.append("")
rows.append(row)
# Index helpers
idx_uid = headers.index("UID")
idx_type = headers.index("AdapterType")
idx_dir = headers.index("AdapterDirection")
idx_addr = headers.index("AdapterAddress")
idx_calls = headers.index("CallsIflow")
idx_called_by = headers.index("IsCalledByIflow")
# Step 1: Build mapping of address → receiver UIDs
receiver_map = {}
for row in rows:
if row[idx_type] == "ProcessDirect" and row[idx_dir] == "Receiver":
addr = normalize_address(row[idx_addr])
if addr:
receiver_map[addr] = row[idx_uid]
# Step 2: Match senders to receivers by address
for row in rows:
if row[idx_type] == "ProcessDirect" and row[idx_dir] == "Sender":
addr = normalize_address(row[idx_addr])
if addr in receiver_map:
sender_uid = row[idx_uid]
receiver_uid = receiver_map[addr]
# Update receiver
for r in rows:
if r[idx_uid] == receiver_uid:
r[idx_called_by] = f"{r[idx_called_by]}, {sender_uid}".strip(", ")
break
# Update sender
row[idx_calls] = f"{row[idx_calls]}, {receiver_uid}".strip(", ")
# Write to temp dir
output_file = "final_output.csv"
output_path = os.path.join(output_dir, output_file)
with open(output_path, 'w', newline='', encoding='utf-8') as f:
writer = csv.writer(f, quoting=csv.QUOTE_ALL)
writer.writerow(headers)
writer.writerows(rows)
# Return path for app.py (stdout)
print(output_path)
def main():
if len(sys.argv) < 2:
print("❌ No input directory provided.", file=sys.stderr)
return
input_dir = sys.argv[1]
csv_files = [f for f in os.listdir(input_dir) if f.lower().endswith('.csv')]
if not csv_files:
print("⚠️ No CSV files found in directory.", file=sys.stderr)
return
# We assume processing only the first CSV
file_path = os.path.join(input_dir, csv_files[0])
process_csv_file(file_path, input_dir)
if __name__ == "__main__":
main()