Ejemplo n.º 1
0
def main(script):
    if '--visjs' in sys.argv:
        import python_to_visjs
        return python_to_visjs.main( script )
    else:
        code = ''
        if '--dart' in sys.argv:
            a = python_to_pythonjs(script, dart=True)
            code = pythonjs_to_dart( a )
        elif '--coffee' in sys.argv:
            a = python_to_pythonjs(script, coffee=True)
            code = pythonjs_to_coffee( a )
        elif '--lua' in sys.argv:
            a = python_to_pythonjs(script, lua=True)
            try: code = pythonjs_to_lua( a )
            except SyntaxError:
                err = traceback.format_exc()
                lineno = 0
                for line in err.splitlines():
                    if "<unknown>" in line:
                        lineno = int(line.split()[-1])

                b = a.splitlines()[ lineno ]
                sys.stderr.write( '\n'.join([err,b]) )
                
        elif '--luajs' in sys.argv:
            a = python_to_pythonjs(script, lua=True)
            code = pythonjs_to_luajs( a )
        else:
            a = python_to_pythonjs(script)
            code = pythonjs_to_javascript( a )

        return code
Ejemplo n.º 2
0
def main(script):
    if '--visjs' in sys.argv:
        import python_to_visjs
        return python_to_visjs.main( script )
    else:
        a = python_to_pythonjs(script)
        if '--dart' in sys.argv:
            return pythonjs_to_dart( a )
        elif '--coffee' in sys.argv:
            return pythonjs_to_coffee( a )
        elif '--lua' in sys.argv:
            return pythonjs_to_lua( a )
        elif '--luajs' in sys.argv:
            return pythonjs_to_luajs( a )
        else:
            return pythonjs_to_javascript( a )
Ejemplo n.º 3
0
def main(script, module_path=None):
    if '--visjs' in sys.argv:
        import python_to_visjs
        return python_to_visjs.main( script )
    else:
        code = ''
        if '--dart' in sys.argv:
            a = python_to_pythonjs(script, dart=True, module_path=module_path)
            code = pythonjs_to_dart( a )
        elif '--coffee' in sys.argv:
            a = python_to_pythonjs(script, coffee=True, module_path=module_path)
            code = pythonjs_to_coffee( a )
        elif '--lua' in sys.argv:
            a = python_to_pythonjs(script, lua=True, module_path=module_path)
            try: code = pythonjs_to_lua( a )
            except SyntaxError:
                err = traceback.format_exc()
                lineno = 0
                for line in err.splitlines():
                    if "<unknown>" in line:
                        lineno = int(line.split()[-1])

                b = a.splitlines()[ lineno ]
                sys.stderr.write( '\n'.join([err,b]) )
                
        elif '--luajs' in sys.argv:  ## converts back to javascript
            a = python_to_pythonjs(script, lua=True, module_path=module_path)
            code = pythonjs_to_luajs( a )
        else:
            a = python_to_pythonjs(script, module_path=module_path)
            if isinstance(a, dict):
                res = {}
                for jsfile in a:
                    res[ jsfile ] = pythonjs_to_javascript( a[jsfile], webworker=jsfile != 'main' )
                return res
            else:
                ## requirejs module is on by default, this wraps the code in a `define` function
                ## and returns `__module__`
                ## if --no-wrapper is used, then the raw javascript is returned.
                code = pythonjs_to_javascript( a, requirejs='--no-wrapper' not in sys.argv )

        return code
