PolicyContext

The PolicyContext class is used to initialize a Check instance with component metadata from different sources. It provides methods to load data from JSON objects or files, and allows accessing checks for testing purposes.

Constructor

PolicyContext(data)
  • data (dict): A dictionary containing component metadata

Class Methods

from_component_json

@classmethod
from_component_json(cls, json_data)

Creates a PolicyContext instance from a JSON object. Note that a PolicyContext instance created from the component JSON only will not contain the deltas. While this can be useful for testing most policies, it will not represent a realistic component's data when testing Check.get_all.

  • json_data (dict): A dictionary containing component metadata

  • Returns: A new PolicyContext instance

Example:

component_json = {
    "readme": {
        "lines": 50,
        "missing": False
    }
}
policy_context = PolicyContext.from_component_json(component_json)

from_component_json_file

@classmethod
from_component_json_file(cls, file_path)

Creates a PolicyContext instance from a JSON file. Note that a PolicyContext instance created from the component JSON only will not contain the deltas. While this can be useful for testing most policies, it will not represent a realistic component's data when testing Check.get_all.

  • file_path (str): Path to a JSON file containing component metadata

  • Returns: A new PolicyContext instance

Example:

policy_context = PolicyContext.from_component_json_file("path/to/component.json")

from_bundle_json

@classmethod
from_bundle_json(cls, bundle_data)

Creates a PolicyContext instance from a bundle JSON object.

  • bundle_data (dict): A dictionary containing bundle data

  • Returns: A new PolicyContext instance

from_bundle_file

@classmethod
from_bundle_file(cls, file_path)

Creates a PolicyContext instance from a bundle JSON file.

  • file_path (str): Path to a JSON file containing bundle data

  • Returns: A new PolicyContext instance

Instance Methods

get_checks

get_checks()

Returns all checks that have been executed within this policy context.

  • Returns: A list of Check instances

get_last_check

get_last_check()

Returns the most recently executed check within this policy context.

  • Returns: A Check instance, or None if no checks have been executed

Last updated