from __future__ import print_function

import epics

from .mgnt_auto_checkout_test import AutoCheckoutTest


class ECONTest(AutoCheckoutTest):
    test_id = 'econ'
    description = 'ECON'
    read_only = True
    deferred = False

    def __init__(self):
        super(ECONTest, self).__init__(
            pre_checkout_routines=[]
        )

    def execute(self, magnet):
        """Ensures that a magnet has the attribute ECON.

        1. Attempt to `caget <magnet>:ECON`.
        2. PASS if `caget` finds the PV, otherwise FAIL.

        Args:
            magnet (dict): Information about a magnet from Oracle (Elements, Polynomial, Additional Data)

        Returns:
            None

        """
        econ_pv = magnet['device'] + ':ECON'
        econ = epics.caget(econ_pv)
        if econ is not None:
            self.data = 'Exists'
            self.status = True
            print('PASS')

        else:
            self.data = '{} not found'.format(econ_pv)
            self.status = False
            print('FAIL')
