forked from adafruit/Adafruit_Blinka
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimporting.py
More file actions
55 lines (46 loc) · 1.95 KB
/
importing.py
File metadata and controls
55 lines (46 loc) · 1.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# SPDX-FileCopyrightText: 2021 Melissa LeBlanc-Williams for Adafruit Industries
#
# SPDX-License-Identifier: MIT
"""
`adafruit_blinka.importing` - Import utilities that run on Linux and MicroPython
======================================================================================
* Author(s): Melissa LeBlanc-Williams
"""
try:
from importlib import import_module
except ImportError as e:
def import_module(module_name: str, package: str = None):
"""importlib not available, define an alternate import_module function"""
package_list = []
if package is not None:
package_list.append(package)
return __import__(module_name, globals(), locals(), package_list)
def get_import_file(json_file_name, script_file_location):
"""Get the full path to the microcontroller imports file."""
try:
from pathlib import Path
script_folder = Path(script_file_location).parent.absolute()
return script_folder / json_file_name
except ImportError:
# MicroPython doesn't have pathlib, so we have to do it manually
if script_file_location.startswith("/"):
script_folder = "/".join(script_file_location.split("/")[:-1])
else:
script_folder = "."
return f"{script_folder}/{json_file_name}"
def import_mod(caller_globals, module_name: str, package_name: str = "*"):
"""Function to allow importing with * or specific package name."""
if package_name == "*":
module = import_module(module_name)
caller_globals.update(
{name: getattr(module, name) for name in module.__all__}
if hasattr(module, "__all__")
else {
key: value
for (key, value) in module.__dict__.items()
if not key.startswith("_")
}
)
else:
module = import_module(module_name, package=package_name)
caller_globals[package_name] = getattr(module, package_name)