From ecd2c63650ed37e412f4bc57e9f7bfd3309590d8 Mon Sep 17 00:00:00 2001 From: James Vega Date: Wed, 11 Mar 2009 15:55:08 -0400 Subject: [PATCH] Make universalImport support 'from ModuleA import ModuleB' Signed-off-by: James Vega --- src/utils/python.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/utils/python.py b/src/utils/python.py index 6a0125fe9..70571fe0b 100644 --- a/src/utils/python.py +++ b/src/utils/python.py @@ -1,5 +1,6 @@ ### # Copyright (c) 2005, Jeremiah Fincher +# Copyright (c) 2009, James Vega # All rights reserved. # # Redistribution and use in source and binary forms, with or without @@ -32,6 +33,10 @@ import types import threading def universalImport(*names): + """Attempt to import the given modules, in order, returning the first + successfully imported module. ImportError will be raised, as usual, if + no imports succeed. To emulate ``from ModuleA import ModuleB'', pass the + string 'ModuleA.ModuleB'""" f = sys._getframe(1) for name in names: try: @@ -39,6 +44,11 @@ def universalImport(*names): except ImportError: continue else: + if '.' in name: + parts = name.split('.')[1:] + while parts: + ret = getattr(ret, parts[0]) + del parts[0] return ret raise ImportError, ','.join(names)