nitpick.formats module

Configuration file formats.

class nitpick.formats.BaseFormat(*, path: Union[pathlib.Path, str] = None, string: str = None, data: Union[Dict[str, Any], ruamel.yaml.comments.CommentedSeq, ruamel.yaml.comments.CommentedMap, BaseFormat] = None, ignore_keys: 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

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

property as_string

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: Union[Dict[str, Any], BaseFormat] = None, transform_function: Callable = None)nitpick.formats.Comparison[source]

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

compare_with_flatten(expected: Union[Dict[str, Any], 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

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, BaseFormat], expected: Union[Dict[str, Any], ruamel.yaml.comments.CommentedSeq, ruamel.yaml.comments.CommentedMap, BaseFormat], format_class: Type[BaseFormat])[source]

Bases: object

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

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: Union[pathlib.Path, str] = None, string: str = None, data: Union[Dict[str, Any], ruamel.yaml.comments.CommentedSeq, ruamel.yaml.comments.CommentedMap, BaseFormat] = None, ignore_keys: List[str] = None)[source]

Bases: nitpick.formats.BaseFormat

JSON configuration format.

property as_data

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

property as_string

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: Union[Dict[str, Any], BaseFormat] = None, transform_function: Callable = None)nitpick.formats.Comparison

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

compare_with_flatten(expected: Union[Dict[str, Any], 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

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

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

Bases: nitpick.formats.BaseFormat

TOML configuration format.

property as_data

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

property as_string

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: Union[Dict[str, Any], BaseFormat] = None, transform_function: Callable = None)nitpick.formats.Comparison

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

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

Compare two flattened dictionaries and compute missing and different items.

static group_name_for(file_name: str)str[source]

Return a TOML group name for a file name.

If the file name begins with a dot, remove it. Otherwise an error is raised: TomlDecodeError: Invalid group name ‘xxx’. Try quoting it.

load()bool[source]

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

property reformatted

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

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

Bases: nitpick.formats.BaseFormat

YAML configuration format.

property as_data

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

property as_list

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

property as_string

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: Union[Dict[str, Any], BaseFormat] = None, transform_function: Callable = None)nitpick.formats.Comparison

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

compare_with_flatten(expected: Union[Dict[str, Any], 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

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