Skip to content

Commit

Permalink
Inherit from FPrintException
Browse files Browse the repository at this point in the history
  • Loading branch information
xantares authored and luksan committed Jan 2, 2018
1 parent 0640f29 commit b9f67b8
Showing 1 changed file with 41 additions and 38 deletions.
79 changes: 41 additions & 38 deletions pyfprint/pyfprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@
_init_ok = False


class FPrintException(Exception):
pass

def _dbg(*arg):
# print arg
pass
Expand All @@ -41,7 +44,7 @@ def fp_init():
"""Call this before doing anything else."""
_init_ok = (pyf.fp_init() == 0)
if not _init_ok:
raise Exception("fprint initialization failed.")
raise FPrintException("fprint initialization failed.")


def fp_exit():
Expand Down Expand Up @@ -76,7 +79,7 @@ def __init__(self, dev_ptr=None, dscv_ptr=None, DscvList=None):
self.dscv = dscv_ptr
self.DscvList = DscvList
if dscv_ptr and DscvList == None:
raise Exception("Programming error? Device contructed with dscv without DscvList.")
raise FPrintException("Programming error? Device contructed with dscv without DscvList.")

def close(self):
"""Closes the device. No more methods, except open(), may be called after this."""
Expand All @@ -87,10 +90,10 @@ def close(self):
def open(self):
"""Connects to the device."""
if self.dev:
raise Exception("Device already open")
raise FPrintException("Device already open")
self.dev = pyf.fp_dev_open(self.dscv)
if not self.dev:
raise Exception("device open failed")
raise FPrintException("device open failed")

def driver(self):
"""
Expand Down Expand Up @@ -121,7 +124,7 @@ def nr_enroll_stages(self):
"""
if self.dev:
return pyf.fp_dev_get_nr_enroll_stages(self.dev)
raise Exception("Device not open")
raise FPrintException("Device not open")

def is_compatible(self, fprint):
"""
Expand All @@ -134,32 +137,32 @@ def is_compatible(self, fprint):
return pyf.fp_dev_supports_print_data(self.dev, fprint.data_ptr) == 1
if fprint.dscv_ptr:
return pyf.fp_dev_supports_dscv_print(self.dev, fprint.dscv_ptr) == 1
raise Exception("No print found")
raise FPrintException("No print found")
if self.dscv:
if fprint.data_ptr:
return pyf.fp_dscv_dev_supports_print_data(self.dscv, fprint.data_ptr) == 1
if fprint.dscv_ptr:
return pyf.fp_dscv_dev_supports_dscv_print(self.dscv, fprint.dscv_ptr) == 1
raise Exception("No print found")
raise Exception("No device found")
raise FPrintException("No print found")
raise FPrintException("No device found")

def supports_imaging(self):
"""If true, the device can return an image of the finger."""
if self.dev:
return pyf.fp_dev_supports_imaging(self.dev) == 1
raise Exception("Device not open")
raise FPrintException("Device not open")

def img_width(self):
"""Return the width of the images scanned by the device, in pixels."""
if self.dev:
return pyf.fp_dev_get_img_width(self.dev)
raise Exception("Device not open")
raise FPrintException("Device not open")

def img_height(self):
"""Return the height of the images scanned by the device, in pixels."""
if self.dev:
return pyf.fp_dev_get_img_height(self.dev)
raise Exception("Device not open")
raise FPrintException("Device not open")

def capture_image(self, wait_for_finger):
"""
Expand All @@ -168,7 +171,7 @@ def capture_image(self, wait_for_finger):
on the sensor before image capture.
"""
if not self.dev:
raise Exception("Device not open")
raise FPrintException("Device not open")

if not self.supports_imaging():
return None
Expand All @@ -180,7 +183,7 @@ def capture_image(self, wait_for_finger):
(r, img) = pyf.pyfp_dev_img_capture(self.dev, unconditional)
img = Image(img)
if r != 0:
raise Exception("image_capture failed. error: %i" % r)
raise FPrintException("image_capture failed. error: %i" % r)
return img

def enroll_finger(self):
Expand All @@ -194,12 +197,12 @@ def enroll_finger(self):
the enrolled image
"""
if not self.dev:
raise Exception("Device not open")
raise FPrintException("Device not open")
r = pyf.FP_ENROLL_RETRY
while r != pyf.FP_ENROLL_COMPLETE:
r, fprint, img = pyf.pyfp_enroll_finger_img(self.dev)
if r < 0:
raise Exception("Internal I/O error while enrolling: %i" % i)
raise FPrintException("Internal I/O error while enrolling: %i" % i)
img = Image(img)
if r == pyf.FP_ENROLL_COMPLETE:
_dbg("enroll complete")
Expand Down Expand Up @@ -238,13 +241,13 @@ def verify_finger(self, fprint):
true if the finger and the Fprint match
"""
if not self.dev:
raise Exception("Device not open")
raise FPrintException("Device not open")
while True:
r, img = pyf.pyfp_verify_finger_img(
self.dev, fprint._get_print_data_ptr())
img = Image(img)
if r < 0:
raise Exception("verify error: %i" % r)
raise FPrintException("verify error: %i" % r)
if r == pyf.FP_VERIFY_NO_MATCH:
return False, img
if r == pyf.FP_VERIFY_MATCH:
Expand All @@ -262,7 +265,7 @@ def verify_finger(self, fprint):
def supports_identification(self):
"""Return True if the device supports the identify_finger method."""
if not self.dev:
raise Exception("Device not open")
raise FPrintException("Device not open")
return pyf.fp_dev_supports_identification(self.dev) == 1

def identify_finger(self, fprints):
Expand All @@ -278,16 +281,16 @@ def identify_finger(self, fprints):
"""

