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.3.tar.gz (37.3 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.3-py3-none-any.whl (28.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: alap_python-0.1.3.tar.gz
  • Upload date:
  • Size: 37.3 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.3.tar.gz
Algorithm Hash digest
SHA256 072e31489300d2cc5ab9eca8f747da0eeaa67f26c7eeb8e9276099c2bd88831e
MD5 3c1f710e85629f10ca454d7198930ccd
BLAKE2b-256 2f161017aa1ced4257f52fc62953f1e6fe1114410e8cd8005a5e9b905e3c66bd

See more details on using hashes here.

File details

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

File metadata

  • Download URL: alap_python-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 28.2 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.3-py3-none-any.whl
Algorithm Hash digest
SHA256 589cf989ccb105143aa96347c3e494a966b1a9577c50827261e1dc99c650436a
MD5 ed57ef332d535f9b85b6323b81f04583
BLAKE2b-256 54273d49ae252250467c7cc5fa529ca9732594646693ac1689327de8084e92d9

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