LogoLogo
HomeBlogGet a Demo
  • Introduction
  • Install Lunar
  • Learn the basics
  • 📖Docs
    • Key concepts
    • Component JSON
    • Catalog JSON
    • Lunar CLI Reference
  • 📝Configuration
    • lunar-config.yml
      • catalogers
      • catalogers/hooks
      • domains
      • components
      • collectors
      • collectors/hooks
      • policies
    • lunar.yml
  • 🛠️Plugin SDKs
    • Plugins configuration
      • lunar-cataloger.yml
      • lunar-collector.yml
      • lunar-policy.yml
    • Bash SDK
      • Cataloger
      • Collector
    • Python SDK
      • Collector
      • Policy
        • Check
        • CheckStatus
        • Path
        • PolicyContext
        • NoDataError
        • Utility Functions
  • ⚙️SQL API
    • Overview
    • Views
      • domains
      • components
      • component_deltas
      • initiatives
      • policies
      • checks
      • prs
      • catalog
Powered by GitBook

©️ Earthly Technologies

On this page
  • Constructor
  • Class Methods
  • from_component_json
  • from_component_json_file
  • from_bundle_json
  • from_bundle_file
  • Instance Methods
  • get_checks
  • get_last_check
  1. Plugin SDKs
  2. Python SDK
  3. Policy

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

PreviousPathNextNoDataError

Last updated 27 days ago

🛠️