-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ZkpwgenWidget.py
134 lines (97 loc) · 3.02 KB
/
ZkpwgenWidget.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
129
130
131
132
133
134
#!python3
from collections import namedtuple
import appex
import ui
import os
import json
import random
import clipboard
import zkpwgen
widget_name = 'ZkpwgenView'
ViewModel = namedtuple('ViewModel', ['length', 'letters', 'numbers', 'secure'])
current_model = ViewModel(8, True, True, False)
settings_file = '.ZkpwgenWidget.json'
def copy_model(model=None, **kwargs):
if model is None:
model = current_model
curr_vals = model._asdict()
curr_vals.update(kwargs)
return ViewModel(**curr_vals)
try:
with open(settings_file) as f:
persist_vals = json.load(f)
current_model = copy_model(**persist_vals)
except:
pass
class TextFieldDelegate(object):
def textfield_should_begin_editing(self):
return False
def regen(sender):
update_view(sender.superview, current_model)
def increment_length(sender):
model = copy_model(length=current_model.length + 1)
update_view(sender.superview, model)
def decrement_length(sender):
model = copy_model(length=current_model.length - 1)
update_view(sender.superview, model)
def set_numbers(sender):
vals = {'numbers': sender.value}
if not sender.value:
vals['secure'] = False
model = copy_model(**vals)
update_view(sender.superview, model)
def set_letters(sender):
vals = {'letters': sender.value}
if not sender.value:
vals['secure'] = False
model = copy_model(**vals)
update_view(sender.superview, model)
def set_secure(sender):
vals = {'secure': sender.value}
if sender.value:
vals['letters'] = True
vals['numbers'] = True
model = copy_model(**vals)
update_view(sender.superview, model)
def init_view():
v = ui.load_view(widget_name)
v['PasswordField'].delegate = TextFieldDelegate
update_view(v, current_model)
return v
def generate(model):
return zkpwgen.generate_simple(model.length,
letters=model.letters,
numerals=model.numbers,
secure=model.secure)
def can_generate(model):
try:
generate(model)
return True
except:
return False
def update_view(view, model):
try:
pw = generate(model)
view['NumbersSwitch'].value = model.numbers
view['LettersSwitch'].value = model.letters
view['SecureSwitch'].value = model.secure
view['LengthDownButton'].enabled = can_generate(
copy_model(model, length=model.length - 1))
view['LengthValueLabel'].text = str(model.length)
view['PasswordField'].text = pw
clipboard.set(pw)
persist_model(model)
except:
update_view(view, current_model)
def persist_model(model):
global current_model
current_model = model
with open(settings_file, 'w') as f:
json.dump(model._asdict(), f)
def main():
v = appex.get_widget_view()
if v is not None and v.name == widget_name:
return
appex.set_widget_view(init_view())
if __name__ == '__main__':
main()