Luke Murphy
4 years ago
5 changed files with 135 additions and 41 deletions
@ -0,0 +1,7 @@ |
|||
from pathlib import Path |
|||
|
|||
_CWD = Path(__file__).absolute().parent |
|||
|
|||
UI_DIR = f"{_CWD}/ui" |
|||
|
|||
DEFAULT_DROP_LABEL = "Drag a file to send" |
@ -0,0 +1,35 @@ |
|||
from os.path import basename, getsize |
|||
|
|||
|
|||
class PendingTransfer: |
|||
"""A pending transfer context and config.""" |
|||
|
|||
def __init__(self, fpath, code, scope): |
|||
self.fpath = fpath |
|||
self.code = code |
|||
self.scope = scope |
|||
|
|||
@property |
|||
def fname(self): |
|||
"""Filename being transferred.""" |
|||
return basename(self.fpath) |
|||
|
|||
@property |
|||
def size(self): |
|||
"""Filename size.""" |
|||
return self._human_readable_size(getsize(self.fpath)) |
|||
|
|||
def cancel(self): |
|||
"""Cancel the transfer.""" |
|||
self.scope.cancel() |
|||
|
|||
def _human_readable_size(self, num, suffix="B"): |
|||
"""Convert file size to human readable format. |
|||
|
|||
Thanks Fred. See https://stackoverflow.com/a/1094933. |
|||
""" |
|||
for unit in ["", "Ki", "Mi", "Gi", "Ti", "Pi", "Ei", "Zi"]: |
|||
if abs(num) < 1024.0: |
|||
return "%3.1f%s%s" % (num, unit, suffix) |
|||
num /= 1024.0 |
|||
return "%.1f%s%s" % (num, "Yi", suffix) |
@ -0,0 +1,37 @@ |
|||
from subprocess import PIPE |
|||
|
|||
from trio import TASK_STATUS_IGNORED, CancelScope, open_process, run_process |
|||
|
|||
from dropship import log |
|||
|
|||
|
|||
async def wormhole_send(fpath, task_status=TASK_STATUS_IGNORED): |
|||
"""Run `wormhole send` on a local file path.""" |
|||
with CancelScope() as scope: |
|||
command = ["wormhole", "send", fpath] |
|||
process = await open_process(command, stderr=PIPE) |
|||
output = await process.stderr.receive_some() |
|||
code = output.decode().split()[-1] |
|||
task_status.started((code, scope,)) |
|||
log.info(f"wormhole_send: now waiting for other side ({code})") |
|||
await process.wait() |
|||
log.info(f"wormhole_send: succesfully transfered ({code})") |
|||
|
|||
if scope.cancel_called: |
|||
process.terminate() |
|||
log.info(f"wormhole_send: succesfully terminated process ({code})") |
|||
|
|||
|
|||
async def wormhole_recv(code, task_status=TASK_STATUS_IGNORED): |
|||
"""Run `wormhole receive` on a pending transfer code.""" |
|||
with CancelScope() as scope: |
|||
command = ["wormhole", "receive", "--accept-file", code] |
|||
process = await open_process(command, stderr=PIPE) |
|||
task_status.started((scope,)) |
|||
log.info(f"wormhole_recv: now starting receiving process ({code})") |
|||
await process.wait() |
|||
log.info(f"wormhole_recv: succesfully received ({code})") |
|||
|
|||
if scope.cancel_called: |
|||
process.terminate() |
|||
log.info(f"wormhole_recv: succesfully terminated process ({code})") |
Loading…
Reference in new issue