mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Add remove file action
This commit is contained in:
parent
e3f6c88d2c
commit
04f6473f38
@ -19,6 +19,7 @@ from calibre.utils.ipc.simple_worker import start_pipe_worker
|
|||||||
lock = Lock()
|
lock = Lock()
|
||||||
worker = None
|
worker = None
|
||||||
RMTREE_ACTION = 'rmtree'
|
RMTREE_ACTION = 'rmtree'
|
||||||
|
UNLINK_ACTION = 'unlink'
|
||||||
|
|
||||||
|
|
||||||
def thread_safe(f):
|
def thread_safe(f):
|
||||||
@ -35,6 +36,18 @@ def remove_folder(path: str) -> None:
|
|||||||
_send_command(RMTREE_ACTION, os.path.abspath(path))
|
_send_command(RMTREE_ACTION, os.path.abspath(path))
|
||||||
|
|
||||||
|
|
||||||
|
@thread_safe
|
||||||
|
def remove_file(path: str) -> None:
|
||||||
|
_send_command(UNLINK_ACTION, os.path.abspath(path))
|
||||||
|
|
||||||
|
|
||||||
|
def unlink(path):
|
||||||
|
with suppress(Exception):
|
||||||
|
import os as oss
|
||||||
|
if oss.path.exists(path):
|
||||||
|
oss.remove(path)
|
||||||
|
|
||||||
|
|
||||||
def ensure_worker():
|
def ensure_worker():
|
||||||
global worker
|
global worker
|
||||||
if worker is None:
|
if worker is None:
|
||||||
@ -79,12 +92,12 @@ else:
|
|||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
|
ac_map = {RMTREE_ACTION: remove_dir, UNLINK_ACTION: unlink}
|
||||||
for line in sys.stdin.buffer:
|
for line in sys.stdin.buffer:
|
||||||
if line:
|
if line:
|
||||||
try:
|
try:
|
||||||
cmd = json.loads(line)
|
cmd = json.loads(line)
|
||||||
if cmd['action'] == RMTREE_ACTION:
|
atexit.register(ac_map[cmd['action']], cmd['payload'])
|
||||||
atexit.register(remove_dir, cmd['payload'])
|
|
||||||
except Exception:
|
except Exception:
|
||||||
import traceback
|
import traceback
|
||||||
traceback.print_exc()
|
traceback.print_exc()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user