CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-dukpy

Simple JavaScript interpreter for Python built on top of duktape engine without any external dependencies

Pending
Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

SecuritybySnyk

Pending

The risk profile of this skill

Overview
Eval results
Files

index.mddocs/

DukPy

A simple JavaScript interpreter for Python built on top of the duktape engine without any external dependencies. DukPy provides comprehensive JavaScript execution environment within Python applications and includes built-in transpilers for popular JavaScript preprocessors including CoffeeScript, BabelJS, TypeScript, JSX, and LESS.

Package Information

  • Package Name: dukpy
  • Language: Python
  • Installation: pip install dukpy

Core Imports

import dukpy

For specific functionality:

from dukpy import evaljs, JSInterpreter, coffee_compile, babel_compile, jsx_compile, typescript_compile, less_compile, install_jspackage
from dukpy.nodelike import NodeLikeInterpreter
from dukpy.module_loader import JSModuleLoader

Basic Usage

import dukpy

# One-off JavaScript execution
result = dukpy.evaljs("5 + 3")
print(result)  # 8

# Passing Python variables to JavaScript
result = dukpy.evaljs("dukpy.value * 2", value=10)
print(result)  # 20

# Persistent JavaScript interpreter
interpreter = dukpy.JSInterpreter()
interpreter.evaljs("var counter = 0")
counter = interpreter.evaljs("++counter")
print(counter)  # 1

# JavaScript transpilation
es6_code = "const greeting = (name) => `Hello, ${name}!`"
compiled = dukpy.babel_compile(es6_code)
print(compiled['code'])

# CoffeeScript compilation  
coffee_code = "greeting = (name) -> \"Hello, #{name}!\""
js_code = dukpy.coffee_compile(coffee_code)
print(js_code)

Architecture

DukPy provides multiple layers of JavaScript integration:

  • Core Interpreter: The _dukpy C extension wraps the duktape JavaScript engine
  • evaljs: Simple one-off JavaScript execution with automatic cleanup
  • JSInterpreter: Persistent JavaScript context with module loading capabilities
  • Module System: CommonJS-compatible module loading with filesystem integration
  • Transpilers: Built-in support for popular JavaScript/CSS preprocessors
  • WebAssets: Integration filters for asset pipeline automation
  • Package Management: npm package installation with dependency resolution

Capabilities

JavaScript Evaluation

Core JavaScript execution functionality supporting both one-off evaluation and persistent interpreter contexts with variable passing, module loading, and Python function exports.

def evaljs(code, **kwargs):
    """Evaluates JavaScript code and returns the result"""

class JSInterpreter:
    def evaljs(self, code, **kwargs):
        """Runs JavaScript code in persistent context"""
    def export_function(self, name, func):
        """Exports Python function to JavaScript"""

JavaScript Evaluation

JavaScript Transpilers

Built-in compilation support for modern JavaScript dialects and preprocessors including CoffeeScript, ES6+ via Babel, TypeScript, JSX, and LESS CSS preprocessing.

def coffee_compile(source):
    """Compiles CoffeeScript to JavaScript"""
    
def babel_compile(source, **kwargs):
    """Compiles ES6+ to ES5 using BabelJS"""
    
def jsx_compile(source, **kwargs):
    """Compiles JSX to React-compatible JavaScript"""
    
def typescript_compile(source):
    """Compiles TypeScript to ES5"""
    
def less_compile(source, options=None):
    """Compiles LESS to CSS"""

Transpilers

Package Management

npm package installation with automatic dependency resolution, enabling JavaScript module usage within Python applications through the persistent interpreter's require() system.

def install_jspackage(package_name, version, modulesdir):
    """Installs JavaScript packages from npmjs.org"""

Package Management

Command Line Interface

Built-in CLI tools for package installation and JavaScript execution, available as console scripts after installation.

def main():
    """
    CLI entry point for running JavaScript files.
    Available as 'dukpy' command after installation.
    Supports shebang removal and uses NodeLikeInterpreter.
    """

Usage:

# Install JavaScript packages
dukpy-install react 16.14.0 ./js_modules

# Run JavaScript files  
dukpy script.js

WebAssets Integration

Filter classes for integrating JavaScript transpilation into WebAssets asset pipeline, supporting automatic compilation of CoffeeScript, ES6+, TypeScript, JSX, and LESS in web applications.

class BabelJS(Filter):
    """WebAssets filter for ES6+ compilation"""
    
class TypeScript(Filter):
    """WebAssets filter for TypeScript compilation"""
    
class CompileLess(Filter):
    """WebAssets filter for LESS compilation"""
    
class BabelJSX(Filter):
    """WebAssets filter for JSX compilation"""

WebAssets

Types

class JSRuntimeError(Exception):
    """Exception raised when JavaScript execution fails"""

class JSPackageInstallError(Exception):
    """Exception raised when package installation fails"""
    def __init__(self, msg, error_code):
        super().__init__(msg)
        self.error_code = error_code

class LessCompilerError(Exception):
    """Exception raised when LESS compilation fails"""

class JSModuleLoader:
    """Manages finding and loading JS modules in CommonJS format"""
    def register_path(self, path):
        """Registers a directory where to look for modules"""
    def lookup(self, module_name):
        """Searches for a file providing given module"""
    def load(self, module_name):
        """Returns source code and normalized module id"""

class NodeLikeInterpreter(JSInterpreter):
    """A DukPy Interpreter that provides a minimal compatibility layer with NodeJS"""
    def __init__(self):
        """Creates NodeLikeInterpreter with filesystem and jscore module support"""

class FS:
    """Provides oversimplified fs.js native functions for JavaScript"""
    @classmethod
    def exists(cls, filepath):
        """Checks if a file or directory exists"""
    @classmethod  
    def read(cls, path, encoding):
        """Reads file contents with optional encoding"""

docs

index.md

javascript-evaluation.md

package-management.md

transpilers.md

webassets.md

tile.json