db/trustwords.py
author Krista Grothoff <krista@pep-project.org>
Mon, 24 Oct 2016 21:33:35 +0200
branchENGINE-128
changeset 1334 85973d7d2859
parent 246 1546f1595882
child 1513 e7f7e42385b5
permissions -rwxr-xr-x
ENGINE-128: closing branch
vb@2
     1
#!/usr/bin/env python3
vb@2
     2
vb@2
     3
from sqlite3 import connect
vb@2
     4
from os import environ
vb@2
     5
from argparse import ArgumentParser
vb@2
     6
from re import sub
vb@2
     7
vb@2
     8
try:
vb@2
     9
    environ["ALLUSERSPROFILE"]
vb@2
    10
except KeyError:
vb@2
    11
    db_file = "/usr/local/share/pEp/system.db"
vb@2
    12
else:
vb@2
    13
    db_file = environ["ALLUSERSPROFILE"] + r"\pEp\system.db"
vb@2
    14
vb@246
    15
p = ArgumentParser(description="show trustwords instead of hex fingerprint")
vb@2
    16
p.add_argument('--db-path', '-d', type=str, default=db_file,
vb@2
    17
        help='path to pEp system db (default: ' + db_file + ')')
vb@2
    18
p.add_argument('--lang', '-l', type=str, default="en",
vb@2
    19
        help='use dictionary for language LANG (default: en)')
vb@2
    20
p.add_argument('--short', '-s', action='store_true',
vb@246
    21
        help='display the first 5 of the trustwords')
vb@2
    22
p.add_argument('hex', metavar="hex", type=str, nargs='+',
vb@2
    23
        help='hex values of fingerprint')
vb@2
    24
vb@2
    25
args = p.parse_args()
vb@2
    26
vb@2
    27
c = connect(args.db_path).cursor()
vb@2
    28
hex_string = sub(r"\W", "", "".join(args.hex))
vb@2
    29
vb@2
    30
def hex_word(s):
vb@2
    31
    n = min(20, len(s)) if args.short else len(s)
vb@2
    32
    for i in range(0, n, 4):
vb@2
    33
        yield s[i:i+4]
vb@2
    34
vb@2
    35
r = []
vb@2
    36
vb@2
    37
for arg in hex_word(hex_string):
vb@2
    38
    c.execute("select word from wordlist where id = {} and lang = lower('{}')".format(
vb@2
    39
            str(int(arg, 16)), args.lang))
vb@2
    40
    r.append(c.fetchall()[0][0])
vb@2
    41
vb@2
    42
print(" ".join(r))