|
| 1 | +#! /usr/bin/env python3 |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | +# vim: set et sw=4 fenc=utf-8: |
| 4 | +# |
| 5 | +# wrapper.py |
| 6 | + |
| 7 | +""" |
| 8 | +This script is a revised version for calling Node.js to execute |
| 9 | +JavaScript using a wrapper to load the secrets.js package. |
| 10 | +This version handles multiple commands with a list, encodes the |
| 11 | +list to base36 for the CLI, and has improved error handling. |
| 12 | +""" |
| 13 | + |
| 14 | +import json |
| 15 | +import os |
| 16 | +import subprocess |
| 17 | +import warnings |
| 18 | + |
| 19 | +# Path to the Node.js wrapper script |
| 20 | +# JS_FILE_PATH = "./javascript/wrapper.js" |
| 21 | + |
| 22 | + |
| 23 | +def wrapper(input_data): |
| 24 | + """ |
| 25 | + Run a JavaScript function using the Node.js wrapper. |
| 26 | +
|
| 27 | + Args: |
| 28 | + input_data [list]: List of functions with arguments. |
| 29 | +
|
| 30 | + Returns: |
| 31 | + The result of the JavaScript function or None if there is an error. |
| 32 | + """ |
| 33 | + |
| 34 | + # Get the directory where this Python script is located |
| 35 | + script_directory = os.path.dirname(os.path.realpath(__file__)) |
| 36 | + |
| 37 | + # Change the current working directory to the directory |
| 38 | + # containing the wrapper.js file |
| 39 | + os.chdir(os.path.join(script_directory, "..", "javascript")) |
| 40 | + |
| 41 | + # Call the wrapper |
| 42 | + js_command = ["node", "wrapper.js", input_data] |
| 43 | + |
| 44 | + try: |
| 45 | + # Run the command and capture the output and stderr |
| 46 | + result = subprocess.run( |
| 47 | + js_command, |
| 48 | + stdout=subprocess.PIPE, |
| 49 | + stderr=subprocess.PIPE, |
| 50 | + text=True, |
| 51 | + check=True, |
| 52 | + ) |
| 53 | + |
| 54 | + # Debugging statements to print stdout and stderr |
| 55 | + # print("Input data sent to JavaScript:", input_data) |
| 56 | + # print("stdout:", result.stdout) |
| 57 | + # print("stderr:", result.stderr) |
| 58 | + |
| 59 | + try: |
| 60 | + # Attempt to load the entire stdout as JSON |
| 61 | + js_result = json.loads(result.stdout) |
| 62 | + # print('json.loads') |
| 63 | + |
| 64 | + # Debugging stderr if it exists |
| 65 | + # if result.stderr: |
| 66 | + # print("JavaScript stderr:", result.stderr) |
| 67 | + |
| 68 | + return js_result |
| 69 | + |
| 70 | + except json.JSONDecodeError as e: |
| 71 | + # print("Python error decoding JSON:", e) |
| 72 | + # print("Raw stdout content:", result.stdout) |
| 73 | + warning_message = "Python error decoding JSON: " + str(e) |
| 74 | + warnings.warn(warning_message, category=Warning) |
| 75 | + return None |
| 76 | + |
| 77 | + except FileNotFoundError: |
| 78 | + # Handle the case where Node.js is not installed |
| 79 | + print( |
| 80 | + "Error: Node.js is required. Please install Node.js to continue." |
| 81 | + ) # pragma: no cover |
| 82 | + exit(1) # pragma: no cover |
| 83 | + except subprocess.CalledProcessError as e: |
| 84 | + # Print the error from the JavaScript script |
| 85 | + js_error = e.stderr.strip() # Use e.stderr instead of result.stderr |
| 86 | + # print("JavaScript error:", js_error) |
| 87 | + warning_message = "JavaScript error: " + js_error |
| 88 | + warnings.warn(warning_message, category=Warning) |
| 89 | + return None |
0 commit comments