hammurabi.reporters package

Submodules

hammurabi.reporters.base module

This module contains the definition of Reporters which is responsible for exposing the execution results in several formats.

class hammurabi.reporters.base.AdditionalData(*, started: str = '0001-01-01T00:00:00', finished: str = '0001-01-01T00:00:00', pull_request_url: str = '')[source]

Bases: pydantic.main.BaseModel

Additional data which may not be set for every execution.

finished: str
pull_request_url: str
started: str
class hammurabi.reporters.base.LawItem(*, name: str, description: str)[source]

Bases: pydantic.main.BaseModel

LawItem represents the basic summary of a low attached to a rule.

description: str
name: str
class hammurabi.reporters.base.Report(*, passed: List[hammurabi.reporters.base.RuleItem] = [], failed: List[hammurabi.reporters.base.RuleItem] = [], skipped: List[hammurabi.reporters.base.RuleItem] = [], additional_data: hammurabi.reporters.base.AdditionalData = AdditionalData(started='0001-01-01T00:00:00', finished='0001-01-01T00:00:00', pull_request_url=''))[source]

Bases: pydantic.main.BaseModel

The report object which contains all the necessary and optional data for the report will be generated.

additional_data: hammurabi.reporters.base.AdditionalData
failed: List[hammurabi.reporters.base.RuleItem]
passed: List[hammurabi.reporters.base.RuleItem]
skipped: List[hammurabi.reporters.base.RuleItem]
class hammurabi.reporters.base.Reporter(laws: List[hammurabi.law.Law])[source]

Bases: abc.ABC

Abstract class which describes the bare minimum and helper functions for Reporters. A reporter can generate different outputs from the results of the execution. Also, reporters can be extended by additional data which may not contain data for every execution like GitHub pull request url. The report file’s name set by report_name config parameter.

Note

Reporters measures the execution time for the complete execution from checking out the git branch until the pull request creation finished. Although the completion time is measured, it is not detailed for the rules. At this moment measuring execution time of rules is not planned.

Example usage:

>>> from hammurabi.reporters.base import Reporter
>>>
>>>
>>> class JsonReporter(Reporter):
>>>     def report(self) -> str:
>>>         return self._get_report().json()
Parameters

laws (Iterable[Law]) – Iterable Law objects which will be included to the report

abstract report() → Any[source]

Do the actual reporting based on the report assembled.

class hammurabi.reporters.base.RuleItem(*, name: str, law: hammurabi.reporters.base.LawItem)[source]

Bases: pydantic.main.BaseModel

RuleItem represents the registered rule and its status.

The rule (as normally) has the status of the execution which can be passed, failed or skipped.

law: hammurabi.reporters.base.LawItem
name: str

hammurabi.reporters.json module

class hammurabi.reporters.json.JsonReporter(laws: List[hammurabi.law.Law])[source]

Bases: hammurabi.reporters.base.Reporter

Generate reports in Json format and write into file. JsonReporter is the default reporter of the pillar. The example below shows the way how to replace a reporter which could base on the JsonReporter.

The report will be written into the configured report file. The report file’s name set by report_name config parameter.

Example usage:

>>> from pathlib import Path
>>> from hammurabi import Law, Pillar, OwnerChanged
>>> from my_company import MyJsonReporter
>>>
>>> example_law = Law(
>>>     name="Name of the law",
>>>     description="Well detailed description what this law does.",
>>>     rules=(
>>>         OwnerChanged(
>>>             name="Change ownership of nginx config",
>>>             path=Path("./nginx.conf"),
>>>             new_value="www:web_admin"
>>>         ),
>>>     )
>>> )
>>>
>>> # override pillar's default JsonReporter reporter
>>> pillar = Pillar(reporter_class=MyJsonReporter)
report() → None[source]

Do the actual reporting based on the report assembled in Json format. The report will be written into the configured report file.

Module contents