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