In show_file, use os.remove to remove temporary images

This commit is contained in:
Andrew Murray 2022-01-17 08:59:17 +11:00
parent c930be0758
commit 427221ef5f

View File

@ -15,7 +15,6 @@ import os
import shutil
import subprocess
import sys
import tempfile
from shlex import quote
from PIL import Image
@ -147,16 +146,15 @@ class MacViewer(Viewer):
def show_file(self, file, **options):
"""Display given file"""
fd, path = tempfile.mkstemp()
with os.fdopen(fd, "w") as f:
f.write(file)
with open(path) as f:
subprocess.Popen(
["im=$(cat); open -a Preview.app $im; sleep 20; rm -f $im"],
shell=True,
stdin=f,
)
os.remove(path)
subprocess.call(["open", "-a", "Preview.app", file])
subprocess.Popen(
[
sys.executable,
"-c",
"import os, sys, time;time.sleep(20);os.remove(sys.argv[1])",
file,
]
)
return 1
@ -172,19 +170,6 @@ class UnixViewer(Viewer):
command = self.get_command_ex(file, **options)[0]
return f"({command} {quote(file)}; rm -f {quote(file)})&"
def show_file(self, file, **options):
"""Display given file"""
fd, path = tempfile.mkstemp()
with os.fdopen(fd, "w") as f:
f.write(file)
with open(path) as f:
command = self.get_command_ex(file, **options)[0]
subprocess.Popen(
["im=$(cat);" + command + " $im; rm -f $im"], shell=True, stdin=f
)
os.remove(path)
return 1
class XDGViewer(UnixViewer):
"""
@ -195,6 +180,11 @@ class XDGViewer(UnixViewer):
command = executable = "xdg-open"
return command, executable
def show_file(self, file, **options):
subprocess.Popen(["xdg-open", file])
os.remove(file)
return 1
class DisplayViewer(UnixViewer):
"""
@ -208,6 +198,16 @@ class DisplayViewer(UnixViewer):
command += f" -name {quote(title)}"
return command, executable
def show_file(self, file, **options):
args = ["display"]
if "title" in options:
args += ["-name", options["title"]]
args.append(file)
subprocess.Popen(args)
os.remove(file)
return 1
class GmDisplayViewer(UnixViewer):
"""The GraphicsMagick ``gm display`` command."""
@ -217,6 +217,11 @@ class GmDisplayViewer(UnixViewer):
command = "gm display"
return command, executable
def show_file(self, file, **options):
subprocess.Popen(["gm", "display", file])
os.remove(file)
return 1
class EogViewer(UnixViewer):
"""The GNOME Image Viewer ``eog`` command."""
@ -226,6 +231,11 @@ class EogViewer(UnixViewer):
command = "eog -n"
return command, executable
def show_file(self, file, **options):
subprocess.Popen(["eog", "-n", file])
os.remove(file)
return 1
class XVViewer(UnixViewer):
"""
@ -241,6 +251,16 @@ class XVViewer(UnixViewer):
command += f" -name {quote(title)}"
return command, executable
def show_file(self, file, **options):
args = ["xv"]
if "title" in options:
args += ["-name", options["title"]]
args.append(file)
subprocess.Popen(args)
os.remove(file)
return 1
if sys.platform not in ("win32", "darwin"): # unixoids
if shutil.which("xdg-open"):