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