aboutsummaryrefslogtreecommitdiff
path: root/fprintd_tk_lib.py
blob: 1f1b022d9605e9b1ad298d9f00a156e5bd806069 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#!/usr/bin/env python3
# Startdate: 2024-09-22-1 15:33
# Purpose: backend for fprintd_tk that uses fprintd-* binaries, in case I ever rewrite this to use dbus directly?
# Dependencies:
#    /usr/bin/fprintd-*
# Improve:
#    add a verbose/debug param to everything?

import os, subprocess, re

_user = os.getenv("USER")
#fre = re.compile("^.* . #[0-9]: \w+$")
fre = re.compile ("^.* - #[0-9]+: ([^ ]+)$")

def get_enrolled_fingers(user = None):
   # return list of full strings of fingers that are enrolled.
   enrolled_fingers = []
   if user is None:
      user = _user
   proc = subprocess.Popen(
      ["fprintd-list",user],
      stdout = subprocess.PIPE,
      universal_newlines = True # or maybe text=True
   )
   #print(result.stdout)
   while True:
      line = proc.stdout.readline()
      if not line:
         break
      if fre.match(line):
         #print(f"Got {fre.match(line).groups()[0]}")
         enrolled_fingers.append(fre.match(line).groups()[0].strip())
      else:
         #print(f"Not-matching: {line}",end="")
         pass
   return enrolled_fingers

def enroll_finger(finger, status_function = None, user = None):
   if user is None:
      user = _user
   proc = subprocess.Popen(
      ["fprintd-enroll","-f",finger,user],
      stdout = subprocess.PIPE,
      universal_newlines = True
   )
   while True:
      line = proc.stdout.readline()
      if not line:
         break
      if re.match("^.*enroll-duplicate.*",line):
         # Using the same finger as any other finger enrolled by any user already is considered an error by fprintd, and there is nothing we can do about it. It is an error and we must raise an error here.
         return "enroll-duplicate"
      elif re.match("^.*enroll-completed.*",line):
         return "enroll-completed"
      elif re.match("^.*enroll-stage-passed.*",line):
         if status_function:
            #print(f"Will use function {status_function}(\"enroll-stage-passed\")")
            status_function("enroll-stage-passed")
         print(f"Great, keep going!")
   # so the process has ended, now what?
   return f"Last line was: {line}"

def verify_finger(finger, status_function = None, user = None):
   if user is None:
      user = _user
   proc = subprocess.Popen(
      ["fprintd-verify","-f",finger,user],
      stdout = subprocess.PIPE,
      universal_newlines = True
   )
   while True:
      line = proc.stdout.readline()
      if not line:
         break
      if re.match("^.*verify-match (done).*",line):
         return "verify-match"
      else:
         if status_function:
            #print(f"Will use function {status_function}(\"enroll-stage-passed\")")
            status_function(line)
   return line
bgstack15