Start outsourcing KeePass handling into kpwriter.py

This commit is contained in:
Markus Birth 2021-08-19 00:25:39 +02:00
parent 5847073699
commit ad69b83aa6
Signed by: mbirth
GPG Key ID: A9928D7A098C3A9A
3 changed files with 26 additions and 8 deletions

View File

@ -4,9 +4,9 @@ import argparse
import datetime
import json
import onepif
import kpwriter
from os.path import splitext
from pykeepass import create_database
from urllib.parse import urlparse, quote_plus
parser = argparse.ArgumentParser(description="Convert 1Password 1PIF exports into a KeePass KDBX file.")
@ -15,15 +15,23 @@ parser.add_argument("outfile", metavar="output.kdbx", nargs="?", help="Desired f
args = parser.parse_args()
# If no outfile given, use infile name
if not args.outfile:
fileparts = splitext(args.inpath)
args.outfile = "{}.kdbx".format(fileparts[0])
# If given outfile doesn't have .kdbx extension, add it
outparts = splitext(args.outfile)
if outparts[1] != ".kdbx":
args.outfile += ".kdbx"
kp = create_database(args.outfile, password="test")
# Open input file
print("Input file: {}".format(args.inpath))
opif = onepif.OnepifReader("{}/data.1pif".format(args.inpath))
# Open output file
print("Output file: {}".format(args.outfile))
kp = kpwriter.KpWriter(args.outfile, "test")
def getField(item, designation):
@ -49,7 +57,8 @@ def getTotp(item):
return None
opif = onepif.OnepifReader("{}/data.1pif".format(args.inpath))
# FIXME: Convert everything to use KpWriter class
kp = kp.kp
for item in opif:
if item.get("trashed"):
@ -166,7 +175,7 @@ for item in opif:
entry.set_custom_property(ft, str(d))
elif k == "address":
# needs special handling!
pass # for now
pass # for now
else:
raise Exception("Unknown k: {}".format(k))

6
kpwriter.py Normal file
View File

@ -0,0 +1,6 @@
from pykeepass import create_database
class KpWriter:
def __init__(self, filename, password="test"):
self.kp = create_database(filename, password)

View File

@ -26,10 +26,13 @@ class OnepifEntry():
def __init__(self, data):
self.raw = data
if data["typeName"] not in TYPES:
raise Exception("Unknown record type: {}".format(data["typeName"]))
self.type = data["typeName"]
self.type_name = TYPES[data["typeName"]]
self.set_type(data["typeName"])
def set_type(self, new_type):
if new_type not in TYPES:
raise Exception("Unknown record type: {}".format(new_type))
self.type = new_type
self.type_name = TYPES[new_type]
def __getattr__(self, name):
if name not in self.raw: