nitpick.formats module

Configuration file formats.

class nitpick.formats.BaseFormat(*, path: Optional[Union[pathlib.Path, str]] = None, string: Optional[str] = None, data: Optional[Union[Dict[str, Any], ruamel.yaml.comments.CommentedSeq, ruamel.yaml.comments.CommentedMap, nitpick.formats.BaseFormat]] = None, ignore_keys: Optional[List[str]] = None)[source]

Bases: object

Base class for configuration file formats.

Parameters
  • path – Path of the config file to be loaded.

  • string – Config in string format.

  • data – Config data in Python format (dict, YAMLFormat, TOMLFormat instances).

  • ignore_keys – List of keys to ignore when using the comparison methods.

property as_data: Union[Dict[str, Any], ruamel.yaml.comments.CommentedSeq, ruamel.yaml.comments.CommentedMap]

String content converted to a Python data structure (a dict, YAML data, etc.).

property as_string: str

Contents of the file or the original string provided when the instance was created.

classmethod cleanup(*args: List[Any]) List[Any][source]

Cleanup similar values according to the specific format. E.g.: YAMLFormat accepts ‘True’ or ‘true’.

compare_with_dictdiffer(expected: Optional[Union[Dict[str, Any], nitpick.formats.BaseFormat]] = None, transform_function: Optional[Callable] = None) nitpick.formats.Comparison[source]

Compare two structures and compute missing and different items using dictdiffer.

compare_with_flatten(expected: Optional[Union[Dict[str, Any], nitpick.formats.BaseFormat]] = None) nitpick.formats.Comparison[source]

Compare two flattened dictionaries and compute missing and different items.

abstract load()[source]

Load the configuration from a file, a string or a dict.

property reformatted: str

Reformat the configuration dict as a new string (it might not match the original string/file contents).

class nitpick.formats.Comparison(actual: Union[Dict[str, Any], ruamel.yaml.comments.CommentedSeq, ruamel.yaml.comments.CommentedMap, nitpick.formats.BaseFormat], expected: Union[Dict[str, Any], ruamel.yaml.comments.CommentedSeq, ruamel.yaml.comments.CommentedMap, nitpick.formats.BaseFormat], format_class: Type[nitpick.formats.BaseFormat])[source]

Bases: object

A comparison between two dictionaries, computing missing items and differences.

property has_changes: bool

Return True is there is a difference or something missing.

set_diff(diff_dict)[source]

Set the diff dict and corresponding format.

set_missing(missing_dict)[source]

Set the missing dict and corresponding format.

update_pair(key, raw_expected)[source]

Update a key on one of the comparison dicts, with its raw expected value.

class nitpick.formats.JSONFormat(*, path: Optional[Union[pathlib.Path, str]] = None, string: Optional[str] = None, data: Optional[Union[Dict[str, Any], ruamel.yaml.comments.CommentedSeq, ruamel.yaml.comments.CommentedMap, nitpick.formats.BaseFormat]] = None, ignore_keys: Optional[List[str]] = None)[source]

Bases: nitpick.formats.BaseFormat

JSON configuration format.

property as_data: Union[Dict[str, Any], ruamel.yaml.comments.CommentedSeq, ruamel.yaml.comments.CommentedMap]

String content converted to a Python data structure (a dict, YAML data, etc.).

property as_string: str

Contents of the file or the original string provided when the instance was created.

classmethod cleanup(*args: List[Any]) List[Any][source]

Cleanup similar values according to the specific format. E.g.: YAMLFormat accepts ‘True’ or ‘true’.

compare_with_dictdiffer(expected: Optional[Union[Dict[str, Any], nitpick.formats.BaseFormat]] = None, transform_function: Optional[Callable] = None) nitpick.formats.Comparison

Compare two structures and compute missing and different items using dictdiffer.

compare_with_flatten(expected: Optional[Union[Dict[str, Any], nitpick.formats.BaseFormat]] = None) nitpick.formats.Comparison

Compare two flattened dictionaries and compute missing and different items.

load() bool[source]

Load a JSON file by its path, a string or a dict.

property reformatted: str

Reformat the configuration dict as a new string (it might not match the original string/file contents).

class nitpick.formats.TOMLFormat(*, path: Optional[Union[pathlib.Path, str]] = None, string: Optional[str] = None, data: Optional[Union[Dict[str, Any], ruamel.yaml.comments.CommentedSeq, ruamel.yaml.comments.CommentedMap, nitpick.formats.BaseFormat]] = None, ignore_keys: Optional[List[str]] = None)[source]

Bases: nitpick.formats.BaseFormat

TOML configuration format.

property as_data: Union[Dict[str, Any], ruamel.yaml.comments.CommentedSeq, ruamel.yaml.comments.CommentedMap]

String content converted to a Python data structure (a dict, YAML data, etc.).

property as_string: str

Contents of the file or the original string provided when the instance was created.

classmethod cleanup(*args: List[Any]) List[Any]

Cleanup similar values according to the specific format. E.g.: YAMLFormat accepts ‘True’ or ‘true’.

compare_with_dictdiffer(expected: Optional[Union[Dict[str, Any], nitpick.formats.BaseFormat]] = None, transform_function: Optional[Callable] = None) nitpick.formats.Comparison

Compare two structures and compute missing and different items using dictdiffer.

compare_with_flatten(expected: Optional[Union[Dict[str, Any], nitpick.formats.BaseFormat]] = None) nitpick.formats.Comparison

Compare two flattened dictionaries and compute missing and different items.

load() bool[source]

Load a TOML file by its path, a string or a dict.

property reformatted: str

Reformat the configuration dict as a new string (it might not match the original string/file contents).

class nitpick.formats.YAMLFormat(*, path: Optional[Union[pathlib.Path, str]] = None, string: Optional[str] = None, data: Optional[Union[Dict[str, Any], ruamel.yaml.comments.CommentedSeq, ruamel.yaml.comments.CommentedMap, nitpick.formats.BaseFormat]] = None, ignore_keys: Optional[List[str]] = None)[source]

Bases: nitpick.formats.BaseFormat

YAML configuration format.

property as_data: ruamel.yaml.comments.CommentedMap

On YAML, this dict is a special object with comments and ordered keys.

property as_list: ruamel.yaml.comments.CommentedSeq

A list of dicts. On YAML, as_data might contain a list. This property is just a proxy for typing.

property as_string: str

Contents of the file or the original string provided when the instance was created.

classmethod cleanup(*args: List[Any]) List[Any][source]

A boolean “True” or “true” might have the same effect on YAML.

compare_with_dictdiffer(expected: Optional[Union[Dict[str, Any], nitpick.formats.BaseFormat]] = None, transform_function: Optional[Callable] = None) nitpick.formats.Comparison

Compare two structures and compute missing and different items using dictdiffer.

compare_with_flatten(expected: Optional[Union[Dict[str, Any], nitpick.formats.BaseFormat]] = None) nitpick.formats.Comparison

Compare two flattened dictionaries and compute missing and different items.

load() bool[source]

Load a YAML file by its path, a string or a dict.

property reformatted: str

Reformat the configuration dict as a new string (it might not match the original string/file contents).