paul@39 | 1 | #!/usr/bin/env python |
paul@39 | 2 | |
paul@39 | 3 | """ |
paul@39 | 4 | Import utilities. |
paul@39 | 5 | |
paul@85 | 6 | Copyright (C) 2017, 2018 Paul Boddie <paul@boddie.org.uk> |
paul@39 | 7 | |
paul@39 | 8 | This program is free software; you can redistribute it and/or modify it under |
paul@39 | 9 | the terms of the GNU General Public License as published by the Free Software |
paul@39 | 10 | Foundation; either version 3 of the License, or (at your option) any later |
paul@39 | 11 | version. |
paul@39 | 12 | |
paul@39 | 13 | This program is distributed in the hope that it will be useful, but WITHOUT |
paul@39 | 14 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paul@39 | 15 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
paul@39 | 16 | details. |
paul@39 | 17 | |
paul@39 | 18 | You should have received a copy of the GNU General Public License along with |
paul@39 | 19 | this program. If not, see <http://www.gnu.org/licenses/>. |
paul@39 | 20 | """ |
paul@39 | 21 | |
paul@109 | 22 | from os.path import isdir, join, split, splitext |
paul@39 | 23 | from os import listdir |
paul@39 | 24 | from importlib import import_module |
paul@39 | 25 | |
paul@109 | 26 | reserved = ["__init__", "common", "manifest"] |
paul@109 | 27 | |
paul@85 | 28 | def get_extensions(dirname, modname, stores, reserved, prefix=None): |
paul@39 | 29 | |
paul@39 | 30 | "Import extensions inside 'dirname'." |
paul@39 | 31 | |
paul@39 | 32 | for filename in listdir(dirname): |
paul@39 | 33 | pathname = join(dirname, filename) |
paul@39 | 34 | |
paul@85 | 35 | # Descend into directories, prefixing the identified modules. |
paul@39 | 36 | |
paul@39 | 37 | if isdir(pathname): |
paul@85 | 38 | store_name = prefix and "%s.%s" % (prefix, modname) or filename |
paul@39 | 39 | get_extensions(pathname, "%s.%s" % (modname, filename), |
paul@85 | 40 | stores, reserved, store_name) |
paul@39 | 41 | continue |
paul@39 | 42 | |
paul@39 | 43 | # Identify modules and import them. |
paul@39 | 44 | |
paul@39 | 45 | leafname, ext = splitext(filename) |
paul@39 | 46 | |
paul@39 | 47 | if ext == ".py" and leafname not in reserved: |
paul@85 | 48 | store_name = prefix and "%s.%s" % (prefix, leafname) or leafname |
paul@85 | 49 | stores[store_name] = import_module("%s.%s" % (modname, leafname)) |
paul@39 | 50 | |
paul@109 | 51 | def get_mapping(modules, get_key, get_value): |
paul@109 | 52 | |
paul@109 | 53 | """ |
paul@109 | 54 | Using the 'modules' mapping, employ 'get_key' and 'get_value' to register |
paul@109 | 55 | objects provided by the modules in a mapping. |
paul@109 | 56 | """ |
paul@109 | 57 | |
paul@109 | 58 | mapping = {} |
paul@109 | 59 | |
paul@109 | 60 | # Use the callables to obtain the keys and values from modules. |
paul@109 | 61 | |
paul@109 | 62 | for name, module in modules.items(): |
paul@109 | 63 | mapping[get_key(name, module)] = get_value(module) |
paul@109 | 64 | |
paul@109 | 65 | return mapping |
paul@109 | 66 | |
paul@109 | 67 | def get_modules(module_file, module_name): |
paul@109 | 68 | |
paul@109 | 69 | """ |
paul@109 | 70 | Obtain details of the indicated module's package using 'module_file', |
paul@109 | 71 | corresponding to a __file__ attribute, and 'module_name', corresponding to |
paul@109 | 72 | a __name__ attribute. |
paul@109 | 73 | """ |
paul@109 | 74 | |
paul@109 | 75 | dirname = split(module_file)[0] |
paul@109 | 76 | package = module_name.rsplit(".", 1)[0] |
paul@109 | 77 | |
paul@109 | 78 | # Define an attribute mapping names to modules. |
paul@109 | 79 | |
paul@109 | 80 | modules = {} |
paul@109 | 81 | get_extensions(dirname, package, modules, reserved) |
paul@109 | 82 | return modules |
paul@109 | 83 | |
paul@39 | 84 | # vim: tabstop=4 expandtab shiftwidth=4 |