Skip to content

Commit

Permalink
extend doImport to allow member func
Browse files Browse the repository at this point in the history
  • Loading branch information
n8pease committed Sep 27, 2016
1 parent 317d727 commit 84db7e9
Showing 1 changed file with 17 additions and 8 deletions.
25 changes: 17 additions & 8 deletions python/lsst/daf/persistence/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,13 +103,22 @@ def setify(x):

def doImport(pythonType):
"""Import a python object given an importable string"""
if not isinstance(pythonType, basestring):
raise TypeError("Unhandled type of pythonType, val:%s" % pythonType)
# import this pythonType dynamically
try:
if not isinstance(pythonType, basestring):
raise TypeError("Unhandled type of pythonType, val:%s" % pythonType)
# import this pythonType dynamically
pythonTypeTokenList = pythonType.split('.')
importClassString = pythonTypeTokenList.pop()
importClassString = importClassString.strip()
importPackage = ".".join(pythonTypeTokenList)
importType = __import__(importPackage, globals(), locals(), [importClassString], 0)
pythonType = getattr(importType, importClassString)
return pythonType
except ImportError:
pass
# maybe python type is a member function, in the form: path.to.object.Class.funcname
pythonTypeTokenList = pythonType.split('.')
importClassString = pythonTypeTokenList.pop()
importClassString = importClassString.strip()
importPackage = ".".join(pythonTypeTokenList)
importType = __import__(importPackage, globals(), locals(), [importClassString], 0)
pythonType = getattr(importType, importClassString)
importClassString = '.'.join(pythonTypeTokenList[0:-1])
importedClass = doImport(importClassString)
pythonType = getattr(importedClass, pythonTypeTokenList[-1])
return pythonType

0 comments on commit 84db7e9

Please sign in to comment.