diff options
author | Hugo Hörnquist <hugo@lysator.liu.se> | 2022-12-09 23:57:31 +0100 |
---|---|---|
committer | Hugo Hörnquist <hugo@lysator.liu.se> | 2022-12-09 23:57:31 +0100 |
commit | 34bef1cd82dadd97dba9c1a7b61751312cc64c09 (patch) | |
tree | 5f690c57ac74834287c5e4994921bfb6d59791ec | |
parent | Add comment that python-pam is recomended. (diff) | |
download | mu4web-34bef1cd82dadd97dba9c1a7b61751312cc64c09.tar.gz mu4web-34bef1cd82dadd97dba9c1a7b61751312cc64c09.tar.xz |
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.
-rw-r--r-- | mu4web/mu.py | 47 | ||||
-rw-r--r-- | mu4web/xapian.py | 16 | ||||
-rw-r--r-- | setup.cfg | 1 |
3 files changed, 53 insertions, 11 deletions
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() + @@ -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 = |