Make universalImport support 'from ModuleA import ModuleB'

Signed-off-by: James Vega <jamessan@users.sourceforge.net>
This commit is contained in:
James Vega 2009-03-11 15:55:08 -04:00
parent ed32faabe1
commit ecd2c63650
1 changed files with 10 additions and 0 deletions

View File

@ -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)