Skip to content

Commit

Permalink
Merge pull request #394 from eumiro/fstring
Browse files Browse the repository at this point in the history
Use f-strings
  • Loading branch information
liZe committed Aug 5, 2023
2 parents 595d847 + 8091c24 commit 3fb99d8
Show file tree
Hide file tree
Showing 9 changed files with 22 additions and 24 deletions.
2 changes: 1 addition & 1 deletion cairosvg/bounding_box.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def bounding_box_path(surface, node):

# Normalize path data for correct parsing
for letter in PATH_LETTERS:
path_data = path_data.replace(letter, ' {} '.format(letter))
path_data = path_data.replace(letter, f' {letter} ')
path_data = normalize(path_data)

bounding_box = EMPTY_BOUNDING_BOX
Expand Down
11 changes: 4 additions & 7 deletions cairosvg/defs.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def update_def_href(surface, def_name, def_dict):
update_def_href(surface, href, def_dict)
href_node = def_dict[href]
def_dict[def_name] = Tree(
url='#{}'.format(def_name), url_fetcher=def_node.url_fetcher,
url=f'#{def_name}', url_fetcher=def_node.url_fetcher,
parent=href_node, parent_children=(not def_node.children),
tree_cache=surface.tree_cache, unsafe=def_node.unsafe)
# Inherit attributes generally not inherited
Expand All @@ -64,7 +64,7 @@ def parse_def(surface, node):
'marker', 'gradient', 'pattern', 'path', 'mask', 'filter',
'image'):
if def_type in node.tag.lower() and 'id' in node:
getattr(surface, '{}s'.format(def_type))[node['id']] = node
getattr(surface, f'{def_type}s')[node['id']] = node


def gradient_or_pattern(surface, node, name, opacity):
Expand Down Expand Up @@ -139,9 +139,7 @@ def paint_mask(surface, node, name, opacity):
if mask_node.get('maskUnits') == 'userSpaceOnUse':
x = mask_node['x']
y = mask_node['y']
mask_node['viewBox'] = '{} {} {} {}'.format(
mask_node['x'], mask_node['y'],
mask_node['width'], mask_node['height'])
mask_node['viewBox'] = '{x} {y} {width} {height}'.format(**mask_node)

from .surface import SVGSurface # circular import
mask_surface = SVGSurface(mask_node, None, surface.dpi, surface)
Expand Down Expand Up @@ -248,8 +246,7 @@ def draw_pattern(surface, node, name, opacity):
pattern_node['width'] = pattern_width
pattern_node['height'] = pattern_height
if pattern_node.get('patternContentUnits') == 'objectBoundingBox':
pattern_node['transform'] = 'scale({}, {})'.format(
width, height)
pattern_node['transform'] = f'scale({width}, {height})'

# Fail if pattern has an invalid size
if pattern_width == 0.0 or pattern_height == 0.0:
Expand Down
5 changes: 3 additions & 2 deletions cairosvg/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,9 @@ def preserve_ratio(surface, node, width=None, height=None):
viewbox_width, viewbox_height = node.image_width, node.image_height
else:
raise TypeError(
('Root node is {}. Should be one of '
'marker, svg, image, or g.').format(node.tag))
f'Root node is {node.tag}. Should be one of '
'marker, svg, image, or g.'
)

translate_x = 0
translate_y = 0
Expand Down
4 changes: 2 additions & 2 deletions cairosvg/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ def __init__(self, element, style, url_fetcher, parent=None,
self.tag = (
element.local_name
if element.namespace_url in ('', 'http://www.w3.org/2000/svg') else
'{%s}%s' % (element.namespace_url, element.local_name))
f'{{{element.namespace_url}}}{element.local_name}')
self.text = node.text
self.url_fetcher = url_fetcher
self.unsafe = unsafe
Expand Down Expand Up @@ -406,7 +406,7 @@ def __init__(self, **kwargs):
break
else:
raise TypeError(
'No tag with id="{}" found.'.format(element_id))
f'No tag with id="{element_id}" found.')
super().__init__(
root, style, self.url_fetcher, parent, parent_children, self.url,
unsafe)
Expand Down
8 changes: 4 additions & 4 deletions cairosvg/path.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def draw_markers(surface, node):
markers = {}
common_marker = parse_url(node.get('marker', '')).fragment
for position in ('start', 'mid', 'end'):
attribute = 'marker-{}'.format(position)
attribute = f'marker-{position}'
if attribute in node:
markers[position] = parse_url(node[attribute]).fragment
else:
Expand Down Expand Up @@ -125,7 +125,7 @@ def path(surface, node):
node.vertices = []

