Python Formatter

Contents:

Readme

Introduction

Test Status Coverage Status

formatter2 is a Python formatter based on the tokenize library in Python. Due to a bug with line continuations we are currently running a fork of the tokenize library however.

During every format sequence the compile method is used to check if no code has been broken, but backing up is still a good idea.

The library is made to be very extendable and configurable but it’s still in the beginning phase. Eventually this will become a fully featured formatting tool for Python.

Install

To install simply execute python setup.py install or pip install formatter2. If you want to run the tests first, run nosetests

Usage

To format all of your code recursively (MAKE BACKUPS!):

python-formatter -r DIRECTORY

Or:

format-python -r DIRECTORY

Formatting stdin is also possible:

cat some_python_file.py | format-python - > some_formatted_file.py

formatter Package

formatter Package

class formatter2.Formatter[source]

Bases: object

format_directory(directory)[source]
format_file(name)[source]
format_path(path, recursive=False)[source]
classmethod format_string(string)[source]
class formatter2.Token(offsets, tok_type, token, begin, end, line)[source]

Bases: object

begin

The begin coordinates.

Settings this to a tuple will set the first to the row and the latter to the col.

This returns a SmartList with the row and the column.

begin_col

The begin column.

begin_row

The begin row.

col

The column.

Setting this to a tuple will set the first to the begin and the latter to the end.

This returns a SmartList with the begin and end column.

end

The end.

Settings this to a tuple will set the first to the row and the latter to the col.

This returns a SmartList with the row and column.

end_col

The end column.

end_row

The end row.

line

The line.

Setting the line automatically updates the column as well.

Returns a str() object with the line.

preprocess()[source]

formatter2.types.TokenType.preprocess()

row

The row.

Setting this to a tuple will set the first to the begin and the latter to the end.

This returns a SmartList with the begin and end row.

type
class formatter2.Tokens(readline)[source]

Bases: object

format()[source]

Format the string, expects the extra whitespace to be removed already

classmethod from_fh(fh)[source]
classmethod from_readline(readline)[source]
generate_tokens(readline)[source]
strip()[source]

Strip all whitespace so we can begin formatting

to_str()[source]

Convert the tokens back to a string

class formatter2.TokenOffsets(parent, default_type=None)[source]

Bases: dict

get(key, recurse=False)[source]
get_key(key)[source]
update(other)[source]
class formatter2.TokenOffset(parent, type_, token, pre=0, post=0, children=None, end=None, pre_collapse=True, post_collapse=True)[source]

Bases: object

copy(type_=None, token=None)[source]
end

Set the end token in case of children.

Should either be a token or a tuple with TokenType and token which will be a string.

parent

The parent. TokenOffsets

surround

Surround the token with this amount of space. Setting will set the pre and post when given a tuple or int().

type

The type. TokenType

update(other)[source]
class formatter2.DefaultTokenOffset(parent=None)[source]

Bases: formatter2.offsets.TokenOffset

pprint(stream=<open file '<stderr>', mode 'w'>)[source]

formatter Module

class formatter2.formatter.Formatter[source]

Bases: object

format_directory(directory)[source]
format_file(name)[source]
format_path(path, recursive=False)[source]
classmethod format_string(string)[source]

main Module

formatter2.main.main(*argv)[source]

offsets Module

class formatter2.offsets.DefaultTokenOffset(parent=None)[source]

Bases: formatter2.offsets.TokenOffset

pprint(stream=<open file '<stderr>', mode 'w'>)[source]
class formatter2.offsets.TokenOffset(parent, type_, token, pre=0, post=0, children=None, end=None, pre_collapse=True, post_collapse=True)[source]

Bases: object

copy(type_=None, token=None)[source]
end

Set the end token in case of children.

Should either be a token or a tuple with TokenType and token which will be a string.

parent

The parent. TokenOffsets

surround

Surround the token with this amount of space. Setting will set the pre and post when given a tuple or int().

type

The type. TokenType

update(other)[source]
class formatter2.offsets.TokenOffsets(parent, default_type=None)[source]

Bases: dict

get(key, recurse=False)[source]
get_key(key)[source]
update(other)[source]
formatter2.offsets.get_token_offsets()[source]

tokens Module

class formatter2.tokens.SmartList(*items, **kwargs)[source]

Bases: list

copy()[source]
set(value)[source]
class formatter2.tokens.Token(offsets, tok_type, token, begin, end, line)[source]

Bases: object

begin

The begin coordinates.

Settings this to a tuple will set the first to the row and the latter to the col.

This returns a SmartList with the row and the column.

begin_col

The begin column.

begin_row

The begin row.

col

The column.

Setting this to a tuple will set the first to the begin and the latter to the end.

This returns a SmartList with the begin and end column.

end

The end.

Settings this to a tuple will set the first to the row and the latter to the col.

This returns a SmartList with the row and column.

end_col

The end column.

end_row

The end row.

line

The line.

Setting the line automatically updates the column as well.

Returns a str() object with the line.

preprocess()[source]

formatter2.types.TokenType.preprocess()

row

The row.

Setting this to a tuple will set the first to the begin and the latter to the end.

This returns a SmartList with the begin and end row.

type
class formatter2.tokens.Tokens(readline)[source]

Bases: object

format()[source]

Format the string, expects the extra whitespace to be removed already

classmethod from_fh(fh)[source]
classmethod from_readline(readline)[source]
generate_tokens(readline)[source]
strip()[source]

Strip all whitespace so we can begin formatting

to_str()[source]

Convert the tokens back to a string

types Module

class formatter2.types.CommentTokenType(id_, name)[source]

Bases: formatter2.types.TokenType

name = 'COMMENT'
preprocess(token)[source]
class formatter2.types.DefaultTokenType[source]

Bases: formatter2.types.TokenType

class formatter2.types.IndentTokenType(id_, name)[source]

Bases: formatter2.types.TokenType

name = 'INDENT'
preprocess(token)[source]

Replace tabs with spaces as pep8 dictates

class formatter2.types.StringTokenType(id_, name)[source]

Bases: formatter2.types.TokenType

name = 'STRING'
preprocess(token)[source]

Preprocess the token

This automatically replaces strings with ” to ‘ if possible

class formatter2.types.TokenType(id_, name)[source]

Bases: object

get_keys()[source]
preprocess(token)[source]

Preprocess the token, this can do pretty much anything including changing the column

class formatter2.types.TokenTypes[source]

Bases: dict

register(token_type)[source]
formatter2.types.get_token_types()[source]

Indices and tables