Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
booox opened this issue Dec 27, 2022 · 2 comments
Open

Comments

@booox
Copy link

booox commented Dec 27, 2022

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
Copy link

lumirth commented Mar 2, 2023

Boost. Similar issue here.

@worstperson
Copy link
Collaborator

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants