Skip to content

Multi-file projects (Importing modules, take 3) - #37

Merged
laurensvalk merged 1 commit into
masterfrom
multifile-3
Aug 26, 2022
Merged

laurensvalk merged 1 commit into
masterfrom
multifile-3

Conversation

@laurensvalk

Copy link
Copy Markdown
Member

This makes use of recent experimental firmware changes (pybricks/pybricks-micropython#106) to allow the main script to import modules in the same folder. The modules can also import other modules. To ensure a smooth transition until a firmware with this feature is released, scripts without local imports will be downloaded in the old format (a single raw mpy data blob).

Unlike in #33, there is no special filtering done on the modules this time. They are imported as real modules, and only when the import statements are run.

Quick example of what works:

image

@laurensvalk

Copy link
Copy Markdown
Member Author

This shouldn't be merged yet, but it is meant to facilitate testing of pybricks/pybricks-micropython#106

This makes use of recent firmware changes to allow the main script to
import modules in the same folder. The modules can also import other
modules.

To ensure a smooth transition until a firmware with this feature is
released, scripts without local imports will be downloaded in the old
format (a single raw mpy data blob).
@laurensvalk

Copy link
Copy Markdown
Member Author

Updated to match pybricks/pybricks-micropython#115.

Support for this is considered experimental and subject to change.

@laurensvalk
laurensvalk merged commit 22e1d38 into master Aug 26, 2022
@laurensvalk
laurensvalk deleted the multifile-3 branch August 26, 2022 19:27
@laurensvalk

Copy link
Copy Markdown
Member Author

This could be extended with something like the following to allow relative or even "package" imports:

     # Find all dependencies recursively
     def find_dependencies(module_name):
         try:
-            with open(source_dir / (module_name + ".py")) as source:
+            path = source_dir / Path(*module_name.split(".")).with_suffix(".py")
+            with open(path) as source:
                 # Search non-recursively through current module
                 local_dependencies = set()
                 for line in source:
@@ -142,8 +141,8 @@ async def compile_multi_file(
                     elif result := re.search("import (.*)", line):
                         local_dependencies.add(result.group(1))
 
-                # If each file that wasn't already done, add it and find its
-                # dependencies too.
+                # Add each file that wasn't already done, and find its
+                # dependencies.
                 for dep in local_dependencies.difference(dependencies):
                     if dep not in dependencies:
                         dependencies.add(dep)
@@ -174,7 +173,9 @@ async def compile_multi_file(
     blob = bytearray([])
     for module in modules:
         name = module.encode() + b"\x00"
-        mpy = await compile_file(source_dir / (module + ".py"), abi)
+        mpy = await compile_file(
+            source_dir / Path(*module.split(".")).with_suffix(".py"), abi
+        )
         size = len(mpy).to_bytes(4, "little")
         blob += size + name + mpy
     return blob

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant