Commits

Aleš Erjavec committed 26ecc3a

Refactored utility functions.

  • Participants
  • Parent commits aa4629f

Comments (0)

Files changed (8)

Orange/OrangeCanvas/preview/previewbrowser.py

 
 from PyQt4.QtCore import pyqtSignal as Signal
 
-from ..scheme.utils import check_type
+from ..utils import check_type
 from ..gui.dropshadow import DropShadowFrame
 from . import previewmodel
 

Orange/OrangeCanvas/scheme/__init__.py

 )
 
 from .errors import *
-
-from . import utils

Orange/OrangeCanvas/scheme/annotations.py

 from PyQt4.QtCore import pyqtSignal as Signal
 from PyQt4.QtCore import pyqtProperty as Property
 
-from .utils import check_type
+from ..utils import check_type
 
 
 class BaseSchemeAnnotation(QObject):

Orange/OrangeCanvas/scheme/link.py

 from PyQt4.QtCore import pyqtSignal as Signal
 from PyQt4.QtCore import pyqtProperty as Property
 
-from .utils import name_lookup
+from ..utils import name_lookup
 from .errors import IncompatibleChannelTypeError
 
 

Orange/OrangeCanvas/scheme/scheme.py

 from .link import SchemeLink, compatible_channels
 from .annotations import BaseSchemeAnnotation
 
-from .utils import check_arg, check_type
+from ..utils import check_arg, check_type
 
 from .errors import (
     SchemeCycleError, IncompatibleChannelTypeError, SinkChannelError,

Orange/OrangeCanvas/scheme/utils.py

-"""
-Utility functions.
-
-"""
-
-
-def name_lookup(qualified_name, globals={}):
-    """
-    Return the object referenced by a qualified name (doted name).
-    """
-    module_name, class_name = qualified_name.rsplit(".", 1)
-    module = __import__(module_name, fromlist=[class_name], globals=globals)
-    return getattr(module, class_name)
-
-
-def qualified_name(qualified_object):
-    """
-    Return a qualified name for `qualified_obj` (type or function).
-    """
-    return "%s.%s" % (qualified_object.__module__, qualified_object.__name__)
-
-
-def check_type(obj, type_or_tuple):
-    if not isinstance(obj, type_or_tuple):
-        raise TypeError("Expected %r. Got %r" % (type_or_tuple, type(obj)))
-
-
-def check_subclass(cls, class_or_tuple):
-    if not issubclass(cls, class_or_tuple):
-        raise TypeError("Expected %r. Got %r" % (class_or_tuple, type(cls)))
-
-
-def check_arg(pred, value):
-    if not pred:
-        raise ValueError(value)

Orange/OrangeCanvas/scheme/widgetsscheme.py

 from .signalmanager import SignalManager, compress_signals, can_enable_dynamic
 from .scheme import Scheme, SchemeNode
 from .node import UserMessage
-from .utils import name_lookup, check_arg, check_type
+from ..utils import name_lookup, check_arg, check_type
 from ..resources import icon_loader
 from ..config import rc
 

Orange/OrangeCanvas/utils/__init__.py

-from .qtcompat import sip_getapi, toPyObject
+from .qtcompat import sip_getapi, toPyObject
+
+
+def dotted_getattr(obj, name):
+    """
+    `getattr` like function accepting a dotted name for attribute lookup.
+    """
+    return reduce(getattr, name.split("."), obj)
+
+
+def qualified_name(obj):
+    """
+    Return a qualified name for `obj` (type or function).
+    """
+    if obj.__name__ == "__builtin__":
+        return obj.__name__
+    else:
+        return "%s.%s" % (obj.__module__, obj.__name__)
+
+
+def name_lookup(qualified_name):
+    """
+    Return the object referenced by a qualified name (dotted name).
+    """
+    if "." not in qualified_name:
+        qualified_name = "__builtin__." + qualified_name
+
+    module_name, class_name = qualified_name.rsplit(".", 1)
+    module = __import__(module_name, fromlist=[class_name])
+    return getattr(module, class_name)
+
+
+def asmodule(module):
+    """
+    Return the :class:`module` instance named by `module`.
+
+    If `module` is already a module instance and not a string, return
+    it unchanged.
+
+    """
+    if isinstance(module, str):
+        module = __import__(module, fromlist=[])
+    return module
+
+
+def check_type(obj, type_or_tuple):
+    if not isinstance(obj, type_or_tuple):
+        raise TypeError("Expected %r. Got %r" % (type_or_tuple, type(obj)))
+
+
+def check_subclass(cls, class_or_tuple):
+    if not issubclass(cls, class_or_tuple):
+        raise TypeError("Expected %r. Got %r" % (class_or_tuple, type(cls)))
+
+
+def check_arg(pred, value):
+    if not pred:
+        raise ValueError(value)