Skip to content

Commit

Permalink
Merge pull request #63 from lsst/tickets/DM-15871
Browse files Browse the repository at this point in the history
DM-15871: add smart doImport function from daf_butler
  • Loading branch information
TallJimbo committed Sep 27, 2018
2 parents bca2faa + 32ecc7e commit 5cf5345
Show file tree
Hide file tree
Showing 3 changed files with 156 additions and 0 deletions.
1 change: 1 addition & 0 deletions python/lsst/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
from .demangle import *
from .utils import *
from .get_caller_name import *
from .doImport import *
from .wrappers import *
from .python import *
from .version import *
88 changes: 88 additions & 0 deletions python/lsst/utils/doImport.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
# This file is part of utils.
#
# Developed for the LSST Data Management System.
# This product includes software developed by the LSST Project
# (http://www.lsst.org).
# See the COPYRIGHT file at the top-level directory of this distribution
# for details of code ownership.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.


import importlib

__all__ = ("doImport",)


def doImport(importable):
"""Import a python object given an importable string and return it.
Parameters
----------
importable : `str`
String containing dot-separated path of a Python class, module,
or member function.
Returns
-------
type : `type`
Type object. Either a module or class or a function.
Raises
------
TypeError
``importable`` is not a `str`.
ModuleNotFoundError
No module in the supplied import string could be found.
ImportError
``importable`` is found but can not be imported or the requested
item could not be retrieved from the imported module.
"""
if not isinstance(importable, str):
raise TypeError(f"Unhandled type of importable, val: {importable}")

def tryImport(module, fromlist):
pytype = importlib.import_module(module)
# Can have functions inside classes inside modules
for f in fromlist:
try:
pytype = getattr(pytype, f)
except AttributeError as e:
raise ImportError(f"Could not get attribute '{f}' from '{module}'")
return pytype

# Go through the import path attempting to load the module
# and retrieve the class or function as an attribute. Shift components
# from the module list to the attribute list until something works.
moduleComponents = importable.split(".")
infileComponents = []

while moduleComponents:
try:
pytype = tryImport(".".join(moduleComponents), infileComponents)
if not infileComponents and hasattr(pytype, moduleComponents[-1]):
# This module has an attribute with the same name as the
# module itself (like doImport.doImport, actually!).
# If that attribute was lifted to the package, we should
# return the attribute, not the module.
try:
return tryImport(".".join(moduleComponents[:-1]), moduleComponents[-1:])
except ModuleNotFoundError:
pass
return pytype
except ModuleNotFoundError:
# Move element from module to file and try again
infileComponents.insert(0, moduleComponents.pop())

raise ModuleNotFoundError(f"Unable to import {importable}")
67 changes: 67 additions & 0 deletions tests/test_doImport.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# This file is part of daf_butler.
#
# Developed for the LSST Data Management System.
# This product includes software developed by the LSST Project
# (http://www.lsst.org).
# See the COPYRIGHT file at the top-level directory of this distribution
# for details of code ownership.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

import unittest
import inspect

import lsst.utils.tests

from lsst.utils import doImport


class ImportTestCase(lsst.utils.tests.TestCase):
"""Basic tests of doImport."""

def testDoImport(self):
c = doImport("lsst.utils.tests.TestCase")
self.assertEqual(c, lsst.utils.tests.TestCase)

c = doImport("lsst.utils.doImport")
self.assertEqual(type(c), type(doImport))
self.assertTrue(inspect.isfunction(c))

c = doImport("lsst.utils")
self.assertTrue(inspect.ismodule(c))

with self.assertRaises(ImportError):
doImport("lsst.utils.tests.TestCase.xyprint")

with self.assertRaises(ImportError):
doImport("lsst.utils.nothere")

with self.assertRaises(ModuleNotFoundError):
doImport("missing module")

with self.assertRaises(ModuleNotFoundError):
doImport("lsstdummy.import.fail")

with self.assertRaises(ImportError):
doImport("lsst.import.fail")

with self.assertRaises(ImportError):
doImport("lsst.utils.x")

with self.assertRaises(TypeError):
doImport([])


if __name__ == "__main__":
unittest.main()

0 comments on commit 5cf5345

Please sign in to comment.