Skip to content

Commit 06411b2

Browse files
committed
no more saving jason file on disk
1 parent bf7f3b2 commit 06411b2

File tree

1 file changed

+41
-59
lines changed

1 file changed

+41
-59
lines changed
Lines changed: 41 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,93 +1,75 @@
11
import depthai as dai
22
import zipfile
3-
from json import dump , JSONEncoder
4-
from os.path import exists
5-
import os
3+
from json import dump, JSONEncoder
4+
import io
65

76
class CustomEncoder(JSONEncoder):
87
def default(self, obj):
9-
# If the object has a `__str__` method, use it
108
if hasattr(obj, '__str__'):
119
return str(obj)
12-
# Otherwise, use the default encoder's method
1310
return super().default(obj)
1411

1512
data = {}
1613

1714
with dai.Device() as device:
18-
1915
data['BootloaderVersion'] = device.getBootloaderVersion()
2016
data['ConnectedIMU'] = device.getConnectedIMU()
2117
data['imuFirmwareVersion'] = device.getIMUFirmwareVersion()
22-
filename = 'metadata.json'
23-
with open(filename, 'w') as f:
24-
dump(data, f, indent=2, cls=CustomEncoder)
2518

19+
filenames = [
20+
"metadata.json",
21+
"userCalib.json",
22+
"factoryCalib.json"
23+
24+
]
2625

27-
usercalibPath = 'userCalib.json'
26+
# Create in-memory file-like objects
27+
metadata_io = io.StringIO()
28+
usercalib_io = io.StringIO()
29+
factcalib_io = io.StringIO()
30+
crashDump_io = io.StringIO()
31+
# Dump data into these in-memory files
32+
dump(data, metadata_io, indent=2, cls=CustomEncoder)
33+
2834
try:
29-
30-
with open(usercalibPath,'w') as f:
31-
dump(device.readCalibration2().eepromToJson(), f, indent=2)
32-
dump(device.readCalibrationRaw(), f, indent=4)
35+
dump(device.readCalibration2().eepromToJson(), usercalib_io, indent=2)
36+
dump(device.readCalibrationRaw(), usercalib_io, indent=4)
3337
except Exception as ex:
34-
35-
with open(usercalibPath,'w') as f:
36-
dump(str(ex), f, indent=2)
38+
dump(str(ex), usercalib_io, indent=2)
3739

38-
factcalibPath = 'factoryCalib.json'
3940
try:
40-
41-
with open(factcalibPath,'w') as f:
42-
dump(device.readFactoryCalibration().eepromToJson(), f, indent=2)
43-
dump(device.readFactoryCalibrationRaw(), f, indent=4)
41+
dump(device.readFactoryCalibration().eepromToJson(), factcalib_io, indent=2)
42+
dump(device.readFactoryCalibrationRaw(), factcalib_io, indent=4)
4443
except Exception as ex:
45-
46-
with open(factcalibPath,'w') as f:
47-
dump(str(ex), f, indent=2)
44+
dump(str(ex), factcalib_io, indent=2)
45+
46+
in_memory_files = [metadata_io, usercalib_io, factcalib_io]
4847

4948
zip_name = 'device_info.zip'
49+
50+
with zipfile.ZipFile(zip_name, 'w') as z:
51+
# Zip in-memory file-like objects without writing to disk
52+
for file_io, filename in zip(in_memory_files, filenames):
53+
file_io.seek(0) # Reset cursor to the beginning of the file
54+
z.writestr(filename, file_io.getvalue())
55+
5056
if device.hasCrashDump():
5157
crashDump = device.getCrashDump()
5258
commitHash = crashDump.depthaiCommitHash
5359
deviceId = crashDump.deviceId
60+
filenames.append("crashreport_" + commitHash + "_" + deviceId + ".json")
61+
dump(crashDump.serializeToJson(), crashDump_io, indent=2)
62+
in_memory_files.append(crashDump_io)
5463

55-
json = crashDump.serializeToJson()
56-
57-
i = -1
58-
while True:
59-
i += 1
60-
destPath = "crashDump_" + str(i) + "_" + deviceId + "_" + commitHash + ".json"
61-
if exists(destPath):
62-
continue
63-
64-
with open(destPath, 'w', encoding='utf-8') as f:
65-
dump(json, f, ensure_ascii=False, indent=4)
64+
with zipfile.ZipFile(zip_name, 'w') as z:
65+
# Zip in-memory file-like objects without writing to disk
66+
for file_io, filename in zip(in_memory_files, filenames):
67+
file_io.seek(0) # Reset cursor to the beginning of the file
68+
z.writestr(filename, file_io.getvalue())
6669

67-
print("Crash dump found on your device!")
68-
print(f"Saved to {destPath}")
69-
print("Please report to developers!")
70-
with zipfile.ZipFile(zip_name, 'w') as z:
71-
z.write(filename)
72-
z.write(usercalibPath)
73-
z.write(factcalibPath)
74-
z.write(destPath)
75-
os.remove(filename)
76-
os.remove(usercalibPath)
77-
os.remove(factcalibPath)
78-
os.remove(destPath)
79-
break
70+
print("Crash dump found on your device!")
71+
print("Please report to developers!")
8072
else:
81-
with zipfile.ZipFile(zip_name, 'w') as z:
82-
z.write(filename)
83-
z.write(usercalibPath)
84-
z.write(factcalibPath)
85-
os.remove(filename)
86-
os.remove(usercalibPath)
87-
os.remove(factcalibPath)
8873
print("There was no crash dump found on your device!")
89-
# Zip the JSON file
90-
91-
9274

9375
print("Please report to developers via forum with the zip attached!")

0 commit comments

Comments
 (0)