-
Notifications
You must be signed in to change notification settings - Fork 1
/
lisanalyze_gui.pyw
151 lines (134 loc) · 4.35 KB
/
lisanalyze_gui.pyw
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
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import tkinter as tk
#import Tkinter as tk
from tkinter import filedialog as tkFileDialog
#import tkFileDialog
import sys
import ast
import itertools
import re
import os
import subprocess
class Application(tk.Frame):
"""
Simple GUI for lisanalyze.py for those unwilling or unable to
use the command line. ;)
Currently known to work on Python 3.4.3, but shouldn't be too hard
to backport to Python 2, since only a few package names have changed.
That said, the subprocess.check_output() function call used herein
failed on Python 2.7.8 (which is outdated) while the exact same call
worked on Python 3.4.3.
"""
def __init__(self, master=None):
"""
Initialize our app.
"""
tk.Frame.__init__(self, master)
self.grid()
self.createWidgets()
def createWidgets(self):
self.topFrame = tk.Frame(padx=10, pady=10)
self.topFrame.grid()
# Variables required by widgets go here
self.entryText = tk.StringVar()
self.resultsText = tk.StringVar()
self.messageText = tk.StringVar()
self.fileOpt = {}
self.fileListVar = tk.StringVar()
# Left half: messages, buttons, filename list
self.lFrame = tk.Frame(self.topFrame)
self.lFrame.grid(column=0)
# Messages
self.messageBox = tk.Label(self.lFrame,
textvariable=self.messageText
)
self.messageBox.grid(row=1, column=0)
# Buttons
self.buttonFrame = tk.Frame(self.lFrame)
self.buttonFrame.grid(row=2, column=0)
self.addButton = tk.Button(self.buttonFrame,
command=self.addFiles, text="Add files"
)
self.addButton.grid(row=0, column=0)
self.removeButton = tk.Button(self.buttonFrame,
command=self.removeFiles, text="Remove files"
)
self.removeButton.grid(row=0, column=1)
self.runButton = tk.Button(self.buttonFrame,
command=self.runAnalyzer, text="Run")
self.runButton.grid(row=0, column=2)
self.quitButton = tk.Button(self.buttonFrame,
command=self.quit, text="Quit"
)
self.quitButton.grid(row=0, column=3)
# List of files
self.fileList = tk.Listbox(self.lFrame,
listvariable=self.fileListVar, width=50,
selectmode=tk.MULTIPLE
)
self.fileList.grid(row=3, column=0)
# Right half: results box
self.rFrame = tk.LabelFrame(self.topFrame, text="Results")
self.rFrame.grid(row=0, column=1)
self.resultsBox = tk.Label(self.rFrame,
textvariable=self.resultsText,
justify=tk.LEFT,
wraplength=300,
height=20, width=40
)
self.resultsBox.grid()
# additional functions go here
def addFiles(self):
filenames = tkFileDialog.askopenfilenames(**self.fileOpt)
# using a loop below, otherwise multiple items (if selected at once)
# will end up on the same line
for i in filenames:
# For Windows compatability
j = os.path.normpath(i)
self.fileList.insert(tk.END, j)
return True
def removeFiles(self):
"""
Removes files from file list in lower lefthand corner. Spaces
in filenames are a PITA, especially combined with Tk's use of
spaces as list item separators.
"""
filename_indexes = self.fileList.curselection() # indexes are strings
#pathRegex = "[\w." + os.sep + "]+"
#print("Before:" + self.fileListVar.get())
#print("Escape:" + self.fileListVar.get().replace(' ', '\ ')) # escape the spaces first
pathRegex = "'.+?'"
filenameList = re.findall(pathRegex, self.fileListVar.get().replace(' ', '\ '))
#print("FNL:", filenameList)
for i in filename_indexes:
print(int(i), filenameList[int(i)])
del filenameList[int(i)]
self.fileListVar.set(' '.join(filenameList))
#print("Join:" + ' '.join(filenameList))
#print("After:" + self.fileListVar.get())
return True
def runAnalyzer(self):
"""
Calls lisnalyzer.py. The runAnalyzer function is actually
pretty generic, just a wrapper around a creation of a
subprocess. Might block due to the subprocess call.
"""
filenames = ' '.join(ast.literal_eval(self.fileListVar.get()))
if (filenames[0] == filenames[-1]) and filenames[0] in ("'", '"'):
filenames = filenames[1:-1] # unquote filenames if necessary
lisanalyzePath = "./lisanalyze.py"
self.messageText.set("Current path to lisanalyze.py: " + lisanalyzePath)
#print(filenames)
self.resultsText.set(
str(
subprocess.check_output(
["python", lisanalyzePath, "-f", "-r", filenames],
universal_newlines=True
)
)
)
return True
app = Application()
app.master.title('lisanalyze_gui 0.1 "Heat haze"')
app.mainloop()