Preconditions

Base precondition

class hammurabi.preconditions.base.Precondition(name: Optional[str] = None, param: Optional[Any] = None)[source]

This class which describes the bare minimum and helper functions for Preconditions. A precondition defines what and how should be checked/validated before executing a Rule. Since preconditions are special rules, all the functions available what can be used for hammurabi.rules.base.AbstractRule.

As said, preconditions are special from different angles. While this is not true for Rules, Preconditions will always have a name, hence giving a name to a Precondition is not necessary. In case no name given to a precondition, the name will be the name of the class and ” precondition” suffix.

Example usage:

>>> import logging
>>> from typing import Optional
>>> from pathlib import Path
>>> from hammurabi import Precondition
>>>
>>> class IsFileExist(Precondition):
>>>     def __init__(self, path: Optional[Path] = None, **kwargs) -> None:
>>>         super().__init__(param=path, **kwargs)
>>>
>>>     def task(self) -> bool:
>>>         return self.param and self.param.exists()
Parameters
  • name (Optional[str]) – Name of the rule which will be used for printing

  • param (Any) – Input parameter of the rule will be used as self.param

Attributes

IsOwnedBy

class hammurabi.preconditions.attributes.IsOwnedBy(path: pathlib.Path, owner: str, **kwargs)[source]

Check if the given file or directory has the required ownership.

To check only the user use owner="username". To check only the group use owner=":group_name" (please note the colon :). It is also possible to check both username and group at the same time by using owner="username:group_name".

Example usage:

>>> from pathlib import Path
>>> from hammurabi import Law, Pillar, Renamed, IsOwnedBy
>>>
>>> example_law = Law(
>>>     name="Name of the law",
>>>     description="Well detailed description what this law does.",
>>>     rules=(
>>>         Renamed(
>>>             name="Rename pyproject.toml if owned by gabor",
>>>             path=Path("./pyproject.toml"),
>>>             new_name="gabor-pyproject.toml"
>>>             preconditions=[
>>>                 IsOwnedBy(path=Path("./pyproject.toml"), owner="gabor")
>>>             ]
>>>         ),
>>>     )
>>> )
>>>
>>> pillar = Pillar()
>>> pillar.register(example_law)
Parameters
  • path (Path) – Input file’s path

  • owner (str) – Owner user and/or group of the file/directory separated by colon

IsNotOwnedBy

class hammurabi.preconditions.attributes.IsNotOwnedBy(path: pathlib.Path, owner: str, **kwargs)[source]

Opposite of hammurabi.preconditions.attributes.IsOwnedBy.

HasMode

class hammurabi.preconditions.attributes.HasMode(path: pathlib.Path, mode: int, **kwargs)[source]

Check if the given file or directory has the required permissions/mode.

You can read more about the available modes at https://docs.python.org/3/library/stat.html.

Example usage:

>>> import stat
>>> from pathlib import Path
>>> from hammurabi import Law, Pillar, Renamed, HasMode
>>>
>>> example_law = Law(
>>>     name="Name of the law",
>>>     description="Well detailed description what this law does.",
>>>     rules=(
>>>         Renamed(
>>>             name="Rename pyproject.toml if owned by gabor",
>>>             path=Path("./pyproject.toml"),
>>>             new_name="gabor-pyproject.toml"
>>>             preconditions=[
>>>                 HasMode(path=Path("scripts/run_unittests.sh"), mode=stat.S_IXOTH)
>>>             ]
>>>         ),
>>>     )
>>> )
>>>
>>> pillar = Pillar()
>>> pillar.register(example_law)
Parameters
  • path (Path) – Input file’s path

  • mode (str) – The desired mode to check

HasNoMode

class hammurabi.preconditions.attributes.HasNoMode(path: pathlib.Path, mode: int, **kwargs)[source]

Opposite of hammurabi.preconditions.attributes.HasMode.

Directories

IsDirectoryExist

class hammurabi.preconditions.directories.IsDirectoryExist(path: pathlib.Path, **kwargs)[source]

Check if the given directory exists.

Example usage:

>>> from pathlib import Path
>>> from hammurabi import Law, Pillar, Renamed, IsDirectoryExist
>>>
>>> example_law = Law(
>>>     name="Name of the law",
>>>     description="Well detailed description what this law does.",
>>>     rules=(
>>>         Renamed(
>>>             name="Rename the dir if an other one exists",
>>>             path=Path("old-name"),
>>>             new_name="new-name",
>>>             preconditions=[
>>>                 IsDirectoryExist(path=Path("other-dir"))
>>>             ]
>>>         ),
>>>     )
>>> )
>>>
>>> pillar = Pillar()
>>> pillar.register(example_law)
Parameters

path (Path) – Input directory’s path

IsDirectoryNotExist

class hammurabi.preconditions.directories.IsDirectoryNotExist(path: pathlib.Path, **kwargs)[source]

Opposite of hammurabi.preconditions.directories.IsDirectoryExist.

Files

IsFileExist

class hammurabi.preconditions.files.IsFileExist(path: pathlib.Path, **kwargs)[source]

Check if the given file exists.

Example usage:

>>> from pathlib import Path
>>> from hammurabi import Law, Pillar, Renamed, IsFileExist
>>>
>>> example_law = Law(
>>>     name="Name of the law",
>>>     description="Well detailed description what this law does.",
>>>     rules=(
>>>         Renamed(
>>>             name="Rename the file if an other one exists",
>>>             path=Path("old-name"),
>>>             new_name="new-name",
>>>             preconditions=[
>>>                 IsFileExist(path=Path("other-file"))
>>>             ]
>>>         ),
>>>     )
>>> )
>>>
>>> pillar = Pillar()
>>> pillar.register(example_law)
Parameters

path (Path) – Input files’s path

IsFileNotExist

class hammurabi.preconditions.files.IsFileNotExist(path: pathlib.Path, **kwargs)[source]

Opposite of hammurabi.preconditions.files.IsFileExist.

Text files

IsLineExist

class hammurabi.preconditions.text.IsLineExist(path: pathlib.Path, criteria: str, **kwargs)[source]

Check if the given line exists.

Example usage:

>>> from pathlib import Path
>>> from hammurabi import Law, Pillar, Renamed, IsLineExist
>>>
>>> example_law = Law(
>>>     name="Name of the law",
>>>     description="Well detailed description what this law does.",
>>>     rules=(
>>>         Renamed(
>>>             name="Rename the file if an other one exists",
>>>             path=Path("old-name"),
>>>             new_name="new-name",
>>>             preconditions=[
>>>                 IsLineExist(path=Path("other-file"), criteria=r"^string=some-value$")
>>>             ]
>>>         ),
>>>     )
>>> )
>>>
>>> pillar = Pillar()
>>> pillar.register(example_law)
Parameters
  • path (Path) – Input files’s path

  • criteria (str) – Regexp of the desired line

Warning

When using criteria be aware that partial matches will be recognized as well. This means you must be as strict with regular expressions as it is needed. Example of a partial match:

>>> import re
>>> pattern = re.compile(r"apple")
>>> text = "appletree"
>>> pattern.match(text).group()
>>> 'apple'

IsLineNotExist

class hammurabi.preconditions.text.IsLineNotExist(path: pathlib.Path, criteria: str, **kwargs)[source]

Opposite of hammurabi.preconditions.text.IsLineExist.