|
| 1 | +# SPDX-FileCopyrightText: 2021 Melissa LeBlanc-Williams for Adafruit Industries |
| 2 | +# |
| 3 | +# SPDX-License-Identifier: MIT |
| 4 | + |
| 5 | +""" |
| 6 | +`adafruit_blinka.importing` - Import utilities that run on Linux and MicroPython |
| 7 | +====================================================================================== |
| 8 | +
|
| 9 | +* Author(s): Melissa LeBlanc-Williams |
| 10 | +""" |
| 11 | + |
| 12 | + |
| 13 | +try: |
| 14 | + from importlib import import_module |
| 15 | +except ImportError as e: |
| 16 | + |
| 17 | + def import_module(module_name: str, package: str = None): |
| 18 | + """importlib not available, define an alternate import_module function""" |
| 19 | + package_list = [] |
| 20 | + if package is not None: |
| 21 | + package_list.append(package) |
| 22 | + return __import__(module_name, globals(), locals(), package_list) |
| 23 | + |
| 24 | + |
| 25 | +from adafruit_blinka.agnostic import detector |
| 26 | + |
| 27 | + |
| 28 | +def get_import_file(json_file_name, script_file_location): |
| 29 | + """Get the full path to the microcontroller imports file.""" |
| 30 | + try: |
| 31 | + from pathlib import Path |
| 32 | + |
| 33 | + script_folder = Path(script_file_location).parent.absolute() |
| 34 | + return script_folder / json_file_name |
| 35 | + except ImportError: |
| 36 | + # MicroPython doesn't have pathlib, so we have to do it manually |
| 37 | + if script_file_location.startswith("/"): |
| 38 | + script_folder = "/".join(script_file_location.split("/")[:-1]) |
| 39 | + else: |
| 40 | + script_folder = "." |
| 41 | + return f"{script_folder}/{json_file_name}" |
| 42 | + |
| 43 | + |
| 44 | +def import_mod(caller_globals, module_name: str, package_name: str = "*"): |
| 45 | + """Function to allow importing with * or specific package name.""" |
| 46 | + if package_name == "*": |
| 47 | + module = import_module(module_name) |
| 48 | + caller_globals.update( |
| 49 | + {name: getattr(module, name) for name in module.__all__} |
| 50 | + if hasattr(module, "__all__") |
| 51 | + else { |
| 52 | + key: value |
| 53 | + for (key, value) in module.__dict__.items() |
| 54 | + if not key.startswith("_") |
| 55 | + } |
| 56 | + ) |
| 57 | + else: |
| 58 | + module = import_module(module_name, package=package_name) |
| 59 | + caller_globals[package_name] = getattr(module, package_name) |
| 60 | + |
| 61 | + |
| 62 | +def import_microcontroller( |
| 63 | + caller_globals, |
| 64 | + microcontroller_imports, |
| 65 | + module_extension: str = "", |
| 66 | + package_name: str = "*", |
| 67 | +): |
| 68 | + """Detect and import the microcontroller module and package based on detected hardware""" |
| 69 | + if module_extension[0:1] != "." and module_extension != "": |
| 70 | + # Make sure the module extension starts with a dot if it's not empty |
| 71 | + module_extension = f".{module_extension}" |
| 72 | + for chip_key, chip_module in microcontroller_imports.items(): |
| 73 | + if getattr(detector.chip, chip_key): |
| 74 | + if isinstance(chip_module, dict): |
| 75 | + # Loop through the children and import the first one that matches |
| 76 | + for board_key, board_chip_module in chip_module.items(): |
| 77 | + if board_key.startswith("any_") and getattr( |
| 78 | + detector.board, board_key |
| 79 | + ): |
| 80 | + # import Pin from the microcontroller module |
| 81 | + import_mod( |
| 82 | + caller_globals, |
| 83 | + f"{board_chip_module}{module_extension}", |
| 84 | + package_name, |
| 85 | + ) |
| 86 | + return True |
| 87 | + if board_key == getattr(detector.board, board_key): |
| 88 | + print(f"Detected board: {board_key}") |
| 89 | + import_mod( |
| 90 | + caller_globals, |
| 91 | + f"{board_chip_module}{module_extension}", |
| 92 | + package_name, |
| 93 | + ) |
| 94 | + return True |
| 95 | + import_mod( |
| 96 | + caller_globals, |
| 97 | + f"{chip_module['default']}{module_extension}", |
| 98 | + package_name, |
| 99 | + ) |
| 100 | + return True |
| 101 | + import_mod(caller_globals, f"{chip_module}{module_extension}", package_name) |
| 102 | + return True |
| 103 | + return False |
0 commit comments