backup file changed from pickle to json (for python2&3 support)

This commit is contained in:
Arturo Hernandez 2018-09-03 20:00:30 -04:00
parent 84e4d98eb1
commit 69275a79bd
4 changed files with 53 additions and 18 deletions

BIN
-

Binary file not shown.

View File

@ -8,7 +8,7 @@ import datetime
import codecs
from builtins import input
import pickle
import json
sys.path.append("zk")
@ -55,7 +55,7 @@ try:
fp_version = conn.get_fp_version()
print ('Serial Number : {}'.format(serialnumber))
print ('Finger Version : {}'.format(fp_version))
filename = args.filename if args.filename else "{}.bak".format(serialnumber)
filename = args.filename if args.filename else "{}.json.bak".format(serialnumber)
print ('')
if not args.restore:
print ('--- sizes & capacity ---')
@ -76,29 +76,33 @@ try:
#save to file!
print ('')
print ('Saving to file {} ...'.format(filename))
output = open(filename, 'wb')
output = open(filename, 'w')
data = {
'version':'1.00ut',
'version':'1.00jut',
'serial': serialnumber,
'fp_version': fp_version,
'users': users,
'templates':templates
'users': [u.__dict__ for u in users],
'templates':[t.json_pack() for t in templates]
}
pickle.dump(data, output, -1)
json.dump(data, output, indent=1)
output.close()
else:
print ('Reading file {}'.format(filename))
infile = open(filename, 'rb')
data = pickle.load(infile)
infile = open(filename, 'r')
data = json.load(infile)
infile.close()
#compare versions...
if data['version'] != '1.00ut':
if data['version'] != '1.00jut':
raise BasicException("file with different version... aborting!")
if data['fp_version'] != fp_version:
raise BasicException("fingerprint version mismmatch {} != {} ... aborting!".format(fp_version, data['fp_version']))
#TODO: check data consistency...
print ("INFO: ready to write {} users".format(len(data['users'])))
print ("INFO: ready to write {} templates".format(len(data['templates'])))
users = [User.json_unpack(u) for u in data['users']]
#print (users)
print ("INFO: ready to write {} users".format(len(users)))
templates = [Finger.json_unpack(t) for t in data['templates']]
#print (templates)
print ("INFO: ready to write {} templates".format(len(templates)))
#input serial number to corroborate.
print ('WARNING! the next step will erase the current device content.')
print ('Please input the serialnumber of this device [{}] to acknowledge the ERASING!'.format(serialnumber))
@ -111,10 +115,12 @@ try:
if args.clear_attendance:
print ('Clearing attendance too!')
conn.clear_attendance()
conn.read_sizes()
print (conn)
print ('Restoring Data...')
for u in data['users']:
for u in users:
#look for Templates
temps = list(filter(lambda f: f.uid ==u.uid, data['templates']))
temps = list(filter(lambda f: f.uid ==u.uid, templates))
#print ("user {} has {} fingers".format(u.uid, len(temps)))
conn.save_user_template(u,temps)
conn.enable_device()

View File

@ -16,10 +16,26 @@ class Finger(object):
def repack_only(self): #only template
return pack("H%is" % (self.size), self.size, self.template)
def __eq__(self, other):
@staticmethod
def json_unpack(json):
return Finger(
uid=json['uid'],
fid=json['fid'],
valid=json['valid'],
template=codecs.decode(json['template'],'hex')
)
def json_pack(self): #packs for json
return {
"size": self.size,
"uid": self.uid,
"fid": self.fid,
"valid": self.valid,
"template": codecs.encode(self.template, 'hex').decode('ascii')
}
def __eq__(self, other):
return self.__dict__ == other.__dict__
def __str__(self):
return "<Finger> [uid:{:>3}, fid:{}, size:{:>4} v:{} t:{}]".format(self.uid, self.fid, self.size, self.valid, self.mark)

View File

@ -10,7 +10,20 @@ class User(object):
self.password = str(password)
self.group_id = str(group_id)
self.user_id = user_id
self.card = card # 64 int to 40 bit int
self.card = int(card) # 64 int to 40 bit int
@staticmethod
def json_unpack(json):
#validate?
return User(
uid=json['uid'],
name=json['name'],
privilege=json['privilege'],
password=json['password'],
group_id=json['group_id'],
user_id=json['user_id'],
card=json['card']
)
def repack29(self): # with 02 for zk6 (size 29)
return pack("<BHB5s8sIxBhI", 2, self.uid, self.privilege, self.password.encode(User.encoding, errors='ignore'), self.name.encode(User.encoding, errors='ignore'), self.card, int(self.group_id) if self.group_id else 0, 0, int(self.user_id))