# -*- coding: utf-8 -*-
from __future__ import print_function
import os, sys
from acq4.util.Mutex import Mutex

# singleton MMCorePy instance
_mmc = None

# default location to search for micromanager
# microManagerPath = 'C:\\Program Files\\Micro-Manager-1.4'
microManagerPath = 'C:\\Program Files\\Micro-Manager-2.0gamma'


def getMMCorePy(path=None):
    """Return a singleton MMCorePy instance that is shared by all devices for accessing micromanager.
    """
    global _mmc
    if _mmc is None:
        try:
            import pymmcore
            _mmc = pymmcore.CMMCore()
            _mmc.setDeviceAdapterSearchPaths([microManagerPath])
        except ImportError:

            try:
                import MMCorePy
            except ImportError:
                if sys.platform != 'win32':
                    raise
                # MM does not install itself to standard path. User should take care of this,
                # but we can make a guess..
                if path is None:
                    path = microManagerPath
                sys.path.append(path)
                os.environ['PATH'] = os.environ['PATH'] + ';' + path
                try:
                    import MMCorePy
                finally:
                    sys.path.pop()

            _mmc = MMCorePy.CMMCore()

    return _mmc

@@ -319,16 +319,562 @@
 amma'%0A%0A%0A
+class MMCWrapper:%0A    %22%22%22Wraps MMCorePy to raise more helpfule exceptions%0A    %22%22%22%0A    def __init__(self, mmc):%0A        self.__mmc = mmc%0A%0A    def __getattr__(self, name):%0A        attr = getattr(self.__mmc, name)%0A        if not callable(attr):%0A            return attr%0A%0A        def fn(*args, **kwds):%0A            try:%0A               return attr(*args, **kwds) %0A            except RuntimeError as exc:%0A                raise RuntimeError(exc.args%5B0%5D.getFullMsg() + %22 (calling mmc.%25s)%22%25name)%0A        fn.__name__ = name + '_wrapped'%0A        return fn%0A%0A%0A
 def getM
@@ -1100,16 +1100,27 @@
  _mmc = 
+MMCWrapper(
 pymmcore
@@ -1129,16 +1129,17 @@
 MMCore()
+)
 %0A       

raise more helpful error messages from micromanager