Ejemplo n.º 4
0
def main(script):
    if '--visjs' in sys.argv:
        import python_to_visjs
        return python_to_visjs.main(script)
    else:
        code = ''
        if '--dart' in sys.argv:
            a = python_to_pythonjs(script, dart=True)
            code = pythonjs_to_dart(a)
        elif '--coffee' in sys.argv:
            a = python_to_pythonjs(script, coffee=True)
            code = pythonjs_to_coffee(a)
        elif '--lua' in sys.argv:
            a = python_to_pythonjs(script, lua=True)
            try:
                code = pythonjs_to_lua(a)
            except SyntaxError:
                err = traceback.format_exc()
                lineno = 0
                for line in err.splitlines():
                    if "<unknown>" in line:
                        lineno = int(line.split()[-1])

                b = a.splitlines()[lineno]
                sys.stderr.write('\n'.join([err, b]))

        elif '--luajs' in sys.argv:
            a = python_to_pythonjs(script, lua=True)
            code = pythonjs_to_luajs(a)
        else:
            a = python_to_pythonjs(script)
            if isinstance(a, dict):
                res = {}
                for jsfile in a:
                    res[jsfile] = pythonjs_to_javascript(
                        a[jsfile], webworker=jsfile != 'main')
                return res
            else:
                code = pythonjs_to_javascript(a)

        return code
Ejemplo n.º 5
0
def main(script, module_path=None):
	if '--visjs' in sys.argv:
		import python_to_visjs
		return python_to_visjs.main( script )
	else:
		code = ''
		res = None
		if '--go' in sys.argv:
			a = python_to_pythonjs(script, go=True, module_path=module_path)
			code = pythonjs_to_go( a )
		elif '--gopherjs' in sys.argv:
			a = python_to_pythonjs(script, go=True, module_path=module_path)
			code = pythonjs_to_go( a )

			exe = os.path.expanduser('~/go/bin/gopherjs')
			if not os.path.isfile(exe):
				raise RuntimeError('gopherjs not installed to ~/go/bin/gopherjs')
			import subprocess
			path = '/tmp/gopherjs-input.go'
			open(path, 'wb').write(code)
			subprocess.check_call([exe, 'build', path], cwd='/tmp')
			code = open('/tmp/gopherjs-input.js', 'rb').read()

		elif '--dart' in sys.argv:
			a = python_to_pythonjs(script, dart=True, module_path=module_path)
			code = pythonjs_to_dart( a )
		elif '--coffee' in sys.argv:
			a = python_to_pythonjs(script, coffee=True, module_path=module_path)
			code = pythonjs_to_coffee( a )
		elif '--lua' in sys.argv:
			a = python_to_pythonjs(script, lua=True, module_path=module_path)
			try: code = pythonjs_to_lua( a )
			except SyntaxError:
				err = traceback.format_exc()
				lineno = 0
				for line in err.splitlines():
					if "<unknown>" in line:
						lineno = int(line.split()[-1])

				b = a.splitlines()[ lineno ]
				sys.stderr.write( '\n'.join([err,b]) )
				
		elif '--luajs' in sys.argv:  ## converts back to javascript
			a = python_to_pythonjs(script, lua=True, module_path=module_path)
			code = pythonjs_to_luajs( a )
		else:
			a = python_to_pythonjs(
				script, 
				module_path=module_path,
				fast_javascript = '--fast-javascript' in sys.argv,
				modules  = '--modules' in sys.argv,
				pure_javascript = '--pure-javascript' in sys.argv
			)

			if isinstance(a, dict):
				res = {}
				for jsfile in a:
					res[ jsfile ] = pythonjs_to_javascript( a[jsfile], webworker=jsfile != 'main' )
				return res
			else:
				## requirejs module is on by default, this wraps the code in a `define` function
				## and returns `__module__`
				## if --no-wrapper is used, then the raw javascript is returned.
				## by default the pythonjs runtime is inserted, this can be disabled with `--no-runtime`
				code = pythonjs_to_javascript(
					a, 
					requirejs='--no-wrapper' not in sys.argv, 
					insert_runtime='--no-runtime' not in sys.argv,
					fast_javascript = '--fast-javascript' in sys.argv,
					fast_loops      = '--fast-loops' in sys.argv
				)
				if isinstance(code, dict):
					assert '--modules' in sys.argv
					path = 'build'
					if os.path.isdir(path):
						for name in code:
							open( os.path.join(path,name), 'wb' ).write( code[name] )
						return 'modules written to: ' + path
					else:
						raise RuntimeError('the option --modules requires a folder named "build" in your current directory')


		if '--analyze' in sys.argv:
			dartanalyzer = os.path.expanduser('~/dart-sdk/bin/dartanalyzer')
			#dart2js = os.path.expanduser('~/dart-sdk/bin/dart2js')
			assert os.path.isfile( dartanalyzer )

			x = python_to_pythonjs(script, dart=True, module_path=module_path)
			dartcode = pythonjs_to_dart( x )
			path = '/tmp/debug.dart'
			open(path, 'wb').write( dartcode )
			import subprocess
			try:
				subprocess.check_output( [dartanalyzer, path] )
			except subprocess.CalledProcessError as err:
				dartcodelines = dartcode.splitlines()
				for line in err.output.splitlines():
					if line.startswith('[error]'):
						a,b = line.split( path )
						a = a[:-1]
						print( '\x1B[0;31m' + a + '\x1B[0m' )
						lineno = int( b.split('line ')[-1].split(',')[0] )
						print('line: %s' %lineno)
						print( dartcodelines[lineno-1] )
				sys.exit(1)

		if res:  ## dict return
			return res
		else:
			return code
