From 34bef1cd82dadd97dba9c1a7b61751312cc64c09 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hugo=20H=C3=B6rnquist?= Date: Fri, 9 Dec 2022 23:57:31 +0100 Subject: Change info gathering to proper db reads. Running `xapian info` and parsing the output breaks way to easily, here we instead just open the database file and look for ourselves. --- mu4web/mu.py | 47 ++++++++++++++++++++++++++++++++++++----------- mu4web/xapian.py | 16 ++++++++++++++++ setup.cfg | 1 + 3 files changed, 53 insertions(+), 11 deletions(-) create mode 100644 mu4web/xapian.py diff --git a/mu4web/mu.py b/mu4web/mu.py index f101245..d75d7a9 100644 --- a/mu4web/mu.py +++ b/mu4web/mu.py @@ -9,6 +9,10 @@ import subprocess from subprocess import PIPE import xml.dom.minidom import xml.dom +from xdg.BaseDirectory import xdg_cache_home +import os.path +import xapian +from datetime import datetime from typing import ( Optional, @@ -114,16 +118,37 @@ def search(query: str, return message_list +def base_directory(): + """ + Returns where where mu stores its files. + + Defaults to $XDG_CACHE_HOME/mu, but can be changed through the + environment variable MUHOME. + + TODO make this configurable. + """ + return os.getenv('MUHOME') or os.path.join(xdg_cache_home, 'mu') + + def info(): - cmd = subprocess.Popen('mu info --nocolor'.split(' '), - stdout=subprocess.PIPE, - text=True) - out = {} + + db = os.path.join(base_directory(), "xapian") + + info = { + 'database-path': db, + } + + for key in ['changed', 'created', 'indexed']: + info[key] = datetime.fromtimestamp(int(xapian.metadata_get(db, key), 16)) + + for key in ['maildir', 'schema-version']: + info[key] = xapian.metadata_get(db, key) + + cmd = subprocess.Popen(['xapian-delve', '-V3', '-1', db], stdout=subprocess.PIPE) + # Start at minus one to ignore header + count = -1 for line in cmd.stdout: - if not line: - continue - if line[0] == '+': - continue - key, *value = [s.strip() for s in line.split('|') if s and not s.isspace()] - out[key] = '|'.join(value) - return out + count += 1 + info['messages-in-store'] = count + + return info diff --git a/mu4web/xapian.py b/mu4web/xapian.py new file mode 100644 index 0000000..8aebf72 --- /dev/null +++ b/mu4web/xapian.py @@ -0,0 +1,16 @@ +import subprocess +from typing import Optional + +def metadata_list(db, prefix: Optional[str] = None) -> list[str]: + cmdline = ['xapian-metadata', 'list', db] + if prefix: + cmdline.append(prefix) + cmd = subprocess.run(cmdline, capture_output=True, text=True) + return cmd.stdout.split('\n') + + +def metadata_get(db, key: str) -> str: + cmd = subprocess.run(['xapian-metadata', 'get', db, key], + capture_output=True, text=True) + return cmd.stdout.strip() + diff --git a/setup.cfg b/setup.cfg index b78ec59..ae42a9b 100644 --- a/setup.cfg +++ b/setup.cfg @@ -15,6 +15,7 @@ install_requires = flask >= 2.2.2 flask-login >= 0.6 urllib3 >= 1.26 + pyxdg >= 0.28 # optionally natsort >= 8.2 # python-pam >= 2.0.2 for pam integration setup_requires = -- cgit v1.2.3