Skip to content

Commit a8435a1

Browse files
committed
lint
1 parent c867f9f commit a8435a1

File tree

7 files changed

+248
-120
lines changed

7 files changed

+248
-120
lines changed

.github/workflows/test.yaml

+4-2
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,10 @@ jobs:
2121

2222
- name: Lint code
2323
run: |
24-
pip install lintlizard==0.18.0 "click<8.1"
25-
lintlizard
24+
pip install ruff==0.5.0
25+
ruff check
26+
ruff format --check
27+
ruff check --select I
2628
2729
# Run tests
2830
test:

pyproject.toml

+53-10
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,53 @@
1-
[tool.black]
2-
target-version = ['py37']
3-
exclude = '''
4-
/(
5-
\.git
6-
| \.venv
7-
| venv
8-
| src
9-
)/
10-
'''
1+
[tool.ruff]
2+
target-version = "py39"
3+
line-length = 79
4+
5+
[tool.ruff.lint]
6+
ignore = [
7+
"ISC001",
8+
"PLR2004",
9+
"S101",
10+
"S201",
11+
# Some patterns contain special characters.
12+
"RUF001",
13+
]
14+
select = [
15+
"A001",
16+
"B",
17+
"C",
18+
"E",
19+
"EXE",
20+
"F",
21+
"G",
22+
"I",
23+
"INP",
24+
"ISC",
25+
"N",
26+
"PGH",
27+
"PIE",
28+
"PL",
29+
"PT",
30+
"RET",
31+
"RUF",
32+
"S",
33+
"SIM",
34+
"T",
35+
"TCH",
36+
"TID25",
37+
"TRY",
38+
"UP",
39+
"W",
40+
# Consider enabling later.
41+
# "ANN",
42+
# "PTH",
43+
]
44+
45+
[tool.ruff.lint.isort]
46+
combine-as-imports = true
47+
forced-separate = ["tests"]
48+
49+
[tool.ruff.lint.mccabe]
50+
max-complexity = 15
51+
52+
[tool.ruff.lint.pylint]
53+
max-branches = 11

quotequail/__init__.py

+37-32
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
# -*- coding: utf-8 -*-
21
# quotequail
32
# a library that identifies quoted text in email messages
43

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

24-
found = _internal.find_quote_position(lines, _patterns.MAX_WRAP_LINES, limit)
23+
found = _internal.find_quote_position(
24+
lines, _patterns.MAX_WRAP_LINES, limit
25+
)
2526

