Skip to content

Instantly share code, notes, and snippets.

@darkfeline
Created May 26, 2017 23:18
Show Gist options
  • Save darkfeline/6ec9d0f8ecb25bd3adeaf97a8f4e2c2f to your computer and use it in GitHub Desktop.
Save darkfeline/6ec9d0f8ecb25bd3adeaf97a8f4e2c2f to your computer and use it in GitHub Desktop.
pass exporter
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()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment