Skip to content

Commit

Permalink
Ruff linting, Python 3.9+ (#45)
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasst authored Jun 27, 2024
1 parent c867f9f commit 7acc68c
Show file tree
Hide file tree
Showing 9 changed files with 263 additions and 129 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:
- uses: actions/setup-python@v2
name: Install Python
with:
python-version: 3.8
python-version: 3.9

- run: |
pip install packaging
Expand Down
10 changes: 6 additions & 4 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
lint:
strategy:
matrix:
python-version: [ '3.8', '3.9', '3.10', '3.11' ]
python-version: ['3.9', '3.10', '3.11']
name: Lint ${{ matrix.python-version }}
runs-on: 'ubuntu-20.04'
container: python:${{ matrix.python-version }}
Expand All @@ -21,14 +21,16 @@ jobs:

- name: Lint code
run: |
pip install lintlizard==0.18.0 "click<8.1"
lintlizard
pip install ruff==0.5.0
ruff check
ruff format --check
ruff check --select I
# Run tests
test:
strategy:
matrix:
python-version: ['3.8', '3.9', '3.10', '3.11']
python-version: ['3.9', '3.10', '3.11']
# Do not cancel any jobs when a single job fails
fail-fast: false
name: Python ${{ matrix.python-version }}
Expand Down
66 changes: 56 additions & 10 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,10 +1,56 @@
[tool.black]
target-version = ['py37']
exclude = '''
/(
\.git
| \.venv
| venv
| src
)/
'''
[tool.ruff]
target-version = "py39"
line-length = 79

[tool.ruff.lint]
ignore = [
"ISC001",
"PLR2004",
"TRY003",
# Some patterns contain special characters.
"PLR0911",
"RUF001",
]
select = [
"A001",
"B",
"C",
"E",
"EXE",
"F",
"G",
"I",
"INP",
"ISC",
"N",
"PGH",
"PIE",
"PL",
"PT",
"RET",
"RUF",
"S",
"SIM",
"T",
"TCH",
"TID25",
"TRY",
"UP",
"W",
# Consider enabling later.
# "ANN",
# "PTH",
]

[tool.ruff.lint.isort]
combine-as-imports = true
forced-separate = ["tests"]

[tool.ruff.lint.mccabe]
max-complexity = 15

[tool.ruff.lint.pylint]
max-branches = 16

[tool.ruff.lint.per-file-ignores]
"tests/test_quotequail.py" = ["E501", "PT009"]
69 changes: 37 additions & 32 deletions quotequail/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
# quotequail
# a library that identifies quoted text in email messages

Expand All @@ -16,12 +15,14 @@ def quote(text, limit=1000):
Example: [(True, 'expanded text'), (False, '> Some quoted text')]
Unless the limit param is set to None, the text will automatically be quoted
starting at the line where the limit is reached.
Unless the limit param is set to None, the text will automatically be
quoted starting at the line where the limit is reached.
"""
lines = text.split("\n")

found = _internal.find_quote_position(lines, _patterns.MAX_WRAP_LINES, limit)
found = _internal.find_quote_position(
lines, _patterns.MAX_WRAP_LINES, limit
)

if found is not None:
return [
Expand Down Expand Up @@ -49,11 +50,11 @@ def quote_html(html, limit=1000):
if found is None:
# No quoting found and we're below limit. We're done.
return [(True, _html.render_html_tree(tree))]
else:
start_tree = _html.slice_tree(
tree, start_refs, end_refs, (0, found + 1), html_copy=html
)
end_tree = _html.slice_tree(tree, start_refs, end_refs, (found + 1, None))

start_tree = _html.slice_tree(
tree, start_refs, end_refs, (0, found + 1), html_copy=html
)
end_tree = _html.slice_tree(tree, start_refs, end_refs, (found + 1, None))

return [
(True, _html.render_html_tree(start_tree)),
Expand Down Expand Up @@ -83,35 +84,37 @@ def unwrap(text):
_patterns.MIN_HEADER_LINES,
_patterns.MIN_QUOTED_LINES,
)
if result:
typ, top_range, hdrs, main_range, bottom_range, needs_unindent = result
if not result:
return None

text_top = lines[slice(*top_range)] if top_range else ""
text = lines[slice(*main_range)] if main_range else ""
text_bottom = lines[slice(*bottom_range)] if bottom_range else ""
typ, top_range, hdrs, main_range, bottom_range, needs_unindent = result

if needs_unindent:
text = _internal.unindent_lines(text)
text_top = lines[slice(*top_range)] if top_range else ""
text = lines[slice(*main_range)] if main_range else ""
text_bottom = lines[slice(*bottom_range)] if bottom_range else ""

result = {
"type": typ,
}
if needs_unindent:
text = _internal.unindent_lines(text)

text = "\n".join(text).strip()
text_top = "\n".join(text_top).strip()
text_bottom = "\n".join(text_bottom).strip()
result = {
"type": typ,
}

if text:
result["text"] = text
if text_top:
result["text_top"] = text_top
if text_bottom:
result["text_bottom"] = text_bottom
text = "\n".join(text).strip()
text_top = "\n".join(text_top).strip()
text_bottom = "\n".join(text_bottom).strip()

if hdrs:
result.update(hdrs)
if text:
result["text"] = text
if text_top:
result["text_top"] = text_top
if text_bottom:
result["text_bottom"] = text_bottom

return result
if hdrs:
result.update(hdrs)

return result


def unwrap_html(html):
Expand Down Expand Up @@ -164,7 +167,9 @@ def unwrap_html(html):
result["html_bottom"] = html_bottom

if main_range:
main_tree = _html.slice_tree(tree, start_refs, end_refs, main_range)
main_tree = _html.slice_tree(
tree, start_refs, end_refs, main_range
)
if needs_unindent:
_html.unindent_tree(main_tree)
html = _html.render_html_tree(main_tree)
Expand Down
23 changes: 14 additions & 9 deletions quotequail/_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ def get_html_tree(html):
for el in tree.iter():
if el.nsmap or (isinstance(el.tag, str) and ":" in el.tag):
if el.nsmap:
actual_tag_name = "{}:{}".format(list(el.nsmap.keys())[0], el.tag)
actual_tag_name = f"{next(iter(el.nsmap.keys()))}:{el.tag}"
else:
actual_tag_name = el.tag
el.tag = "span"
Expand Down Expand Up @@ -355,7 +355,9 @@ def _trim_spaces(text):
line_break = tag_name == "br" and state == BEGIN
is_block = tag_name not in INLINE_TAGS
is_forward = (
is_block and state == BEGIN and el.attrib.get("style") in FORWARD_STYLES
is_block
and state == BEGIN
and el.attrib.get("style") in FORWARD_STYLES
)

if is_block or line_break:
Expand All @@ -371,7 +373,12 @@ def _trim_spaces(text):

if is_forward:
# Simulate forward
yield (end_ref, end_ref, start_indentation_level, FORWARD_LINE)
yield (
end_ref,
end_ref,
start_indentation_level,
FORWARD_LINE,
)
counter += 1
if max_lines is not None and counter > max_lines:
return
Expand All @@ -384,7 +391,7 @@ def _trim_spaces(text):
line += token

else:
raise RuntimeError("invalid token: {}".format(token))
raise RuntimeError(f"invalid token: {token}")

line = _trim_spaces(line)
if line:
Expand All @@ -402,9 +409,8 @@ def indented_tree_line_generator(el, max_lines=None):
gen = tree_line_generator(el, max_lines)
for start_ref, end_ref, indentation_level, line in gen:
# Escape line
if line.startswith(">"):
line = "\\" + line
yield start_ref, end_ref, "> " * indentation_level + line
full_line = "\\" + line if line.startswith(">") else line
yield start_ref, end_ref, "> " * indentation_level + full_line


def get_line_info(tree, max_lines=None):
Expand All @@ -417,5 +423,4 @@ def get_line_info(tree, max_lines=None):
line_gen_result = list(zip(*line_gen))
if line_gen_result:
return line_gen_result
else:
return [], [], []
return [], [], []
Loading

0 comments on commit 7acc68c

Please sign in to comment.