conky-config/scripts/seafile_status

64 lines
1.4 KiB
Python
Executable File

#! /bin/env python
# determine the status of the seafile client
import subprocess as sp
import os
SEAF_FILE = "/tmp/.conky_seafile"
CMD = ["seaf-cli", "status"]
# possible statuses (according to the source here: https://github.com/haiwen/seafile/blob/91e5d897395c728a1e862dbdaf3d8a519c2ed73e/daemon/sync-mgr.c#L471)
# ranked in order from least to most severe in terms of "being synced"
STATUS_RANK = [
"synchronized",
"committing",
"initializing",
"downloading",
"merging",
"uploading",
"error",
"canceled",
"cancel pending",
]
def get_conf_dir():
try:
conf_dir = os.environ["CCNET_CONF_DIR"]
return ["-c", conf_dir]
except KeyError:
return []
def collapse_statuses(xs):
try:
return STATUS_RANK[max(STATUS_RANK.index(x) for x in xs)]
except ValueError:
return "unknown"
def query_statuses():
args = get_conf_dir()
res = sp.run(CMD + args, capture_output=True, check=True)
raw = res.stdout.rstrip().split(b"\n")[1:]
statuses = [ln.split(b"\t")[1].strip().decode() for ln in raw]
return collapse_statuses(statuses)
def write_output(status):
with open(SEAF_FILE, "w", encoding="utf8") as f:
f.write(status.capitalize())
def main():
try:
status = query_statuses()
write_output(status)
except Exception as e:
print(str(e))
main()