2627
if found is not None:
2728
return [
@@ -49,11 +50,11 @@ def quote_html(html, limit=1000):
4950
if found is None:
5051
# No quoting found and we're below limit. We're done.
5152
return [(True, _html.render_html_tree(tree))]
52-
else:
53-
start_tree = _html.slice_tree(
54-
tree, start_refs, end_refs, (0, found + 1), html_copy=html
55-
)
56-
end_tree = _html.slice_tree(tree, start_refs, end_refs, (found + 1, None))
53+
54+
start_tree = _html.slice_tree(
55+
tree, start_refs, end_refs, (0, found + 1), html_copy=html
56+
)
57+
end_tree = _html.slice_tree(tree, start_refs, end_refs, (found + 1, None))
5758

5859
return [
5960
(True, _html.render_html_tree(start_tree)),
@@ -83,35 +84,37 @@ def unwrap(text):
8384
_patterns.MIN_HEADER_LINES,
8485
_patterns.MIN_QUOTED_LINES,
8586
)
86-
if result:
87-
typ, top_range, hdrs, main_range, bottom_range, needs_unindent = result
87+
if not result:
88+
return None
8889

89-
text_top = lines[slice(*top_range)] if top_range else ""
90-
text = lines[slice(*main_range)] if main_range else ""
91-
text_bottom = lines[slice(*bottom_range)] if bottom_range else ""
90+
typ, top_range, hdrs, main_range, bottom_range, needs_unindent = result
9291

93-
if needs_unindent:
94-
text = _internal.unindent_lines(text)
92+
text_top = lines[slice(*top_range)] if top_range else ""
93+
text = lines[slice(*main_range)] if main_range else ""
94+
text_bottom = lines[slice(*bottom_range)] if bottom_range else ""
9595

96-
result = {
97-
"type": typ,
98-
}
96+
if needs_unindent:
97+
text = _internal.unindent_lines(text)
9998

100-
text = "\n".join(text).strip()
101-
text_top = "\n".join(text_top).strip()
102-
text_bottom = "\n".join(text_bottom).strip()
99+
result = {
100+
"type": typ,
101+
}
103102

104-
if text:
105-
result["text"] = text
106-
if text_top:
107-
result["text_top"] = text_top
108-
if text_bottom:
109-
result["text_bottom"] = text_bottom
103+
text = "\n".join(text).strip()
104+
text_top = "\n".join(text_top).strip()
105+
text_bottom = "\n".join(text_bottom).strip()
110106

111-
if hdrs:
112-
result.update(hdrs)
107+
if text:
108+
result["text"] = text
109+
if text_top:
110+
result["text_top"] = text_top
111+
if text_bottom:
112+
result["text_bottom"] = text_bottom
113113

114-
return result
114+
if hdrs:
115+
result.update(hdrs)
116+
117+
return result
115118

116119

117120
def unwrap_html(html):
@@ -164,7 +167,9 @@ def unwrap_html(html):
164167
result["html_bottom"] = html_bottom
165168

166169
if main_range:
167-
main_tree = _html.slice_tree(tree, start_refs, end_refs, main_range)
170+
main_tree = _html.slice_tree(
171+
tree, start_refs, end_refs, main_range
172+
)
168173
if needs_unindent:
169174
_html.unindent_tree(main_tree)
170175
html = _html.render_html_tree(main_tree)

quotequail/_html.py

+13-8
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ def get_html_tree(html):
220220
for el in tree.iter():
221221
if el.nsmap or (isinstance(el.tag, str) and ":" in el.tag):
222222
if el.nsmap:
223-
actual_tag_name = "{}:{}".format(list(el.nsmap.keys())[0], el.tag)
223+
actual_tag_name = f"{next(iter(el.nsmap.keys()))}:{el.tag}"
224224
else:
225225
actual_tag_name = el.tag
226226
el.tag = "span"
@@ -355,7 +355,9 @@ def _trim_spaces(text):
355355
line_break = tag_name == "br" and state == BEGIN
356356
is_block = tag_name not in INLINE_TAGS
357357
is_forward = (
358-
is_block and state == BEGIN and el.attrib.get("style") in FORWARD_STYLES
358+
is_block
359+
and state == BEGIN
360+
and el.attrib.get("style") in FORWARD_STYLES
359361
)
360362

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

372374
if is_forward:
373375
# Simulate forward
374-
yield (end_ref, end_ref, start_indentation_level, FORWARD_LINE)
376+
yield (
377+
end_ref,
378+
end_ref,
379+
start_indentation_level,
380+
FORWARD_LINE,
381+
)
375382
counter += 1
376383
if max_lines is not None and counter > max_lines:
377384
return
@@ -402,9 +409,8 @@ def indented_tree_line_generator(el, max_lines=None):
402409
gen = tree_line_generator(el, max_lines)
403410
for start_ref, end_ref, indentation_level, line in gen:
404411
# Escape line
405-
if line.startswith(">"):
406-
line = "\\" + line
407-
yield start_ref, end_ref, "> " * indentation_level + line
412+
full_line = "\\" + line if line.startswith(">") else line
413+
yield start_ref, end_ref, "> " * indentation_level + full_line
408414

409415

410416
def get_line_info(tree, max_lines=None):
@@ -417,5 +423,4 @@ def get_line_info(tree, max_lines=None):
417423
line_gen_result = list(zip(*line_gen))
418424
if line_gen_result:
419425
return line_gen_result
420-
else:
421-
return [], [], []
426+
return [], [], []

0 commit comments

Comments
 (0)