Skip to content

Commit c4b45f9

Browse files
committed
chore(ruff): Manual fixes aafig.py
docs/_ext/aafig.py:61:23: RUF012 Mutable class attributes should be annotated with `typing.ClassVar` docs/_ext/aafig.py:147:15: TRY003 Avoid specifying long messages outside the exception class docs/_ext/aafig.py:176:29: SIM115 Use context handler for opening files docs/_ext/aafig.py:179:25: B904 Within an `except` clause, raise exceptions with `raise ... from err` or `raise ... from None` to distinguish them from errors in exception handling docs/_ext/aafig.py:179:25: TRY200 Use `raise from` to specify exception cause docs/_ext/aafig.py:193:9: B904 Within an `except` clause, raise exceptions with `raise ... from err` or `raise ... from None` to distinguish them from errors in exception handling docs/_ext/aafig.py:193:9: TRY200 Use `raise from` to specify exception cause docs/_ext/aafig.py:198:13: SIM115 Use context handler for opening files
1 parent 47427db commit c4b45f9

File tree

1 file changed

+17
-12
lines changed

1 file changed

+17
-12
lines changed

docs/_ext/aafig.py

+17-12
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"""
1313
import logging
1414
import posixpath
15+
import typing as t
1516
from hashlib import sha1 as sha
1617
from os import path
1718

@@ -32,7 +33,7 @@
3233

3334

3435
def merge_dict(dst, src):
35-
for (k, v) in src.items():
36+
for k, v in src.items():
3637
if k not in dst:
3738
dst[k] = v
3839
return dst
@@ -58,7 +59,7 @@ class AafigDirective(images.Image):
5859

5960
has_content = True
6061
required_arguments = 0
61-
own_option_spec = {
62+
own_option_spec: t.ClassVar = {
6263
"line_width": float,
6364
"background": str,
6465
"foreground": str,
@@ -73,7 +74,7 @@ class AafigDirective(images.Image):
7374
def run(self):
7475
aafig_options = {}
7576
own_options_keys = [self.own_option_spec.keys(), "scale"]
76-
for (k, v) in self.options.items():
77+
for k, v in self.options.items():
7778
if k in own_options_keys:
7879
# convert flags to booleans
7980
if v is None:
@@ -138,13 +139,18 @@ def render_aafig_images(app, doctree):
138139
img["height"] = height
139140

140141

142+
class AafigureNotInstalled(AafigError):
143+
def __init__(self, *args: object, **kwargs: object) -> None:
144+
return super().__init__("aafigure module not installed", *args, **kwargs)
145+
146+
141147
def render_aafigure(app, text, options):
142148
"""
143149
Render an ASCII art figure into the requested format output file.
144150
"""
145151

146152
if aafigure is None:
147-
raise AafigError("aafigure module not installed")
153+
raise AafigureNotInstalled()
148154

149155
fname = get_basename(text, options)
150156
fname = "{}.{}".format(get_basename(text, options), options["format"])
@@ -173,10 +179,10 @@ def render_aafigure(app, text, options):
173179
f = None
174180
try:
175181
try:
176-
f = open(metadata_fname)
177-
extra = f.read()
178-
except Exception:
179-
raise AafigError()
182+
with open(metadata_fname) as f:
183+
extra = f.read()
184+
except Exception as e:
185+
raise AafigError() from e
180186
finally:
181187
if f is not None:
182188
f.close()
@@ -190,14 +196,13 @@ def render_aafigure(app, text, options):
190196
(visitor, output) = aafigure.render(text, outfn, options)
191197
output.close()
192198
except aafigure.UnsupportedFormatError as e:
193-
raise AafigError(str(e))
199+
raise AafigError(str(e)) from e
194200

195201
extra = None
196202
if options["format"].lower() == "svg":
197203
extra = visitor.get_size_attrs()
198-
f = open(metadata_fname, "w")
199-
f.write(extra)
200-
f.close()
204+
with open(metadata_fname, "w") as f:
205+
f.write(extra)
201206

202207
return relfn, outfn, id, extra
203208

0 commit comments

Comments
 (0)