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