2246_verso_tests/util/ziath_to_tubes.py

65 lines
1.4 KiB
Python

import json
from sys import argv
from os import path, kill
from dataclasses import dataclass
@dataclass
class Tube:
barcode: str
row: int
col: int
def main():
input_file = argv[1]
output_file = argv[2]
if not test_file(input_file):
print("Input file does not exist")
kill(1)
if test_file(output_file):
print("Output file already exists")
kill(1)
tubes = read_and_parse_ziath_json(input_file)
json_obj = tubes_to_tubes_json(tubes)
with open(output_file, 'w') as file:
json.dump(json_obj, file)
def test_file(p: str) -> bool:
return path.exists(p) and path.isfile(p)
def read_and_parse_ziath_json(p: str) -> [Tube]:
with open(p, 'r') as file:
j = json.load(file)
ziath_tubes = j["tubeBarcode"]
return [Tube(tube["barcode"], tube["row"], tube["column"])
for tube in ziath_tubes]
def tubes_to_tubes_json(i: [Tube]) -> dict:
items_list = []
target_positions_list = []
for tube in i:
items_list.append(tube.barcode)
target_positions_list.append((tube.row - 1) * 12
+ tube.col)
output_obj = {}
output_obj["Items"] = [{"BarcodeValue": b}
for b in items_list]
output_obj["TargetPositions"] = [{"PositionIndex": p}
for p in target_positions_list]
return output_obj
if __name__ == "__main__":
main()