db/trustwords.py
changeset 246 1546f1595882
parent 2 f1fc3afacfd6
child 1513 e7f7e42385b5
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/db/trustwords.py	Tue May 05 17:37:49 2015 +0200
     1.3 @@ -0,0 +1,42 @@
     1.4 +#!/usr/bin/env python3
     1.5 +
     1.6 +from sqlite3 import connect
     1.7 +from os import environ
     1.8 +from argparse import ArgumentParser
     1.9 +from re import sub
    1.10 +
    1.11 +try:
    1.12 +    environ["ALLUSERSPROFILE"]
    1.13 +except KeyError:
    1.14 +    db_file = "/usr/local/share/pEp/system.db"
    1.15 +else:
    1.16 +    db_file = environ["ALLUSERSPROFILE"] + r"\pEp\system.db"
    1.17 +
    1.18 +p = ArgumentParser(description="show trustwords instead of hex fingerprint")
    1.19 +p.add_argument('--db-path', '-d', type=str, default=db_file,
    1.20 +        help='path to pEp system db (default: ' + db_file + ')')
    1.21 +p.add_argument('--lang', '-l', type=str, default="en",
    1.22 +        help='use dictionary for language LANG (default: en)')
    1.23 +p.add_argument('--short', '-s', action='store_true',
    1.24 +        help='display the first 5 of the trustwords')
    1.25 +p.add_argument('hex', metavar="hex", type=str, nargs='+',
    1.26 +        help='hex values of fingerprint')
    1.27 +
    1.28 +args = p.parse_args()
    1.29 +
    1.30 +c = connect(args.db_path).cursor()
    1.31 +hex_string = sub(r"\W", "", "".join(args.hex))
    1.32 +
    1.33 +def hex_word(s):
    1.34 +    n = min(20, len(s)) if args.short else len(s)
    1.35 +    for i in range(0, n, 4):
    1.36 +        yield s[i:i+4]
    1.37 +
    1.38 +r = []
    1.39 +
    1.40 +for arg in hex_word(hex_string):
    1.41 +    c.execute("select word from wordlist where id = {} and lang = lower('{}')".format(
    1.42 +            str(int(arg, 16)), args.lang))
    1.43 +    r.append(c.fetchall()[0][0])
    1.44 +
    1.45 +print(" ".join(r))