-
Notifications
You must be signed in to change notification settings - Fork 2
/
gui.py
239 lines (204 loc) · 7.9 KB
/
gui.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# -*- coding: utf-8 -*-
"""
Created on Wed Nov 14 16:26:35 2018
@author: chris
"""
import tkinter as tk
import json
import os
import operator
import webbrowser
from abuse_detection_steemit import AbuseDetection
class SlveDat(tk.Frame):
"""
Initialise the gui class.
"""
def __init__(self, master, path=None):
tk.Frame.__init__(self, master)
self.pack(side='top', fill=tk.BOTH, expand=1)
# Declare tk variables
self.abuse_category = tk.StringVar()
self.ordering = tk.StringVar()
# Set tk variables
self.abuse_category.set('Voters')
self.ordering.set('Value')
# Create frames
self.viewer_frame = tk.Frame(self, width=30)
self.descrp_frame = tk.Frame(self, width=15)
self.values_frame = tk.Frame(self, width=15)
self.filter_frame = tk.Frame(self, width=30)
# Create widgets
# --- listbox
self.user_browser = tk.Listbox(self.viewer_frame, width=30, height=50)
self.scrollbar = tk.Scrollbar(self.viewer_frame)
# --- static labels
self.name_label = tk.Label(self.descrp_frame, text='Username:')
self.total_rcvd_label = tk.Label(self.descrp_frame, text='Total $:')
self.total_lmv_label = tk.Label(self.descrp_frame, text='Total #:')
# --- user info
self.user_name_label = tk.Entry(self.values_frame, width=20)
self.rcvd_label = tk.Entry(self.values_frame, width=20)
self.lmv_label = tk.Entry(self.values_frame, width=20)
self.account_lookup = tk.Button(self.values_frame,
text='Account lookup',
command=self.lookup,
width=17)
# --- searching options
self.perspective_option = tk.OptionMenu(
self.filter_frame,
self.abuse_category,
'Voters',
'Recievers',
command=self.populate
)
self.order_option = tk.OptionMenu(
self.filter_frame,
self.ordering,
'Value',
'Quantity',
'Alphabetic',
command=self.populate
)
self.namefilter_entry = tk.Entry(self.filter_frame)
# Widget settings
self.user_browser.bind('<<ListboxSelect>>', self.selection)
self.user_browser.config(yscrollcommand=self.scrollbar.set)
self.scrollbar.config(command=self.user_browser.yview)
self.namefilter_entry.bind('<KeyRelease>', self.populate)
# Declare frame and widet groupings
frames = [
self.viewer_frame,
self.descrp_frame,
self.values_frame,
self.filter_frame
]
self.entries = [
self.user_name_label,
self.rcvd_label,
self.lmv_label
]
# Pack the frames
for i in frames:
i.pack(side='left', fill=tk.Y, expand=1)
# Pack the widgets onto the frame
# --- listbox
self.scrollbar.pack(side=tk.RIGHT, fill=tk.Y)
self.user_browser.pack()
# --- static labels
self.name_label.pack(side='top', anchor='w')
self.total_rcvd_label.pack(side='top', anchor='w')
self.total_lmv_label.pack(side='top', anchor='w')
# --- user info
self.user_name_label.pack(side='top')
self.rcvd_label.pack(side='top')
self.lmv_label.pack(side='top')
self.account_lookup.pack(side='top')
# --- searching options
self.perspective_option.grid(sticky='news')
self.order_option.grid(sticky='news')
self.namefilter_entry.grid()
# Startup functions
# --- get current path
if not path:
path = os.getcwd()
# --- load the db
self.db_loader(path=path)
# --- start the stream in the background
self.ad = AbuseDetection(data=self.data)
self.ad.stream()
"""
Populates the listbox with all users in the selected dataset (voters/
recievers), in order (alphabetical/$/#), with users filtered out based on
user search. This is called whenever there is a keypress in the
namefilter_entry widget
@param event
"""
def populate(self, event=None):
# Get filtration settings
dataset = self.get_dataset()
order = self.ordering.get()
filtertext = self.namefilter_entry.get()
# Create a temporary dictionary so entries can be ordered after filters
temp_dict = dict()
# Loops through every user in the selected dataset
for k, v in self.data[dataset].items():
# Check if the username cointains the filtertext
if filtertext.lower() in k:
# Add correct value to temporary list
if order == 'Value':
temp_dict[k] = v['value']
else:
temp_dict[k] = v['quantity']
# Order the dictionary
if order == 'Alphabetic':
temp_dict = sorted(temp_dict.items(), key=operator.itemgetter(0))
else:
temp_dict = sorted(temp_dict.items(), key=operator.itemgetter(1))
# Clear listbox
self.user_browser.delete(0, tk.END)
# Fill listbox with filtered, ordered users
for user, values in temp_dict:
self.user_browser.insert(0, user)
"""
Loads the databases. If no path is supplied, it will use the working
directory. Is called at instance initialisation.
@param path
"""
def db_loader(self, path=None):
try:
# Load data from databases
x = open(path + r'\abuse_log.json', 'r').read()
# Load data into dictionaries
self.data = json.loads(x)
# Update the listbox
self.populate()
except:
self.data = {'voters':{},"recievers":{}}
"""
This procedure is called whenever a user makes a selection in the
user_browser widget. It updates the Entry widgets to contain information
relating to the selected user.
@param event
"""
def selection(self, event=None):
# Get the selected uername
index = self.user_browser.curselection()
user = self.user_browser.get(index[0])
# Get the dataset to look in
dataset = self.get_dataset()
# Find the value and quantity of votes cast/recieved by user
value = round(self.data[dataset][user]['value'], 3)
total_lmv = self.data[dataset][user]['quantity']
# Enable widget editing
for widget in self.entries:
widget.config(state='normal')
# Clear entries
self.user_name_label.delete(0, tk.END)
self.rcvd_label.delete(0, tk.END)
self.lmv_label.delete(0, tk.END)
# Insert new entries
self.user_name_label.insert(0, user)
self.rcvd_label.insert(0, value)
self.lmv_label.insert(0, total_lmv)
# Disable widget editing
for widget in self.entries:
widget.config(state='readonly')
print(user)
"""
Get the dataset to use (voter/recievers) and make it lowercase before
returning.
@return dataset
"""
def get_dataset(self):
dataset = self.abuse_category.get()
dataset = dataset.lower()
return dataset
def lookup(self):
username = self.user_name_label.get()
webbrowser.open('https://steemit.com/@' + username + '/')
if __name__ == '__main__':
root = tk.Tk()
root.title('SLVE - Data Analysis Tool')
root.geometry('520x200')
slve_dat = SlveDat(root)
root.mainloop()