Description
The idea is to simplify the API of MetaCall in order to make it natural for high level languages. So if we have a NodeJS module like:
asd.js
function hello() {
console.log("hello world");
}
module.exports = {
hello
};
We should be able to import it from Python like this (if the script is being executed by metacallcli):
import asd
asd.hello();
Or like this (if the script is being executed by python interpreter):
import metacall # Monkey Patch import
import asd
asd.hello();
To achieve this, we must implement the monkey patch mechanism into Python Port, and force all loaders to insert their respective ports into the loaded scripts. Forcing the loaders to import the ports at the beginning is an extra for removing the need of having metacall dependency in the requirements.txt (or equivalent).
The priority of this issue is to improve Python Port API with syntax sugar and make it transparent.
Here is a tutorial for hooking import in python, the tool we should use to implement the feature: https://pymotw.com/3/sys/imports.html .
The import class should search for the scripts in the folder obtained by the environment variable LOADER_SCRIPTS_PATH.
There is a problem related to python, it is impossible to specify the extension of the module to be loaded, so the ideal would be to have a mapping from file extensions to loaders:
{
[js, node]: node, // this one should be used instead of js loader (v8) by default if both are loaded
[py]: py,
[vb, cs]: cs,
[js]: js,
[rb]: rb,
}
And the import mechanism should search for each file + extension, and if it exists, try to load it with the loader it belongs to.
The mechanism should be implemented here: https://github.com/metacall/core/blob/develop/source/ports/py_port/package/metacall/__init__.py