Update Gui testing

This commit is contained in:
Lerking
2018-02-27 13:18:20 +01:00
committed by GitHub
parent 1a978407c0
commit 4280264fa6
11 changed files with 1355 additions and 60 deletions
+131 -60
View File
@@ -9,11 +9,58 @@
import os
import sys
import multiprocessing as mp
import io
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, Gio, GdkPixbuf
from gi.repository import Gtk, Gio, GObject as gobject
import time
from multiprocessing import Process, Value, Lock, Pool, Queue
import threading
gobject.threads_init()
class Listener(gobject.GObject):
__gsignals__ = {
'updated' : (gobject.SIGNAL_RUN_LAST,
gobject.TYPE_NONE,
(gobject.TYPE_FLOAT, gobject.TYPE_STRING, gobject.TYPE_INT)),
'finished': (gobject.SIGNAL_RUN_LAST,
gobject.TYPE_NONE,
())
}
def __init__(self, queue):
gobject.GObject.__init__(self)
self.queue = queue
def go(self):
print("Listener has started")
while True:
# Listen for results on the queue and process them accordingly
data = self.queue.get()
# Check if finished
if data[1]=="finished":
print("Listener is finishing.")
self.emit("finished")
return
else:
self.emit('updated', data[0], data[1], data[2])
gobject.type_register(Listener)
class Worker():
def __init__(self, queue, filecnt, filelist):
self.queue = queue
self.filecnt = filecnt
def go(self):
print("The worker has started doing some work (counting from 0 to 9)")
for i in range(self.filecnt):
proportion = (float(i+1))/self.filecnt
self.queue.put((proportion, "working...", i))
process_file(filelist[i])
self.queue.put((1.0, "finished"))
print("The worker has finished.")
tupline = []
preproc = ()
@@ -22,8 +69,9 @@ folderlist = []
cnt = 0
srcdir = ''
destdir = ''
num_cores = mp.cpu_count()
#num_cores = multiprocessing.cpu_count()
fileindex = 0
filecnt = 0
def sourcedir_filecnt(sourcedir):
### Return the number of files, ending with '.h', in sourcedir - including subdirectories ###
@@ -110,12 +158,66 @@ def process_file(data):
newfile.write(outfile)
newfile.close()
def async_process(num):
pool = mp.Pool(processes=num)
pool.map(process_file, filelist)
class ExampleApp:
class Handler:
global app
global destlabel
def __init__(self):
self.filecnt = 0
self.fileindex = 0
self.process = None
self.app = Gtk.Application.new("org.h2inc", Gio.ApplicationFlags(0))
self.app.connect("activate", self.on_app_activate)
self.app.connect("shutdown", self.on_app_shutdown)
app = self.app
def on_app_activate(self, app):
builder = Gtk.Builder()
builder.add_from_file("h2inc.glade")
builder.connect_signals(self)
self.obj = builder.get_object
self.obj("window").set_application(app)
self.obj("window").set_wmclass("h2inc_gtk","h2inc_gtk")
self.obj("window").show_all()
button = Gtk.Button.new_from_stock(Gtk.STOCK_CANCEL)
button.set_property("can-default",True)
self.obj("filechooser_dialog").add_action_widget(button, Gtk.ResponseType.CANCEL)
button = Gtk.Button.new_from_stock(Gtk.STOCK_APPLY)
button.set_property("can-default",True)
self.obj("filechooser_dialog").add_action_widget(button, Gtk.ResponseType.OK)
def on_app_shutdown(self, app):
self.app.quit()
def run(self, argv):
self.app.run(argv)
def callbackDisplay(self, obj, fraction, text, index, data=None):
self.obj("progress_label").set_text("{} of {}".format(index+1, self.filecnt))
self.obj("progressbar").set_fraction(fraction)
self.obj("progressbar").set_text(text)
def callbackFinished(self, obj, data=None):
if self.process==None:
raise RuntimeError("No worker process started")
print("all done; joining worker process")
self.process.join()
self.process = None
self.obj("progressbar").set_fraction(1.0)
self.obj("progressbar").set_text("done")
self.obj("sourceframe").set_sensitive(True)
self.obj("translate_button").set_sensitive(True)
self.obj("destination_label").set_sensitive(True)
self.obj("destination_entry").set_sensitive(True)
self.obj("destination_button").set_sensitive(True)
self.obj("include_checkbutton").set_sensitive(True)
self.obj("translation_frame").set_sensitive(True)
def on_window_destroy(self,window):
window.close()
@@ -130,17 +232,6 @@ class Handler:
print("File selection: %s" % widget.get_filename())
self.on_dialog_close(widget)
def on_filechooser_dialog_file_activated(self,widget):
self.on_filechooser_dialog_response(widget,-5)
def on_filechooser_dialog_update_preview(self,widget):
if widget.get_filename() != None and os.path.isfile(widget.get_filename()):
pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_scale(widget.get_filename(),200,200,True)
app.obj("preview").set_from_pixbuf(pixbuf)
def on_file_button_clicked(self,widget):
app.obj("filechooser_dialog").show_all()
def on_source_button_clicked(self,widget):
dialog = Gtk.FileChooserDialog("Select source directory!",
@@ -152,15 +243,15 @@ class Handler:
response = dialog.run()
if response == Gtk.ResponseType.OK:
filecnt = sourcedir_filecnt(dialog.get_filename())
if filecnt >0:
print(filecnt)
self.filecnt = sourcedir_filecnt(dialog.get_filename())
if self.filecnt >0:
print(self.filecnt)
app.obj("source_entry").set_text(dialog.get_filename())
app.obj("destination_label").set_sensitive(True)
app.obj("destination_entry").set_sensitive(True)
app.obj("destination_button").set_sensitive(True)
app.obj("numfiles_label").set_text(str(filecnt))
app.obj("progress_label").set_text("{} of {}".format(fileindex, filecnt))
app.obj("numfiles_label").set_text(str(self.filecnt))
app.obj("progress_label").set_text("{} of {}".format(fileindex, self.filecnt))
foldercnt = sourcedir_foldercnt(dialog.get_filename())
if foldercnt >0:
app.obj("numfolders_label").set_text(str(foldercnt))
@@ -192,51 +283,31 @@ class Handler:
dialog.destroy()
def on_translate_button_clicked(self, widget):
def on_translate_button_clicked(self, widget, data=None):
app.obj("sourceframe").set_sensitive(False)
app.obj("translate_button").set_sensitive(False)
process_files()
def update_pbar(self, widget):
self.val += 1/filecnt
self.pbar.set_fraction(self.val)
while Gtk.events_pending():
Gtk.main_iteration()
class ExampleApp:
global app
global destlabel
def __init__(self):
self.app = Gtk.Application.new("org.h2inc", Gio.ApplicationFlags(0))
self.app.connect("activate", self.on_app_activate)
self.app.connect("shutdown", self.on_app_shutdown)
app = self.app
if self.process!=None:
return
def on_app_activate(self, app):
builder = Gtk.Builder()
builder.add_from_file("h2inc.glade")
builder.connect_signals(Handler())
print("Creating shared Queue")
queue = Queue()
self.obj = builder.get_object
self.obj("window").set_application(app)
self.obj("window").set_wmclass("h2inc_gtk","h2inc_gtk")
self.obj("window").show_all()
print("Creating Worker")
worker = Worker(queue, self.filecnt, filelist)
button = Gtk.Button.new_from_stock(Gtk.STOCK_CANCEL)
button.set_property("can-default",True)
self.obj("filechooser_dialog").add_action_widget(button, Gtk.ResponseType.CANCEL)
button = Gtk.Button.new_from_stock(Gtk.STOCK_APPLY)
button.set_property("can-default",True)
self.obj("filechooser_dialog").add_action_widget(button, Gtk.ResponseType.OK)
print("Creating Listener")
listener = Listener(queue)
listener.connect("updated",self.callbackDisplay)
listener.connect("finished",self.callbackFinished)
def on_app_shutdown(self, app):
self.app.quit()
print("Starting Worker")
self.process = Process(target=worker.go, args=())
self.process.start()
def run(self, argv):
self.app.run(argv)
print("Starting Listener")
thread = threading.Thread(target=listener.go, args=())
thread.start()
app = ExampleApp()
app.run(sys.argv)