Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/open-policy-agent/rego-python
Python library for interacting with Rego ASTs.
https://github.com/open-policy-agent/rego-python
Last synced: 2 months ago
JSON representation
Python library for interacting with Rego ASTs.
- Host: GitHub
- URL: https://github.com/open-policy-agent/rego-python
- Owner: open-policy-agent
- License: apache-2.0
- Created: 2018-06-28T22:54:19.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2021-07-15T18:21:36.000Z (over 3 years ago)
- Last Synced: 2024-08-04T21:07:11.661Z (6 months ago)
- Language: Python
- Size: 10.7 KB
- Stars: 47
- Watchers: 11
- Forks: 16
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-opa - Rego Python - Python package for interacting with Rego (Language and Platform Integrations / Python)
README
# rego-python
This repository contains a Python package for interacting with the [Open Policy Agent](https://github.com/open-policy-agent/opa) project's policy language: [Rego](https://www.openpolicyagent.org/docs/how-do-i-write-policies.html).
## Overview
module | description
--- | ---
`ast` | contains types representing Abstract Syntax Tree (AST) nodes in Rego.
`walk` | contains a visitor pattern implementation for `ast` types.## Example: Load Rego AST from JSON
```python
import json
import requestsfrom rego import ast, walk
# Load some pi into OPA as data (yum!)
requests.put("http://localhost:8181/v1/data", data=json.dumps({
"pi": 3.14,
}))# Use OPA's Compile API to partially evaluate a query. Treat 'input.radius' as unknown.
resp = requests.post("http://localhost:8181/v1/compile", json.dumps({
"query": "(data.pi * input.radius * 2) >= input.min_radius",
"input": {
"min_radius": 4,
},
"unknowns": ["input.radius"],
}))# Load the resulting set of query ASTs out of the JSON response.
tree = resp.json()["result"]["queries"]
qs = ast.QuerySet.from_data(tree)# Pretty print the ASTs.
walk.pretty_print(qs)
```In a new terminal:
```
$ opa run --server
```In another terminal, run the example.
```bash
$ git clone https://github.com/open-policy-agent/rego-python.git
$ cd rego-python
$ virtualenv env
$ source env/bin/activate
$ pip install -r example-requirements.txt
$ pip install -e .
$ python example.py
```Example output.
```bash
QuerySet (1 queries)
Query (1 exprs)
Expr
Term
Ref
Term
Var(gte)
Term
Call
Term
Ref
Term
Var(mul)
Term
Call
Term
Ref
Term
Var(mul)
Term
Scalar(3.14)
Term
Ref
Term
Var(input)
Term
Scalar(radius)
Term
Scalar(2)
Term
Scalar(4)
```