Optimize imports, use tempdir
This commit is contained in:
parent
58bcc8fe7a
commit
3e853ac1bd
1 changed files with 7 additions and 7 deletions
|
|
@ -1,21 +1,21 @@
|
|||
import platform
|
||||
import subprocess
|
||||
from argparse import ArgumentParser
|
||||
from os import unlink
|
||||
from pathlib import Path
|
||||
from platform import release, system
|
||||
from random import randint
|
||||
from subprocess import check_call
|
||||
from tempfile import mktemp
|
||||
from uuid import uuid4
|
||||
|
||||
|
||||
def copy_to_clipboard(val: str) -> bool:
|
||||
sys = platform.system()
|
||||
sys = system()
|
||||
copy_cmd = ""
|
||||
if sys == "Windows":
|
||||
copy_cmd = "clip.exe"
|
||||
elif sys == "macOS":
|
||||
copy_cmd = "pbcopy"
|
||||
elif sys == "Linux":
|
||||
if "WSL" in platform.release():
|
||||
if "WSL" in release():
|
||||
copy_cmd = "clip.exe" # Windows native clip.exe still works in WSL
|
||||
else:
|
||||
copy_cmd = "xclip -selection clipboard"
|
||||
|
|
@ -23,12 +23,12 @@ def copy_to_clipboard(val: str) -> bool:
|
|||
print(f"System {sys} is not supported - cannot copy to clipboard")
|
||||
return False
|
||||
|
||||
tmp_path = Path(__file__).joinpath("..").resolve().joinpath(str(uuid4()))
|
||||
tmp_path = mktemp()
|
||||
with open(tmp_path, "w", encoding="utf-8") as fh:
|
||||
fh.write(val.strip())
|
||||
|
||||
try:
|
||||
ret_val = subprocess.check_call(f"cat {str(tmp_path)} | {copy_cmd}", shell=True)
|
||||
ret_val = check_call(f"cat {str(tmp_path)} | {copy_cmd}", shell=True)
|
||||
finally:
|
||||
unlink(tmp_path)
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue