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