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

API cleanup #48

Closed
delijati opened this issue May 16, 2016 · 6 comments
Closed

API cleanup #48

delijati opened this issue May 16, 2016 · 6 comments

Comments

@delijati
Copy link

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'
@JdeH
Copy link
Collaborator

JdeH commented 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')

@delijati
Copy link
Author

delijati commented Nov 2, 2016

It should be left open if its "postponed"!?

@JdeH
Copy link
Collaborator

JdeH commented Nov 2, 2016

Stricktly taken: Yes.

By closing it I am trying to make a distinction between short term and long term.

What you're proposing is certainly useful but it requires a reorganization of the compiler that is bound to initially break several things. It is something that only should be done in a period of relative "inertia" of the code.

So I've shoved it from the "open" list (that stares in my face each morning) to the closed list, that doesn't. This is admittedly a psychological trick to not be overwhelmed by what the future holds in store.

Come to think of it, maybe a better way to do this is assigning priority classes and keeping "postponeds" open.

By filtering on priority I could also keep my piece of mind...

@JdeH
Copy link
Collaborator

JdeH commented Nov 2, 2016

By the way could you give me an assessment of how important this feature is for you. Personally I can live without it, but that isn't the norm.

@JdeH
Copy link
Collaborator

JdeH commented Nov 2, 2016

After some thought: closing sends the wrong message.
Reopened and prio's introduced:

Must have
Want to have
Nice to have
Could have

The labels are a result of balancing benefit, effort and risk.

KR & thanks for the feedback!
Jacques

@JdeH
Copy link
Collaborator

JdeH commented 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.

@JdeH JdeH closed this as completed Jan 10, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants