2015-03-09 11:53:47 +00:00
|
|
|
dukpy
|
|
|
|
=====
|
|
|
|
|
2022-04-09 16:19:18 +00:00
|
|
|
.. image:: https://github.com/amol-/dukpy/actions/workflows/run-tests.yml/badge.svg
|
2023-05-03 22:14:58 +00:00
|
|
|
:target: https://github.com/amol-/dukpy/actions/workflows/run-tests.yml
|
2015-11-06 15:25:11 +00:00
|
|
|
|
|
|
|
.. image:: https://coveralls.io/repos/amol-/dukpy/badge.png?branch=master
|
2016-03-01 12:58:20 +00:00
|
|
|
:target: https://coveralls.io/r/amol-/dukpy?branch=master
|
2015-11-06 15:25:11 +00:00
|
|
|
|
2016-04-24 20:54:24 +00:00
|
|
|
.. image:: https://img.shields.io/pypi/v/dukpy.svg
|
2018-04-18 22:26:55 +00:00
|
|
|
:target: https://pypi.org/p/dukpy
|
2016-04-24 20:54:24 +00:00
|
|
|
|
|
|
|
|
2015-03-09 21:16:02 +00:00
|
|
|
DukPy is a simple javascript interpreter for Python built on top of
|
2015-03-10 08:28:08 +00:00
|
|
|
duktape engine **without any external dependency**.
|
2015-11-23 16:58:23 +00:00
|
|
|
It comes with a bunch of common transpilers built-in for convenience:
|
|
|
|
|
|
|
|
- *CoffeeScript*
|
|
|
|
- *BabelJS*
|
|
|
|
- *TypeScript*
|
2016-04-09 20:30:55 +00:00
|
|
|
- *JSX*
|
2016-08-16 22:53:48 +00:00
|
|
|
- *LESS*
|
2015-03-09 11:53:47 +00:00
|
|
|
|
2015-03-09 21:16:02 +00:00
|
|
|
CoffeeScript Compiler
|
|
|
|
---------------------
|
|
|
|
|
2016-03-01 12:58:20 +00:00
|
|
|
Using the coffeescript compiler is as easy as running:
|
|
|
|
|
|
|
|
.. code:: python
|
2015-03-09 21:16:02 +00:00
|
|
|
|
|
|
|
>>> import dukpy
|
|
|
|
>>> dukpy.coffee_compile('''
|
|
|
|
... fill = (container, liquid = "coffee") ->
|
|
|
|
... "Filling the #{container} with #{liquid}..."
|
|
|
|
... ''')
|
|
|
|
'(function() {\n var fill;\n\n fill = function*(container, liquid) {\n if (liquid == null) {\n liquid = "coffee";\n }\n return "Filling the " + container + " with " + liquid + "...";\n };\n\n}).call(this);\n'
|
|
|
|
|
2015-11-23 16:58:23 +00:00
|
|
|
TypeScript Transpiler
|
|
|
|
---------------------
|
|
|
|
|
2016-03-01 12:58:20 +00:00
|
|
|
The TypeScript compiler can be used through the
|
|
|
|
``dukpy.typescript_compile`` function:
|
|
|
|
|
|
|
|
.. code:: python
|
2015-11-23 16:58:23 +00:00
|
|
|
|
|
|
|
>>> import dukpy
|
|
|
|
>>> dukpy.typescript_compile('''
|
|
|
|
... class Greeter {
|
|
|
|
... constructor(public greeting: string) { }
|
|
|
|
... greet() {
|
|
|
|
... return "<h1>" + this.greeting + "</h1>";
|
|
|
|
... }
|
|
|
|
... };
|
|
|
|
...
|
|
|
|
... var greeter = new Greeter("Hello, world!");
|
|
|
|
... ''')
|
|
|
|
'var Greeter = (function () {\n function Greeter(greeting) {\n this.greeting = greeting;\n }\n Greeter.prototype.greet = function () {\n return "<h1>" + this.greeting + "</h1>";\n };\n return Greeter;\n})();\n;\nvar greeter = new Greeter("Hello, world!");\n'
|
|
|
|
|
|
|
|
Currently the compiler has built-in options and doesn't accept additional ones,
|
|
|
|
|
2016-03-17 15:10:51 +00:00
|
|
|
The DukPY based TypeScript compiler also provides a WebAssets (
|
|
|
|
http://webassets.readthedocs.org/en/latest/ ) filter to automatically
|
2016-03-17 15:18:16 +00:00
|
|
|
compile TypeScript code in your assets pipeline. You register this filter as
|
2016-03-17 15:10:51 +00:00
|
|
|
``typescript`` within WebAssets using:
|
|
|
|
|
|
|
|
.. code:: python
|
|
|
|
|
|
|
|
from webassets.filter import register_filter
|
|
|
|
from dukpy.webassets import TypeScript
|
|
|
|
|
|
|
|
register_filter(TypeScript)
|
|
|
|
|
|
|
|
Which makes the filter available with the ``typescript`` name.
|
2015-11-23 16:58:23 +00:00
|
|
|
|
2016-03-21 13:05:26 +00:00
|
|
|
**NOTE:** When using the TypeScript compiler for code that needs to run
|
|
|
|
in the browser, make sure to add
|
|
|
|
https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.24/system.js
|
|
|
|
dependency. As ``import`` statements are resolved using SystemJS.
|
|
|
|
|
2015-04-26 21:05:24 +00:00
|
|
|
EcmaScript6 BabelJS Transpiler
|
|
|
|
------------------------------
|
|
|
|
|
2016-03-01 12:58:20 +00:00
|
|
|
To compile ES6 code to ES5 for everyday usage you can use
|
|
|
|
``dukpy.babel_compile``:
|
|
|
|
|
|
|
|
.. code:: python
|
2015-04-26 21:05:24 +00:00
|
|
|
|
|
|
|
>>> import dukpy
|
|
|
|
>>> dukpy.babel_compile('''
|
|
|
|
... class Point {
|
|
|
|
... constructor(x, y) {
|
|
|
|
... this.x = x;
|
|
|
|
... this.y = y;
|
|
|
|
... }
|
|
|
|
... toString() {
|
|
|
|
... return '(' + this.x + ', ' + this.y + ')';
|
|
|
|
... }
|
|
|
|
... }
|
|
|
|
... ''')
|
|
|
|
'"use strict";\n\nvar _prototypeProperties = function (child, staticProps, instanceProps) { if (staticProps) Object.defineProperties(child, staticProps); if (instanceProps) Object.defineProperties(child.prototype, instanceProps); };\n\nvar _classCallCheck = function (instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } };\n\nvar Point = (function () {\n function Point(x, y) {\n _classCallCheck(this, Point);\n\n this.x = x;\n this.y = y;\n }\n\n _prototypeProperties(Point, null, {\n toString: {\n value: function toString() {\n return "(" + this.x + ", " + this.y + ")";\n },\n writable: true,\n configurable: true\n }\n });\n\n return Point;\n})();\n'
|
|
|
|
|
2016-03-01 13:04:04 +00:00
|
|
|
You can pass `options`__ to the BabelJS compiler just as keywords on
|
2016-03-01 13:01:33 +00:00
|
|
|
the call to ``babel_compile()``.
|
2016-03-01 12:57:20 +00:00
|
|
|
|
|
|
|
__ http://babeljs.io/docs/usage/options/
|
2015-11-06 14:59:59 +00:00
|
|
|
|
2016-03-01 12:58:20 +00:00
|
|
|
The DukPY based BabelJS compiler also provides a WebAssets (
|
|
|
|
http://webassets.readthedocs.org/en/latest/ ) filter to automatically
|
|
|
|
compile ES6 code in your assets pipeline. You register this filter as
|
|
|
|
``babeljs`` within WebAssets using:
|
|
|
|
|
|
|
|
.. code:: python
|
|
|
|
|
2015-11-06 14:59:59 +00:00
|
|
|
from webassets.filter import register_filter
|
2015-11-09 11:31:01 +00:00
|
|
|
from dukpy.webassets import BabelJS
|
2016-03-01 12:57:20 +00:00
|
|
|
|
2015-11-06 14:59:59 +00:00
|
|
|
register_filter(BabelJS)
|
|
|
|
|
|
|
|
Which makes the filter available with the ``babeljs`` name.
|
2016-09-25 08:37:32 +00:00
|
|
|
Only supported filter option is currently `BABEL_MODULES_LOADER` with value
|
|
|
|
``systemjs`` or ``umd`` to specify that compiled code should use SystemJS
|
|
|
|
or UMD instead of CommonJS for modules.
|
2015-11-06 14:59:59 +00:00
|
|
|
|
2016-03-01 12:57:20 +00:00
|
|
|
**NOTE:** When using the BabelJS compiler for code that needs to run
|
|
|
|
in the browser, make sure to add
|
2019-12-02 14:00:04 +00:00
|
|
|
https://cdnjs.cloudflare.com/ajax/libs/babel-polyfill/6.26.0/polyfill.min.js
|
2016-03-01 12:57:20 +00:00
|
|
|
dependency.
|
|
|
|
|
2016-04-09 20:30:27 +00:00
|
|
|
JSX to React Transpiling
|
|
|
|
------------------------
|
2016-04-09 20:23:20 +00:00
|
|
|
|
|
|
|
DukPy provides a built-in compiler from JSX to React, this is available as
|
|
|
|
``dukpy.jsx_compile``:
|
|
|
|
|
|
|
|
.. code:: python
|
|
|
|
|
|
|
|
>>> import dukpy
|
|
|
|
>>> dukpy.jsx_compile('var react_hello = <h1>Hello, world!</h1>;')
|
|
|
|
u'"use strict";\n\nvar react_hello = React.createElement(\n "h1",\n null,\n "Hello, world!"\n);'
|
|
|
|
|
2016-08-17 22:54:42 +00:00
|
|
|
The DukPY based JSX compiler also provides a WebAssets (
|
|
|
|
http://webassets.readthedocs.org/en/latest/ ) filter to automatically
|
|
|
|
compile JSX+ES6 code in your assets pipeline. You register this filter as
|
|
|
|
``babeljsx`` within WebAssets using:
|
|
|
|
|
|
|
|
.. code:: python
|
|
|
|
|
|
|
|
from webassets.filter import register_filter
|
|
|
|
from dukpy.webassets import BabelJSX
|
|
|
|
|
|
|
|
register_filter(BabelJSX)
|
|
|
|
|
|
|
|
Which makes the filter available with the ``babeljsx`` name.
|
2016-09-16 23:29:02 +00:00
|
|
|
This filter supports the same options as the babel one.
|
2016-08-17 22:54:42 +00:00
|
|
|
|
2016-08-16 22:53:48 +00:00
|
|
|
Less Transpiling
|
|
|
|
----------------
|
|
|
|
|
|
|
|
DukPy provides a built-in distribution of the less compiler available
|
|
|
|
through `dukpy.less_compile`:
|
|
|
|
|
|
|
|
.. code:: python
|
|
|
|
|
|
|
|
>>> import dukpy
|
|
|
|
>>> dukpy.less_compile('.class { width: (1 + 1) }')
|
|
|
|
'.class {\n width: 2;\n}\n'
|
2015-09-04 09:30:39 +00:00
|
|
|
|
2016-08-17 22:54:42 +00:00
|
|
|
|
|
|
|
The DukPY based LESS compiler also provides a WebAssets (
|
|
|
|
http://webassets.readthedocs.org/en/latest/ ) filter to automatically
|
|
|
|
compile LESS code in your assets pipeline. You register this filter as
|
|
|
|
``lessc`` within WebAssets using:
|
|
|
|
|
|
|
|
.. code:: python
|
|
|
|
|
|
|
|
from webassets.filter import register_filter
|
|
|
|
from dukpy.webassets import CompileLess
|
|
|
|
|
|
|
|
register_filter(CompileLess)
|
|
|
|
|
|
|
|
Which makes the filter available with the ``lessc`` name.
|
|
|
|
|
|
|
|
|
2015-03-09 21:16:02 +00:00
|
|
|
Using the JavaScript Interpreter
|
|
|
|
--------------------------------
|
|
|
|
|
|
|
|
Using dukpy is as simple as calling the ``dukpy.evaljs`` function with
|
2016-03-01 12:57:20 +00:00
|
|
|
the javascript code:
|
|
|
|
|
|
|
|
.. code:: python
|
2015-03-09 21:16:02 +00:00
|
|
|
|
|
|
|
>>> import dukpy
|
|
|
|
>>> dukpy.evaljs("var o = {'value': 5}; o['value'] += 3; o")
|
|
|
|
{'value': 8}
|
|
|
|
|
|
|
|
|
|
|
|
The ``evaljs`` function executes the javascript and returns the
|
|
|
|
resulting value as far as it is possible to encode it in JSON.
|
|
|
|
|
|
|
|
If execution fails a ``dukpy.JSRuntimeError`` exception is raised
|
|
|
|
with the failure reason.
|
|
|
|
|
|
|
|
Passing Arguments
|
2016-03-30 19:51:37 +00:00
|
|
|
~~~~~~~~~~~~~~~~~
|
2015-03-09 21:16:02 +00:00
|
|
|
|
|
|
|
Any argument passed to ``evaljs`` is available in JavaScript inside
|
|
|
|
the ``dukpy`` object in javascript. It must be possible to encode
|
2016-03-01 12:58:20 +00:00
|
|
|
the arguments using JSON for them to be available in Javascript:
|
|
|
|
|
|
|
|
.. code:: python
|
2015-03-09 21:16:02 +00:00
|
|
|
|
|
|
|
>>> import dukpy
|
|
|
|
>>>
|
|
|
|
>>> def sum3(value):
|
|
|
|
... return dukpy.evaljs("dukpy['value'] + 3", value=value)
|
|
|
|
...
|
|
|
|
>>> sum3(7)
|
|
|
|
10
|
|
|
|
|
|
|
|
Running Multiple Scripts
|
2016-03-30 19:51:37 +00:00
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~
|
2015-03-09 21:16:02 +00:00
|
|
|
|
|
|
|
The ``evaljs`` function supports providing multiple source codes to
|
|
|
|
be executed in the same context.
|
|
|
|
|
2016-03-01 12:58:20 +00:00
|
|
|
Multiple script can be passed in a list or tuple:
|
|
|
|
|
|
|
|
.. code:: python
|
2015-03-09 21:16:02 +00:00
|
|
|
|
|
|
|
>>> import dukpy
|
|
|
|
>>> dukpy.evaljs(["var o = {'value': 5}",
|
|
|
|
... "o['value'] += 3",
|
|
|
|
... "o"])
|
|
|
|
{'value': 8}
|
|
|
|
|
|
|
|
This is useful when your code requires dependencies to work,
|
|
|
|
as you can load the dependency and then your code.
|
|
|
|
|
|
|
|
This is actually how the coffeescript compiler is implemented
|
2016-03-01 12:58:20 +00:00
|
|
|
by DukPy itself:
|
|
|
|
|
|
|
|
.. code:: python
|
2015-03-09 21:16:02 +00:00
|
|
|
|
|
|
|
def coffee_compile(source):
|
|
|
|
with open(COFFEE_COMPILER, 'r') as coffeescript_js:
|
|
|
|
return evaljs((coffeescript_js.read(), 'CoffeeScript.compile(dukpy.coffeecode)'),
|
|
|
|
coffeecode=source)
|
2016-03-30 19:51:37 +00:00
|
|
|
|
|
|
|
Using a persistent JavaScript Interpreter
|
|
|
|
-----------------------------------------
|
|
|
|
|
|
|
|
The ``evaljs`` function creates a new interpreter on each call,
|
|
|
|
this is usually convenient and avoid errors due to dirt global variables
|
|
|
|
or unexpected execution status.
|
|
|
|
|
|
|
|
In some cases you might want to run code that has a slow bootstrap, so
|
|
|
|
it's convenient to reuse the same interpreter between two different calls
|
|
|
|
so that the bootstrap cost has already been paid during the first execution.
|
|
|
|
|
|
|
|
This can be achieved by using the ``dukpy.JSInterpreter`` object.
|
|
|
|
|
|
|
|
Creating a ``dukpy.JSInterpreter`` permits to evaluate code inside that interpreter
|
|
|
|
and multiple ``eval`` calls will share the same interpreter and global status:
|
|
|
|
|
|
|
|
|
|
|
|
.. code:: python
|
|
|
|
|
|
|
|
>>> import dukpy
|
|
|
|
>>> interpreter = dukpy.JSInterpreter()
|
2016-04-01 20:28:14 +00:00
|
|
|
>>> interpreter.evaljs("var o = {'value': 5}; o")
|
2016-03-30 19:51:37 +00:00
|
|
|
{u'value': 5}
|
2016-04-01 20:28:14 +00:00
|
|
|
>>> interpreter.evaljs("o.value += 1; o")
|
2016-03-30 19:51:37 +00:00
|
|
|
{u'value': 6}
|
|
|
|
|
2016-08-16 22:54:56 +00:00
|
|
|
Loading modules with require
|
2016-04-24 21:44:49 +00:00
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
When using the ``dukpy.JSInterpreter`` object it is possible to use
|
2016-04-24 21:46:04 +00:00
|
|
|
the ``require('modulename')`` instruction to load a module inside javascript.
|
2016-04-24 21:44:49 +00:00
|
|
|
|
2016-04-24 21:46:04 +00:00
|
|
|
Modules are looked up in all directories registered with
|
|
|
|
``dukpy.JSInterpreter.loader.register_path``:
|
2016-04-24 21:44:49 +00:00
|
|
|
|
|
|
|
.. code:: python
|
|
|
|
|
|
|
|
>>> import dukpy
|
|
|
|
>>> jsi = dukpy.JSInterpreter()
|
|
|
|
>>> jsi.loader.register_path('./js_modules')
|
|
|
|
>>> jsi.evaljs("isEmpty = require('fbjs/lib/isEmpty'); isEmpty([1])")
|
|
|
|
False
|
|
|
|
|
|
|
|
Installing packages from npmjs.org
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
When using the persistent javascript interpreter it is also possible to install packages
|
2016-04-24 21:46:04 +00:00
|
|
|
from *npmjs.org* through the ``dukpy.install_jspackage`` function:
|
2016-04-24 21:44:49 +00:00
|
|
|
|
|
|
|
.. code:: python
|
|
|
|
|
|
|
|
>>> import dukpy
|
|
|
|
>>> jsi = dukpy.JSInterpreter()
|
|
|
|
>>> dukpy.install_jspackage('promise', None, './js_modules')
|
|
|
|
Packages going to be installed: promise->7.1.1, asap->2.0.3
|
|
|
|
Fetching https://registry.npmjs.org/promise/-/promise-7.1.1.tgz..........................
|
|
|
|
Fetching https://registry.npmjs.org/asap/-/asap-2.0.3.tgz............
|
|
|
|
Installing promise in ./js_modules Done!
|
|
|
|
|
2016-04-24 21:46:04 +00:00
|
|
|
The same functionality is also provided by the ``dukpy-install`` shell command::
|
2016-04-24 21:44:49 +00:00
|
|
|
|
|
|
|
$ dukpy-install -d ./js_modules promise
|
|
|
|
Packages going to be installed: promise->7.1.1, asap->2.0.3
|
|
|
|
Fetching https://registry.npmjs.org/promise/-/promise-7.1.1.tgz..........................
|
|
|
|
Fetching https://registry.npmjs.org/asap/-/asap-2.0.3.tgz............
|
|
|
|
Installing promise in ./js_modules Done!
|
|
|
|
|
|
|
|
Please note that currently `install_jspackage` is not able to resolve conflicting
|
|
|
|
dependencies.
|