Skip to content

SQRT Policy Parser

check

check(sqrt_code: str) -> None

Check SQRT code syntax, raising an exception if invalid.

Parameters:

  • sqrt_code

    (str) –

    The SQRT policy code to check.

Raises:

Example
check('tool "foo" { must allow always; }')  # No exception

check('tool "foo" { broken }')  # Raises SqrtParseError
# Traceback (most recent call last):
#     ...
# SqrtParseError: ...
Source code in src/sequrity/sqrt/parser.py
def check(sqrt_code: str) -> None:
    """Check SQRT code syntax, raising an exception if invalid.

    Args:
        sqrt_code: The SQRT policy code to check.

    Raises:
        SqrtParseError: If there's a syntax error in the code.

    Example:
        ```python
        check('tool "foo" { must allow always; }')  # No exception

        check('tool "foo" { broken }')  # Raises SqrtParseError
        # Traceback (most recent call last):
        #     ...
        # SqrtParseError: ...
        ```
    """
    result = parse(sqrt_code)
    if not result.valid:
        if result.error is None:
            raise ValueError("Parse failed but no error was provided")
        raise result.error

check_file

check_file(file_path: str | Path) -> None

Check SQRT file syntax, raising an exception if invalid.

Parameters:

  • file_path

    (str | Path) –

    Path to the SQRT file to check.

Raises:

Source code in src/sequrity/sqrt/parser.py
def check_file(file_path: str | Path) -> None:
    """Check SQRT file syntax, raising an exception if invalid.

    Args:
        file_path: Path to the SQRT file to check.

    Raises:
        FileNotFoundError: If the file doesn't exist.
        SqrtParseError: If there's a syntax error in the code.
    """
    file_path = Path(file_path)
    with open(file_path, encoding="utf-8") as f:
        sqrt_code = f.read()
    check(sqrt_code)

options: show_root_heading: true

validate

validate(sqrt_code: str) -> bool

Validate SQRT code syntax.

A simple convenience function that returns True if the code is valid, False otherwise. Use parse() if you need error details.

Parameters:

  • sqrt_code

    (str) –

    The SQRT policy code to validate.

Returns:

  • bool

    True if the syntax is valid, False otherwise.

Example
validate('tool "foo" { must allow always; }')
# True

validate('tool "foo" { broken }')
# False
Source code in src/sequrity/sqrt/parser.py
def validate(sqrt_code: str) -> bool:
    """Validate SQRT code syntax.

    A simple convenience function that returns True if the code is valid,
    False otherwise. Use parse() if you need error details.

    Args:
        sqrt_code: The SQRT policy code to validate.

    Returns:
        True if the syntax is valid, False otherwise.

    Example:
        ```python
        validate('tool "foo" { must allow always; }')
        # True

        validate('tool "foo" { broken }')
        # False
        ```
    """
    return parse(sqrt_code).valid

parse

Parse SQRT code and return the result.

This function only validates syntax. Use this to check if SQRT code is well-formed.

Parameters:

  • sqrt_code

    (str) –

    The SQRT policy code to parse.

Returns:

  • ParseResult ( ParseResult ) –

    The result of parsing, including validity, parse tree, and any syntax error.

Example
result = parse('tool "foo" { must allow always; }')
print(result.valid)
# True

result = parse('tool "foo" { invalid syntax }')
print(result.valid)
# False

print(result.error)
# Unexpected token...
Source code in src/sequrity/sqrt/parser.py
def parse(sqrt_code: str) -> ParseResult:
    """Parse SQRT code and return the result.

    This function only validates syntax. Use this to check if SQRT code is well-formed.

    Args:
        sqrt_code: The SQRT policy code to parse.

    Returns:
        ParseResult: The result of parsing, including validity, parse tree, and any syntax error.

    Example:
        ```python
        result = parse('tool "foo" { must allow always; }')
        print(result.valid)
        # True

        result = parse('tool "foo" { invalid syntax }')
        print(result.valid)
        # False

        print(result.error)
        # Unexpected token...
        ```
    """
    try:
        parser = _get_parser()
        tree = parser.parse(sqrt_code)
        return ParseResult(valid=True, tree=tree, error=None)

    except (UnexpectedCharacters, UnexpectedToken) as e:
        line = getattr(e, "line", None)
        column = getattr(e, "column", None)
        source_snippet = _get_source_line(sqrt_code, line) if line else None
        error = SqrtParseError(str(e), line=line, column=column, source_snippet=source_snippet)
        return ParseResult(valid=False, tree=None, error=error)

    except LarkError as e:
        line = getattr(e, "line", None)
        column = getattr(e, "column", None)
        source_snippet = _get_source_line(sqrt_code, line) if line else None
        error = SqrtParseError(str(e), line=line, column=column, source_snippet=source_snippet)
        return ParseResult(valid=False, tree=None, error=error)

ParseResult

Result of parsing SQRT code.

Attributes:

  • valid (bool) –

    Whether the code is syntactically valid.

  • tree (Tree | None) –

    The parse tree if valid, None otherwise.

  • error (SqrtParseError | None) –

    The parse error if invalid, None otherwise.

valid instance-attribute

valid: bool

Whether the code is syntactically valid.

tree instance-attribute

tree: Tree | None

The parse tree if valid, None otherwise.

error instance-attribute

error: SqrtParseError | None

The parse error if invalid, None otherwise.

SqrtParseError

SqrtParseError(
    message: str,
    line: int | None = None,
    column: int | None = None,
    source_snippet: str | None = None,
)

Raised when SQRT code has a syntax error.

Source code in src/sequrity/sqrt/parser.py
def __init__(
    self,
    message: str,
    line: int | None = None,
    column: int | None = None,
    source_snippet: str | None = None,
):
    self.line = line
    self.column = column
    self.source_snippet = source_snippet
    super().__init__(self._format_message(message, line, column, source_snippet))