8000 fstringify backend_ps more. · matplotlib/matplotlib@854687d · GitHub
[go: up one dir, main page]

Skip to content

Commit 854687d

Browse files
committed
fstringify backend_ps more.
Admittedly the nested conditional on %%Title is a bit ugly, but I think the new form clearly shows the document structure better.
1 parent 1c06de3 commit 854687d

File tree

1 file changed

+39
-55
lines changed

1 file changed

+39
-55
lines changed

lib/matplotlib/backends/backend_ps.py

Lines changed: 39 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -991,9 +991,9 @@ def print_figure_impl(fh):
991991
if is_eps:
992992
print("%!PS-Adobe-3.0 EPSF-3.0", file=fh)
993993
else:
994-
print("%!PS-Adobe-3.0\n"
995-
"%%DocumentPaperSizes: {papertype}\n"
996-
"%%Pages: 1\n".format(papertype=papertype),
994+
print(f"%!PS-Adobe-3.0\n"
995+
f"%%DocumentPaperSizes: {papertype}\n"
996+
f"%%Pages: 1\n",
997997
end="", file=fh)
998998
if title:
999999
print("%%Title: " + title, file=fh)
@@ -1005,13 +1005,11 @@ def print_figure_impl(fh):
10051005
int(source_date_epoch)).strftime("%a %b %d %H:%M:%S %Y")
10061006
else:
10071007
source_date = time.ctime()
1008-
print("%%Creator: {creator_str}\n"
1009-
"%%CreationDate: {source_date}\n"
1010-
"%%Orientation: {orientation}\n"
1011-
"%%BoundingBox: {bbox[0]} {bbox[1]} {bbox[2]} {bbox[3]}\n"
1012-
"%%EndComments\n"
1013-
.format(creator_str=creator_str, source_date=source_date,
1014-
orientation=orientation, bbox=bbox),
1008+
print(f"%%Creator: {creator_str}\n"
1009+
f"%%CreationDate: {source_date}\n"
1010+
f"%%Orientation: {orientation}\n"
1011+
f"%%BoundingBox: {bbox[0]} {bbox[1]} {bbox[2]} {bbox[3]}\n"
1012+
f"%%EndComments\n",
10151013
end="", file=fh)
10161014

10171015
Ndict = len(psDefs)
@@ -1188,51 +1186,37 @@ def write(self, *args, **kwargs):
11881186

11891187
with TemporaryDirectory() as tmpdir:
11901188
tmpfile = os.path.join(tmpdir, "tmp.ps")
1191-
with open(tmpfile, 'w', encoding='latin-1') as fh:
1192-
# write the Encapsulated PostScript headers
1193-
print("%!PS-Adobe-3.0 EPSF-3.0", file=fh)
1194-
if title:
1195-
print("%%Title: "+title, file=fh)
1196-
# get source date from SOURCE_DATE_EPOCH, if set
1197-
# See https://reproducible-builds.org/specs/source-date-epoch/
1198-
source_date_epoch = os.getenv("SOURCE_DATE_EPOCH")
1199-
if source_date_epoch:
1200-
source_date = datetime.datetime.utcfromtimestamp(
1201-
int(source_date_epoch)).strftime(
1202-
"%a %b %d %H:%M:%S %Y")
1203-
else:
1204-
source_date = time.ctime()
1205-
print(
1206-
"%%Creator: {creator_str}\n"
1207-
"%%CreationDate: {source_date}\n"
1208-
"%%BoundingBox: {bbox[0]} {bbox[1]} {bbox[2]} {bbox[3]}\n"
1209-
"%%EndComments\n"
1210-
.format(creator_str=creator_str, source_date=source_date,
1211-
bbox=bbox),
1212-
end="", file=fh)
1213-
1214-
print("%%BeginProlog\n"
1215-
"/mpldict {len_psDefs} dict def\n"
1216-
"mpldict begin\n"
1217-
"{psDefs}\n"
1218-
"end\n"
1219-
"%%EndProlog\n"
1220-
.format(len_psDefs=len(psDefs),
1221-
psDefs="\n".join(psDefs)),
1222-
end="", file=fh)
1223-
1224-
print("mpldict begin", file=fh)
1225-
print("%s translate" % _nums_to_str(xo, yo), file=fh)
1226-
print("%s clipbox" % _nums_to_str(width*72, height*72, 0, 0),
1227-
file=fh)
1228-
1229-
# write the figure
1230-
print(self._pswriter.getvalue(), file=fh)
1231-
1232-
# write the trailer
1233-
print("end", file=fh)
1234-
print("showpage", file=fh)
1235-
fh.flush()
1189+
# get source date from SOURCE_DATE_EPOCH, if set
1190+
# See https://reproducible-builds.org/specs/source-date-epoch/
1191+
source_date_epoch = os.getenv("SOURCE_DATE_EPOCH")
1192+
if source_date_epoch:
1193+
source_date = datetime.datetime.utcfromtimestamp(
1194+
int(source_date_epoch)).strftime("%a %b %d %H:%M:%S %Y")
1195+
else:
1196+
source_date = time.ctime()
1197+
pathlib.Path(tmpfile).write_text(
1198+
f"""\
1199+
%!PS-Adobe-3.0 EPSF-3.0
1200+
{f'''%%Title: {title}
1201+
''' if title else ""}\
1202+
%%Creator: {creator_str}
1203+
%%CreationDate: {source_date}
1204+
%%BoundingBox: {bbox[0]} {bbox[1]} {bbox[2]} {bbox[3]}
1205+
%%EndComments
1206+
%%BeginProlog
1207+
/mpldict {len(psDefs)} dict def
1208+
mpldict begin
1209+
{"".join(psDefs)}
1210+
end
1211+
%%EndProlog
1212+
mpldict begin
1213+
{_nums_to_str(xo, yo)} translate
1214+
{_nums_to_str(width*72, height*72)} 0 0 clipbox
1215+
{self._pswriter.getvalue()}
1216+
end
1217+
showpage
1218+
""",
1219+
encoding="latin-1")
12361220

12371221
if is_landscape: # now we are ready to rotate
12381222
is_landscape = True

0 commit comments

Comments
 (0)
0