Add borg module for i3status

This commit is contained in:
Théophile Bastian 2025-02-14 12:49:45 +01:00
parent 1b2a9436a6
commit cf747089c6

90
files/.i3/py3status/borg.py Executable file
View file

@ -0,0 +1,90 @@
# -*- coding: utf-8 -*-
"""
Checks the last backup time
Configuration parameters:
recent_color: when the backup is recent enough
aging_color: when the backup starts to get older
old_color: when the backup is clearly too old
aging_threshold: number of days after which a backup is aging
old_threshold: number of days after which a backup is old
repo: name of the repository
remote: remote on which the backup is made
name: human-readable name to display
"""
from pathlib import Path
import datetime
import os
import typing as ty
class BorgStore(ty.NamedTuple):
repository: str
remote: str
human_readable: str
@property
def last_backup_path(self) -> Path:
return Path.home() / ".borg" / self.repository / self.remote
def last_backup_time(self) -> ty.Optional[datetime.datetime]:
last_backup = self.last_backup_path
if not last_backup.exists():
return None
mtime = os.path.getmtime(last_backup)
return datetime.datetime.fromtimestamp(mtime)
class Py3status:
hddicon = "\uf0c7" # FontAwesome
format = "{hddicon} {name} {time}"
aging_threshold = 2
old_threshold = 4
repo = None
remote = None
name = None
def _format_text(self, time):
return self.format.format(
hddicon=self.hddicon,
name=self.store.human_readable,
repo=self.store.repository,
remote=self.store.remote,
time=time,
)
return f"{self.hdd_icon}{self.store.human_readable} {time}"
def borg(self):
if None in [self.repo, self.remote, self.name]:
return {"full_text": "Must be configured: repo, remote, name"}
self.store: BorgStore = BorgStore(
self.repo,
self.remote,
self.name,
)
last_backup = self.store.last_backup_time()
color = self.py3.COLOR_GOOD
urgent = False
if last_backup is None:
timestr = "EVER"
urgent = True
color = self.py3.COLOR_BAD
else:
elapsed = datetime.datetime.now() - last_backup
if elapsed.days >= self.old_threshold:
color = self.py3.COLOR_BAD
urgent = True
elif elapsed.days >= self.aging_threshold:
color = self.py3.COLOR_DEGRADED
timestr = f"{elapsed.days}d"
return {
"full_text": self._format_text(timestr),
"color": color,
"urgent": urgent,
}