if not self.dev:
raise Exception("Device not open")
raise FPrintException("Device not open")
gallery = pyf.pyfp_print_data_array(len(fprints))
for x in fprints:
if not self.is_compatible(x):
raise Exception("can't verify uncompatible print")
raise FPrintException("can't verify uncompatible print")
gallery.append(x._get_print_data_ptr())
(r, offset, img) = pyf.pyfp_identify_finger_img(self.dev, gallery.list)
img = Image(img)
if r < 0:
raise Exception("identification error")
raise FPrintException("identification error")
if r == pyf.FP_VERIFY_NO_MATCH:
return (None, None, img)
if r == pyf.FP_VERIFY_MATCH:
Expand All @@ -311,10 +314,10 @@ def load_print_from_disk(self, finger):
Return a Fprint.
"""
if not self.dev:
raise Exception("Device not open")
raise FPrintException("Device not open")
(r, print_ptr) = pyf.fp_print_data_load(self.dev, finger)
if r != 0:
raise Exception("could not load print from disk")
raise FPrintException("could not load print from disk")
return Fprint(data_ptr=print_ptr)

def delete_stored_finger(self, finger):
Expand All @@ -324,7 +327,7 @@ def delete_stored_finger(self, finger):
- finger should be a value from Fingers.
"""
if not self.dev:
raise Exception("Device not open")
raise FPrintException("Device not open")
r = pyf.fp_print_data_delete(self.dev, finger)
if r != 0:
raise "delete failed"
Expand Down Expand Up @@ -382,7 +385,7 @@ def save_to_file(self, filename):
"""Save the image as a pgm file."""
r = pyf.fp_img_save_to_file(self._img, filename)
if r != 0:
raise Exception("Save failed")
raise FPrintException("Save failed")

def standardize(self):
"""Normalize orientation and colors of the image."""
Expand All @@ -403,7 +406,7 @@ def binarize(self):
self.standardize()
i = pyf.fp_img_binarize(self._img)
if i == None:
raise Exception("Binarize failed")
raise FPrintException("Binarize failed")
i = Image(img_ptr=i, bin=True)
i._minutiae = self._minutiae
return i
Expand All @@ -417,7 +420,7 @@ def minutiae(self):
if self._minutiae:
return self._minutiae
if self._bin:
raise Exception("Cannot find minutiae in binarized image")
raise FPrintException("Cannot find minutiae in binarized image")
if not self._std:
self.standardize()
(min_list, nr) = pyf.fp_img_get_minutiae(self._img)
Expand Down Expand Up @@ -477,7 +480,7 @@ def __init__(self, serial_data=None, data_ptr=None, dscv_ptr=None, DscvList=None
return

if dscv_ptr != None and DscvList == None:
raise Exception("Programming error: Fprint constructed with dscv_prt with DscvList == None")
raise FPrintException("Programming error: Fprint constructed with dscv_prt with DscvList == None")

def __del__(self):
if self.data_ptr:
Expand All @@ -496,23 +499,23 @@ def driver_id(self):
return pyf.fp_print_data_get_driver_id(self.data_ptr)
elif self.dscv_ptr:
return pyf.fp_dscv_print_get_driver_id(self.dscv_ptr)
raise Exception("no print")
raise FPrintException("no print")

def devtype(self):
"""Return an integer representing the type of device used to scan this print."""
if self.data_ptr:
return pyf.fp_print_data_get_devtype(self.data_ptr)
elif self.dscv_ptr:
return pyf.fp_dscv_print_get_devtype(self.dscv_ptr)
raise Exception("no print")
raise FPrintException("no print")

def finger(self):
"""
If the Fprint was returned from discover_prints(), return
the Finger the Fprint represents. Otherwise raise an exception.
"""
if not self.dscv_ptr:
raise Exception("finger() needs a discovered print")
raise FPrintException("finger() needs a discovered print")
return pyf.fp_dscv_print_get_finger(self.dscv_ptr)

def delete_from_disk(self):
Expand All @@ -521,7 +524,7 @@ def delete_from_disk(self):
from the users home directory. Otherwise raise an exception.
"""
if not self.dscv_ptr:
raise Exception("delete needs a discovered print")
raise FPrintException("delete needs a discovered print")
return pyf.fp_dscv_print_delete(self.dscv_ptr)

def save_to_disk(self, finger):
Expand All @@ -532,16 +535,16 @@ def save_to_disk(self, finger):
"""
r = pyf.fp_print_data_save(self.data_ptr, finger)
if r != 0:
raise Exception("save failed")
raise FPrintException("save failed")

def _data_from_dscv(self):
if self.data_ptr:
return
if not self.dscv_ptr:
raise Exception("no print")
raise FPrintException("no print")
(r, ptr) = pyf.fp_print_data_from_dscv_print(self.dscv_ptr)
if r != 0:
raise Exception("print data from dscv failed")
raise FPrintException("print data from dscv failed")
self.data_ptr = ptr

def data(self):
Expand All @@ -551,10 +554,10 @@ def data(self):
contructor of Fprint.
"""
if not self.data_ptr:
raise Exception("no print")
raise FPrintException("no print")
s = pyf.pyfp_print_get_data(self.data_ptr)
if not len(s):
raise Exception("serialization failed")
raise FPrintException("serialization failed")
return s


Expand Down Expand Up @@ -627,5 +630,5 @@ def discover_devices():
devs = pyf.fp_discover_devs()

if not devs:
raise Exception("Device discovery failed")
raise FPrintException("Device discovery failed")
return DiscoveredDevices(devs)

0 comments on commit b9f67b8

Please sign in to comment.