db/trustwords.py
author Krista Bennett <krista@pep-project.org>
Thu, 18 Jan 2018 00:46:19 +0100
changeset 2410 721952accdee
parent 1513 e7f7e42385b5
permissions -rw-r--r--
DANGER! MERGED IN ENGINE-289! SOME DOC BUGS STILL EXIST - USE WITH CARE!!!
     1 #!/usr/bin/env python3
     2 
     3 # This file is under GNU General Public License 3.0
     4 # see LICENSE.txt
     5 
     6 
     7 from sqlite3 import connect
     8 from os import environ
     9 from argparse import ArgumentParser
    10 from re import sub
    11 
    12 try:
    13     environ["ALLUSERSPROFILE"]
    14 except KeyError:
    15     db_file = "/usr/local/share/pEp/system.db"
    16 else:
    17     db_file = environ["ALLUSERSPROFILE"] + r"\pEp\system.db"
    18 
    19 p = ArgumentParser(description="show trustwords instead of hex fingerprint")
    20 p.add_argument('--db-path', '-d', type=str, default=db_file,
    21         help='path to pEp system db (default: ' + db_file + ')')
    22 p.add_argument('--lang', '-l', type=str, default="en",
    23         help='use dictionary for language LANG (default: en)')
    24 p.add_argument('--short', '-s', action='store_true',
    25         help='display the first 5 of the trustwords')
    26 p.add_argument('hex', metavar="hex", type=str, nargs='+',
    27         help='hex values of fingerprint')
    28 
    29 args = p.parse_args()
    30 
    31 c = connect(args.db_path).cursor()
    32 hex_string = sub(r"\W", "", "".join(args.hex))
    33 
    34 def hex_word(s):
    35     n = min(20, len(s)) if args.short else len(s)
    36     for i in range(0, n, 4):
    37         yield s[i:i+4]
    38 
    39 r = []
    40 
    41 for arg in hex_word(hex_string):
    42     c.execute("select word from wordlist where id = {} and lang = lower('{}')".format(
    43             str(int(arg, 16)), args.lang))
    44     r.append(c.fetchall()[0][0])
    45 
    46 print(" ".join(r))