latexgit.formatters package

Code formatters.

Submodules

latexgit.formatters.python module

A formatter for python code.

latexgit.formatters.python.format_python(code, strip_docstrings=True, strip_comments=True, strip_hints=True)[source]

Format a python code fragment.

Parameters:
  • code (Iterable[str]) – the code fragment

  • strip_docstrings (bool, default: True) – should we delete docstrings?

  • strip_comments (bool, default: True) – should we delete comments?

  • strip_hints (bool, default: True) – should we delete type hints?

Return type:

list[str]

Returns:

the formatted code

latexgit.formatters.python.preprocess_python(code, lines=None, labels=None, params=None)[source]

Preprocess Python code.

First, we select all lines of the code we want to keep. If labels are defined, then lines can be kept as ranges or as single lines. Otherwise, all lines are selected in this step.

Then, if line numbers are provided, we selected the lines based on the line numbers from the lines we have preserved.

Finally, the Python formatter is applied.

Parameters:
  • code (list[str]) – the code loaded from a file

  • lines (list[int] | None, default: None) – the lines to keep, or None if we keep all

  • labels (Optional[Iterable[str]], default: None) – a list of labels marking start and end of code snippets to include

  • params (set[str] | None, default: None) – the arguments for the code formatter

Return type:

str

Returns:

the formatted code string

latexgit.formatters.source_tools module

In this file, we put some shared tools for rendering source codes.

latexgit.formatters.source_tools.format_empty_lines(lines, empty_before=<function <lambda>>, no_empty_after=<function <lambda>>, force_no_empty_after=<function <lambda>>, max_consecutive_empty_lines=2)[source]

Obtain a generator that strips any consecutive empty lines.

Parameters:
  • lines (Iterable[str]) – the original line iterable

  • empty_before (Callable, default: <function <lambda> at 0x7f58b06837e0>) – a function checking whether an empty line is required before a certain string

  • no_empty_after (Callable, default: <function <lambda> at 0x7f58b0683880>) – a function checking whether an empty line is prohibited after a string

  • force_no_empty_after (Callable, default: <function <lambda> at 0x7f58b0683920>) – a function checking whether an empty line is prohibited after a string

  • max_consecutive_empty_lines (int, default: 2) – the maximum number of permitted consecutive empty lines

Return type:

list[str]

Returns:

the generation

>>> code = ["", "a", "", "b", "", "", "c", "", "", "", "d", "e", ""]
>>> format_empty_lines(code, max_consecutive_empty_lines=3)
['a', '', 'b', '', '', 'c', '', '', '', 'd', 'e']
>>> format_empty_lines(code, max_consecutive_empty_lines=2)
['a', '', 'b', '', '', 'c', '', '', 'd', 'e']
>>> format_empty_lines(code, max_consecutive_empty_lines=1)
['a', '', 'b', '', 'c', '', 'd', 'e']
>>> format_empty_lines(code, max_consecutive_empty_lines=0)
['a', 'b', 'c', 'd', 'e']
>>> format_empty_lines(code, max_consecutive_empty_lines=2,
...                    no_empty_after=lambda s: s == "b")
['a', '', 'b', 'c', '', '', 'd', 'e']
>>> format_empty_lines(code, max_consecutive_empty_lines=2,
...                    no_empty_after=lambda s: s == "b",
...                    empty_before=lambda s: s == "e")
['a', '', 'b', 'c', '', '', 'd', '', 'e']
>>> format_empty_lines(code, max_consecutive_empty_lines=2,
...                    no_empty_after=lambda s: s == "b",
...                    empty_before=lambda s: s == "e",
...                    force_no_empty_after=lambda s: s == "d")
['a', '', 'b', 'c', '', '', 'd', 'e']
latexgit.formatters.source_tools.select_lines(code, lines=None, labels=None, line_comment_start='#', max_consecutive_empty_lines=2)[source]

Select lines of source code based on labels and line indices.

First, we select all lines of the code we want to keep. If labels are defined, then lines are kept as ranges or as single lines for all pre-defined labels. Ranges may overlap and/or intersect. Otherwise, all lines are selected in this step.

Then, if line numbers are provided, we selected the lines based on the line numbers from the lines we have preserved.

Finally, leading and trailing empty lines as well as superfluous empty lines are removed.

Parameters:
  • code (Iterable[str]) – the code loaded from a file

  • lines (Optional[Iterable[int]], default: None) – the lines to keep, or None if we keep all

  • labels (Optional[Iterable[str]], default: None) – a list of labels marking start and end of code snippets to include

  • line_comment_start (str, default: '#') – the string marking the line comment start

  • max_consecutive_empty_lines (int, default: 2) – the maximum number of permitted consecutive empty lines

Return type:

list[str]

Returns:

the list of selected lines

>>> select_lines(["def a():", "    b=c", "    return x"])
['def a():', '    b=c', '    return x']
>>> pc = ["# start x", "def a():", " b=c # -x", "    return x", "# end x"]
>>> select_lines(pc, labels={"x"})
['def a():', '    return x']
>>> select_lines(["def a():", "    b=c", "    return x"], lines=[0, 2])
['def a():', '    return x']
latexgit.formatters.source_tools.split_labels(labels)[source]

Get a sequence of labels from a string.

Parameters:

labels (str | None) – the labels string or None

Return type:

set[str]

Returns:

the labels set

>>> print(split_labels(None))
set()
>>> print(split_labels(""))
set()
>>> print(split_labels(","))
set()
>>> sorted(split_labels("a;b;d;c"))
['a', 'b', 'c', 'd']
>>> sorted(split_labels("a,b c+a;a\td;c"))
['a', 'b', 'c', 'd']
latexgit.formatters.source_tools.split_line_choices(lines)[source]

Split line choices to iterables of int or None.

This function converts 1-based line selections into 0-based ones.

Parameters:

lines (str | None) – the line choices

Return type:

list[int] | None

Returns:

the split line choices.

>>> print(split_line_choices(None))
None
>>> print(split_line_choices(""))
None
>>> print(split_line_choices(","))
None
>>> split_line_choices("1")
[0]
>>> split_line_choices("1,2")
[0, 1]
>>> split_line_choices("1-5")
[0, 1, 2, 3, 4]
>>> split_line_choices("3;4-5;7-7;22+12-15;")
[2, 3, 4, 6, 21, 11, 12, 13, 14]
latexgit.formatters.source_tools.strip_common_whitespace_prefix(lines)[source]

Strip a common whitespace prefix from a list of strings and merge them.

Parameters:

lines (Iterable[str]) – the lines

Return type:

list[str]

Returns:

the code with the white space prefix stripped

>>> strip_common_whitespace_prefix([" a", "  b"])
['a', ' b']
>>> strip_common_whitespace_prefix([" a", " b"])
['a', 'b']
>>> strip_common_whitespace_prefix(["  a", "  b"])
['a', 'b']
>>> strip_common_whitespace_prefix(["  a", "  b", "c"])
['  a', '  b', 'c']
>>> strip_common_whitespace_prefix([" a", "  b", "c"])
[' a', '  b', 'c']
>>> strip_common_whitespace_prefix(["  a", "  b", "    c"])
['a', 'b', '  c']