mirror of
https://github.com/Mikaela/Limnoria.git
synced 2025-02-17 06:00:42 +01:00
Make universalImport support 'from ModuleA import ModuleB'
Signed-off-by: James Vega <jamessan@users.sourceforge.net>
This commit is contained in:
parent
ed32faabe1
commit
ecd2c63650
@ -1,5 +1,6 @@
|
|||||||
###
|
###
|
||||||
# Copyright (c) 2005, Jeremiah Fincher
|
# Copyright (c) 2005, Jeremiah Fincher
|
||||||
|
# Copyright (c) 2009, James Vega
|
||||||
# All rights reserved.
|
# All rights reserved.
|
||||||
#
|
#
|
||||||
# Redistribution and use in source and binary forms, with or without
|
# Redistribution and use in source and binary forms, with or without
|
||||||
@ -32,6 +33,10 @@ import types
|
|||||||
import threading
|
import threading
|
||||||
|
|
||||||
def universalImport(*names):
|
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)
|
f = sys._getframe(1)
|
||||||
for name in names:
|
for name in names:
|
||||||
try:
|
try:
|
||||||
@ -39,6 +44,11 @@ def universalImport(*names):
|
|||||||
except ImportError:
|
except ImportError:
|
||||||
continue
|
continue
|
||||||
else:
|
else:
|
||||||
|
if '.' in name:
|
||||||
|
parts = name.split('.')[1:]
|
||||||
|
while parts:
|
||||||
|
ret = getattr(ret, parts[0])
|
||||||
|
del parts[0]
|
||||||
return ret
|
return ret
|
||||||
raise ImportError, ','.join(names)
|
raise ImportError, ','.join(names)
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user