|
| 1 | +#!/usr/bin/env python |
| 2 | +# |
| 3 | +# import_tools.py |
| 4 | +""" |
| 5 | +Functions for importing classes. |
| 6 | +""" |
| 7 | +# |
| 8 | +# Copyright © 2020 Dominic Davis-Foster <[email protected]> |
| 9 | +# |
| 10 | +# This program is free software; you can redistribute it and/or modify |
| 11 | +# it under the terms of the GNU Lesser General Public License as published by |
| 12 | +# the Free Software Foundation; either version 3 of the License, or |
| 13 | +# (at your option) any later version. |
| 14 | +# |
| 15 | +# This program is distributed in the hope that it will be useful, |
| 16 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | +# GNU Lesser General Public License for more details. |
| 19 | +# |
| 20 | +# You should have received a copy of the GNU Lesser General Public License |
| 21 | +# along with this program; if not, write to the Free Software |
| 22 | +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
| 23 | +# MA 02110-1301, USA. |
| 24 | +# |
| 25 | +# Based on https://github.com/asottile/git-code-debt/blob/master/git_code_debt/util/discovery.py |
| 26 | +# Copyright (c) 2014 Anthony Sottile |
| 27 | +# Licensed under the MIT License |
| 28 | +# | Permission is hereby granted, free of charge, to any person obtaining a copy |
| 29 | +# | of this software and associated documentation files (the "Software"), to deal |
| 30 | +# | in the Software without restriction, including without limitation the rights |
| 31 | +# | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 32 | +# | copies of the Software, and to permit persons to whom the Software is |
| 33 | +# | furnished to do so, subject to the following conditions: |
| 34 | +# | |
| 35 | +# | The above copyright notice and this permission notice shall be included in |
| 36 | +# | all copies or substantial portions of the Software. |
| 37 | +# | |
| 38 | +# | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 39 | +# | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 40 | +# | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 41 | +# | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 42 | +# | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 43 | +# | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 44 | +# | THE SOFTWARE. |
| 45 | +# |
| 46 | + |
| 47 | +# stdlib |
| 48 | +import inspect |
| 49 | +import pkgutil |
| 50 | +from types import ModuleType |
| 51 | +from typing import Any, Callable, List, Optional, Type |
| 52 | + |
| 53 | + |
| 54 | +def discover( |
| 55 | + package: ModuleType, |
| 56 | + match_func: Optional[Callable[[Any], bool]] = None, |
| 57 | + ) -> List[Type[Any]]: |
| 58 | + """ |
| 59 | + Returns a set of objects in the directory matched by match_func |
| 60 | +
|
| 61 | + :param package: A Python package |
| 62 | + :param match_func: Function taking an object and returning true if the object is to be included in the output. |
| 63 | +
|
| 64 | + :return: |
| 65 | + :rtype: |
| 66 | + """ |
| 67 | + |
| 68 | + matched_classes = list() |
| 69 | + |
| 70 | + for _, module_name, _ in pkgutil.walk_packages( |
| 71 | + # https://github.com/python/mypy/issues/1422 |
| 72 | + # Stalled PRs: https://github.com/python/mypy/pull/3527 |
| 73 | + # https://github.com/python/mypy/pull/5212 |
| 74 | + package.__path__, # type: ignore |
| 75 | + prefix=package.__name__ + '.', |
| 76 | + ): |
| 77 | + module = __import__(module_name, fromlist=['__trash'], level=0) |
| 78 | + |
| 79 | + # Check all the functions in that module |
| 80 | + for _, imported_objects in inspect.getmembers(module, match_func): |
| 81 | + if not hasattr(imported_objects, "__module__"): |
| 82 | + continue |
| 83 | + |
| 84 | + # Don't include things that are only there due to a side effect of importing |
| 85 | + if imported_objects.__module__ != module.__name__: |
| 86 | + continue |
| 87 | + |
| 88 | + matched_classes.append(imported_objects) |
| 89 | + |
| 90 | + return matched_classes |
0 commit comments