from __future__ import print_function
from collections import OrderedDict
from importlib import import_module
from . import Device
from ..util.debug import printExc


def getDeviceClass(name):
    """Return a device class given its name.

    The class must have been defined already, or it must be importable from ``acq4.devices.name``.
    """
    devClasses = getDeviceClasses()

    # If we don't recognize the class name, try importing from builtin devices
    # Note: eventually it would be nice if all device classes can be safely/cheaply
    # imported at startup, rather than dynamically importing them.
    if name not in devClasses:
        try:
            import_module('acq4.devices.' + name)
            devClasses = getDeviceClasses()
        except ImportError as exc:
            print("Warning: error importing device class %s: %s" % (name, str(exc)))

    try:
        return devClasses[name]
    except KeyError:
        raise KeyError('No registered device class named "%s"' % name)


def getDeviceClasses():
    """Return a dict containing name:class pairs for all defined Device subclasses.
    """
    devClasses = OrderedDict()

    # recursively find all imported Device subclasses
    subclasses = [Device.Device]
    for cls in subclasses:
        subclasses.extend(cls.__subclasses__())
        if cls is Device.Device:
            continue
        devClasses[cls.__name__] = cls
    
    return devClasses

@@ -31,16 +31,17 @@
 unction%0A
+%0A
 from col
@@ -104,16 +104,17 @@
 _module%0A
+%0A
 from . i
@@ -130,42 +130,8 @@
 ice%0A
-from ..util.debug import printExc%0A
 %0A%0Ade
@@ -834,32 +834,70 @@
 xc)))%0A%0A    try:%0A
+        clsName = name.split(%22.%22)%5B-1%5D%0A
         return d
@@ -906,17 +906,20 @@
 Classes%5B
-n
+clsN
 ame%5D%0A   
@@ -1412,20 +1412,16 @@
 %5D = cls%0A
-    
 %0A    ret

allow for device names to include subdirs by pruning the name for its class