Ejemplo n.º 6
0
def main(script, module_path=None):
	if '--visjs' in sys.argv:
		import python_to_visjs
		return python_to_visjs.main( script )
	else:
		code = ''
		res = None
		if '--go' in sys.argv:
			a = python_to_pythonjs(script, go=True, module_path=module_path)
			code = pythonjs_to_go( a )
		elif '--gopherjs' in sys.argv:
			a = python_to_pythonjs(script, go=True, module_path=module_path)
			code = pythonjs_to_go( a )

			exe = os.path.expanduser('~/go/bin/gopherjs')
			if not os.path.isfile(exe):
				raise RuntimeError('gopherjs not installed to ~/go/bin/gopherjs')
			import subprocess
			path = '/tmp/gopherjs-input.go'
			open(path, 'wb').write(code)
			subprocess.check_call([exe, 'build', path], cwd='/tmp')
			code = open('/tmp/gopherjs-input.js', 'rb').read()

		elif '--dart' in sys.argv:
			a = python_to_pythonjs(script, dart=True, module_path=module_path)
			code = pythonjs_to_dart( a )
		elif '--coffee' in sys.argv:
			a = python_to_pythonjs(script, coffee=True, module_path=module_path)
			code = pythonjs_to_coffee( a )
		elif '--lua' in sys.argv:
			a = python_to_pythonjs(script, lua=True, module_path=module_path)
			try: code = pythonjs_to_lua( a )
			except SyntaxError:
				err = traceback.format_exc()
				lineno = 0
				for line in err.splitlines():
					if "<unknown>" in line:
						lineno = int(line.split()[-1])

				b = a.splitlines()[ lineno ]
				sys.stderr.write( '\n'.join([err,b]) )
				
		elif '--luajs' in sys.argv:  ## converts back to javascript
			a = python_to_pythonjs(script, lua=True, module_path=module_path)
			code = pythonjs_to_luajs( a )
		else:
			a = python_to_pythonjs(script, module_path=module_path)

			if isinstance(a, dict):
				res = {}
				for jsfile in a:
					res[ jsfile ] = pythonjs_to_javascript( a[jsfile], webworker=jsfile != 'main' )
				return res
			else:
				## requirejs module is on by default, this wraps the code in a `define` function
				## and returns `__module__`
				## if --no-wrapper is used, then the raw javascript is returned.
				code = pythonjs_to_javascript( a, requirejs='--no-wrapper' not in sys.argv )

		if '--analyze' in sys.argv:
			dartanalyzer = os.path.expanduser('~/dart-sdk/bin/dartanalyzer')
			#dart2js = os.path.expanduser('~/dart-sdk/bin/dart2js')
			assert os.path.isfile( dartanalyzer )

			x = python_to_pythonjs(script, dart=True, module_path=module_path)
			dartcode = pythonjs_to_dart( x )
			path = '/tmp/debug.dart'
			open(path, 'wb').write( dartcode )
			import subprocess
			try:
				subprocess.check_output( [dartanalyzer, path] )
			except subprocess.CalledProcessError as err:
				dartcodelines = dartcode.splitlines()
				for line in err.output.splitlines():
					if line.startswith('[error]'):
						a,b = line.split( path )
						a = a[:-1]
						print( '\x1B[0;31m' + a + '\x1B[0m' )
						lineno = int( b.split('line ')[-1].split(',')[0] )
						print('line: %s' %lineno)
						print( dartcodelines[lineno-1] )
				sys.exit(1)

		if res:  ## dict return
			return res
		else:
			return code
