import logging from pathlib import Path import re import subprocess from typing import NamedTuple STORE = Path('~/.password-store').expanduser() logger = logging.getLogger(__name__) def main(): logging.basicConfig(level='DEBUG') sites = STORE / 'websites' for p in sorted(sites.iterdir()): p = _get_store_path(p) info = _get_pass(p) logger.debug('info: %r', info) print(_format_info(info)) def _get_store_path(p): return p.relative_to(STORE).with_suffix('') class _Info(NamedTuple): name: str fields: dict other: str def _get_pass(p): proc = subprocess.run(['pass', 'show', str(p)], stdout=subprocess.PIPE) output_lines = proc.stdout.decode().splitlines() fields = { 'password': output_lines[0].strip(), } other = [] for line in output_lines[1:]: key, val = _get_field(line) if val: fields[key] = val else: other.append(line) return _Info(p.name, fields, ''.join(other)) def _get_field(line): key, _, val = line.partition(':') return key.strip(), val.strip() def _format_info(info): props = '\n'.join(f':{re.sub(r"[^a-zA-Z]", "", key.title())}: {val}' for key, val in info.fields.items()) return f'''\ *** {info.name} :PROPERTIES: {props} :END: {info.other} ''' main()