2246_verso_tests/verso_spelling/verso_spell_via_script.py

89 lines
2.4 KiB
Python
Raw Permalink Normal View History

2024-10-24 23:25:05 +00:00
# Cellario modules
2024-10-24 18:59:10 +00:00
from HRB.Cellario.Scripting import *
from HRB.Cellario.Scripting.API import *
import json
from os import path, listdir
from dataclasses import dataclass
2024-10-24 23:25:05 +00:00
def Execute(api: PythonScriptingApi):
2024-10-24 18:59:10 +00:00
available_barcodes = "C:/temp/availabletubes.txt"
letter_dir = "C:/temp/letters"
input = "EH"
2024-10-24 23:25:05 +00:00
2024-10-24 18:59:10 +00:00
available_tubes = read_available(available_barcodes)
letters = []
for letter in input:
x = get_tubes_for_letter(letter,
len(letters) + 1,
available_tubes,
letter_dir)
available_tubes = available_tubes[len(x):]
letters += x
output = letter_tubes_to_obj(letters)
2024-10-24 23:25:05 +00:00
2024-10-24 18:59:10 +00:00
api.Messaging.WriteDiagnostic(ScriptLogLevel.Normal, json.dumps(output))
2024-10-24 23:25:05 +00:00
list(api.CurrentRun.RunOrderParameters)[
0].ParameterValue = json.dumps(output)
2024-10-24 18:59:10 +00:00
@dataclass(frozen=True)
class Tube:
barcode: str
row: int
col: int
@dataclass(frozen=True)
class LetterTube:
barcode: str
index: int
group: int
def letter_tubes_to_obj(x):
output = {}
output["Items"] = [{"BarcodeValue": y.barcode} for y in x]
output["TargetPositions"] = [{"PositionIndex": y.index,
"GroupIndex": y.group}
for y in x]
return output
def get_tubes_for_letter(letter: str,
letter_index: int,
available_tubes,
letter_dir: str):
positions = get_letter_positions(letter, letter_dir)
if len(positions) > len(available_tubes):
raise Exception("Insufficient tubes to fill order")
pixels = []
for i, position in enumerate(positions):
pixel = LetterTube(available_tubes[i],
position,
letter_index)
pixels.append(pixel)
return pixels
def read_available(p: str):
with open(p, 'r') as file:
return [x.strip() for x in file.readlines()]
def validate_word(input: str, letter_dir: str) -> bool:
letters_available = [x[0] for x in listdir(letter_dir)]
has_required_letters = all([x in letters_available for x in input])
return has_required_letters
def get_letter_positions(letter: str, letter_dir: str):
filename = [f for f in listdir(letter_dir) if f[0] == letter][0]
with open(path.join(letter_dir, filename), 'r') as file:
2024-10-24 23:25:05 +00:00
return [int(x.strip()) for x in file.readlines()]