hammurabi.preconditions package¶
Submodules¶
hammurabi.preconditions.attributes module¶
This module contains the definition of Preconditions which are related to attributes of a file or directory.
-
class
hammurabi.preconditions.attributes.
HasMode
(path: pathlib.Path, mode: int, **kwargs)[source]¶ Bases:
hammurabi.preconditions.base.Precondition
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
-
made_changes
¶
-
name
¶
-
param
¶
-
class
hammurabi.preconditions.attributes.
HasNoMode
(path: pathlib.Path, mode: int, **kwargs)[source]¶ Bases:
hammurabi.preconditions.attributes.HasMode
Opposite of
hammurabi.preconditions.attributes.HasMode
.-
made_changes
¶
-
name
¶
-
param
¶
-
-
class
hammurabi.preconditions.attributes.
IsNotOwnedBy
(path: pathlib.Path, owner: str, **kwargs)[source]¶ Bases:
hammurabi.preconditions.attributes.IsOwnedBy
Opposite of
hammurabi.preconditions.attributes.IsOwnedBy
.-
made_changes
¶
-
name
¶
-
param
¶
-
-
class
hammurabi.preconditions.attributes.
IsOwnedBy
(path: pathlib.Path, owner: str, **kwargs)[source]¶ Bases:
hammurabi.preconditions.base.Precondition
Check if the given file or directory has the required ownership.
To check only the user use
owner="username"
. To check only the group useowner=":group_name"
(please note the colon:
). It is also possible to check both username and group at the same time by usingowner="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
-
made_changes
¶
-
name
¶
-
param
¶
hammurabi.preconditions.base module¶
This module contains the definition of Preconditions which describes what to do with the received parameter and does the necessary changes. The preconditions are used to enable developers skipping or enabling rules based on a set of conditions.
Warning
The precondition is for checking that a rule should or shouldn’t run, not for breaking/aborting the execution. To indicate a precondition failure as an error in the logs, create a precondition which raises an exception if the requirements doesn’t match.
-
class
hammurabi.preconditions.base.
Precondition
(name: Optional[str] = None, param: Optional[Any] = None)[source]¶ Bases:
hammurabi.rules.abstract.AbstractRule
,abc.ABC
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 IsFileExists(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
-
made_changes
¶
-
name
¶
-
param
¶
-
abstract
task
() → bool[source]¶ Abstract method representing how a
hammurabi.rules.base.Precondition.task()
must be parameterized. Any difference in the parameters or return type will result in pylint/mypy errors.To be able to use the power of
pipe
andchildren
, return something which can be generally used for other rules as in input.- Returns
Returns an output which can be used as an input for other rules
- Return type
Any (usually same as self.param’s type)
hammurabi.preconditions.directories module¶
This module contains the definition of Preconditions which are related to directories.
-
class
hammurabi.preconditions.directories.
IsDirectoryExists
(path: pathlib.Path, **kwargs)[source]¶ Bases:
hammurabi.preconditions.base.Precondition
Check if the given directory exists.
Example usage:
>>> from pathlib import Path >>> from hammurabi import Law, Pillar, Renamed, IsDirectoryExists >>> >>> 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=[ >>> IsDirectoryExists(path=Path("other-dir")) >>> ] >>> ), >>> ) >>> ) >>> >>> pillar = Pillar() >>> pillar.register(example_law)
- Parameters
path (Path) – Input directory’s path
-
made_changes
¶
-
name
¶
-
param
¶
-
class
hammurabi.preconditions.directories.
IsDirectoryNotExists
(path: pathlib.Path, **kwargs)[source]¶ Bases:
hammurabi.preconditions.directories.IsDirectoryExists
Opposite of
hammurabi.preconditions.directories.IsDirectoryExists
.-
made_changes
¶
-
name
¶
-
param
¶
-
hammurabi.preconditions.files module¶
Files preconditions module contains simple preconditions used for checking file existence.
-
class
hammurabi.preconditions.files.
IsFileExists
(path: pathlib.Path, **kwargs)[source]¶ Bases:
hammurabi.preconditions.base.Precondition
Check if the given file exists.
Example usage:
>>> from pathlib import Path >>> from hammurabi import Law, Pillar, Renamed, IsFileExists >>> >>> 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=[ >>> IsFileExists(path=Path("other-file")) >>> ] >>> ), >>> ) >>> ) >>> >>> pillar = Pillar() >>> pillar.register(example_law)
- Parameters
path (Path) – Input files’s path
-
made_changes
¶
-
name
¶
-
param
¶
-
class
hammurabi.preconditions.files.
IsFileNotExists
(path: pathlib.Path, **kwargs)[source]¶ Bases:
hammurabi.preconditions.files.IsFileExists
Opposite of
hammurabi.preconditions.files.IsFileExists
.-
made_changes
¶
-
name
¶
-
param
¶
-
hammurabi.preconditions.text module¶
This module contains the definition of Preconditions which are related to general text files.
-
class
hammurabi.preconditions.text.
IsLineExists
(path: pathlib.Path, criteria: str, **kwargs)[source]¶ Bases:
hammurabi.preconditions.base.Precondition
Check if the given line exists.
Example usage:
>>> from pathlib import Path >>> from hammurabi import Law, Pillar, Renamed, IsLineExists >>> >>> 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=[ >>> IsLineExists(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
-
made_changes
¶
-
name
¶
-
param
¶
-
class
hammurabi.preconditions.text.
IsLineNotExists
(path: pathlib.Path, criteria: str, **kwargs)[source]¶ Bases:
hammurabi.preconditions.text.IsLineExists
Opposite of
hammurabi.preconditions.text.IsLineExists
.-
made_changes
¶
-
name
¶
-
param
¶
-