db/safewords.py
author vb
Wed, 25 Jun 2014 18:48:18 +0200
changeset 2 f1fc3afacfd6
permissions -rw-r--r--
extra files for db and testing
     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 safewords 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 safewords')
    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))