pycep-parser

0.7.0 · active · verified Fri Apr 10

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

Install

Imports

Quickstart

This quickstart demonstrates how to initialize the BicepParser and parse a simple Bicep content string into an Abstract Syntax Tree (AST). The `ast.pretty()` method provides a human-readable representation of the parsed structure.

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}")

view raw JSON →