PiotrDabkowski / Js2Py

JavaScript to Python Translator & JavaScript interpreter written in 100% pure Python🚀 Try it online:
http://piter.io/projects/js2py
MIT License
2.45k stars 259 forks source link

Trouble with require a module name with a `@`, `.require('@tryfabric/martian')` #298

Open booox opened 1 year ago

booox commented 1 year ago

Hi, I'm encountering trouble when trying to require a module with a @

envs

OS: Windows 10 Python3.11 js2py installed with pip install js2py

Tries

Try docs examples, it's OK

CryptoJS = js2py.require('crypto-js')
data = [{'id': 1}, {'id': 2}]
JSON = js2py.eval_js('JSON')

ciphertext = CryptoJS.AES.encrypt(JSON.stringify(data), 'secret key 123')
bytes  = CryptoJS.AES.decrypt(ciphertext.toString(), 'secret key 123')
decryptedData = JSON.parse(bytes.toString(CryptoJS.enc.Utf8)).to_list()
print(decryptedData)

Try require @tryfabric/martian

Martian = js2py.require('@tryfabric/martian')

Traceback (most recent call last):
  File "...\test-js2py-with-martian.py", line 21, in <module>
    Martian = js2py.require('@tryfabric/martian')
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\ProgramData\Python311\Lib\site-packages\js2py\node_import.py", line 155, in require
    py_code = _get_and_translate_npm_module(module_name, include_polyfill=include_polyfill, update=update,
              ^^^^^^^^^^^^
  File "D:\ProgramData\Python311\Lib\site-packages\js2py\node_import.py", line 74, in _get_and_translate_npm_module
    var_name = _get_module_var_name(module_name)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\ProgramData\Python311\Lib\site-packages\js2py\node_import.py", line 63, in _get_module_var_name
    raise ValueError(
ValueError: Invalid Python module name '' (generated from ''). Unsupported/invalid npm module specification?

Try replace node_import.py with @hellmolt 's code

code link

And replaced pkg_name with module_name in line 169-173

        assert subprocess.call([
            'npm',
            'install',
            module_name
        ], shell=True, cwd=DIRNAME)==0, 'Could not install the required module: ' + module_name

AssertionError:

+ @tryfabric/martian@1.2.4
added 65 packages from 105 contributors and audited 752 packages in 14.844s

66 packages are looking for funding
  run `npm fund` for details

found 17 vulnerabilities (10 low, 7 high)
  run `npm audit fix` to fix them, or `npm audit` for details
D:\ProgramData\Python311\Lib\site-packages\js2py\node_modules\babel-core\lib\transformation\file\index.js:558
      throw err;
      ^

SyntaxError: unknown: Unexpected token (218:8)
  216 |         code: false,
  217 |         color: 'default',
> 218 |         ...(options.annotations || {}),
      |         ^
  219 |     };
  220 |     if (options.type === 'equation')
  221 |         return {
    at Parser.pp$5.raise (D:\ProgramData\Python311\Lib\site-packages\js2py\node_modules\babylon\lib\index.js:4454:13)
    at Parser.pp.unexpected (D:\ProgramData\Python311\Lib\site-packages\js2py\node_modules\babylon\lib\index.js:1761:8)
    at Parser.pp$3.parseIdentifier (D:\ProgramData\Python311\Lib\site-packages\js2py\node_modules\babylon\lib\index.js:4332:10)
    at Parser.pp$3.parsePropertyName (D:\ProgramData\Python311\Lib\site-packages\js2py\node_modules\babylon\lib\index.js:4156:96)
    at Parser.pp$3.parseObj (D:\ProgramData\Python311\Lib\site-packages\js2py\node_modules\babylon\lib\index.js:4045:12)
    at Parser.pp$3.parseExprAtom (D:\ProgramData\Python311\Lib\site-packages\js2py\node_modules\babylon\lib\index.js:3719:19)
    at Parser.pp$3.parseExprSubscripts (D:\ProgramData\Python311\Lib\site-packages\js2py\node_modules\babylon\lib\index.js:3494:19)
    at Parser.pp$3.parseMaybeUnary (D:\ProgramData\Python311\Lib\site-packages\js2py\node_modules\babylon\lib\index.js:3474:19)
    at Parser.pp$3.parseExprOps (D:\ProgramData\Python311\Lib\site-packages\js2py\node_modules\babylon\lib\index.js:3404:19)
    at Parser.pp$3.parseMaybeConditional (D:\ProgramData\Python311\Lib\site-packages\js2py\node_modules\babylon\lib\index.js:3381:19) {
  pos: 6860,
  loc: Position { line: 218, column: 8 },
  _babel: true,
  codeFrame: '  216 |         code: false,\n' +
    "  217 |         color: 'default',\n" +
    '> 218 |         ...(options.annotations || {}),\n' +
    '      |         ^\n' +
    '  219 |     };\n' +
    "  220 |     if (options.type === 'equation')\n" +
    '  221 |         return {'
}
Traceback (most recent call last):
  File "...\test-js2py-with-martian.py", line 21, in <module>
    Martian = js2py.require('@tryfabric/martian')
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\ProgramData\Python311\Lib\site-packages\js2py\node_import.py", line 176, in require
    assert subprocess.call(
           ^^^^^^^^^^^^^^^^
AssertionError: Error when converting module to the js bundle

How to solve this?

Any help will be appriciated.

lumirth commented 1 year ago

Boost. Similar issue here.

worstperson commented 1 year ago

Js2Py seems to be missing support for this format, this hacky patch to node_import.py will get it working:

92c92,96
<         pkg_name = module_name.partition('/')[0]
---
>         #pkg_name = module_name.partition('/')[0]
>         if module_name.startswith("@"):
>             pkg_name = ''.join(module_name.partition('/')[:3])
>         else:
>             pkg_name = module_name.partition('/')[0]
153c157,162
<     module_name, maybe_version = (module_name+"@@@").split('@')[:2]
---
>     #module_name, maybe_version = (module_name+"@@@").split('@')[:2]
>     if module_name.startswith("@"):
>         module_name, maybe_version = (module_name+"@@@").split('@')[1:3]
>         module_name = "@" + module_name
>     else:
>         module_name, maybe_version = (module_name+"@@@").split('@')[:2]

Your module still doesn't load though, generates an Unexpected token error in the Babel parser.