Repository navigation
Multi-file projects (Importing modules, take 3) - #37
Merged
Merged
Conversation
laurensvalk
force-pushed
the
multifile-3
branch
from
August 9, 2022 19:32
1de83cb to
3c81025
Compare
Member
Author
|
This shouldn't be merged yet, but it is meant to facilitate testing of pybricks/pybricks-micropython#106 |
This was referenced Aug 9, 2022
This was referenced Aug 23, 2022
laurensvalk
force-pushed
the
multifile-3
branch
from
August 26, 2022 19:03
3c81025 to
50bbf8b
Compare
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
force-pushed
the
multifile-3
branch
from
August 26, 2022 19:25
50bbf8b to
d191686
Compare
Member
Author
|
Updated to match pybricks/pybricks-micropython#115. Support for this is considered experimental and subject to change. |
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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: