Created
May 26, 2017 23:18
-
-
Save darkfeline/6ec9d0f8ecb25bd3adeaf97a8f4e2c2f to your computer and use it in GitHub Desktop.
pass exporter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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