for letter in PATH_LETTERS:
string = string.replace(letter, ' {} '.format(letter))
string = string.replace(letter, f' {letter} ')

last_letter = None
string = normalize(string)
Expand Down Expand Up @@ -190,10 +190,10 @@ def path(surface, node):
# As we replace the current operation by l, we must be sure
# that the next letter is set to the real current letter (a
# or A) in case it’s omitted
next_letter = '{} '.format(letter)
next_letter = f'{letter} '
else:
next_letter = ''
string = 'l {} {} {}{}'.format(x3, y3, next_letter, string)
string = f'l {x3} {y3} {next_letter}{string}'
continue

radii_ratio = ry / rx
Expand Down
2 changes: 1 addition & 1 deletion cairosvg/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def test_api():
# Read from a filename
assert svg2png(url=temp_0) == expected_content
assert svg2png(
url='file://{}'.format(temp_0)) == expected_content
url=f'file://{temp_0}') == expected_content

with open(temp_0, 'rb') as file_object:
# Read from a real file object
Expand Down
4 changes: 2 additions & 2 deletions cairosvg/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,14 @@ def text(surface, node, draw_as_text=False):
font_family = (
(node.get('font-family') or 'sans-serif').split(',')[0].strip('"\' '))
font_style = getattr(
cairo, ('font_slant_{}'.format(node.get('font-style')).upper()),
cairo, f'font_slant_{node.get("font-style")}'.upper(),
cairo.FONT_SLANT_NORMAL)
node_font_weight = node.get('font-weight')
if (node_font_weight and node_font_weight.isdigit()
and int(node_font_weight) >= 550):
node_font_weight = 'bold'
font_weight = getattr(
cairo, ('font_weight_{}'.format(node_font_weight).upper()),
cairo, (f'font_weight_{node_font_weight}'.upper()),
cairo.FONT_WEIGHT_NORMAL)
surface.context.select_font_face(font_family, font_style, font_weight)
surface.context.set_font_size(surface.font_size)
Expand Down
6 changes: 3 additions & 3 deletions cairosvg/url.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

from . import VERSION

HTTP_HEADERS = {'User-Agent': 'CairoSVG {}'.format(VERSION)}
HTTP_HEADERS = {'User-Agent': f'CairoSVG {VERSION}'}

URL = re.compile(r'url\((.+)\)')

Expand Down Expand Up @@ -132,7 +132,7 @@ def parse_url(url, base=None):
else:
url = ''
if parsed_url.fragment:
url = '{}#{}'.format(url, parsed_url.fragment)
url = f'{url}#{parsed_url.fragment}'
elif parsed_url.scheme in ('', parsed_base.scheme):
# `urljoin` automatically uses the "folder" part of `base`
url = urljoin(base, url)
Expand All @@ -148,7 +148,7 @@ def read_url(url, url_fetcher, resource_type):
if url.scheme:
url = url.geturl()
else:
url = 'file://{}'.format(os.path.abspath(url.geturl()))
url = f'file://{os.path.abspath(url.geturl())}'
url = normalize_url(url)

return url_fetcher(url, resource_type)
4 changes: 2 additions & 2 deletions test_non_regression/test_non_regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,5 @@ def test_image(svg_filename):
test_surface.finish()

raise AssertionError(
'Images are different: {} {}'.format(
ref_png.name, test_png.name))
f'Images are different: {ref_png.name} {test_png.name}'
)

0 comments on commit 3fb99d8

Please sign in to comment.