Skip to main content

Alap expression parser for Python — resolve link expressions server-side

Project description

Alap Expression Parser — Python

Alap is a JavaScript library that turns links into dynamic menus with multiple curated targets. This is the server-side Python port of the expression parser, enabling expression resolution in Python servers without a Node.js sidecar.

What's included

  • expression_parser.py — Recursive descent parser for the Alap expression grammar, macro expansion, regex search, config merging
  • validate_regex.py — ReDoS guard for user-supplied regex patterns

What's NOT included

This is the server-side subset of alap/core. It covers expression parsing, config merging, and regex validation — everything a server needs to resolve cherry-pick and query requests.

Browser-side concerns (DOM rendering, menu positioning, event handling, URL sanitization) are handled by the JavaScript client and are not ported here.

Supported expression syntax

item1, item2              # item IDs (comma-separated)
.coffee                   # tag query
.nyc + .bridge            # AND (intersection)
.nyc | .sf                # OR (union)
.nyc - .tourist           # WITHOUT (subtraction)
(.nyc | .sf) + .open      # parenthesized grouping
@favorites                # macro expansion
/mypattern/               # regex search (by pattern key)
/mypattern/lu             # regex with field options

Usage

from expression_parser import ExpressionParser, resolve_expression, cherry_pick_links, merge_configs

config = {
    "allLinks": {
        "item1": {"label": "Example", "url": "https://example.com", "tags": ["demo"]},
        "item2": {"label": "Other",   "url": "https://other.com",   "tags": ["demo", "test"]},
    },
    "macros": {
        "all": {"linkItems": ".demo"}
    }
}

# Low-level: get matching IDs
parser = ExpressionParser(config)
ids = parser.query(".demo")              # ["item1", "item2"]
ids = parser.query(".demo - .test")      # ["item1"]

# Convenience: expression -> full link objects
results = resolve_expression(config, ".demo")
# [{"id": "item1", "label": "Example", ...}, {"id": "item2", ...}]

# Cherry-pick: expression -> { id: link } dict
subset = cherry_pick_links(config, ".test")
# {"item2": {"label": "Other", ...}}

# Merge multiple configs
merged = merge_configs(config1, config2)

Installation

Copy expression_parser.py and validate_regex.py into your project, or install from PyPI:

pip install alap
# or, with uv (recommended):
uv add alap

Used by

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

alap_python-0.1.1.tar.gz (19.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

alap_python-0.1.1-py3-none-any.whl (13.9 kB view details)

Uploaded Python 3

File details

Details for the file alap_python-0.1.1.tar.gz.

File metadata

  • Download URL: alap_python-0.1.1.tar.gz
  • Upload date:
  • Size: 19.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.3

File hashes

Hashes for alap_python-0.1.1.tar.gz
Algorithm Hash digest
SHA256 0bb5bd4144ef1daf706a1c15865d67565050c64bab154c8003a6eaf7e82fda44
MD5 7027f0aaa38a4307f457b051284bd414
BLAKE2b-256 1836e16572ea53b316580291d449ac595c5e6affcb07f16fc7cf931d0b1c967f

See more details on using hashes here.

File details

Details for the file alap_python-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: alap_python-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 13.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.3

File hashes

Hashes for alap_python-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 c6d185b08303b956bd0bde8a4fadd72517dea775baf995391f98a8a6b5375dce
MD5 72d75eb0523dd39f2969caefe53e287e
BLAKE2b-256 da6bcff39d8d251113213968783f6692dc42ad1c59fbeb1261dbb1866144050f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page