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.0.tar.gz (8.8 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.0-py3-none-any.whl (2.4 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: alap_python-0.1.0.tar.gz
  • Upload date:
  • Size: 8.8 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.0.tar.gz
Algorithm Hash digest
SHA256 5f17b245796a1fa03901c4babf73e7a393e98e6dfa375ab18ad25ddf58c9113c
MD5 a3c4c0f3decff9b312c1e34773927462
BLAKE2b-256 91f35e9f858ac26bb3783f12a9ae87a82894b52495a66b529fa08e9c2eceff96

See more details on using hashes here.

File details

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

File metadata

  • Download URL: alap_python-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 2.4 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.0-py3-none-any.whl
Algorithm Hash digest
SHA256 9f20cdcb92bd260d11248fca64fff826db018cb685504f774c413cd717f50875
MD5 e867856dcfae95137656445ee9716df2
BLAKE2b-256 679a9b899201d9acfb5c7e4eb9cfd576f7b52b911b6d253078d34dbb36b3b89b

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