forked from Programvareverkstedet/dibbler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.py
128 lines (115 loc) · 4.14 KB
/
helpers.py
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
from db import *
from sqlalchemy import or_, and_
import pwd
import subprocess
import os
import signal
def search_user(string, session, ignorethisflag=None):
string = string.lower()
exact_match = session.query(User).filter(or_(User.name == string, User.card == string, User.rfid == string)).first()
if exact_match:
return exact_match
user_list = session.query(User).filter(or_(User.name.ilike(f'%{string}%'),
User.card.ilike(f'%{string}%'),
User.rfid.ilike(f'%{string}%'))).all()
return user_list
def search_product(string, session, find_hidden_products=True):
if find_hidden_products:
exact_match = session.query(Product).filter(or_(Product.bar_code == string, Product.name == string)).first()
else:
exact_match = session.query(Product).filter(or_(Product.bar_code == string,
and_(Product.name == string, Product.hidden == False))).first()
if exact_match:
return exact_match
if find_hidden_products:
product_list = session.query(Product).filter(or_(Product.bar_code.ilike(f'%{string}%'),
Product.name.ilike(f'%{string}%'))).all()
else:
product_list = session.query(Product).filter(or_(Product.bar_code.ilike(f'%{string}%'),
and_(Product.name.ilike(f'%{string}%'),
Product.hidden == False))).all()
return product_list
def system_user_exists(username):
try:
pwd.getpwnam(username)
except KeyError:
return False
except UnicodeEncodeError:
return False
else:
return True
def guess_data_type(string):
if string.startswith('ntnu') and string[4:].isdigit():
return 'card'
if string.isdigit() and len(string) == 10:
return 'rfid'
if string.isdigit() and len(string) in [8,13]:
return 'bar_code'
# if string.isdigit() and len(string) > 5:
# return 'card'
if string.isalpha() and string.islower() and system_user_exists(string):
return 'username'
return None
# def retrieve_user(string, session):
# # first = session.query(User).filter(or_(User.name==string, User.card==string)).first()
# search = search_user(string,session)
# if isinstance(search,User):
# print "Found user "+search.name
# return search
# else:
# if len(search) == 0:
# print "No users found matching your search"
# return None
# if len(search) == 1:
# print "Found one user: "+list[0].name
# if confirm():
# return list[0]
# else:
# return None
# else:
# print "Found "+str(len(search))+" users:"
# return select_from_list(search)
# def confirm(prompt='Confirm? (y/n) '):
# while True:
# input = raw_input(prompt)
# if input in ["y","yes"]:
# return True
# elif input in ["n","no"]:
# return False
# else:
# print "Nonsense!"
# def select_from_list(list):
# while True:
# for i in range(len(list)):
# print i+1, " ) ", list[i].name
# choice = raw_input("Select user :\n")
# if choice in [str(x+1) for x in range(len(list))]:
# return list[int(choice)-1]
# else:
# return None
def argmax(d, all=False, value=None):
maxarg = None
maxargs = []
if value != None:
dd = d
d = {}
for key in list(dd.keys()):
d[key] = value(dd[key])
for key in list(d.keys()):
if maxarg == None or d[key] > d[maxarg]:
maxarg = key
if all:
return [k for k in list(d.keys()) if d[k] == d[maxarg]]
return maxarg
def less(string):
'''
Run less with string as input; wait until it finishes.
'''
# If we don't ignore SIGINT while running the `less` process,
# it will become a zombie when someone presses C-c.
int_handler = signal.signal(signal.SIGINT, signal.SIG_IGN)
env = dict(os.environ)
env['LESSSECURE'] = '1'
proc = subprocess.Popen('less', env=env, encoding='utf-8', stdin=subprocess.PIPE)
proc.communicate(string)
signal.signal(signal.SIGINT, int_handler)