Moved reading 1pif file into own class.

This commit is contained in:
Markus Birth 2021-08-16 23:57:32 +02:00
parent 44686c312d
commit ae8e35a41d
Signed by: mbirth
GPG Key ID: A9928D7A098C3A9A
3 changed files with 30 additions and 7 deletions

View File

@ -3,6 +3,7 @@
import argparse
import datetime
import json
import onepif
from os.path import splitext
from pykeepass import create_database
@ -64,14 +65,9 @@ def getField(item, designation):
return None
with open("{}/data.1pif".format(args.inpath), "r") as fp:
data = fp.read().strip().split("***5642bee8-a5ff-11dc-8314-0800200c9a66***")
opif = onepif.OnepifReader("{}/data.1pif".format(args.inpath))
for line in data:
if line.strip() == "":
continue
item = json.loads(line.strip())
for item in opif:
if item.get("trashed"):
continue

26
onepif/OnepifReader.py Normal file
View File

@ -0,0 +1,26 @@
import json
SEPARATOR = "***5642bee8-a5ff-11dc-8314-0800200c9a66***"
class OnepifReader():
def __init__(self, filename):
self.filename = filename
self.fp = open(self.filename, "rt")
def __iter__(self):
return self
def __next__(self):
buffer = []
is_eof = True
for line in self.fp:
is_eof = False
if line.strip() == SEPARATOR:
break
buffer.append(line)
if is_eof:
raise StopIteration
jsonstr = "".join(buffer)
return json.loads(jsonstr)

1
onepif/__init__.py Normal file
View File

@ -0,0 +1 @@
from .OnepifReader import *