formate

Python formatting mate.

Functions:

call_hooks(hooks, source, filename)

Given a list of hooks (in order), call them in turn to reformat the source.

reformat_file(filename, config[, colour])

Reformat the given file, and show the diff if changes were made.

isort_hook(source, formate_filename[, …])

Call isort, using the given keyword arguments as its configuration.

yapf_hook(source[, formate_global_config])

Call yapf, using the given keyword arguments as its configuration.

Classes:

Reformatter(filename, config)

Reformat a Python source file.

call_hooks(hooks, source, filename)[source]

Given a list of hooks (in order), call them in turn to reformat the source.

Parameters
Return type

str

Returns

The reformatted source.

Changed in version 0.4.3: Added the filename argument.

reformat_file(filename, config, colour=None)[source]

Reformat the given file, and show the diff if changes were made.

Parameters
Return type

int

class Reformatter(filename, config)[source]

Bases: object

Reformat a Python source file.

Parameters

Attributes:

file_to_format

The filename being reformatted, as a POSIX-style path.

filename

The filename being reformatted.

config

The formate configuration, parsed from a TOML file (or similar).

Methods:

run()

Run the reformatter.

get_diff()

Returns the diff between the original and reformatted file content.

to_string()

Return the reformatted file as a string.

to_file()

Write the reformatted source to the original file.

file_to_format

Type:    PathPlus

The filename being reformatted, as a POSIX-style path.

filename

Type:    str

The filename being reformatted.

config

Type:    FormateConfigDict

The formate configuration, parsed from a TOML file (or similar).

run()[source]

Run the reformatter.

Return type

bool

Returns

Whether the file was changed.

get_diff()[source]

Returns the diff between the original and reformatted file content.

Return type

str

to_string()[source]

Return the reformatted file as a string.

Return type

str

to_file()[source]

Write the reformatted source to the original file.

isort_hook(source, formate_filename, formate_global_config=None, **kwargs)[source]

Call isort, using the given keyword arguments as its configuration.

Parameters
  • source (str) – The source to reformat.

  • formate_filename (Union[str, Path, PathLike]) – The path to the file being reformatted.

  • formate_global_config (Optional[Mapping]) – The global configuration dictionary. Optional. Default None.

  • **kwargs

Return type

str

Returns

The reformatted source.

yapf_hook(source, formate_global_config=None, **kwargs)[source]

Call yapf, using the given keyword arguments as its configuration.

Parameters
  • source (str) – The source to reformat.

  • formate_global_config (Optional[Mapping]) – The global configuration dictionary. Optional. Default None.

  • **kwargs

If yapf_style is given as a keyword argument, use that style. If a filename is given as the style it is searched for in the current and parent directories, and the style taken from the configuration in that file.

Return type

str

Returns

The reformatted source.