-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #63 from lsst/tickets/DM-15871
DM-15871: add smart doImport function from daf_butler
- Loading branch information
Showing
3 changed files
with
156 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |