Source code for fastscore.suite.instance


from ..errors import FastScoreError
from ..v1.rest import ApiException

from ..v2.models import ActiveSensorInfo
## patch ActiveSensorInfo generated by Swagger
def _uninstall(self):
    self._inst.uninstall_sensor(self.id)
ActiveSensorInfo.uninstall = _uninstall

class InstanceBase(object):
    """
    The parent of all FastScore instance classes.

    """

    def __init__(self, name, api, swg, swg2=None):
        self.name = name
        self.api = api
        self.swg = swg
        self.swg2 = swg2    # REST API v2

    @property
    def active_sensors(self):
        """
        A collection of currently installed sensors indexed by id.

        >>> engine = connect.lookup('engine')
        >>> engine.active_sensors.keys()
        [8]
        >>> x = engine.active_sensors[8]
        >>> x.id
        8
        >>> x.tap
        manifold.input.records.count
        >>> x.uninstall()
        >>> engine.active_sensors.ids()
        []

        """
        try:
            d = {}
            for x in self.swg2.active_sensor_list(self.name):
                x._inst = self
                d[x.id] = x
            return d
        except Exception as e:
            raise FastScoreError("Unable to retrieve active sensors", caused_by=e)

    @property
    def tapping_points(self):
        """
        A list of tapping points supported by the instance.

        >>> mm.tapping_points
        ['sys.memory',... ]

        """
        try:
            return self.swg2.active_sensor_points(self.name)
        except Exception as e:
            raise FastScoreError("Unable to list tapping points", caused_by=e)

    def check_health(self):
        """
        Retrieves version information from the instance. A successful reply indicates
        that the instance is healthy.

        >>> connect.check_health()
        {
          'id': '366e5030-d773-49cb-8b28-9b1b9d173c79',
          'built_on': 'Thu May 11 12:53:39 UTC 2017',
          'release': '1.5'
        }

        """
        try:
            return self.swg.health_get(self.name)
        except Exception as e:
            raise FastScoreError("Unable to retrieve instance info", caused_by=e)

    def get_swagger(self):
        """
        Retrieves the Swagger specification of the API supported by the
        instance.

        >>> connect.get_swagger()
        {u'info':...}

        """
        try:
            return self.swg.swagger_get(self.name)
        except Exception as e:
            raise FastScoreError("Unable to retrieve Swagger specification", caused_by=e)

    def install_sensor(self, sensor):
        try:
            r = self.swg2.active_sensor_install(self.name, sensor.desc)
            return r.id
        except Exception as e:
            raise FastScoreError("Unable to install sensor", caused_by=e)

    def uninstall_sensor(self, tapid):
        try:
            self.swg2.active_sensor_uninstall(self.name, tapid)
        except Exception as e:
            raise FastScoreError("Unable to uninstall sensor", caused_by=e)