question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Currently the only way of creating a js is calling the cli interface. Even calling the underlying code is not working as it relays on global defined variables for cli. Also the compiler is only importable over a hack.

import tempfile
import sys
import os


def transcrypt_code(code):
    """
    The api is pretty messy so to get it working we create a tem file
    """
    import transcrypt

    transpilerDir = transcrypt.__path__._path[0]
    modulesDir = '{}/modules'.format(transpilerDir)

    # Both CPython and Transcrypt will use all dirs in sys.path as search roots for modules
    # CPython will also search relatively from each module, Transcrypt only
    # from the main module

    sys.path = [modulesDir] + sys.path
    sys.modules.pop('org', None)

    from org.transcrypt import compiler

    name = None

    with tempfile.NamedTemporaryFile() as f:
        name = f.name
        f.write(code.encode())

    compiler.Program(name)
    with open(os.path.join(
            os.path.dirname(name), "__javascript__",
            os.path.basename(name) + ".js")) as f:
        ret = f.read()
    return ret

if __name__ == "__main__":
    transcrypt_code("print('helo')")

This results in an error:

Traceback (most recent call last):
  File "trans.py", line 39, in <module>
    transcrypt_code("print('helo')")
  File "trans.py", line 30, in transcrypt_code
    compiler.Program(name)
  File "/tmp/env3/lib/python3.5/site-packages/transcrypt/modules/org/transcrypt/compiler.py", line 93, in __init__
    self.rawModuleCaption = '\n\n// ============ Source: {} ============\n\n' if utils.commandArgs.anno else ''
AttributeError: 'CommandArgs' object has no attribute 'anno'

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
JdeHcommented, May 16, 2016

Indeed, if you want to invoke Transcrypt from a Python script, simply importing it doesn’t work. Invoking it is possible, but has to be done via the command line, as in shipment_test.py below. It’s admitted that this is less elegant than be able to call the compiler directly. I’ll register this as an enhancement.

import os
import webbrowser

shipDir = os.path.dirname (os.path.abspath (__file__)) .replace ('\\', '/')
rootDir = '/'.join  (shipDir.split ('/')[ : -2])

def getAbsPath (relPath):
    return '{}/{}'.format (rootDir, relPath)

def test (relPath, fileNamePrefix, run = False, switches = ''):
    os.chdir (getAbsPath (relPath))

    os.system ('run_transcrypt -b -m {}{}.py'.format (switches, fileNamePrefix))    

    if run:
        os.chdir (getAbsPath (relPath))
        os.system ('run_transcrypt -r {}.py'.format (fileNamePrefix))       

    webbrowser.open ('file://{}/{}.html'.format (getAbsPath (relPath), fileNamePrefix), new = 2)
    webbrowser.open ('file://{}/{}.min.html'.format (getAbsPath (relPath), fileNamePrefix), new = 2)

for fcallSwitch in ('', '-f '):
    test ('development/automated_tests/hello', 'autotest', True, fcallSwitch)
    test ('development/automated_tests/transcrypt', 'autotest', True, fcallSwitch)
    test ('demos/hello', 'hello', False, fcallSwitch)
    test ('demos/jquery_demo', 'jquery_demo', False, fcallSwitch)
    test ('demos/d3js_demo', 'd3js_demo', False, fcallSwitch)
    test ('demos/pong', 'pong', False, fcallSwitch)
    test ('demos/turtle_demos', 'star', False, fcallSwitch + '-p .user ')
    test ('demos/turtle_demos', 'snowflake', False, fcallSwitch + '-p .user ')

    if fcallSwitch:
        print ('Shipment test completed')
    else:
        input ('Close browser tabs opened by shipment test and press [enter] for fcall test')
0reactions
JdeHcommented, Jan 10, 2017

I don’t expect this to be handled anytime soon, so I’ve added the label postponed and will close it again. Motivation: #258. Postponed issues can easily be searched since they share the same label.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Cleanup API - Vertuna WIKI - ConfiForms
You can use it to cleanup data stored by ConfiForms plugin, all data or filtered by filter criteria. Same format as for filters...
Read more >
API | Testing Library
API. React Testing Library re-exports everything from DOM Testing Library as well as these ... cleanup; act; renderHook; renderHook Options.
Read more >
API cleanup for freshly created projects - DatoCMS
API cleanup for freshly created projects. December 6th, 2022. We want our APIs to be as clean as possible, but we also don't...
Read more >
Cleanup - Oil Spill Prevention and Response - API
Cleanup. When a spill occurs, the prior planning and coordination of the National Response System greatly enhances the speed and effectiveness of the ......
Read more >
Using the API to cleanup redundant unit files in IBM Rhapsody
Unlike "Clean Redundant Source Files" in the Code menu, there is no built-in mechanism to remove file units from the project directory. However, ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found