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.2.tar.gz (23.1 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.2-py3-none-any.whl (18.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: alap_python-0.1.2.tar.gz
  • Upload date:
  • Size: 23.1 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.2.tar.gz
Algorithm Hash digest
SHA256 6005a3480c46d44c08a26c0c3e492541ed359ac8233ee5afdd3cc591b97bbb69
MD5 a31ff403afc18c07f3dac8b025f3cda4
BLAKE2b-256 3010507a4fbab05b3ac6768e9d8bad36bf022fe99e5d509dae90de4fe49017b7

See more details on using hashes here.

File details

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

File metadata

  • Download URL: alap_python-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 18.1 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.2-py3-none-any.whl
Algorithm Hash digest
SHA256 368e78aad9478e8108097b59c96753df9678cfbd7ab983a54c2df3f2b7bc568e
MD5 6786b4bbea64d59aa1cffd8d8f47f544
BLAKE2b-256 b115132d70f7515c801f947b903d1d1d42abd7b258f7b0063ae8c5f6c86de51a

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