def virtual_app():
    path = os.path.join(os.path.dirname(__file__), 'virtual_app_device.py')
    hw = HardwareManager(port="virtual:%s" % path)

    hw.connect(1)
    yield hw
    hw.disconnect()
class HardwareManagerResource(SharedResource):
    """A shared HardwareManager instance.

    Arguments:
        port (str): Optional port argument that should
            be used to connect to a DeviceAdapter.  If not
            specified, the virtual environment default is used.
        connect: (str or int): The UUID of a device to connect to
            when this resource is created and disconnect from when
            it is destroyed.  This is an optional parameter.  If
            it is not specified the HardwareManager is not connected
            upon creation.
        connect_direct: (str or int): The connection string of a device to connect 
            directly to when this resource is created and disconnect from 
            when it is destroyed.  This is an optional parameter.  If
            it is not specified the HardwareManager is not connected
            upon creation.
    """

    ARG_SCHEMA = RESOURCE_ARG_SCHEMA

    def __init__(self, args):
        super(HardwareManagerResource, self).__init__()

        self._port = args.get('port')
        self._connect_id = args.get('connect')
        self._connection_string = args.get('connect_direct')
        self.hwman = None

        if self._connect_id is not None and not isinstance(
                self._connect_id, int):
            self._connect_id = int(self._connect_id, 0)

    def open(self):
        """Open and potentially connect to a device."""

        self.hwman = HardwareManager(port=self._port)
        self.opened = True

        if self._connection_string is not None:
            try:
                self.hwman.connect_direct(self._connection_string)
            except HardwareError:
                self.hwman.close()
                raise

        elif self._connect_id is not None:
            try:
                self.hwman.connect(self._connect_id)
            except HardwareError:
                self.hwman.close()
                raise

    def close(self):
        """Close and potentially disconnect from a device."""

        if self.hwman.stream.connected:
            self.hwman.disconnect()

        self.hwman.close()
        self.opened = False