Esempio n. 1
0
def translatePythonToJavaScript(psychoPyCode):
    """Translate PsychoPy python code into PsychoJS JavaScript code.

	Args:
		psychoPyCode (str): the input PsychoPy python code

	Returns:
		str: the PsychoJS JavaScript code

	Raises:
		(Exception): whenever a step of the translation process failed
	"""

    # get the Abstract Syntax Tree (AST)
    # this checks that the code is valid python
    try:
        astNode = ast.parse(psychoPyCode)
    # print('>>> AST node: ' + ast.dump(astNode))
    except Exception as error:
        raise Exception(
            'unable to parse the PsychoPy code into an abstract syntax tree: '
            + str(error))

    # transform the AST by making PsychoJS-specific substitutions and dealing with python built-ins:
    try:
        transformedAstNode, addons = transformNode(astNode)
    # print('>>> transformed AST node: ' + ast.dump(transformedAstNode))
    # print('>>> addons: ' + str(addons))
    except Exception as error:
        raise Exception('unable to transform the abstract syntax tree: ' +
                        str(error))

    # turn the transformed AST into code:
    try:
        transformedPsychoPyCode = astunparse.unparse(transformedAstNode)
    # print('>>> transformed PsychoPy code:\n' + transformedPsychoPyCode)
    except Exception as error:
        raise Exception(
            'unable to turn the transformed abstract syntax tree back into code: '
            + str(error))

    # translate the python code into JavaScript code:
    try:
        psychoJsCode, psychoJsSourceMap = translates(transformedPsychoPyCode,
                                                     enable_es6=True)
    # print('>>> PsychoJS code:\n' + psychoJsCode)
    except Exception as error:
        raise Exception(
            'unable to translate the transformed PsychoPy code into PsychoJS JavaScript code: '
            + str(error))

    # transform the JavaScript code:
    try:
        transformedPsychoJsCode = transformPsychoJsCode(psychoJsCode, addons)
    except Exception as error:
        raise Exception('unable to transform the PsychoJS JavaScript code: ' +
                        str(error))

    return transformedPsychoJsCode
Esempio n. 2
0
 def translate(obj):
     source_lines, _ = inspect.getsourcelines(obj)
     javascript, _ = translates(source_lines)
     return javascript
Esempio n. 3
0
File: lib.py Progetto: zenixls2/pjx
 def __new__(cls, func, *args, **kwargs):
     name = func.__name__ + '()'
     obj = str.__new__(cls, name)
     obj.source = translates(inspect.getsource(func).split('\n', 1)[1],
                             enable_es6=True)[0]
     return obj
 def test_translate_object_unsupported(self, name, py_code, py_src, options,
                                       expected):
     from metapensiero.pj.processor.exceptions import UnsupportedSyntaxError
     with pytest.raises(UnsupportedSyntaxError):
         translates(py_src, **options)[0]
 def test_translate_object(self, name, py_code, py_src, options, expected):
     dump = translates(py_src, **options)[0]
     assert dump.rstrip() == expected.rstrip()