formate.classes

Core classes.

Classes:

FormateConfigDict

typing.TypedDict representing the configuration mapping parsed from formate.toml or similar.

ExpandedHookDict

typing.TypedDict representing the expanded form of a hook in the mapping parsed from the config file.

EntryPoint(name, obj)

Represents an entry point for a hook.

Hook(name[, priority, args, kwargs, …])

Represents a formate reformatting hook.

Data:

HooksMapping

Type hint for the hooks key of the formate configuration mapping.

typeddict FormateConfigDict[source]

Bases: TypedDict

typing.TypedDict representing the configuration mapping parsed from formate.toml or similar.

Optional Keys
typeddict ExpandedHookDict[source]

Bases: TypedDict

typing.TypedDict representing the expanded form of a hook in the mapping parsed from the config file.

Required Keys
  • priority (int) – The priority of the hook.

Optional Keys
  • args (List[Any]) – The positional arguments passed to the hook function.

  • kwargs (Dict[str, Any]) – The keyword arguments passed to the hook function.

HooksMapping

Type hint for the hooks key of the formate configuration mapping.

Alias of Mapping[str, Union[int, ExpandedHookDict]]

class EntryPoint(name, obj)[source]

Bases: object

Represents an entry point for a hook.

Parameters
  • name (str) – The name of the entry point. The name is normalized into lowercase, with underscores replaced by hyphens.

  • obj (Callable[…, str]) – The object the entry point refers to.

Attributes:

name

The name of the entry point.

obj

The object the entry point refers to.

Methods:

__repr__()

Return a string representation of the EntryPoint.

from_dict(d)

Construct an instance of EntryPoint from a dictionary.

to_dict([convert_values])

Returns a dictionary containing the contents of the EntryPoint object.

name

Type:    str

The name of the entry point. The name is normalized into lowercase, with underscores replaced by hyphens.

obj

Type:    Callable[…, str]

The object the entry point refers to.

__repr__()

Return a string representation of the EntryPoint.

Return type

str

classmethod from_dict(d)

Construct an instance of EntryPoint from a dictionary.

Parameters

d (Mapping[str, Any]) – The dictionary.

to_dict(convert_values=False)

Returns a dictionary containing the contents of the EntryPoint object.

Parameters

convert_values (bool) – Recursively convert values into dictionaries, lists etc. as appropriate. Default False.

Return type

MutableMapping[str, Any]

class Hook(name, priority=10, args=(), kwargs={}, entry_point=None, global_config={})[source]

Bases: object

Represents a formate reformatting hook.

Parameters
  • name (str) – The name of the entry point. The name is normalized into lowercase, with underscores replaced by hyphens.

  • priority (int) – The priority of the hook. Default 10.

  • args – The positional arguments passed to the hook function. Default ().

  • kwargs (Dict[str, Any]) – The keyword arguments passed to the hook function. Default {}.

  • entry_point (Optional[EntryPoint]) – Default None.

  • global_config (Mapping[str, Any]) – A read-only view on the global configuration mapping, for hooks to do with as they wish. Default {}.

Attributes:

name

The name of the hook.

priority

The priority of the hook.

args

The positional arguments passed to the hook function.

kwargs

The keyword arguments passed to the hook function.

global_config

A read-only view on the global configuration mapping, for hooks to do with as they wish.

Methods:

parse(data)

Parse the given mapping into Hooks.

__call__(source, filename)

Call the hook.

__repr__()

Return a string representation of the Hook.

from_dict(d)

Construct an instance of Hook from a dictionary.

to_dict([convert_values])

Returns a dictionary containing the contents of the Hook object.

name

Type:    str

The name of the hook. The name is normalized into lowercase, with underscores replaced by hyphens.

priority

Type:    int

The priority of the hook.

args

Type:    Sequence[Any]

The positional arguments passed to the hook function.

kwargs

Type:    Dict[str, Any]

The keyword arguments passed to the hook function.

global_config

Type:    Mapping[str, Any]

A read-only view on the global configuration mapping, for hooks to do with as they wish.

classmethod parse(data)[source]

Parse the given mapping into Hooks.

Parameters

data (Mapping[str, Union[int, ExpandedHookDict]])

Return type

Iterator[Hook]

__call__(source, filename)[source]

Call the hook.

Parameters
Return type

str

Returns

The reformatted source.

Raises

TypeError if entry_point has not been set.

Changed in version 0.2.0: Added the filename argument.

__repr__()

Return a string representation of the Hook.

Return type

str

classmethod from_dict(d)

Construct an instance of Hook from a dictionary.

Parameters

d (Mapping[str, Any]) – The dictionary.

to_dict(convert_values=False)

Returns a dictionary containing the contents of the Hook object.

Parameters

convert_values (bool) – Recursively convert values into dictionaries, lists etc. as appropriate. Default False.

Return type

MutableMapping[str, Any]