Skip to content
This repository was archived by the owner on Aug 25, 2024. It is now read-only.

Commit 0be7ea7

Browse files
committed
util: testing: manifest: shim: docs: console examples: Show how to load modules remotely on the fly
Signed-off-by: John Andersen <[email protected]>
1 parent c626cb2 commit 0be7ea7

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

dffml/util/testing/manifest/shim.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -748,8 +748,18 @@ def shim(
748748
:test:
749749
:filepath: my_shim_setup.py
750750
751+
import pathlib
752+
import tempfile
753+
import zipimport
754+
import urllib.request
755+
751756
import shim
752757
758+
# For the sake of the example assume you are unable to preinstall
759+
# anything into the environment the shim run in (common reason why we
760+
# use a shim).
761+
PYYAML_URL: str = "https://files.pythonhosted.org/packages/eb/5f/6e6fe6904e1a9c67bc2ca5629a69e7a5a0b17f079da838bab98a1e548b25/PyYAML-6.0-cp37-cp37m-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl"
762+
753763
def setup_shim_func(parsers, next_phase_parsers, **kwargs):
754764
# Declare another parser
755765
parser = shim.ManifestFormatParser(
@@ -762,6 +772,21 @@ def setup_shim_func(parsers, next_phase_parsers, **kwargs):
762772
# Add the parser
763773
next_phase_parsers[(parser.format, parser.version, parser.name)] = parser
764774
775+
# Create a temporary directory to hold the pi
776+
with tempfile.TemporaryDirectory() as tempdir:
777+
# Path to wheel on disk
778+
wheel_path = pathlib.Path(tempdir, "package.whl")
779+
# Download the wheel
780+
with urllib.request.urlopen(PYYAML_URL) as response:
781+
wheel_path.write_bytes(response.read())
782+
# You'll need to change the wheel for this code to work
783+
if True:
784+
return
785+
# Load the module from the downloaded wheel
786+
yaml = zipimport.zipimporter(str(wheel_path)).load_module("yaml")
787+
# Setup the parser for use by the shim
788+
parsers["yaml"] = shim.decode_if_bytes(yaml.safe_load)
789+
765790
.. code-block:: console
766791
:test:
767792

0 commit comments

Comments
 (0)