Ejemplo n.º 7
0
def main(script, module_path=None):
	if '--visjs' in sys.argv:
		import python_to_visjs
		return python_to_visjs.main( script )
	else:
		code = ''
		res = None
		if '--go' in sys.argv:
			a = python_to_pythonjs(script, go=True, module_path=module_path)
			code = pythonjs_to_go( a )
		elif '--dart' in sys.argv:
			a = python_to_pythonjs(script, dart=True, module_path=module_path)
			code = pythonjs_to_dart( a )
		elif '--coffee' in sys.argv:
			a = python_to_pythonjs(script, coffee=True, module_path=module_path)
			code = pythonjs_to_coffee( a )
		elif '--lua' in sys.argv:
			a = python_to_pythonjs(script, lua=True, module_path=module_path)
			try: code = pythonjs_to_lua( a )
			except SyntaxError:
				err = traceback.format_exc()
				lineno = 0
				for line in err.splitlines():
					if "<unknown>" in line:
						lineno = int(line.split()[-1])

				b = a.splitlines()[ lineno ]
				sys.stderr.write( '\n'.join([err,b]) )
				
		elif '--luajs' in sys.argv:  ## converts back to javascript
			a = python_to_pythonjs(script, lua=True, module_path=module_path)
			code = pythonjs_to_luajs( a )
		else:
			a = python_to_pythonjs(script, module_path=module_path)
			if isinstance(a, dict):
				res = {}
				for jsfile in a:
					res[ jsfile ] = pythonjs_to_javascript( a[jsfile], webworker=jsfile != 'main' )
				return res
			else:
				## requirejs module is on by default, this wraps the code in a `define` function
				## and returns `__module__`
				## if --no-wrapper is used, then the raw javascript is returned.
				code = pythonjs_to_javascript( a, requirejs='--no-wrapper' not in sys.argv )

		if '--analyze' in sys.argv:
			dartanalyzer = os.path.expanduser('~/dart-sdk/bin/dartanalyzer')
			#dart2js = os.path.expanduser('~/dart-sdk/bin/dart2js')
			assert os.path.isfile( dartanalyzer )

			x = python_to_pythonjs(script, dart=True, module_path=module_path)
			dartcode = pythonjs_to_dart( x )
			path = '/tmp/debug.dart'
			open(path, 'wb').write( dartcode )
			import subprocess
			try:
				subprocess.check_output( [dartanalyzer, path] )
			except subprocess.CalledProcessError as err:
				dartcodelines = dartcode.splitlines()
				for line in err.output.splitlines():
					if line.startswith('[error]'):
						a,b = line.split( path )
						a = a[:-1]
						print( '\x1B[0;31m' + a + '\x1B[0m' )
						lineno = int( b.split('line ')[-1].split(',')[0] )
						print('line: %s' %lineno)
						print( dartcodelines[lineno-1] )
				sys.exit(1)

		if res:  ## dict return
			return res
		else:
			return code