pycep-parser
pycep-parser is a Python library designed to parse Azure Bicep files, leveraging the Lark parsing library. As of version 0.7.0, it supports a wide range of Bicep language features including various functions, decorators, typed variables, and import/extension elements. The project maintains an active development cycle with regular updates, typically every few months, adding new Bicep syntax support and ensuring compatibility with recent Python versions.
Warnings
- breaking Support for Python 3.8 was officially dropped with version 0.6.1. Users on Python 3.8 must either upgrade their Python environment or pin `pycep-parser` to a version older than 0.6.1.
- breaking Support for Python 3.7 was officially dropped with version 0.5.1. Users on Python 3.7 must either upgrade their Python environment or pin `pycep-parser` to a version older than 0.5.1.
- gotcha The `pycep-parser` library relies on `Lark` as its underlying parsing engine. While typically installed as a dependency, changes or specific versions of `Lark` could potentially influence parsing behavior or introduce subtle differences in the generated AST. It's recommended to consult `Lark`'s documentation for advanced parsing scenarios or debugging.
Install
-
pip install pycep-parser
Imports
- BicepParser
from pycep.parser.bicep import BicepParser
Quickstart
from pycep.parser.bicep import BicepParser
# Initialize the Bicep parser
parser = BicepParser()
# Define your Bicep content as a string
bicep_content = """
resource storage 'Microsoft.Storage/storageAccounts@2021-09-01' = {
name: 'mystorageaccount'
location: resourceGroup().location
sku: {
name: 'Standard_LRS'
}
kind: 'StorageV2'
}
output storageName string = storage.name
"""
try:
# Parse the Bicep content into an Abstract Syntax Tree (AST)
ast = parser.parse(bicep_content)
print("Bicep content parsed successfully. AST:")
print(ast.pretty())
# You can now traverse the 'ast' object to inspect the Bicep structure.
# For example, to get the type of the root element:
# print(f"Root element type: {ast.data}")
except Exception as e:
print(f"Error parsing Bicep content: {e}")