import epics

from mgnt_auto_checkout_test import AutoCheckoutTest


class BarcodeTest(AutoCheckoutTest):
    test_id = 'barcode'
    description = 'Barcode'

    def __init__(self):
        super(BarcodeTest, self).__init__(
            pre_checkout_routines=[]
        )

    def execute(self, magnet):
        barcode_pv = magnet['device'] + ':BARCODE'
        barcode = epics.caget(barcode_pv)
        if barcode is None:
            self.data = '{} not found'.format(barcode_pv)
            self.status = False
            print 'FAIL'

        elif barcode != magnet['barcode']:
            self.data = '{} is {} instead of {}'.format(barcode_pv, barcode, magnet['barcode'])
            self.status = False
            print 'FAIL'

        else:
            self.data = barcode
            self.status = True
            print 'PASS'
