Skip to content

Commit

Permalink
tools/makemanifest.py: Allow passing option args to include().
Browse files Browse the repository at this point in the history
This allows customising which features can be enabled in a frozen library.

e.g. `include("path.py", extra_features=True)`

in path.py:

    options.defaults(standard_features=True)

    if options.standard_features:
        # freeze standard modules.
    if options.extra_features:
        # freeze extra modules.

Signed-off-by: Jim Mussared <[email protected]>
  • Loading branch information
jimmo authored and dpgeorge committed Feb 16, 2021
1 parent 83d2305 commit 5660200
Showing 1 changed file with 28 additions and 2 deletions.
30 changes: 28 additions & 2 deletions tools/makemanifest.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,27 @@
# Public functions to be used in the manifest


def include(manifest):
def include(manifest, **kwargs):
"""Include another manifest.
The manifest argument can be a string (filename) or an iterable of
strings.
Relative paths are resolved with respect to the current manifest file.
Optional kwargs can be provided which will be available to the
included script via the `options` variable.
e.g. include("path.py", extra_features=True)
in path.py:
options.defaults(standard_features=True)
# freeze minimal modules.
if options.standard_features:
# freeze standard modules.
if options.extra_features:
# freeze extra modules.
"""

if not isinstance(manifest, str):
Expand All @@ -53,7 +67,7 @@ def include(manifest):
# Applies to includes and input files.
prev_cwd = os.getcwd()
os.chdir(os.path.dirname(manifest))
exec(f.read())
exec(f.read(), globals(), {"options": IncludeOptions(**kwargs)})
os.chdir(prev_cwd)


Expand Down Expand Up @@ -125,6 +139,18 @@ def freeze_mpy(path, script=None, opt=0):
manifest_list = []


class IncludeOptions:
def __init__(self, **kwargs):
self._kwargs = kwargs
self._defaults = {}

def defaults(self, **kwargs):
self._defaults = kwargs

def __getattr__(self, name):
return self._kwargs.get(name, self._defaults.get(name, None))


class FreezeError(Exception):
pass

Expand Down

0 comments on commit 5660200

Please sign in to comment.