mirror of
https://github.com/Kozea/WeasyPrint.git
synced 2024-10-26 20:57:35 +03:00
Use f-strings when possible
This commit is contained in:
parent
e6021da219
commit
6ade02b234
@ -96,7 +96,7 @@ def write_png(basename, pixels, width, height): # pragma: no cover
|
|||||||
directory = os.path.join(os.path.dirname(__file__), 'results')
|
directory = os.path.join(os.path.dirname(__file__), 'results')
|
||||||
if not os.path.isdir(directory):
|
if not os.path.isdir(directory):
|
||||||
os.mkdir(directory)
|
os.mkdir(directory)
|
||||||
filename = os.path.join(directory, basename + '.png')
|
filename = os.path.join(directory, f'{basename}.png')
|
||||||
image = Image.new('RGB', (width, height))
|
image = Image.new('RGB', (width, height))
|
||||||
image.putdata(pixels)
|
image.putdata(pixels)
|
||||||
image.save(filename)
|
image.save(filename)
|
||||||
@ -135,7 +135,7 @@ def assert_pixels_equal(name, width, height, raw, expected_raw, tolerance=0):
|
|||||||
write_png(name, raw, width, actual_height)
|
write_png(name, raw, width, actual_height)
|
||||||
expected_raw = [
|
expected_raw = [
|
||||||
pixel or (255, 255, 255) for pixel in expected_raw]
|
pixel or (255, 255, 255) for pixel in expected_raw]
|
||||||
write_png(name + '.expected', expected_raw, width, height)
|
write_png(f'{name}.expected', expected_raw, width, height)
|
||||||
x = i % width
|
x = i % width
|
||||||
y = i // width
|
y = i // width
|
||||||
assert 0, (
|
assert 0, (
|
||||||
|
@ -327,10 +327,10 @@ def test_command_line_render(tmpdir):
|
|||||||
_run('combined-UTF-16BE.html out3.pdf --encoding UTF-16BE')
|
_run('combined-UTF-16BE.html out3.pdf --encoding UTF-16BE')
|
||||||
assert tmpdir.join('out3.pdf').read_binary() == pdf_bytes
|
assert tmpdir.join('out3.pdf').read_binary() == pdf_bytes
|
||||||
|
|
||||||
_run(tmpdir.join('combined.html').strpath + ' out4.pdf')
|
_run(f'{tmpdir.join("combined.html").strpath} out4.pdf')
|
||||||
assert tmpdir.join('out4.pdf').read_binary() == pdf_bytes
|
assert tmpdir.join('out4.pdf').read_binary() == pdf_bytes
|
||||||
|
|
||||||
_run(path2url(tmpdir.join('combined.html').strpath) + ' out5.pdf')
|
_run(f'{path2url(tmpdir.join("combined.html").strpath)} out5.pdf')
|
||||||
assert tmpdir.join('out5.pdf').read_binary() == pdf_bytes
|
assert tmpdir.join('out5.pdf').read_binary() == pdf_bytes
|
||||||
|
|
||||||
_run('linked.html --debug out6.pdf') # test relative URLs
|
_run('linked.html --debug out6.pdf') # test relative URLs
|
||||||
@ -1036,10 +1036,10 @@ def test_http():
|
|||||||
(b'<html test=accept-encoding-header-fail>', [])
|
(b'<html test=accept-encoding-header-fail>', [])
|
||||||
),
|
),
|
||||||
}) as root_url:
|
}) as root_url:
|
||||||
assert HTML(root_url + '/gzip').etree_element.get('test') == 'ok'
|
assert HTML(f'{root_url}/gzip').etree_element.get('test') == 'ok'
|
||||||
assert HTML(root_url + '/deflate').etree_element.get('test') == 'ok'
|
assert HTML(f'{root_url}/deflate').etree_element.get('test') == 'ok'
|
||||||
assert HTML(
|
assert HTML(
|
||||||
root_url + '/raw-deflate').etree_element.get('test') == 'ok'
|
f'{root_url}/raw-deflate').etree_element.get('test') == 'ok'
|
||||||
|
|
||||||
|
|
||||||
@assert_no_logs
|
@assert_no_logs
|
||||||
|
@ -707,7 +707,7 @@ def test_hyphenate_character_5():
|
|||||||
@pytest.mark.parametrize('i', (range(1, len('hyphénation'))))
|
@pytest.mark.parametrize('i', (range(1, len('hyphénation'))))
|
||||||
def test_hyphenate_manual_1(i):
|
def test_hyphenate_manual_1(i):
|
||||||
for hyphenate_character in ('!', 'ù ù'):
|
for hyphenate_character in ('!', 'ù ù'):
|
||||||
word = 'hyphénation'[:i] + '\xad' + 'hyphénation'[i:]
|
word = f'{"hyphénation"[:i]}\xad{"hyphénation"[i:]}'
|
||||||
page, = render_pages(
|
page, = render_pages(
|
||||||
'<html style="width: 5em; font-family: weasyprint">'
|
'<html style="width: 5em; font-family: weasyprint">'
|
||||||
'<style>@font-face {'
|
'<style>@font-face {'
|
||||||
@ -729,7 +729,7 @@ def test_hyphenate_manual_1(i):
|
|||||||
@pytest.mark.parametrize('i', (range(1, len('hy phénation'))))
|
@pytest.mark.parametrize('i', (range(1, len('hy phénation'))))
|
||||||
def test_hyphenate_manual_2(i):
|
def test_hyphenate_manual_2(i):
|
||||||
for hyphenate_character in ('!', 'ù ù'):
|
for hyphenate_character in ('!', 'ù ù'):
|
||||||
word = 'hy phénation'[:i] + '\xad' + 'hy phénation'[i:]
|
word = f'{"hy phénation"[:i]}\xad{"hy phénation"[i:]}'
|
||||||
page, = render_pages(
|
page, = render_pages(
|
||||||
'<html style="width: 5em; font-family: weasyprint">'
|
'<html style="width: 5em; font-family: weasyprint">'
|
||||||
'<style>@font-face {'
|
'<style>@font-face {'
|
||||||
|
@ -22,7 +22,7 @@ def test_unicode():
|
|||||||
<p><img src="pattern.png"> {1}</p>
|
<p><img src="pattern.png"> {1}</p>
|
||||||
'''.format(style, text))
|
'''.format(style, text))
|
||||||
|
|
||||||
temp = tempfile.mkdtemp(prefix=text + '-')
|
temp = tempfile.mkdtemp(prefix=f'{text}-')
|
||||||
try:
|
try:
|
||||||
stylesheet = os.path.join(temp, 'style.css')
|
stylesheet = os.path.join(temp, 'style.css')
|
||||||
image = os.path.join(temp, 'pattern.png')
|
image = os.path.join(temp, 'pattern.png')
|
||||||
|
@ -81,7 +81,7 @@ def bounding_box_path(svg, node, font_size):
|
|||||||
while path_data:
|
while path_data:
|
||||||
path_data = path_data.strip()
|
path_data = path_data.strip()
|
||||||
if path_data.split(' ', 1)[0] in PATH_LETTERS:
|
if path_data.split(' ', 1)[0] in PATH_LETTERS:
|
||||||
letter, path_data = (path_data + ' ').split(' ', 1)
|
letter, path_data = (f'{path_data} ').split(' ', 1)
|
||||||
|
|
||||||
if letter in 'aA':
|
if letter in 'aA':
|
||||||
# Elliptical arc curve
|
# Elliptical arc curve
|
||||||
@ -141,7 +141,7 @@ def bounding_box_path(svg, node, font_size):
|
|||||||
|
|
||||||
elif letter in 'hH':
|
elif letter in 'hH':
|
||||||
# Horizontal line
|
# Horizontal line
|
||||||
x, path_data = (path_data + ' ').split(' ', 1)
|
x, path_data = (f'{path_data} ').split(' ', 1)
|
||||||
x, _ = svg.point(x, 0, font_size)
|
x, _ = svg.point(x, 0, font_size)
|
||||||
|
|
||||||
# Relative coordinate, convert to absolute
|
# Relative coordinate, convert to absolute
|
||||||
@ -187,7 +187,7 @@ def bounding_box_path(svg, node, font_size):
|
|||||||
|
|
||||||
elif letter in 'vV':
|
elif letter in 'vV':
|
||||||
# Vertical line
|
# Vertical line
|
||||||
y, path_data = (path_data + ' ').split(' ', 1)
|
y, path_data = (f'{path_data} ').split(' ', 1)
|
||||||
_, y = svg.point(0, y, font_size)
|
_, y = svg.point(0, y, font_size)
|
||||||
|
|
||||||
# Relative coordinate, convert to absolute
|
# Relative coordinate, convert to absolute
|
||||||
|
@ -29,7 +29,7 @@ def path(svg, node, font_size):
|
|||||||
while string:
|
while string:
|
||||||
string = string.strip()
|
string = string.strip()
|
||||||
if string.split(' ', 1)[0] in PATH_LETTERS:
|
if string.split(' ', 1)[0] in PATH_LETTERS:
|
||||||
letter, string = (string + ' ').split(' ', 1)
|
letter, string = (f'{string} ').split(' ', 1)
|
||||||
if last_letter in (None, 'z', 'Z') and letter not in 'mM':
|
if last_letter in (None, 'z', 'Z') and letter not in 'mM':
|
||||||
node.vertices.append(current_point)
|
node.vertices.append(current_point)
|
||||||
first_path_point = current_point
|
first_path_point = current_point
|
||||||
@ -181,7 +181,7 @@ def path(svg, node, font_size):
|
|||||||
|
|
||||||
elif letter in 'hH':
|
elif letter in 'hH':
|
||||||
# Horizontal line
|
# Horizontal line
|
||||||
x, string = (string + ' ').split(' ', 1)
|
x, string = (f'{string} ').split(' ', 1)
|
||||||
old_x, old_y = current_point
|
old_x, old_y = current_point
|
||||||
x, _ = svg.point(x, 0, font_size)
|
x, _ = svg.point(x, 0, font_size)
|
||||||
if letter == 'h':
|
if letter == 'h':
|
||||||
@ -258,7 +258,7 @@ def path(svg, node, font_size):
|
|||||||
|
|
||||||
elif letter in 'vV':
|
elif letter in 'vV':
|
||||||
# Vertical line
|
# Vertical line
|
||||||
y, string = (string + ' ').split(' ', 1)
|
y, string = (f'{string} ').split(' ', 1)
|
||||||
old_x, old_y = current_point
|
old_x, old_y = current_point
|
||||||
_, y = svg.point(0, y, font_size)
|
_, y = svg.point(0, y, font_size)
|
||||||
if letter == 'v':
|
if letter == 'v':
|
||||||
|
@ -432,7 +432,7 @@ def split_first_line(text, style, context, max_width, justification_spacing,
|
|||||||
next_word = f' {next_word}'
|
next_word = f' {next_word}'
|
||||||
layout.set_text(first_line_text)
|
layout.set_text(first_line_text)
|
||||||
first_line, index = layout.get_first_line()
|
first_line, index = layout.get_first_line()
|
||||||
resume_index = len((first_line_text + ' ').encode())
|
resume_index = len((f'{first_line_text} ').encode())
|
||||||
else:
|
else:
|
||||||
first_line_text, next_word = '', first_line_text
|
first_line_text, next_word = '', first_line_text
|
||||||
soft_hyphen_indexes = [
|
soft_hyphen_indexes = [
|
||||||
|
Loading…
Reference in New Issue
Block a user