Skip to content

Commit a3a742d

Browse files
committed
Refactor to use Session.virtualfile_in and StringIO context manager
1 parent a7ed467 commit a3a742d

File tree

1 file changed

+15
-17
lines changed

1 file changed

+15
-17
lines changed

pygmt/src/paragraph.py

+15-17
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,15 @@ def _parse_font_angle_justify(
3333
'+a45'
3434
>>> _parse_font_angle_justify(None, None, "CM")
3535
'+jCM'
36+
>>> _parse_font_angle_justify("10p", 45, None)
37+
'+f10p+a45'
3638
>>> _parse_font_angle_justify("10p,Helvetica-Bold", 45, "CM")
3739
'+f10p,Helvetica-Bold+a45+jCM'
3840
"""
39-
args = ((font, "+f"), (angle, "+a"), (justify, "+j"))
40-
if all(arg is None for arg, _ in args):
41+
args = {"+f": font, "+a": angle, "+j": justify}
42+
if all(arg is None for arg in args.values()):
4143
return None
42-
return "".join(f"{flag}{arg}" for arg, flag in args if arg is not None)
44+
return "".join(f"{prefix}{arg}" for prefix, arg in args.items() if arg is not None)
4345

4446

4547
def paragraph(
@@ -62,8 +64,8 @@ def paragraph(
6264
x/y
6365
The x, y coordinates of the paragraph.
6466
text
65-
The paragraph text to typeset. If a sequence of strings is provided, each
66-
string is treated as a separate paragraph.
67+
The paragraph text to typeset. If a sequence of strings is provided, each string
68+
is treated as a separate paragraph.
6769
parwidth
6870
The width of the paragraph.
6971
linespacing
@@ -91,23 +93,19 @@ def paragraph(
9193
confdict = {}
9294
# Prepare the keyword dictionary for the module options
9395
kwdict = {"M": True, "F": _parse_font_angle_justify(font, angle, justify)}
94-
95-
# Initialize a stringio object for storing the input data.
96-
stringio = io.StringIO()
97-
# The header line.
98-
stringio.write(f"> {x} {y} {linespacing} {parwidth} {alignment[0]}\n")
99-
# The text string to be written to the stringio object.
96+
# Prepare the text string that will be passed to an io.StringIO object.
10097
# Multiple paragraphs are separated by a blank line "\n\n".
10198
_textstr: str = "\n\n".join(text) if is_nonstr_iter(text) else str(text)
10299
# Check the encoding of the text string and convert it to octal if necessary.
103100
if (encoding := _check_encoding(_textstr)) != "ascii":
104101
_textstr = non_ascii_to_octal(_textstr, encoding=encoding)
105102
confdict["PS_CHAR_ENCODING"] = encoding
106-
# Write the text string to the stringio object.
107-
stringio.write(_textstr)
108103

109104
with Session() as lib:
110-
with lib.virtualfile_from_stringio(stringio) as vfile:
111-
lib.call_module(
112-
"text", args=build_arg_list(kwdict, infile=vfile, confdict=confdict)
113-
)
105+
with io.StringIO() as buffer: # Prepare the StringIO input.
106+
buffer.write(f"> {x} {y} {linespacing} {parwidth} {alignment[0]}\n")
107+
buffer.write(_textstr)
108+
with lib.virtualfile_in(data=buffer) as vfile:
109+
lib.call_module(
110+
"text", args=build_arg_list(kwdict, infile=vfile, confdict=confdict)
111+
)

0 commit comments

Comments
 (0)