#!/usr/bin/env python3 import re import autoproc async def cmd_lookup_patient(proc, query): """Lookup patient""" async with proc.sendline, autoproc.expect_async(proc) as expect: proc.sendline('^Patient Inquiry') assert await expect.endswith('\r\nSelect PATIENT NAME: ') proc.sendline(query) assert await expect.startswith(query) res = [] single = False async for prompt, response in expect.promptmatches(( (re.compile(r'^CHOOSE 1-\d+: $'), None), ('Type to continue or \'^\' to exit: ', '^'), ('Do you wish to view active patient record flag details? Yes// ', 'No'), ('Do you want to continue processing this patient record? No// ', None), ('Select PATIENT NAME: ', None), ('Select Patient Appointments/Inpatient Inquiry Option: ', None, True), ), throw=True): proc.sendline(response) if prompt.index <= 4: res.append(prompt.before[:-24] if prompt.index == 0 and prompt.before.endswith('\r\nENTER \'^\' TO STOP, OR \r\n') else prompt.before) if 0 < prompt.index < 4: single = True assert await expect.endswith('\r\nSelect Patient Information and OE/RR Option: ', '\r\nSelect Patient Information and OE/RR Option: ') expect.clear() if single: return [re.search(r'[ ]{2}(?P.+?)[ ]{2}(?:\((?P[^\)]*?)\))?[ ]{6}(?P\S+)[ ]{4}(?P\S+(?:P \*\*Pseudo SSN\*\*)?)[ ]{5}(?P\S+)[ ]{5}(?P.+?)[ ]{6}(?P[^\r\n]*)', res[0].replace('\r\n', '', 1)).groupdict()] return [m.groupdict() for m in re.finditer(r'^[ ]{3}(?P\d+)[ ]{1,3}(?:(?P[A-Za-z][0-9]{4})[ ]{2})?(?P.+?)[ ]{6,8}(?P\S+)[ ]{4}(?P\S+(?:P \*\*Pseudo SSN\*\*)?)[ ]{5}(?P\S+)[ ]{5}(?P.+?)[ ]{6}(?P[^\r\n]*)', re.sub(r'(.{80})\r\n(?! \d)', r'\1', '\r\n\r\n'.join(res)), re.MULTILINE)] async def cmd_lookup_patient_ordinal(proc, query, ordinal, force=False): """Lookup patient""" async with proc.sendline, autoproc.expect_async(proc) as expect: proc.sendline('^Patient Inquiry') assert await expect.endswith('\r\nSelect PATIENT NAME: ') proc.sendline(query) assert await expect.startswith(query) res = [] ordinal = str(int(ordinal)) async for prompt, response in expect.promptmatches(( (re.compile(r'^CHOOSE 1-\d+: $'), None), ('Type to continue or \'^\' to exit: ', None), ('Do you wish to view active patient record flag details? Yes// ', 'No'), ('Do you want to continue processing this patient record? No// ', 'Yes' if force else 'No'), ('Select PATIENT NAME: ', None), ('Select Patient Appointments/Inpatient Inquiry Option: ', None, True), ), timeout_settle=2, throw=True): match prompt: case autoproc.ExpectMatch(index=0, before=before): if re.search(r'^[ ]{3}' + ordinal + r'[ ]+', before, re.MULTILINE): proc.sendline(ordinal) else: proc.sendline() case autoproc.ExpectMatch(index=(1|4), before=before): res.append(before[13:] if before.startswith('\x1b[H\x1b[J\x1b[2J\x1b[H') else before) proc.sendline() case autoproc.ExpectMatch(index=2, before=before, match=match): res.append(before + match) proc.sendline(response) case autoproc.ExpectMatch(index=3, before=before, match=match): res.append(before + match) proc.sendline(response) case autoproc.ExpectMatch(index=5): proc.sendline() assert await expect.endswith('\r\nSelect Patient Information and OE/RR Option: ', '\r\nSelect Patient Information and OE/RR Option: ') expect.clear() return re.sub(r'\r\n\r\n(?:[^\r\n;]+);(?:\([^\)]*?\))? (?:\d+ )?(?:\d{3}-\d{2}-\d{4}P?) (?:[^\r\n]+?)[ ]*?(\r\n={10,}\r\n)\r\n', r'\1', '\r\n'.join(res))