This commit is contained in:
2018-03-27 14:40:34 +02:00
52 changed files with 5912 additions and 96 deletions

12
.idea/h2inc.iml generated Normal file
View File

@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="PYTHON_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
<component name="TestRunnerService">
<option name="projectConfiguration" value="Twisted Trial" />
<option name="PROJECT_TEST_RUNNER" value="Twisted Trial" />
</component>
</module>

4
.idea/misc.xml generated Normal file
View File

@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" project-jdk-name="Python 2.7" project-jdk-type="Python SDK" />
</project>

8
.idea/modules.xml generated Normal file
View File

@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/h2inc.iml" filepath="$PROJECT_DIR$/.idea/h2inc.iml" />
</modules>
</component>
</project>

6
.idea/vcs.xml generated Normal file
View File

@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>

View File

@@ -0,0 +1,145 @@
<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.20.0 -->
<interface>
<requires lib="gtk+" version="3.18"/>
<object class="GtkFileFilter" id="filefilter">
<mime-types>
<mime-type>image/*</mime-type>
</mime-types>
</object>
<object class="GtkFileFilter" id="jpg_filter">
<mime-types>
<mime-type>image/jpeg</mime-type>
</mime-types>
</object>
<object class="GtkFileFilter" id="png_filter">
<mime-types>
<mime-type>image/png</mime-type>
</mime-types>
</object>
<object class="GtkImage" id="preview">
<property name="width_request">200</property>
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="margin_right">5</property>
</object>
<object class="GtkApplicationWindow" id="window">
<property name="width_request">300</property>
<property name="height_request">200</property>
<property name="can_focus">False</property>
<signal name="destroy" handler="on_window_destroy" swapped="no"/>
<child>
<object class="GtkBox">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="orientation">vertical</property>
<property name="homogeneous">True</property>
<child>
<object class="GtkButton" id="file_button">
<property name="label" translatable="yes">Choose an image file...</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<signal name="clicked" handler="on_file_button_clicked" swapped="no"/>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkButton" id="dir_button">
<property name="label" translatable="yes">Choose folder...</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<signal name="clicked" handler="on_dir_button_clicked" swapped="no"/>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
</object>
</child>
<child type="titlebar">
<placeholder/>
</child>
</object>
<object class="GtkFileChooserDialog" id="filechooser_dialog">
<property name="width_request">800</property>
<property name="height_request">500</property>
<property name="can_focus">False</property>
<property name="type_hint">dialog</property>
<property name="transient_for">window</property>
<property name="attached_to">window</property>
<property name="preview_widget">preview</property>
<property name="use_preview_label">False</property>
<signal name="delete-event" handler="on_dialog_close" swapped="no"/>
<signal name="file-activated" handler="on_filechooser_dialog_file_activated" swapped="no"/>
<signal name="response" handler="on_filechooser_dialog_response" swapped="no"/>
<signal name="update-preview" handler="on_filechooser_dialog_update_preview" swapped="no"/>
<child internal-child="vbox">
<object class="GtkBox" id="fcbox">
<property name="can_focus">False</property>
<property name="orientation">vertical</property>
<child internal-child="action_area">
<object class="GtkButtonBox">
<property name="can_focus">False</property>
<child>
<object class="GtkButton" id="button2">
<property name="label">gtk-cancel</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<property name="use_stock">True</property>
</object>
<packing>
<property name="expand">True</property>
<property name="fill">True</property>
<property name="position">2</property>
</packing>
</child>
<child>
<object class="GtkButton" id="button1">
<property name="label">gtk-apply</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<property name="use_stock">True</property>
</object>
<packing>
<property name="expand">True</property>
<property name="fill">True</property>
<property name="position">3</property>
</packing>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">False</property>
<property name="position">0</property>
</packing>
</child>
</object>
</child>
<action-widgets>
<action-widget response="-6">button2</action-widget>
<action-widget response="-5">button1</action-widget>
</action-widgets>
<child type="titlebar">
<object class="GtkHeaderBar">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="title">Choose image...</property>
<property name="show_close_button">True</property>
</object>
</child>
<action-widgets>
<action-widget response="-6">button2</action-widget>
<action-widget response="-5">button1</action-widget>
</action-widgets>
</object>
</interface>

View File

@@ -0,0 +1,95 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-
import os
import sys
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, Gio, GdkPixbuf
class Handler:
def on_window_destroy(self,window):
window.close()
def on_dialog_close(self,widget,*event):
widget.hide_on_delete()
return True
def on_filechooser_dialog_response(self,widget,response):
if response == -6:
print("Cancel")
elif response == -5:
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_dir_button_clicked(self,widget):
dialog = Gtk.FileChooserDialog("Choose a folder",
app.obj("window"),
Gtk.FileChooserAction.SELECT_FOLDER,
(Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
Gtk.STOCK_APPLY, Gtk.ResponseType.OK))
dialog.set_default_size(600, 300)
response = dialog.run()
if response == Gtk.ResponseType.OK:
print("Folder selection: %s" % dialog.get_filename())
elif response == Gtk.ResponseType.CANCEL:
print("Cancel")
dialog.destroy()
class ExampleApp:
def __init__(self):
self.app = Gtk.Application.new("org.application.test", Gio.ApplicationFlags(0))
self.app.connect("activate", self.on_app_activate)
self.app.connect("shutdown", self.on_app_shutdown)
def on_app_activate(self, app):
builder = Gtk.Builder()
builder.add_from_file("16_filechooser.glade")
builder.connect_signals(Handler())
self.obj = builder.get_object
self.obj("window").set_application(app)
self.obj("window").set_wmclass("Filechooser example","Filechooser example")
self.obj("window").show_all()
#add filters to filechooser dialog
self.obj("filefilter").set_name("Image files")
self.obj("filechooser_dialog").add_filter(self.obj("filefilter"))
self.obj("png_filter").set_name("PNG files")
self.obj("filechooser_dialog").add_filter(self.obj("png_filter"))
self.obj("jpg_filter").set_name("JPG files")
self.obj("filechooser_dialog").add_filter(self.obj("jpg_filter"))
#add buttons to headerbar of Glade generated dialog
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)
app = ExampleApp()
app.run(sys.argv)

View File

@@ -0,0 +1,31 @@
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
class Handler:
def onDeleteWindow(self, *args):
Gtk.main_quit(*args)
def on_button1_clicked(self, button):
dialog.set_title('Select source folder')
dialog.set_default_size(800, 400)
response = dialog.run()
if response == Gtk.ResponseType.OK:
print("Folder selected: " + dialog.get_filename())
elif response == Gtk.ResponseType.CANCEL:
print("Cancel clicked")
dialog.destroy()
builder = Gtk.Builder()
builder.add_from_file("test.glade")
builder.connect_signals(Handler())
dialog = builder.get_object("filechooserdialog1")
window = builder.get_object("window1")
window.connect('delete-event', Gtk.main_quit)
window.show_all()
Gtk.main()

48
Gui test/gtk_test.py Normal file
View File

@@ -0,0 +1,48 @@
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
import time
class MyWindow(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self, title = 'Hello World')
self.val = 0
self.box = Gtk.Box(spacing=6)
self.add(self.box)
self.button1 = Gtk.Button(label = 'Hello')
self.button1.connect('clicked', self.on_button1_clicked)
self.box.pack_start(self.button1, True, True, 0)
self.button2 = Gtk.Button(label = 'Goodbye')
self.button2.connect('clicked', self.on_button2_clicked)
self.box.pack_start(self.button2, True, True, 0)
self.pbar = Gtk.ProgressBar()
self.pbar.set_fraction(0.0)
self.box.pack_start(self.pbar, True, True, 0)
def on_button1_clicked(self, widget):
print('Hello')
while self.val < 1:
self.update_pbar(self.pbar)
print('Goodbye')
Gtk.main_quit()
def on_button2_clicked(self, widget):
print('Goodbye')
def update_pbar(self, widget):
self.val += 0.1
self.pbar.set_fraction(self.val)
time.sleep(0.5)
while Gtk.events_pending():
Gtk.main_iteration()
win = MyWindow()
win.connect('delete-event', Gtk.main_quit)
win.show_all()
Gtk.main()

117
Gui test/gtk_test_1.py Normal file
View File

@@ -0,0 +1,117 @@
#!/usr/bin/env python3.5
# You are free to use and/or change this code for
# your own needs.
# Original code (c)2018 Jan Lerking
# Program to convert C-header (*.h) files to nasm include files (*.inc),
# for direct usage in assembly programming using nasm/yasm.
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
class h2incGUI(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self)
self.set_title('Translate C-header files to Nasm include files!')
self.set_default_size(200, 200)
self.set_border_width(10)
self.connect("destroy", Gtk.main_quit)
self.sourcedir = 'Select source directory!'
self.destdir = 'Select destination directory!'
self.addinc = False
self.totalvar = 0
self.filecnt = 0
self.infofolder = 'Number of folders: 0'
self.infofile = 'Number of headers: 0'
self.currentfile = 'Current file: '
self.totprogress = 'Total progress: '
self.val = 0
self.maingrid = Gtk.Grid()
self.add(self.maingrid)
self.frame = Gtk.Frame(label='Select folders')
self.maingrid.add(self.frame)
self.framegrid = Gtk.Grid()
self.framegrid.set_border_width(5)
self.frame.add(self.framegrid)
self.sourcebox = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL, spacing=6)
self.framegrid.attach(self.sourcebox, 0, 0, 5, 1)
self.sourcelabel = Gtk.Label(label='Source: ')
self.sourcelabel.set_justify(Gtk.Justification.RIGHT)
self.sourcebox.add(self.sourcelabel)
self.sourceentry = Gtk.Entry()
self.sourceentry.set_text(self.sourcedir)
self.sourcebox.add(self.sourceentry)
self.sourcedir_button = Gtk.Button(label="Source directory...")
#self.sourcedir_button.connect('clicked', self.select_sourcedir(self.sourcedir))
self.sourcebox.add(self.sourcedir_button)
self.destbox = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL, spacing=6)
self.framegrid.attach(self.destbox, 0, 1, 5, 1)
self.destlabel = Gtk.Label(label='Destination: ')
self.destbox.add(self.destlabel)
self.destentry = Gtk.Entry()
self.destentry.set_text(self.destdir)
self.destbox.add(self.destentry)
self.destdir_button = Gtk.Button(label="Destination directory...")
#self.destdir_button.connect('clicked', self.select_destdir(self.destdir))
self.destbox.add(self.destdir_button)
self.incchkbox = Gtk.CheckButton('Create "include" folder if it does not exist.')
self.incchkbox.set_active(False)
#self.incchkbox.connect("toggled", self.on_button_toggled, self.addinc)
#self.framebox.add(self.incchkbox)
self.transframe = Gtk.Frame(label='Translation')
#self.mainbox.add(self.transframe)
self.framebox_1 = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=6)
#self.transframe.add(self.framebox_1)
self.infoframe = Gtk.Frame(label='Source information')
#self.framebox_1.add(self.infoframe)
self.infobox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=6)
#self.infoframe.add(self.infobox)
self.infofolders = Gtk.Label(label=self.infofolder)
#self.infobox.add(self.infofolders)
self.infofiles = Gtk.Label(label=self.infofile)
#self.infobox.add(self.infofiles)
self.translate_button = Gtk.Button(label="Translate!")
#self.translate_button.connect('clicked', self.translate(self.destdir, self.sourcedir, self.addinc))
#self.framebox_1.add(self.translate_button)
self.progressframe = Gtk.Frame(label='Progress')
#self.framebox_1.add(self.progressframe)
self.progressbox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=6)
#self.progressframe.add(self.progressbox)
self.cfilelabel = Gtk.Label(label=self.currentfile)
#self.progressbox.add(self.cfilelabel)
self.totallabel = Gtk.Label(label=self.totprogress)
#self.progressbox.add(self.totallabel)
self.totalprogress = Gtk.ProgressBar()
self.totalprogress.set_fraction(0.0)
#self.progressbox.add(self.totalprogress)
win = h2incGUI()
win.show_all()
Gtk.main()

82
Gui test/gtk_test_2.py Normal file
View File

@@ -0,0 +1,82 @@
#!/usr/bin/env python3.5
# You are free to use and/or change this code for
# your own needs.
# Original code (c)2018 Jan Lerking
# Program to convert C-header (*.h) files to nasm include files (*.inc),
# for direct usage in assembly programming using nasm/yasm.
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
class FileChooserWindow(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self, title="FileChooser Example")
box = Gtk.Box(spacing=6)
self.add(box)
button1 = Gtk.Button("Choose File")
button1.connect("clicked", self.on_file_clicked)
box.add(button1)
button2 = Gtk.Button("Choose Folder")
button2.connect("clicked", self.on_folder_clicked)
box.add(button2)
def on_file_clicked(self, widget):
dialog = Gtk.FileChooserDialog("Please choose a file", self,
Gtk.FileChooserAction.OPEN,
(Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
Gtk.STOCK_OPEN, Gtk.ResponseType.OK))
self.add_filters(dialog)
response = dialog.run()
if response == Gtk.ResponseType.OK:
print("Open clicked")
print("File selected: " + dialog.get_filename())
elif response == Gtk.ResponseType.CANCEL:
print("Cancel clicked")
dialog.destroy()
def add_filters(self, dialog):
filter_text = Gtk.FileFilter()
filter_text.set_name("Text files")
filter_text.add_mime_type("text/plain")
dialog.add_filter(filter_text)
filter_py = Gtk.FileFilter()
filter_py.set_name("Python files")
filter_py.add_mime_type("text/x-python")
dialog.add_filter(filter_py)
filter_any = Gtk.FileFilter()
filter_any.set_name("Any files")
filter_any.add_pattern("*")
dialog.add_filter(filter_any)
def on_folder_clicked(self, widget):
dialog = Gtk.FileChooserDialog("Please choose a folder", self,
Gtk.FileChooserAction.SELECT_FOLDER,
(Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
"Select", Gtk.ResponseType.OK))
dialog.set_default_size(800, 400)
response = dialog.run()
if response == Gtk.ResponseType.OK:
print("Select clicked")
print("Folder selected: " + dialog.get_filename())
elif response == Gtk.ResponseType.CANCEL:
print("Cancel clicked")
dialog.destroy()
win = FileChooserWindow()
win.connect("delete-event", Gtk.main_quit)
win.show_all()
Gtk.main()

80
Gui test/gtk_test_3.py Normal file
View File

@@ -0,0 +1,80 @@
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
import time
from multiprocessing import Process, Value, Lock, Pool
class Counter(object):
def __init__(self, initval=0.0):
self.val = Value('d', initval)
self.lock = Lock()
def increment(self):
with self.lock:
self.val.value += 1/2542
print(self.val.value)
def value(self):
with self.lock:
return self.val.value
class MyWindow(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self, title = 'Hello World')
#self.val = 0
self.box = Gtk.Box(spacing=6)
self.add(self.box)
self.button1 = Gtk.Button(label = 'Hello')
self.button1.connect('clicked', self.on_button1_clicked)
self.box.pack_start(self.button1, True, True, 0)
self.button2 = Gtk.Button(label = 'Goodbye')
self.button2.connect('clicked', self.on_button2_clicked)
self.box.pack_start(self.button2, True, True, 0)
self.pbar = Gtk.ProgressBar()
self.pbar.set_fraction(0.0)
self.box.pack_start(self.pbar, True, True, 0)
def on_button1_clicked(self, widget):
print('Hello')
#procs = [Process(target=self.func, args=(counter,)) for i in range(10)]
#for p in procs: p.start()
#for p in procs: p.join()
p = Pool(2)
p.map(self.func, range(2542))
print (counter.value())
#while counter.val.value <10:
#self.update_pbar(self.pbar)
print('Goodbye')
Gtk.main_quit()
def on_button2_clicked(self, widget):
print('Goodbye')
Gtk.main_quit()
def update_pbar(self, widget, value):
#self.val += 0.1
self.pbar.set_fraction(value)
#time.sleep(0.5)
while Gtk.events_pending():
Gtk.main_iteration()
def func(self, data):
#for i in range(2542):
time.sleep(0.01)
counter.increment()
#while counter.value() <1:
#self.update_pbar(self.pbar, counter.value())
counter = Counter(0.0)
win = MyWindow()
win.connect('delete-event', Gtk.main_quit)
win.show_all()
Gtk.main()

118
Gui test/gtk_test_4.py Normal file
View File

@@ -0,0 +1,118 @@
import gobject
import pygtk
pygtk.require('2.0')
import gtk
import multiprocessing
import threading
import time
gtk.gdk.threads_init()
class Listener(gobject.GObject):
__gsignals__ = {
'updated' : (gobject.SIGNAL_RUN_LAST,
gobject.TYPE_NONE,
(gobject.TYPE_FLOAT, gobject.TYPE_STRING)),
'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])
gobject.type_register(Listener)
class Worker():
def __init__(self, queue):
self.queue = queue
def go(self):
print("The worker has started doing some work (counting from 0 to 9)")
for i in range(10):
proportion = (float(i+1))/10
self.queue.put((proportion, "working..."))
time.sleep(0.5)
self.queue.put((1.0, "finished"))
print("The worker has finished.")
class Interface:
def __init__(self):
self.process = None
self.progress = gtk.ProgressBar()
button = gtk.Button("Go!")
button.connect("clicked", self.go)
vbox = gtk.VBox(spacing=5)
vbox.pack_start(self.progress)
vbox.pack_start(button)
vbox.show_all()
self.frame = vbox
def main(self):
window = gtk.Window(gtk.WINDOW_TOPLEVEL)
window.set_border_width(10)
window.add(self.frame)
window.show()
window.connect("destroy", self.destroy)
gtk.main()
def destroy(self, widget, data=None):
gtk.main_quit()
def callbackDisplay(self, obj, fraction, text, data=None):
self.progress.set_fraction(fraction)
self.progress.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.progress.set_fraction(1.0)
self.progress.set_text("done")
def go(self, widget, data=None):
if self.process!=None:
return
print("Creating shared Queue")
queue = multiprocessing.Queue()
print("Creating Worker")
worker = Worker(queue)
print("Creating Listener")
listener = Listener(queue)
listener.connect("updated",self.callbackDisplay)
listener.connect("finished",self.callbackFinished)
print("Starting Worker")
self.process = multiprocessing.Process(target=worker.go, args=())
self.process.start()
print("Starting Listener")
thread = threading.Thread(target=listener.go, args=())
thread.start()
if __name__ == '__main__':
gui = Interface()
gui.main()

166
Gui test/gtk_test_5.py Normal file
View File

@@ -0,0 +1,166 @@
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, 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)),
'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])
gobject.type_register(Listener)
class Worker():
def __init__(self, queue):
self.queue = queue
def go(self):
print("The worker has started doing some work (counting from 0 to 9)")
for i in range(2542):
proportion = (float(i+1))/2542
self.queue.put((proportion, "working..."))
time.sleep(0.01)
self.queue.put((1.0, "finished"))
print("The worker has finished.")
class Counter(object):
def __init__(self, initval=0.0):
self.val = Value('d', initval)
self.lock = Lock()
def increment(self):
with self.lock:
self.val.value += 1/2542
print(self.val.value)
def value(self):
with self.lock:
return self.val.value
class MyWindow(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self, title = 'Hello World')
#self.val = 0
self.process = None
self.box = Gtk.Box(spacing=6)
self.add(self.box)
self.button1 = Gtk.Button(label = 'Hello')
self.button1.connect('clicked', self.go)
self.box.pack_start(self.button1, True, True, 0)
self.button2 = Gtk.Button(label = 'Goodbye')
self.button2.connect('clicked', self.on_button2_clicked)
self.box.pack_start(self.button2, True, True, 0)
self.pbar = Gtk.ProgressBar()
self.pbar.set_fraction(0.0)
self.box.pack_start(self.pbar, True, True, 0)
def callbackDisplay(self, obj, fraction, text, data=None):
self.pbar.set_fraction(fraction)
self.pbar.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.pbar.set_fraction(1.0)
self.pbar.set_text("done")
print('Goodbye')
Gtk.main_quit()
def go(self, widget, data=None):
if self.process!=None:
return
print("Creating shared Queue")
queue = Queue()
print("Creating Worker")
worker = Worker(queue)
print("Creating Listener")
listener = Listener(queue)
listener.connect("updated",self.callbackDisplay)
listener.connect("finished",self.callbackFinished)
print("Starting Worker")
self.process = Process(target=worker.go, args=())
self.process.start()
print("Starting Listener")
thread = threading.Thread(target=listener.go, args=())
thread.start()
#def on_button1_clicked(self, widget):
#print('Hello')
#procs = [Process(target=self.func, args=(counter,)) for i in range(10)]
#for p in procs: p.start()
#for p in procs: p.join()
#p = Pool(2)
#p.map(self.func, range(2542))
#print (counter.value())
#while counter.val.value <10:
#self.update_pbar(self.pbar)
#print('Goodbye')
#Gtk.main_quit()
def on_button2_clicked(self, widget):
print('Goodbye')
Gtk.main_quit()
#def update_pbar(self, widget, value):
#self.val += 0.1
#self.pbar.set_fraction(value)
#time.sleep(0.5)
#while Gtk.events_pending():
#Gtk.main_iteration()
#def func(self, data):
#for i in range(2542):
#time.sleep(0.01)
#counter.increment()
#while counter.value() <1:
#self.update_pbar(self.pbar, counter.value())
#counter = Counter(0.0)
win = MyWindow()
win.connect('delete-event', Gtk.main_quit)
win.show_all()
Gtk.main()

481
Gui test/h2inc.glade Normal file
View File

@@ -0,0 +1,481 @@
<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.18.3 -->
<interface>
<requires lib="gtk+" version="3.12"/>
<object class="GtkFileChooserDialog" id="filechooserdialog1">
<property name="can_focus">False</property>
<property name="type_hint">dialog</property>
<child internal-child="vbox">
<object class="GtkBox" id="fcbox">
<property name="can_focus">False</property>
<property name="orientation">vertical</property>
<property name="spacing">2</property>
<child internal-child="action_area">
<object class="GtkButtonBox" id="filechooserdialog-action_area1">
<property name="can_focus">False</property>
<child>
<placeholder/>
</child>
<child>
<placeholder/>
</child>
<child>
<placeholder/>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">False</property>
<property name="position">0</property>
</packing>
</child>
<child>
<placeholder/>
</child>
</object>
</child>
</object>
<object class="GtkApplicationWindow" id="window">
<property name="can_focus">False</property>
<child>
<object class="GtkBox" id="box1">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="orientation">vertical</property>
<child>
<object class="GtkFrame" id="sourceframe">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="margin_left">5</property>
<property name="margin_right">5</property>
<property name="margin_top">5</property>
<property name="margin_bottom">5</property>
<property name="label_xalign">0</property>
<property name="shadow_type">etched-out</property>
<child>
<object class="GtkAlignment" id="alignment1">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="left_padding">12</property>
<child>
<object class="GtkGrid" id="grid2">
<property name="visible">True</property>
<property name="can_focus">False</property>
<child>
<object class="GtkLabel" id="source_label">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="halign">end</property>
<property name="margin_left">5</property>
<property name="margin_right">5</property>
<property name="margin_top">5</property>
<property name="margin_bottom">5</property>
<property name="hexpand">True</property>
<property name="label" translatable="yes">Source:</property>
<property name="justify">right</property>
</object>
<packing>
<property name="left_attach">0</property>
<property name="top_attach">0</property>
<property name="width">2</property>
</packing>
</child>
<child>
<object class="GtkEntry" id="source_entry">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="margin_top">5</property>
<property name="margin_bottom">5</property>
<property name="text" translatable="yes">/usr/include</property>
</object>
<packing>
<property name="left_attach">2</property>
<property name="top_attach">0</property>
<property name="width">2</property>
</packing>
</child>
<child>
<object class="GtkButton" id="source_button">
<property name="label" translatable="yes">Select source directory...</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<property name="halign">start</property>
<property name="margin_left">5</property>
<property name="margin_right">5</property>
<property name="margin_top">5</property>
<property name="margin_bottom">5</property>
<signal name="clicked" handler="on_source_button_clicked" after="yes" swapped="no"/>
</object>
<packing>
<property name="left_attach">4</property>
<property name="top_attach">0</property>
<property name="width">2</property>
</packing>
</child>
<child>
<object class="GtkLabel" id="destination_label">
<property name="visible">True</property>
<property name="sensitive">False</property>
<property name="can_focus">False</property>
<property name="halign">end</property>
<property name="margin_left">5</property>
<property name="margin_right">5</property>
<property name="margin_top">5</property>
<property name="margin_bottom">5</property>
<property name="hexpand">True</property>
<property name="label" translatable="yes">Destination:</property>
<property name="justify">right</property>
<property name="ellipsize">start</property>
<property name="angle">2.2351741811588166e-10</property>
</object>
<packing>
<property name="left_attach">0</property>
<property name="top_attach">1</property>
<property name="width">2</property>
</packing>
</child>
<child>
<object class="GtkEntry" id="destination_entry">
<property name="visible">True</property>
<property name="sensitive">False</property>
<property name="can_focus">True</property>
<property name="margin_top">5</property>
<property name="margin_bottom">5</property>
<property name="text" translatable="yes">~/include</property>
</object>
<packing>
<property name="left_attach">2</property>
<property name="top_attach">1</property>
<property name="width">2</property>
</packing>
</child>
<child>
<object class="GtkButton" id="destination_button">
<property name="label" translatable="yes">Select destination directory...</property>
<property name="visible">True</property>
<property name="sensitive">False</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<property name="halign">start</property>
<property name="margin_left">5</property>
<property name="margin_right">5</property>
<property name="margin_top">5</property>
<property name="margin_bottom">5</property>
<signal name="clicked" handler="on_destination_button_clicked" after="yes" swapped="no"/>
</object>
<packing>
<property name="left_attach">4</property>
<property name="top_attach">1</property>
<property name="width">2</property>
</packing>
</child>
<child>
<object class="GtkCheckButton" id="include_checkbutton">
<property name="label" translatable="yes">Create "include" folder if it does not exist.</property>
<property name="visible">True</property>
<property name="sensitive">False</property>
<property name="can_focus">True</property>
<property name="receives_default">False</property>
<property name="margin_left">5</property>
<property name="margin_right">5</property>
<property name="margin_top">5</property>
<property name="margin_bottom">5</property>
<property name="xalign">0</property>
<property name="draw_indicator">True</property>
<signal name="toggled" handler="on_include_checkbutton_toggled" swapped="no"/>
</object>
<packing>
<property name="left_attach">0</property>
<property name="top_attach">3</property>
<property name="width">6</property>
</packing>
</child>
<child>
<object class="GtkCheckButton" id="default_dir_checkbutton">
<property name="label" translatable="yes">Use default directories!</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">False</property>
<property name="margin_left">5</property>
<property name="margin_right">5</property>
<property name="margin_top">5</property>
<property name="margin_bottom">5</property>
<property name="xalign">0</property>
<property name="draw_indicator">True</property>
<signal name="toggled" handler="on_default_dir_checkbutton_toggled" swapped="no"/>
</object>
<packing>
<property name="left_attach">0</property>
<property name="top_attach">2</property>
<property name="width">6</property>
</packing>
</child>
</object>
</child>
</object>
</child>
<child type="label">
<object class="GtkLabel" id="label1">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="margin_left">5</property>
<property name="margin_right">5</property>
<property name="label" translatable="yes">Select folders</property>
</object>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkFrame" id="translation_frame">
<property name="visible">True</property>
<property name="sensitive">False</property>
<property name="can_focus">False</property>
<property name="margin_left">5</property>
<property name="margin_right">5</property>
<property name="margin_top">5</property>
<property name="margin_bottom">5</property>
<property name="label_xalign">0</property>
<property name="shadow_type">etched-out</property>
<child>
<object class="GtkAlignment" id="alignment2">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="left_padding">12</property>
<child>
<object class="GtkGrid" id="grid3">
<property name="visible">True</property>
<property name="can_focus">False</property>
<child>
<object class="GtkFrame" id="frame2">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="margin_right">5</property>
<property name="margin_top">5</property>
<property name="margin_bottom">5</property>
<property name="hexpand">True</property>
<property name="label_xalign">0</property>
<property name="shadow_type">etched-out</property>
<child>
<object class="GtkAlignment" id="alignment3">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="left_padding">12</property>
<child>
<object class="GtkBox" id="box2">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="orientation">vertical</property>
<child>
<object class="GtkGrid" id="grid1">
<property name="visible">True</property>
<property name="can_focus">False</property>
<child>
<object class="GtkLabel" id="label3">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="margin_top">5</property>
<property name="margin_bottom">5</property>
<property name="label" translatable="yes">Number of folders: </property>
</object>
<packing>
<property name="left_attach">0</property>
<property name="top_attach">0</property>
</packing>
</child>
<child>
<object class="GtkLabel" id="numfolders_label">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">0</property>
</object>
<packing>
<property name="left_attach">1</property>
<property name="top_attach">0</property>
</packing>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkGrid" id="grid5">
<property name="visible">True</property>
<property name="can_focus">False</property>
<child>
<object class="GtkLabel" id="label7">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="margin_top">5</property>
<property name="margin_bottom">5</property>
<property name="label" translatable="yes">Number of files: </property>
</object>
<packing>
<property name="left_attach">0</property>
<property name="top_attach">0</property>
</packing>
</child>
<child>
<object class="GtkLabel" id="numfiles_label">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">0</property>
</object>
<packing>
<property name="left_attach">1</property>
<property name="top_attach">0</property>
</packing>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
</object>
</child>
</object>
</child>
<child type="label">
<object class="GtkLabel" id="label5">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="margin_left">5</property>
<property name="margin_right">5</property>
<property name="label" translatable="yes">Source information</property>
</object>
</child>
</object>
<packing>
<property name="left_attach">0</property>
<property name="top_attach">0</property>
</packing>
</child>
<child>
<object class="GtkButton" id="translate_button">
<property name="label" translatable="yes">Translate!</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<property name="halign">start</property>
<property name="margin_top">5</property>
<property name="margin_bottom">5</property>
<signal name="clicked" handler="on_translate_button_clicked" after="yes" swapped="no"/>
</object>
<packing>
<property name="left_attach">0</property>
<property name="top_attach">1</property>
</packing>
</child>
<child>
<object class="GtkFrame" id="frame3">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="margin_right">5</property>
<property name="margin_top">5</property>
<property name="margin_bottom">5</property>
<property name="label_xalign">0</property>
<property name="shadow_type">etched-out</property>
<child>
<object class="GtkAlignment" id="alignment4">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="left_padding">12</property>
<child>
<object class="GtkGrid" id="grid4">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="hexpand">True</property>
<child>
<object class="GtkLabel" id="label2">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="halign">start</property>
<property name="hexpand">False</property>
<property name="label" translatable="yes">Total progress: </property>
</object>
<packing>
<property name="left_attach">0</property>
<property name="top_attach">0</property>
</packing>
</child>
<child>
<object class="GtkLabel" id="progress_label">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="halign">start</property>
<property name="hexpand">True</property>
<property name="label" translatable="yes">0 of 0</property>
</object>
<packing>
<property name="left_attach">1</property>
<property name="top_attach">0</property>
</packing>
</child>
<child>
<object class="GtkProgressBar" id="progressbar">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="margin_right">5</property>
<property name="margin_top">5</property>
<property name="margin_bottom">5</property>
<property name="hexpand">True</property>
</object>
<packing>
<property name="left_attach">0</property>
<property name="top_attach">1</property>
<property name="width">2</property>
</packing>
</child>
</object>
</child>
</object>
</child>
<child type="label">
<object class="GtkLabel" id="label20">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="margin_left">5</property>
<property name="margin_right">5</property>
<property name="label" translatable="yes">Progress</property>
</object>
</child>
</object>
<packing>
<property name="left_attach">0</property>
<property name="top_attach">2</property>
</packing>
</child>
</object>
</child>
</object>
</child>
<child type="label">
<object class="GtkLabel" id="label4">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="margin_left">5</property>
<property name="margin_right">5</property>
<property name="label" translatable="yes">Translation</property>
</object>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
</object>
</child>
</object>
</interface>

481
Gui test/h2inc.glade~ Normal file
View File

@@ -0,0 +1,481 @@
<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.18.3 -->
<interface>
<requires lib="gtk+" version="3.12"/>
<object class="GtkFileChooserDialog" id="filechooserdialog1">
<property name="can_focus">False</property>
<property name="type_hint">dialog</property>
<child internal-child="vbox">
<object class="GtkBox" id="fcbox">
<property name="can_focus">False</property>
<property name="orientation">vertical</property>
<property name="spacing">2</property>
<child internal-child="action_area">
<object class="GtkButtonBox" id="filechooserdialog-action_area1">
<property name="can_focus">False</property>
<child>
<placeholder/>
</child>
<child>
<placeholder/>
</child>
<child>
<placeholder/>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">False</property>
<property name="position">0</property>
</packing>
</child>
<child>
<placeholder/>
</child>
</object>
</child>
</object>
<object class="GtkApplicationWindow" id="window">
<property name="can_focus">False</property>
<child>
<object class="GtkBox" id="box1">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="orientation">vertical</property>
<child>
<object class="GtkFrame" id="sourceframe">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="margin_left">5</property>
<property name="margin_right">5</property>
<property name="margin_top">5</property>
<property name="margin_bottom">5</property>
<property name="label_xalign">0</property>
<property name="shadow_type">etched-out</property>
<child>
<object class="GtkAlignment" id="alignment1">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="left_padding">12</property>
<child>
<object class="GtkGrid" id="grid2">
<property name="visible">True</property>
<property name="can_focus">False</property>
<child>
<object class="GtkLabel" id="source_label">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="halign">end</property>
<property name="margin_left">5</property>
<property name="margin_right">5</property>
<property name="margin_top">5</property>
<property name="margin_bottom">5</property>
<property name="hexpand">True</property>
<property name="label" translatable="yes">Source:</property>
<property name="justify">right</property>
</object>
<packing>
<property name="left_attach">0</property>
<property name="top_attach">0</property>
<property name="width">2</property>
</packing>
</child>
<child>
<object class="GtkEntry" id="source_entry">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="margin_top">5</property>
<property name="margin_bottom">5</property>
<property name="text" translatable="yes">/usr/include</property>
</object>
<packing>
<property name="left_attach">2</property>
<property name="top_attach">0</property>
<property name="width">2</property>
</packing>
</child>
<child>
<object class="GtkButton" id="source_button">
<property name="label" translatable="yes">Select source directory...</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<property name="halign">start</property>
<property name="margin_left">5</property>
<property name="margin_right">5</property>
<property name="margin_top">5</property>
<property name="margin_bottom">5</property>
<signal name="clicked" handler="on_source_button_clicked" after="yes" swapped="no"/>
</object>
<packing>
<property name="left_attach">4</property>
<property name="top_attach">0</property>
<property name="width">2</property>
</packing>
</child>
<child>
<object class="GtkLabel" id="destination_label">
<property name="visible">True</property>
<property name="sensitive">False</property>
<property name="can_focus">False</property>
<property name="halign">end</property>
<property name="margin_left">5</property>
<property name="margin_right">5</property>
<property name="margin_top">5</property>
<property name="margin_bottom">5</property>
<property name="hexpand">True</property>
<property name="label" translatable="yes">Destination:</property>
<property name="justify">right</property>
<property name="ellipsize">start</property>
<property name="angle">2.2351741811588166e-10</property>
</object>
<packing>
<property name="left_attach">0</property>
<property name="top_attach">1</property>
<property name="width">2</property>
</packing>
</child>
<child>
<object class="GtkEntry" id="destination_entry">
<property name="visible">True</property>
<property name="sensitive">False</property>
<property name="can_focus">True</property>
<property name="margin_top">5</property>
<property name="margin_bottom">5</property>
<property name="text" translatable="yes">~/include</property>
</object>
<packing>
<property name="left_attach">2</property>
<property name="top_attach">1</property>
<property name="width">2</property>
</packing>
</child>
<child>
<object class="GtkButton" id="destination_button">
<property name="label" translatable="yes">Select destination directory...</property>
<property name="visible">True</property>
<property name="sensitive">False</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<property name="halign">start</property>
<property name="margin_left">5</property>
<property name="margin_right">5</property>
<property name="margin_top">5</property>
<property name="margin_bottom">5</property>
<signal name="clicked" handler="on_destination_button_clicked" after="yes" swapped="no"/>
</object>
<packing>
<property name="left_attach">4</property>
<property name="top_attach">1</property>
<property name="width">2</property>
</packing>
</child>
<child>
<object class="GtkCheckButton" id="include_checkbutton">
<property name="label" translatable="yes">Create "include" folder if it does not exist.</property>
<property name="visible">True</property>
<property name="sensitive">False</property>
<property name="can_focus">True</property>
<property name="receives_default">False</property>
<property name="margin_left">5</property>
<property name="margin_right">5</property>
<property name="margin_top">5</property>
<property name="margin_bottom">5</property>
<property name="xalign">0</property>
<property name="draw_indicator">True</property>
<signal name="toggled" handler="on_include_checkbutton_toggled" swapped="no"/>
</object>
<packing>
<property name="left_attach">0</property>
<property name="top_attach">3</property>
<property name="width">6</property>
</packing>
</child>
<child>
<object class="GtkCheckButton" id="default_dir_checkbutton">
<property name="label" translatable="yes">Use default directories!</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">False</property>
<property name="margin_left">5</property>
<property name="margin_right">5</property>
<property name="margin_top">5</property>
<property name="margin_bottom">5</property>
<property name="xalign">0</property>
<property name="draw_indicator">True</property>
<signal name="toggled" handler="on_default_dir_checkbutton_toggled" swapped="no"/>
</object>
<packing>
<property name="left_attach">0</property>
<property name="top_attach">2</property>
<property name="width">6</property>
</packing>
</child>
</object>
</child>
</object>
</child>
<child type="label">
<object class="GtkLabel" id="label1">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="margin_left">5</property>
<property name="margin_right">5</property>
<property name="label" translatable="yes">Select folders</property>
</object>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkFrame" id="translation_frame">
<property name="visible">True</property>
<property name="sensitive">False</property>
<property name="can_focus">False</property>
<property name="margin_left">5</property>
<property name="margin_right">5</property>
<property name="margin_top">5</property>
<property name="margin_bottom">5</property>
<property name="label_xalign">0</property>
<property name="shadow_type">etched-out</property>
<child>
<object class="GtkAlignment" id="alignment2">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="left_padding">12</property>
<child>
<object class="GtkGrid" id="grid3">
<property name="visible">True</property>
<property name="can_focus">False</property>
<child>
<object class="GtkFrame" id="frame2">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="margin_right">5</property>
<property name="margin_top">5</property>
<property name="margin_bottom">5</property>
<property name="hexpand">True</property>
<property name="label_xalign">0</property>
<property name="shadow_type">etched-out</property>
<child>
<object class="GtkAlignment" id="alignment3">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="left_padding">12</property>
<child>
<object class="GtkBox" id="box2">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="orientation">vertical</property>
<child>
<object class="GtkGrid" id="grid1">
<property name="visible">True</property>
<property name="can_focus">False</property>
<child>
<object class="GtkLabel" id="label3">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="margin_top">5</property>
<property name="margin_bottom">5</property>
<property name="label" translatable="yes">Number of folders: </property>
</object>
<packing>
<property name="left_attach">0</property>
<property name="top_attach">0</property>
</packing>
</child>
<child>
<object class="GtkLabel" id="numfolders_label">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">0</property>
</object>
<packing>
<property name="left_attach">1</property>
<property name="top_attach">0</property>
</packing>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkGrid" id="grid5">
<property name="visible">True</property>
<property name="can_focus">False</property>
<child>
<object class="GtkLabel" id="label7">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="margin_top">5</property>
<property name="margin_bottom">5</property>
<property name="label" translatable="yes">Number of files: </property>
</object>
<packing>
<property name="left_attach">0</property>
<property name="top_attach">0</property>
</packing>
</child>
<child>
<object class="GtkLabel" id="numfiles_label">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">0</property>
</object>
<packing>
<property name="left_attach">1</property>
<property name="top_attach">0</property>
</packing>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
</object>
</child>
</object>
</child>
<child type="label">
<object class="GtkLabel" id="label5">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="margin_left">5</property>
<property name="margin_right">5</property>
<property name="label" translatable="yes">Source information</property>
</object>
</child>
</object>
<packing>
<property name="left_attach">0</property>
<property name="top_attach">0</property>
</packing>
</child>
<child>
<object class="GtkButton" id="translate_button">
<property name="label" translatable="yes">Translate!</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<property name="halign">start</property>
<property name="margin_top">5</property>
<property name="margin_bottom">5</property>
<signal name="clicked" handler="on_translate_button_clicked" after="yes" swapped="no"/>
</object>
<packing>
<property name="left_attach">0</property>
<property name="top_attach">1</property>
</packing>
</child>
<child>
<object class="GtkFrame" id="frame3">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="margin_right">5</property>
<property name="margin_top">5</property>
<property name="margin_bottom">5</property>
<property name="label_xalign">0</property>
<property name="shadow_type">etched-out</property>
<child>
<object class="GtkAlignment" id="alignment4">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="left_padding">12</property>
<child>
<object class="GtkGrid" id="grid4">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="hexpand">True</property>
<child>
<object class="GtkLabel" id="label2">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="halign">start</property>
<property name="hexpand">False</property>
<property name="label" translatable="yes">Total progress: </property>
</object>
<packing>
<property name="left_attach">0</property>
<property name="top_attach">0</property>
</packing>
</child>
<child>
<object class="GtkLabel" id="progress_label">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="halign">start</property>
<property name="hexpand">True</property>
<property name="label" translatable="yes">0 of 0</property>
</object>
<packing>
<property name="left_attach">1</property>
<property name="top_attach">0</property>
</packing>
</child>
<child>
<object class="GtkProgressBar" id="progressbar">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="margin_right">5</property>
<property name="margin_top">5</property>
<property name="margin_bottom">5</property>
<property name="hexpand">True</property>
</object>
<packing>
<property name="left_attach">0</property>
<property name="top_attach">1</property>
<property name="width">2</property>
</packing>
</child>
</object>
</child>
</object>
</child>
<child type="label">
<object class="GtkLabel" id="label20">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="margin_left">5</property>
<property name="margin_right">5</property>
<property name="label" translatable="yes">Progress</property>
</object>
</child>
</object>
<packing>
<property name="left_attach">0</property>
<property name="top_attach">2</property>
</packing>
</child>
</object>
</child>
</object>
</child>
<child type="label">
<object class="GtkLabel" id="label4">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="margin_left">5</property>
<property name="margin_right">5</property>
<property name="label" translatable="yes">Translation</property>
</object>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
</object>
</child>
</object>
</interface>

319
Gui test/h2inc_gtk.py Normal file
View File

@@ -0,0 +1,319 @@
#!/usr/bin/env python3.5
# You are free to use and/or change this code for
# your own needs.
# Original code (c)2018 Jan Lerking
# Program to convert C-header (*.h) files to nasm include files (*.inc),
# for direct usage in assembly programming using nasm/yasm.
import os
import sys
import io
import gi
gi.require_version('Gtk', '3.0')
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))
time.sleep(0.01)
process_file(filelist[i])
self.queue.put((1.0, "finished"))
print("The worker has finished.")
tupline = []
preproc = ()
filelist = []
folderlist = []
cnt = 0
srcdir = ''
destdir = ''
#num_cores = multiprocessing.cpu_count()
fileindex = 0
filecnt = 0
incinc = False
defdir = False
def sourcedir_filecnt(sourcedir):
### Return the number of files, ending with '.h', in sourcedir - including subdirectories ###
cnt = 0
global filelist
global srcdir
srcdir = sourcedir
for folderName, subfolders, files in os.walk(sourcedir):
for file in files:
if file.lower().endswith('.h'):
cnt += 1
filelist += [folderName + '/' + file]
print(folderName + '/' + file)
# print(filelist)
return cnt
def sourcedir_foldercnt(sourcedir):
### Return the number of folders, if it contains '*.h' files, in sourcedir - including subdirectories ###
global cnt
global folderlist
for folderName, subfolders, files in os.walk(sourcedir):
if subfolders:
for subfolder in subfolders:
sourcedir_foldercnt(subfolder)
tempf = [file for file in files if file.lower().endswith('.h')]
if tempf:
cnt = cnt + 1
# print(folderName)
folderlist += [folderName]
# print(folderlist)
# print(len(folderlist))
return cnt
def process_file(data):
outfile = ''
inputfile = data
encodings = ['utf-8', 'latin-1', 'windows-1250', 'windows-1252', 'ascii',
'big5', 'big5hkscs', 'cp037', 'cp273', 'cp424', 'cp437', 'cp500',
'cp720', 'cp737', 'cp775', 'cp850', 'cp852', 'cp855', 'cp856',
'cp857', 'cp858', 'cp860', 'cp861', 'cp862', 'cp863', 'cp864', 'cp865',
'cp866', 'cp869', 'cp874', 'cp875', 'cp932', 'cp949', 'cp950', 'cp1006',
'cp1026', 'cp1125', 'cp1140', 'cp1250', 'cp1251', 'cp1252', 'cp1253', 'cp1254',
'cp1255', 'cp1256', 'cp1257', 'cp1258', 'cp65001', 'euc-jp', 'euc-jis-2004',
'euc-jisx0213', 'euc-kr', 'gb2312', 'gbk', 'gb18030', 'hz', 'iso2022-jp',
'iso2022-jp-1', 'iso2022-jp-2', 'iso2022-jp-2004', 'iso2022-jp-3',
'iso2022-jp-ext', 'iso2022-kr', 'iso8859-2', 'iso8859-3', 'iso8859-4',
'iso8859-5', 'iso8859-6', 'iso8859-7', 'iso8859-8', 'iso8859-9', 'iso8859-10',
'iso8859-11', 'iso8859-13', 'iso8859-14', 'iso8859-15', 'iso8859-16', 'johab',
'koi8-r', 'koi8-t', 'koi8-u', 'kz1048', 'mac-cyrillic', 'mac-greek',
'mac-iceland', 'mac-latin2', 'mac-roman', 'mac-turkish', 'ptcp154',
'shift-jis', 'shift-jis-2004', 'shift-jisx0213', 'utf-32', 'utf-32-be',
'utf-32-le', 'utf-16', 'utf-16-be', 'utf-16-le', 'utf-7', 'utf-8-sig']
for e in encodings:
try:
fh = io.open(data, 'r', encoding=e)
fh.readlines()
fh.seek(0)
except UnicodeDecodeError:
print('got unicode error with %s , trying different encoding' % e)
else:
# print('opening the file with encoding: %s ' % e)
break
# print(os.path.basename(data))
for lines in fh:
outfile = outfile + lines
fh.close()
outputfile = os.path.splitext(inputfile)[0] + '.inc'
outputfile = str(outputfile).replace(srcdir, destdir)
print(outputfile)
if not os.path.exists(os.path.dirname(outputfile)):
try:
os.makedirs(os.path.dirname(outputfile))
except OSError as exc: # Guard against race condition
if exc.errno != errno.EEXIST:
raise
newfile = open(outputfile, "w")
newfile.write(outfile)
newfile.close()
class ExampleApp:
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()
self.obj("default_dir_checkbutton").set_active(False)
self.obj("include_checkbutton").set_active(True)
button = Gtk.Button.new_from_stock(Gtk.STOCK_CANCEL)
button.set_property("can-default",True)
button = Gtk.Button.new_from_stock(Gtk.STOCK_APPLY)
button.set_property("can-default",True)
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)
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("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()
def on_dialog_close(self,widget,*event):
widget.hide_on_delete()
return True
def on_filechooser_dialog_response(self,widget,response):
if response == -6:
print("Cancel")
elif response == -5:
print("File selection: %s" % widget.get_filename())
self.on_dialog_close(widget)
def on_source_button_clicked(self,widget):
dialog = Gtk.FileChooserDialog("Select source directory!",
self.obj("window"),
Gtk.FileChooserAction.SELECT_FOLDER,
(Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
Gtk.STOCK_APPLY, Gtk.ResponseType.OK))
dialog.set_default_size(600, 300)
response = dialog.run()
if response == Gtk.ResponseType.OK:
self.filecnt = sourcedir_filecnt(dialog.get_filename())
if self.filecnt >0:
print(self.filecnt)
self.obj("source_entry").set_text(dialog.get_filename())
self.obj("destination_label").set_sensitive(True)
self.obj("destination_entry").set_sensitive(True)
self.obj("destination_button").set_sensitive(True)
self.obj("numfiles_label").set_text(str(self.filecnt))
self.obj("progress_label").set_text("{} of {}".format(fileindex, self.filecnt))
foldercnt = sourcedir_foldercnt(dialog.get_filename())
if foldercnt >0:
self.obj("numfolders_label").set_text(str(foldercnt))
elif response == Gtk.ResponseType.CANCEL:
print("Cancel")
dialog.destroy()
def on_destination_button_clicked(self,widget):
global destdir
dialog = Gtk.FileChooserDialog("Select destination directory!",
self.obj("window"),
Gtk.FileChooserAction.SELECT_FOLDER,
(Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
Gtk.STOCK_APPLY, Gtk.ResponseType.OK))
dialog.set_default_size(600, 300)
response = dialog.run()
if response == Gtk.ResponseType.OK:
destdir = dialog.get_filename()
self.obj("destination_entry").set_text(dialog.get_filename())
self.obj("include_checkbutton").set_sensitive(True)
self.obj("translation_frame").set_sensitive(True)
print(srcdir)
print(destdir)
elif response == Gtk.ResponseType.CANCEL:
print("Cancel")
dialog.destroy()
def on_include_checkbutton_toggled(self, widget):
global incinc
incinc = self.obj("include_checkbutton").get_active()
def on_default_dir_checkbutton_toggled(self, widget):
global defdir
defdir = self.obj("default_dir_checkbutton").get_active()
def on_translate_button_clicked(self, widget, data=None):
self.obj("sourceframe").set_sensitive(False)
self.obj("translate_button").set_sensitive(False)
if self.process!=None:
return
print("Creating shared Queue")
queue = Queue()
print("Creating Worker")
worker = Worker(queue, self.filecnt, filelist)
print("Creating Listener")
listener = Listener(queue)
listener.connect("updated",self.callbackDisplay)
listener.connect("finished",self.callbackFinished)
print("Starting Listener")
thread = threading.Thread(target=listener.go, args=())
thread.start()
print("Starting Worker")
self.process = Process(target=worker.go, args=())
self.process.start()
app = ExampleApp()
app.run(sys.argv)

367
Gui test/h2inc_orig.glade Normal file
View File

@@ -0,0 +1,367 @@
<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.18.3 -->
<interface>
<requires lib="gtk+" version="3.12"/>
<object class="GtkWindow" id="window1">
<property name="can_focus">False</property>
<property name="margin_left">5</property>
<property name="margin_right">5</property>
<property name="margin_top">5</property>
<property name="margin_bottom">5</property>
<child>
<object class="GtkGrid" id="grid1">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="margin_left">5</property>
<property name="margin_right">5</property>
<property name="margin_top">5</property>
<property name="margin_bottom">5</property>
<property name="hexpand">True</property>
<property name="vexpand">True</property>
<child>
<object class="GtkFrame" id="sourceframe">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label_xalign">0</property>
<property name="shadow_type">etched-out</property>
<child>
<object class="GtkAlignment" id="alignment1">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="left_padding">12</property>
<child>
<object class="GtkGrid" id="grid2">
<property name="visible">True</property>
<property name="can_focus">False</property>
<child>
<object class="GtkLabel" id="label2">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="halign">end</property>
<property name="margin_right">5</property>
<property name="margin_top">5</property>
<property name="margin_bottom">5</property>
<property name="hexpand">True</property>
<property name="label" translatable="yes">Source:</property>
<property name="justify">right</property>
</object>
<packing>
<property name="left_attach">0</property>
<property name="top_attach">0</property>
<property name="width">2</property>
</packing>
</child>
<child>
<object class="GtkEntry" id="entry1">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="margin_left">5</property>
<property name="margin_right">5</property>
<property name="margin_top">5</property>
<property name="margin_bottom">5</property>
<property name="text" translatable="yes">Select source directory!</property>
</object>
<packing>
<property name="left_attach">2</property>
<property name="top_attach">0</property>
<property name="width">2</property>
</packing>
</child>
<child>
<object class="GtkButton" id="button1">
<property name="label" translatable="yes">Source directory...</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<property name="halign">start</property>
<property name="margin_left">5</property>
<property name="margin_right">5</property>
<property name="margin_top">5</property>
<property name="margin_bottom">5</property>
</object>
<packing>
<property name="left_attach">4</property>
<property name="top_attach">0</property>
<property name="width">2</property>
</packing>
</child>
<child>
<object class="GtkLabel" id="label3">
<property name="visible">True</property>
<property name="sensitive">False</property>
<property name="can_focus">False</property>
<property name="halign">end</property>
<property name="margin_right">5</property>
<property name="margin_top">5</property>
<property name="margin_bottom">5</property>
<property name="hexpand">True</property>
<property name="label" translatable="yes">Destination:</property>
<property name="justify">right</property>
<property name="ellipsize">start</property>
<property name="angle">2.2351741811588166e-10</property>
</object>
<packing>
<property name="left_attach">0</property>
<property name="top_attach">1</property>
<property name="width">2</property>
</packing>
</child>
<child>
<object class="GtkEntry" id="entry2">
<property name="visible">True</property>
<property name="sensitive">False</property>
<property name="can_focus">True</property>
<property name="margin_left">5</property>
<property name="margin_right">5</property>
<property name="margin_top">5</property>
<property name="margin_bottom">5</property>
<property name="text" translatable="yes">Select destination directory!</property>
</object>
<packing>
<property name="left_attach">2</property>
<property name="top_attach">1</property>
<property name="width">2</property>
</packing>
</child>
<child>
<object class="GtkButton" id="button2">
<property name="label" translatable="yes">Destination directory...</property>
<property name="visible">True</property>
<property name="sensitive">False</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<property name="halign">start</property>
<property name="margin_left">5</property>
<property name="margin_right">5</property>
<property name="margin_top">5</property>
<property name="margin_bottom">5</property>
</object>
<packing>
<property name="left_attach">4</property>
<property name="top_attach">1</property>
<property name="width">2</property>
</packing>
</child>
<child>
<object class="GtkCheckButton" id="checkbutton1">
<property name="label" translatable="yes">Create "include" folder if it does not exist.</property>
<property name="visible">True</property>
<property name="sensitive">False</property>
<property name="can_focus">True</property>
<property name="receives_default">False</property>
<property name="margin_top">5</property>
<property name="margin_bottom">5</property>
<property name="xalign">0</property>
<property name="draw_indicator">True</property>
</object>
<packing>
<property name="left_attach">0</property>
<property name="top_attach">2</property>
<property name="width">6</property>
</packing>
</child>
</object>
</child>
</object>
</child>
<child type="label">
<object class="GtkLabel" id="label1">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">Select folders</property>
</object>
</child>
</object>
<packing>
<property name="left_attach">0</property>
<property name="top_attach">0</property>
<property name="width">5</property>
</packing>
</child>
<child>
<object class="GtkFrame" id="frame1">
<property name="visible">True</property>
<property name="sensitive">False</property>
<property name="can_focus">False</property>
<property name="margin_top">5</property>
<property name="margin_bottom">5</property>
<property name="label_xalign">0</property>
<property name="shadow_type">etched-out</property>
<child>
<object class="GtkAlignment" id="alignment2">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="left_padding">12</property>
<child>
<object class="GtkGrid" id="grid3">
<property name="visible">True</property>
<property name="can_focus">False</property>
<child>
<object class="GtkFrame" id="frame2">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="margin_right">5</property>
<property name="margin_bottom">5</property>
<property name="hexpand">True</property>
<property name="label_xalign">0</property>
<property name="shadow_type">etched-out</property>
<child>
<object class="GtkAlignment" id="alignment3">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="left_padding">12</property>
<child>
<object class="GtkBox" id="box1">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="orientation">vertical</property>
<child>
<object class="GtkLabel" id="label6">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="halign">start</property>
<property name="margin_top">5</property>
<property name="margin_bottom">5</property>
<property name="hexpand">True</property>
<property name="label" translatable="yes">Number of folders: 0</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkLabel" id="label7">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="halign">start</property>
<property name="margin_top">5</property>
<property name="margin_bottom">5</property>
<property name="label" translatable="yes">Number of files: 0</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
</object>
</child>
</object>
</child>
<child type="label">
<object class="GtkLabel" id="label5">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">Source information</property>
</object>
</child>
</object>
<packing>
<property name="left_attach">0</property>
<property name="top_attach">0</property>
</packing>
</child>
<child>
<object class="GtkButton" id="button3">
<property name="label" translatable="yes">Translate!</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<property name="halign">start</property>
<property name="margin_top">5</property>
<property name="margin_bottom">5</property>
</object>
<packing>
<property name="left_attach">0</property>
<property name="top_attach">1</property>
</packing>
</child>
<child>
<object class="GtkFrame" id="frame3">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="margin_right">5</property>
<property name="margin_top">5</property>
<property name="margin_bottom">5</property>
<property name="label_xalign">0</property>
<property name="shadow_type">etched-out</property>
<child>
<object class="GtkAlignment" id="alignment4">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="left_padding">12</property>
<child>
<object class="GtkBox" id="box2">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="orientation">vertical</property>
<child>
<object class="GtkLabel" id="label9">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="halign">start</property>
<property name="margin_top">5</property>
<property name="margin_bottom">5</property>
<property name="label" translatable="yes">Total progress:</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkProgressBar" id="progressbar1">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="margin_right">5</property>
<property name="margin_top">5</property>
<property name="margin_bottom">5</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
</object>
</child>
</object>
</child>
<child type="label">
<object class="GtkLabel" id="label8">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">Progress</property>
</object>
</child>
</object>
<packing>
<property name="left_attach">0</property>
<property name="top_attach">2</property>
</packing>
</child>
</object>
</child>
</object>
</child>
<child type="label">
<object class="GtkLabel" id="label4">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">Translation</property>
</object>
</child>
</object>
<packing>
<property name="left_attach">0</property>
<property name="top_attach">1</property>
<property name="width">5</property>
</packing>
</child>
</object>
</child>
</object>
</interface>

433
Gui test/test.glade Normal file
View File

@@ -0,0 +1,433 @@
<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.18.3 -->
<interface>
<requires lib="gtk+" version="3.12"/>
<object class="GtkApplicationWindow" id="applicationwindow1">
<property name="can_focus">False</property>
<child>
<object class="GtkBox" id="box3">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="orientation">vertical</property>
<child>
<object class="GtkFrame" id="frame5">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="margin_bottom">5</property>
<property name="label_xalign">0</property>
<property name="shadow_type">etched-out</property>
<child>
<object class="GtkAlignment" id="alignment5">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="left_padding">12</property>
<child>
<object class="GtkGrid" id="grid4">
<property name="visible">True</property>
<property name="can_focus">False</property>
<child>
<object class="GtkLabel" id="label11">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="halign">end</property>
<property name="margin_top">5</property>
<property name="margin_bottom">5</property>
<property name="xpad">5</property>
<property name="label" translatable="yes">Source:</property>
<property name="ellipsize">start</property>
</object>
<packing>
<property name="left_attach">0</property>
<property name="top_attach">0</property>
</packing>
</child>
<child>
<object class="GtkLabel" id="label12">
<property name="visible">True</property>
<property name="sensitive">False</property>
<property name="can_focus">False</property>
<property name="halign">end</property>
<property name="margin_top">5</property>
<property name="margin_bottom">5</property>
<property name="xpad">5</property>
<property name="label" translatable="yes">Destination:</property>
<property name="angle">0.059999999999999998</property>
</object>
<packing>
<property name="left_attach">0</property>
<property name="top_attach">1</property>
</packing>
</child>
<child>
<object class="GtkEntry" id="entry3">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="margin_top">5</property>
<property name="margin_bottom">5</property>
</object>
<packing>
<property name="left_attach">1</property>
<property name="top_attach">0</property>
</packing>
</child>
<child>
<object class="GtkEntry" id="entry4">
<property name="visible">True</property>
<property name="sensitive">False</property>
<property name="can_focus">True</property>
<property name="margin_top">5</property>
<property name="margin_bottom">5</property>
</object>
<packing>
<property name="left_attach">1</property>
<property name="top_attach">1</property>
</packing>
</child>
<child>
<object class="GtkButton" id="button6">
<property name="label" translatable="yes">Select source directory...</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<property name="halign">start</property>
<property name="margin_top">5</property>
<property name="margin_bottom">5</property>
<property name="yalign">0.52999997138977051</property>
</object>
<packing>
<property name="left_attach">2</property>
<property name="top_attach">0</property>
</packing>
</child>
<child>
<object class="GtkButton" id="button7">
<property name="label" translatable="yes">Select destination directory...</property>
<property name="visible">True</property>
<property name="sensitive">False</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<property name="halign">start</property>
<property name="margin_top">5</property>
<property name="margin_bottom">5</property>
</object>
<packing>
<property name="left_attach">2</property>
<property name="top_attach">1</property>
</packing>
</child>
<child>
<object class="GtkCheckButton" id="checkbutton2">
<property name="label" translatable="yes">checkbutton</property>
<property name="visible">True</property>
<property name="sensitive">False</property>
<property name="can_focus">True</property>
<property name="receives_default">False</property>
<property name="xalign">0</property>
<property name="active">True</property>
<property name="draw_indicator">True</property>
</object>
<packing>
<property name="left_attach">0</property>
<property name="top_attach">2</property>
<property name="width">3</property>
</packing>
</child>
</object>
</child>
</object>
</child>
<child type="label">
<object class="GtkLabel" id="label13">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">Select folders</property>
</object>
</child>
</object>
<packing>
<property name="expand">True</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkFrame" id="frame6">
<property name="visible">True</property>
<property name="sensitive">False</property>
<property name="can_focus">False</property>
<property name="margin_bottom">5</property>
<property name="label_xalign">0</property>
<property name="shadow_type">etched-out</property>
<child>
<object class="GtkAlignment" id="alignment6">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="left_padding">12</property>
<child>
<object class="GtkBox" id="box4">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="orientation">vertical</property>
<child>
<object class="GtkFrame" id="frame7">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="margin_bottom">5</property>
<property name="label_xalign">0</property>
<property name="shadow_type">etched-out</property>
<child>
<object class="GtkAlignment" id="alignment7">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="left_padding">12</property>
<child>
<object class="GtkGrid" id="grid5">
<property name="visible">True</property>
<property name="can_focus">False</property>
<child>
<object class="GtkLabel" id="label14">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="halign">end</property>
<property name="margin_top">5</property>
<property name="label" translatable="yes">Number of folders: </property>
</object>
<packing>
<property name="left_attach">0</property>
<property name="top_attach">0</property>
</packing>
</child>
<child>
<object class="GtkLabel" id="numFolders1">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="opacity">0.9882352941176471</property>
<property name="halign">start</property>
<property name="margin_top">5</property>
<property name="label" translatable="yes">0</property>
<property name="ellipsize">end</property>
</object>
<packing>
<property name="left_attach">1</property>
<property name="top_attach">0</property>
</packing>
</child>
<child>
<object class="GtkLabel" id="label15">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="halign">end</property>
<property name="margin_top">5</property>
<property name="margin_bottom">5</property>
<property name="label" translatable="yes">Number of files: </property>
</object>
<packing>
<property name="left_attach">0</property>
<property name="top_attach">1</property>
</packing>
</child>
<child>
<object class="GtkLabel" id="numFiles1">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="halign">start</property>
<property name="margin_top">5</property>
<property name="margin_bottom">5</property>
<property name="label" translatable="yes">0</property>
</object>
<packing>
<property name="left_attach">1</property>
<property name="top_attach">1</property>
</packing>
</child>
</object>
</child>
</object>
</child>
<child type="label">
<object class="GtkLabel" id="label16">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">Source information</property>
</object>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkButton" id="button8">
<property name="label" translatable="yes">Translate!</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<property name="halign">start</property>
<property name="margin_top">5</property>
<property name="margin_bottom">5</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">False</property>
<property name="position">1</property>
</packing>
</child>
<child>
<object class="GtkFrame" id="frame8">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="margin_bottom">5</property>
<property name="label_xalign">0</property>
<property name="shadow_type">etched-out</property>
<child>
<object class="GtkAlignment" id="alignment8">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="left_padding">12</property>
<child>
<object class="GtkGrid" id="grid6">
<property name="visible">True</property>
<property name="can_focus">False</property>
<child>
<object class="GtkLabel" id="label17">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="halign">end</property>
<property name="margin_top">5</property>
<property name="margin_bottom">5</property>
<property name="label" translatable="yes">Total progress: </property>
</object>
<packing>
<property name="left_attach">0</property>
<property name="top_attach">0</property>
</packing>
</child>
<child>
<object class="GtkLabel" id="label18">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="halign">start</property>
<property name="margin_top">5</property>
<property name="margin_bottom">5</property>
<property name="hexpand">True</property>
<property name="label" translatable="yes">0 of 0</property>
</object>
<packing>
<property name="left_attach">1</property>
<property name="top_attach">0</property>
</packing>
</child>
<child>
<object class="GtkProgressBar" id="progressbar2">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="margin_bottom">5</property>
</object>
<packing>
<property name="left_attach">0</property>
<property name="top_attach">1</property>
<property name="width">2</property>
</packing>
</child>
</object>
</child>
</object>
</child>
<child type="label">
<object class="GtkLabel" id="label19">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">Progress</property>
</object>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">2</property>
</packing>
</child>
</object>
</child>
</object>
</child>
<child type="label">
<object class="GtkLabel" id="label20">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">Translation!</property>
</object>
</child>
</object>
<packing>
<property name="expand">True</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
</object>
</child>
</object>
<object class="GtkFileChooserDialog" id="filechooserdialog1">
<property name="can_focus">False</property>
<property name="type_hint">dialog</property>
<property name="action">select-folder</property>
<child internal-child="vbox">
<object class="GtkBox" id="filechooserdialog-vbox1">
<property name="can_focus">False</property>
<property name="orientation">vertical</property>
<property name="spacing">2</property>
<child internal-child="action_area">
<object class="GtkButtonBox" id="filechooserdialog-action_area1">
<property name="can_focus">False</property>
<property name="layout_style">end</property>
<child>
<object class="GtkButton" id="button4">
<property name="label">gtk-ok</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<property name="use_stock">True</property>
<property name="always_show_image">True</property>
</object>
<packing>
<property name="expand">True</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkButton" id="button5">
<property name="label">gtk-cancel</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<property name="use_stock">True</property>
<property name="always_show_image">True</property>
</object>
<packing>
<property name="expand">True</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">False</property>
<property name="position">0</property>
</packing>
</child>
<child>
<placeholder/>
</child>
</object>
</child>
<action-widgets>
<action-widget response="-5">button4</action-widget>
<action-widget response="-6">button5</action-widget>
</action-widgets>
</object>
</interface>

View File

@@ -0,0 +1,34 @@
import multiprocessing as mp
import random
import string
random.seed(123)
# Define an output queue
output = mp.Queue()
# define a example function
def rand_string(length, output):
""" Generates a random string of numbers, lower- and uppercase chars. """
rand_str = ''.join(random.choice(
string.ascii_lowercase
+ string.ascii_uppercase
+ string.digits)
for i in range(length))
output.put(rand_str)
# Setup a list of processes that we want to run
processes = [mp.Process(target=rand_string, args=(5, output)) for x in range(4)]
# Run processes
for p in processes:
p.start()
# Exit the completed processes
for p in processes:
p.join()
# Get process results from the output queue
results = [output.get() for p in processes]
print(results)

View File

@@ -0,0 +1,171 @@
#!/usr/bin/env python3
GUI_WIN = 0
GUI_LINUX = 1
import sys
if sys.platform == 'win32':
GUI = GUI_WIN
else:
GUI = GUI_LINUX
from tkinter import (Tk, BOTH, Text, E, W, S, N, END,
NORMAL, DISABLED, StringVar)
from tkinter.ttk import Frame, Label, Button, Progressbar, Entry
from tkinter import scrolledtext
if GUI == GUI_WIN:
from multiprocessing import Process, Manager, Queue, Value
from queue import Empty
else:
from multiprocessing import Queue, Process
import queue
from decimal import Decimal, getcontext
from time import sleep
DELAY1 = 20
DELAY2 = 20
if GUI == GUI_WIN:
# Queue must be global
q = Queue()
class commonUI(Frame):
def __init__(self, parent):
Frame.__init__(self, parent)
self.parent = parent
self.initUI()
def initUI(self):
self.val = Value('i', 0)
self.dig = Value('i', 4000)
self.acc = Value('i', 100)
self.parent.title("Pi computation")
self.pack(fill=BOTH, expand=True)
self.grid_columnconfigure(4, weight=1)
self.grid_rowconfigure(3, weight=1)
lbl1 = Label(self, text="Digits:")
lbl1.grid(row=0, column=0, sticky=E, padx=10, pady=10)
self.ent1 = Entry(self, width=10)
self.ent1.insert(END, self.dig.value)
self.ent1.grid(row=0, column=1, sticky=W)
lbl2 = Label(self, text="Accuracy:")
lbl2.grid(row=0, column=2, sticky=E, padx=10, pady=10)
self.ent2 = Entry(self, width=10)
self.ent2.insert(END, self.acc.value)
self.ent2.grid(row=0, column=3, sticky=W)
self.startBtn = Button(self, text="Start", command=self.onStart)
self.startBtn.grid(row=1, column=0, padx=10, pady=5, sticky=W)
self.pbar = Progressbar(self, mode='determinate')
self.pbar.grid(row=1, column=1, columnspan=3, sticky=W+E)
self.pbar.config(value=0, maximum=self.dig.value)
self.txt = scrolledtext.ScrolledText(self)
self.txt.grid(row=2, column=0, rowspan=4, padx=10, pady=5, columnspan=5, sticky=E+W+S+N)
def onStart(self):
self.startBtn.config(state=DISABLED)
self.txt.delete("1.0", END)
digits = int(self.ent1.get())
accuracy = int(self.ent2.get())
self.p1 = Process(target=generatePi, args=(q, digits, accuracy))
self.p1.start()
self.after(DELAY1, self.onGetValue)
def onGetValue(self):
if (self.p1.is_alive()):
self.val.value += 1000
self.update_pbar()
self.after(DELAY1, self.onGetValue)
return
else:
try:
self.txt.insert('end', q.get(0))
self.txt.insert('end', "\n")
self.startBtn.config(state=NORMAL)
except Empty:
print("queue is empty")
def update_pbar(self):
self.pbar.config(value=self.val.value)
class win_Example(Frame):
def __init__(self, parent):
Frame.__init__(self, parent, name="frame")
commonUI(parent)
if GUI == GUI_WIN:
# Generate function must be a top-level module funtion
def generatePi(q, digs, acc):
getcontext().prec = digs
pi = Decimal(0)
k = 0
n = acc
while k < n:
pi += (Decimal(1)/(16**k))*((Decimal(4)/(8*k+1)) - \
(Decimal(2)/(8*k+4)) - (Decimal(1)/(8*k+5))- \
(Decimal(1)/(8*k+6)))
k += 1
q.put(pi)
time.sleep(1)
class linux_Example(Frame):
def __init__(self, parent, q):
Frame.__init__(self, parent)
self.queue = q
commonUI(parent)
def generatePi(self, queue):
getcontext().prec = self.digits
pi = Decimal(0)
k = 0
n = self.accuracy
while k < n:
pi += (Decimal(1)/(16**k))*((Decimal(4)/(8*k+1)) - \
(Decimal(2)/(8*k+4)) - (Decimal(1)/(8*k+5))- \
(Decimal(1)/(8*k+6)))
k += 1
print (self.p1.is_alive())
queue.put(pi)
print("end")
def main():
if GUI == GUI_LINUX:
q = Queue()
root = Tk()
root.geometry("400x350+300+300")
app = win_Example(root) if GUI == GUI_WIN else linux_Example(root)
root.mainloop()
if __name__ == '__main__':
main()

View File

@@ -0,0 +1,136 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
ZetCode Tkinter e-book
This script produces a long-running task of calculating
a large Pi number, while keeping the GUI responsive.
Author: Jan Bodnar
Last modified: January 2016
Website: www.zetcode.com
"""
from tkinter import (Tk, BOTH, Text, E, W, S, N, END,
NORMAL, DISABLED, StringVar)
from tkinter.ttk import Frame, Label, Button, Progressbar, Entry
from tkinter import scrolledtext
from multiprocessing import Queue, Process
import queue
from decimal import Decimal, getcontext
import time
DELAY1 = 20
DELAY2 = 20
class Example(Frame):
def __init__(self, parent, q):
Frame.__init__(self, parent)
self.queue = q
self.parent = parent
self.initUI()
def initUI(self):
self.parent.title("Pi computation")
self.pack(fill=BOTH, expand=True)
self.grid_columnconfigure(4, weight=1)
self.grid_rowconfigure(3, weight=1)
lbl1 = Label(self, text="Digits:")
lbl1.grid(row=0, column=0, sticky=E, padx=10, pady=10)
self.ent1 = Entry(self, width=10)
self.ent1.insert(END, "4000")
self.ent1.grid(row=0, column=1, sticky=W)
lbl2 = Label(self, text="Accuracy:")
lbl2.grid(row=0, column=2, sticky=E, padx=10, pady=10)
self.ent2 = Entry(self, width=10)
self.ent2.insert(END, "100")
self.ent2.grid(row=0, column=3, sticky=W)
self.startBtn = Button(self, text="Start",
command=self.onStart)
self.startBtn.grid(row=1, column=0, padx=10, pady=5, sticky=W)
self.pbar = Progressbar(self, mode='indeterminate')
self.pbar.grid(row=1, column=1, columnspan=3, sticky=W+E)
self.txt = scrolledtext.ScrolledText(self)
self.txt.grid(row=2, column=0, rowspan=4, padx=10, pady=5,
columnspan=5, sticky=E+W+S+N)
def onStart(self):
self.startBtn.config(state=DISABLED)
self.txt.delete("1.0", END)
self.digits = int(self.ent1.get())
self.accuracy = int(self.ent2.get())
self.p1 = Process(target=self.generatePi, args=(self.queue,))
self.p1.start()
self.pbar.start(DELAY2)
self.after(DELAY1, self.onGetValue)
def onGetValue(self):
if (self.p1.is_alive()):
self.after(DELAY1, self.onGetValue)
return
else:
try:
self.txt.insert('end', self.queue.get(0))
self.txt.insert('end', "\n")
self.pbar.stop()
self.startBtn.config(state=NORMAL)
except queue.Empty:
print("queue is empty")
def generatePi(self, queue):
getcontext().prec = self.digits
pi = Decimal(0)
k = 0
n = self.accuracy
while k < n:
pi += (Decimal(1)/(16**k))*((Decimal(4)/(8*k+1)) - \
(Decimal(2)/(8*k+4)) - (Decimal(1)/(8*k+5))- \
(Decimal(1)/(8*k+6)))
k += 1
print (self.p1.is_alive())
queue.put(pi)
print("end")
time.sleep(1)
def main():
q = Queue()
root = Tk()
root.geometry("400x350+300+300")
app = Example(root, q)
root.mainloop()
if __name__ == '__main__':
main()

View File

@@ -0,0 +1,134 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
ZetCode Tkinter e-book
This script produces a long-running task of calculating
a large Pi number, while keeping the GUI responsive.
This is an example written for Windows.
Author: Jan Bodnar
Last modified: January 2016
Website: www.zetcode.com
"""
from tkinter import (Tk, BOTH, Text, E, W, S, N, END,
NORMAL, DISABLED, StringVar)
from tkinter.ttk import Frame, Label, Button, Progressbar, Entry
from tkinter import scrolledtext
from multiprocessing import Process, Manager, Queue
from queue import Empty
from decimal import Decimal, getcontext
DELAY1 = 80
DELAY2 = 20
# Queue must be global
q = Queue()
class Example(Frame):
def __init__(self, parent):
Frame.__init__(self, parent, name="frame")
self.parent = parent
self.initUI()
def initUI(self):
self.parent.title("Pi computation")
self.pack(fill=BOTH, expand=True)
self.grid_columnconfigure(4, weight=1)
self.grid_rowconfigure(3, weight=1)
lbl1 = Label(self, text="Digits:")
lbl1.grid(row=0, column=0, sticky=E, padx=10, pady=10)
self.ent1 = Entry(self, width=10)
self.ent1.insert(END, "4000")
self.ent1.grid(row=0, column=1, sticky=W)
lbl2 = Label(self, text="Accuracy:")
lbl2.grid(row=0, column=2, sticky=E, padx=10, pady=10)
self.ent2 = Entry(self, width=10)
self.ent2.insert(END, "100")
self.ent2.grid(row=0, column=3, sticky=W)
self.startBtn = Button(self, text="Start",
command=self.onStart)
self.startBtn.grid(row=1, column=0, padx=10, pady=5, sticky=W)
self.pbar = Progressbar(self, mode='indeterminate')
self.pbar.grid(row=1, column=1, columnspan=3, sticky=W+E)
self.txt = scrolledtext.ScrolledText(self)
self.txt.grid(row=2, column=0, rowspan=4, padx=10, pady=5,
columnspan=5, sticky=E+W+S+N)
def onStart(self):
self.startBtn.config(state=DISABLED)
self.txt.delete("1.0", END)
digits = int(self.ent1.get())
accuracy = int(self.ent2.get())
self.p1 = Process(target=generatePi, args=(q, digits, accuracy))
self.p1.start()
self.pbar.start(DELAY2)
self.after(DELAY1, self.onGetValue)
def onGetValue(self):
if (self.p1.is_alive()):
self.after(DELAY1, self.onGetValue)
return
else:
try:
self.txt.insert('end', q.get(0))
self.txt.insert('end', "\n")
self.pbar.stop()
self.startBtn.config(state=NORMAL)
except Empty:
print("queue is empty")
# Generate function must be a top-level module funtion
def generatePi(q, digs, acc):
getcontext().prec = digs
pi = Decimal(0)
k = 0
n = acc
while k < n:
pi += (Decimal(1)/(16**k))*((Decimal(4)/(8*k+1)) - \
(Decimal(2)/(8*k+4)) - (Decimal(1)/(8*k+5))- \
(Decimal(1)/(8*k+6)))
k += 1
q.put(pi)
def main():
root = Tk()
root.geometry("400x350+300+300")
app = Example(root)
root.mainloop()
if __name__ == '__main__':
main()

View File

@@ -0,0 +1,43 @@
# Test Code for Tkinter with threads
import tkinter as Tk
import multiprocessing
from Queue import Empty, Full
import time
class GuiApp(object):
def __init__(self,q):
self.root = Tk.Tk()
self.root.geometry('300x100')
self.text_wid = Tk.Text(self.root,height=100,width=100)
self.text_wid.pack(expand=1,fill=Tk.BOTH)
self.root.after(100,self.CheckQueuePoll,q)
def CheckQueuePoll(self,c_queue):
try:
str = c_queue.get(0)
self.text_wid.insert('end',str)
except Empty:
pass
finally:
self.root.after(100, self.CheckQueuePoll, c_queue)
# Data Generator which will generate Data
def GenerateData(q):
for i in range(10):
print("Generating Some Data, Iteration %s" %(i))
time.sleep(2)
q.put("Some Data from iteration %s \n" %(i))
if __name__ == '__main__':
# Queue which will be used for storing Data
q = multiprocessing.Queue()
q.cancel_join_thread() # or else thread that puts data will not term
gui = GuiApp(q)
t1 = multiprocessing.Process(target=GenerateData,args=(q,))
t1.start()
gui.root.mainloop()
t1.join()
t2.join()

View File

@@ -0,0 +1,29 @@
import time
from multiprocessing import Process, Value, Lock
class Counter(object):
def __init__(self, initval=0):
self.val = Value('i', initval)
self.lock = Lock()
def increment(self):
with self.lock:
self.val.value += 1
def value(self):
with self.lock:
return self.val.value
def func(counter):
for i in range(500):
time.sleep(0.001)
counter.increment()
if __name__ == '__main__':
counter = Counter(0)
procs = [Process(target=func, args=(counter,)) for i in range(10)]
for p in procs: p.start()
for p in procs: p.join()
print (counter.value())

View File

@@ -0,0 +1,40 @@
import tkinter as tk
from tkinter import ttk
import tkinter.messagebox
import time # to simulate long-running function
from threading import Thread
class RedactionSolutions:
def __init__(self, master):
tk.Button(master, text="Run RS Wizard", fg='green', command=self.run).grid(row=5)
def long_running_function(self):
#self.working = tk.Label(root, text='In Progress. Please Wait...')
self.working = ttk.Progressbar(root, orient='horizontal', mode='determinate')
self.working.config(value=0, maximum=10)
self.working.grid(row=6, columnspan=2)
for x in range(10):
print(x)
self.working.config(value=x)
time.sleep(1)
restart = tkinter.messagebox.askquestion('RS Wizard', 'Redaction complete! See file location for result.\nWould you like to redact another file?')
if restart == 'yes':
#self.reset()
self.working.grid_forget()
else:
root.destroy()
def run(self):
Thread(target=self.long_running_function).start()
root = tk.Tk()
root.wm_title("RS Wizard")
RedactionSolutions(root)
root.mainloop()

View File

@@ -0,0 +1,18 @@
from multiprocessing import Process
import os
def info(title):
print(title)
print('module name:', __name__)
print('parent process:', os.getppid())
print('process id:', os.getpid())
def f(name):
info('function f')
print('hello', name)
if __name__ == '__main__':
info('main line')
p = Process(target=f, args=('bob',))
p.start()
p.join()

View File

@@ -0,0 +1,21 @@
import multiprocessing
import time
data = (['a', '2'], ['b', '4'], ['c', '6'], ['d', '8'],
['e', '1'], ['f', '3'], ['g', '5'], ['h', '7']
)
def mp_handler(var1):
for indata in var1:
p = multiprocessing.Process(target=mp_worker, args=(indata[0], indata[1]))
p.start()
def mp_worker(inputs, the_time):
print " Processs %s\tWaiting %s seconds" % (inputs, the_time)
time.sleep(int(the_time))
print " Process %s\tDONE" % inputs
if __name__ == '__main__':
mp_handler(data)

View File

@@ -0,0 +1,352 @@
#!/usr/bin/env python
import sys #exit
import gobject
import pygtk
pygtk.require('2.0')
import gtk
import multiprocessing
import threading
import time
import random
gtk.gdk.threads_init()
# Any time you connect to any of these signals, they should be
# protected with a threading.Lock or similar.
class ObserverBase(gobject.GObject):
__gsignals__ = {
'message_event' : (gobject.SIGNAL_RUN_LAST,
gobject.TYPE_NONE,
(gobject.TYPE_STRING,)),
'updated' : (gobject.SIGNAL_RUN_LAST,
gobject.TYPE_NONE,
(gobject.TYPE_FLOAT,)),
'finished': (gobject.SIGNAL_RUN_LAST,
gobject.TYPE_NONE,
()),
'errored': (gobject.SIGNAL_RUN_LAST,
gobject.TYPE_NONE,
())
}
def __init__(self):
gobject.GObject.__init__(self)
gobject.type_register(ObserverBase)
class Observer(ObserverBase):
def __init__(self, queue):
ObserverBase.__init__(self)
self.queue = queue
def start(self):
# Create new thread to do the listening
self.thread = threading.Thread(target=self.listen, args=())
self.thread.start()
def join(self):
self.thread.join()
def listen(self):
while True:
# Listen for results on the queue and process them accordingly
print "Observer is waiting for data"
data = self.queue.get()
print "Observer received: ",data
dataType = type(data).__name__
if dataType=="bool" and data==True:
self.emit('finished')
return
if dataType=="bool" and data==False:
self.emit('errored')
return
elif dataType=="str":
self.emit('message_event', data)
elif dataType=="float":
self.emit('updated', data)
else:
raise RuntimeError("Queue had an unknown data type ('"+dataType+"') with value '"+str(data)+"'")
gobject.type_register(Observer)
class Process(gobject.GObject):
__gsignals__ = {
'starting': (gobject.SIGNAL_RUN_LAST,
gobject.TYPE_NONE,
()),
'joined': (gobject.SIGNAL_RUN_LAST,
gobject.TYPE_NONE,
())
}
def __init__(self, function, arguments):
gobject.GObject.__init__(self)
# Create queue
self.queue = multiprocessing.Queue()
# Create process
if arguments==(): arguments=(self.queue,)
self.process = multiprocessing.Process(target=function, args=arguments)
# Create observer
self.observer = Observer(self.queue)
def getObserver(self):
return self.observer
def start(self):
# Starting observer
self.emit("starting")
print "starting observer"
self.observer.start()
# Start process
print "starting process"
self.process.start()
def join(self):
# Clean up after process
print "cleaning up after process"
self.process.join()
self.emit("joined")
# Clean up after observer
print "cleaning up after observer"
self.queue.put(False) # If the false is ever read, it's an error
self.observer.join()
# Clean up queue
#self.queue.close()
gobject.type_register(Process)
class CombinedObserver(ObserverBase):
def __init__(self, observers, weights=None):
ObserverBase.__init__(self)
self.observers = observers
numObservers = len(observers)
if weights==None:
self.weights = [1 for i in range(numObservers)]
else:
self.weights = weights
sum_weights = sum(self.weights)
self.weights = [float(w)/sum_weights for w in self.weights]
print "weights = ",self.weights
index = 0
for observer in self.observers:
#observer.connect("message_event", onMessageReceived, index)
observer.connect("updated", self.onUpdate, index)
observer.connect("finished", self.onFinished, index)
observer.connect("errored", self.onErrored, index)
index = index+1
self.fractions = [0 for i in range(numObservers)]
self.finished = [False for i in range(numObservers)]
self.errored = [False for i in range(numObservers)]
self.lock = threading.RLock()
def onUpdate(self, widget, fraction, index):
self.lock.acquire()
self.fractions[index] = fraction * self.weights[index]
self.emit("updated", sum(self.fractions))
self.lock.release()
def onFinished(self, widget, index):
self.lock.acquire()
self.finished[index] = True
if all(self.finished):
self.emit("finished")
self.lock.release()
def onErrored(self, widget, index):
self.lock.acquire()
self.errored[index] = True
self.emit("errored")
self.lock.release()
gobject.type_register(CombinedObserver)
# A ProgressWorker is responsible for (a) getting the job done and (b)
# reporting progress. Progress is reported via the queue. Fractions
# (from 0 to 1) indicate the proportion of the job that is finished.
# Strings may indicate what has been done or what is about to be done.
# The boolean 'True' is placed on the queue when the job is finished.
class ProgressWorker():
def getResult(self):
return None
def setQueue(self, queue):
self.queue = queue
def go(self):
raise RuntimeError("Worker.go() has not been overridden")
class WorkerExample(ProgressWorker):
def __init__(self):
#Worker.__init__(self)
manager = multiprocessing.Manager()
self.result = manager.list()
def getResult(self):
return self.result
def go(self, queue):
self.setQueue(queue)
print "The worker has started doing some work (counting from 0 to 9)"
for i in range(10):
self.queue.put("working on step "+str(i)+"...")
proportion = (float(i)+1)/10
slp = random.uniform(0.50, 3.50)
time.sleep(slp)
self.queue.put(proportion)
self.queue.put("done.")
self.queue.put(1.0)
self.queue.put(True)
print "The worker has finished."
self.result.append("The the work has been finished and the result isn't 42.")
class ProgressBar(gobject.GObject):
__gsignals__ = {
'updated' : (gobject.SIGNAL_RUN_LAST,
gobject.TYPE_NONE,
(gobject.TYPE_FLOAT,)),
'finished': (gobject.SIGNAL_RUN_LAST,
gobject.TYPE_NONE,
())
}
def __init__(self, observer):
gobject.GObject.__init__(self)
self.observer = observer
self.observer.connect("updated",self.displayUpdate)
self.observer.connect("message_event",self.displayMessage)
self.observer.connect("finished",self.finish)
self.progress = gtk.ProgressBar()
self.progress.connect("destroy", self.destroy)
self.process=None
self.lock = threading.RLock()
def displayUpdate(self, obj, fraction):
self.lock.acquire()
self.progress.set_fraction(fraction)
self.emit("updated", fraction)
self.lock.release()
def displayMessage(self, obj, text):
self.lock.acquire()
self.progress.set_text(text)
self.lock.release()
def finish(self, obj, data=None):
self.emit("finished")
def destroy(self, widget, data=None):
print "Destroying"
if self.process==None:
return
self.queue.put("finished")
self.process.join()
self.thread.join()
gobject.type_register(ProgressBar)
class ProgressBarWindow:
def __init__(self, observer, title):
self.progressBar = ProgressBar(observer)
self.progressBar.connect("finished", self.done)
window = gtk.Window(gtk.WINDOW_TOPLEVEL)
window.set_border_width(10)
window.set_title(title)
window.add(self.progressBar.progress)
window.show_all()
window.connect("destroy", self.destroy)
def main(self):
gtk.main()
def done(self, widget, data=None):
#self.destroy()
print "FINISHED!"
def destroy(self, widget=None, data=None):
gtk.main_quit()
# Test
if __name__ == '__main__':
# Three workers
worker1 = WorkerExample()
process1 = Process(worker1.go, ())
observer1 = process1.getObserver()
worker2 = WorkerExample()
process2 = Process(worker2.go, ())
observer2 = process2.getObserver()
worker3 = WorkerExample()
process3 = Process(worker3.go, ())
observer3 = process3.getObserver()
combo = CombinedObserver([observer1, observer2, observer3])
window0 = ProgressBarWindow(combo, "Workers' progress")
window1 = ProgressBarWindow(observer1, "Workers One's progress")
window2 = ProgressBarWindow(observer2, "Workers Two's progress")
window3 = ProgressBarWindow(observer3, "Workers Three's progress")
process1.start()
#time.sleep(0.1)
process2.start()
#time.sleep(0.1)
process3.start()
gtk.main()
#process1.join()
#process2.join()
#process3.join()
print "finished"
sys.exit()
# Two workers
worker1 = WorkerExample()
process1 = Process(worker1.go, ())
observer1 = process1.getObserver()
worker2 = WorkerExample()
process2 = Process(worker2.go, ())
observer2 = process2.getObserver()
combo = CombinedObserver([observer1, observer2])
window0 = ProgressBarWindow(combo, "Workers' progress")
window1 = ProgressBarWindow(observer1, "Workers One's progress")
window2 = ProgressBarWindow(observer2, "Workers Two's progress")
process1.start()
time.sleep(0.1)
process2.start()
gtk.main()
process1.join()
process2.join()
print "finished"
sys.exit()
# One worker
print "creating worker1"
worker1 = WorkerExample() # The task to do, carefully constructed to give feedback via a queue
print "creating process1"
process1 = Process(worker1.go, ()) # Responsible for looking after the worker: supplying the queue, setting up the observer, starting process
observer1 = process1.getObserver() # An observer of the worker's progress that listens to the queue
window1 = ProgressBarWindow(observer1, "Workers One's progress") # A display of the progress, connected to the observer
process1.start()
gtk.main()
process1.join()
print "result = ", worker1.result
print "finished"
sys.exit()

View File

@@ -0,0 +1,300 @@
#!/usr/bin/env python
import sys #exit
import gobject
import pygtk
pygtk.require('2.0')
import gtk
import multiprocessing
import threading
import time
import random
gtk.gdk.threads_init()
# Any time you connect to any of these signals, they should be
# protected with a threading.Lock or similar.
class ObserverBase(gobject.GObject):
__gsignals__ = {
'message_event' : (gobject.SIGNAL_RUN_LAST,
gobject.TYPE_NONE,
(gobject.TYPE_STRING,)),
'updated' : (gobject.SIGNAL_RUN_LAST,
gobject.TYPE_NONE,
(gobject.TYPE_FLOAT,)),
'finished': (gobject.SIGNAL_RUN_LAST,
gobject.TYPE_NONE,
()),
'errored': (gobject.SIGNAL_RUN_LAST,
gobject.TYPE_NONE,
())
}
def __init__(self):
gobject.GObject.__init__(self)
gobject.type_register(ObserverBase)
class Observer(ObserverBase):
def __init__(self, queue):
ObserverBase.__init__(self)
self.queue = queue
def start(self):
# Create new thread to do the listening
self.thread = threading.Thread(target=self.listen, args=())
self.thread.start()
def join(self):
self.thread.join()
def listen(self):
while True:
# Listen for results on the queue and process them accordingly
print "Observer is waiting for data"
data = self.queue.get()
print "Observer received: ",data
dataType = type(data).__name__
if dataType=="bool" and data==True:
self.emit('finished')
return
if dataType=="bool" and data==False:
self.emit('errored')
return
elif dataType=="str":
self.emit('message_event', data)
elif dataType=="float":
self.emit('updated', data)
else:
raise RuntimeError("Queue had an unknown data type ('"+dataType+"') with value '"+str(data)+"'")
gobject.type_register(Observer)
class Process(gobject.GObject):
__gsignals__ = {
'starting': (gobject.SIGNAL_RUN_LAST,
gobject.TYPE_NONE,
()),
'joined': (gobject.SIGNAL_RUN_LAST,
gobject.TYPE_NONE,
())
}
def __init__(self, function, arguments):
gobject.GObject.__init__(self)
# Create queue
self.queue = multiprocessing.Queue()
# Create process
if arguments==(): arguments=(self.queue,)
self.process = multiprocessing.Process(target=function, args=arguments)
# Create observer
self.observer = Observer(self.queue)
def getObserver(self):
return self.observer
def start(self):
# Starting observer
self.emit("starting")
print "starting observer"
self.observer.start()
# Start process
print "starting process"
self.process.start()
def join(self):
# Clean up after process
print "cleaning up after process"
self.process.join()
self.emit("joined")
# Clean up after observer
print "cleaning up after observer"
self.queue.put(False) # If the false is ever read, it's an error
self.observer.join()
# Clean up queue
#self.queue.close()
gobject.type_register(Process)
class CombinedObserver(ObserverBase):
def __init__(self, observers, weights=None):
ObserverBase.__init__(self)
self.observers = observers
numObservers = len(observers)
if weights==None:
self.weights = [1 for i in range(numObservers)]
else:
self.weights = weights
sum_weights = sum(self.weights)
self.weights = [float(w)/sum_weights for w in self.weights]
print "weights = ",self.weights
index = 0
for observer in self.observers:
#observer.connect("message_event", onMessageReceived, index)
observer.connect("updated", self.onUpdate, index)
observer.connect("finished", self.onFinished, index)
observer.connect("errored", self.onErrored, index)
index = index+1
self.fractions = [0 for i in range(numObservers)]
self.finished = [False for i in range(numObservers)]
self.errored = [False for i in range(numObservers)]
self.lock = threading.RLock()
def onUpdate(self, widget, fraction, index):
self.lock.acquire()
self.fractions[index] = fraction * self.weights[index]
self.emit("updated", sum(self.fractions))
self.lock.release()
def onFinished(self, widget, index):
self.lock.acquire()
self.finished[index] = True
if all(self.finished):
self.emit("finished")
self.lock.release()
def onErrored(self, widget, index):
self.lock.acquire()
self.errored[index] = True
self.emit("errored")
self.lock.release()
gobject.type_register(CombinedObserver)
# A ProgressWorker is responsible for (a) getting the job done and (b)
# reporting progress. Progress is reported via the queue. Fractions
# (from 0 to 1) indicate the proportion of the job that is finished.
# Strings may indicate what has been done or what is about to be done.
# The boolean 'True' is placed on the queue when the job is finished.
class ProgressWorker():
def getResult(self):
return None
def setQueue(self, queue):
self.queue = queue
def go(self):
raise RuntimeError("Worker.go() has not been overridden")
class WorkerExample(ProgressWorker):
def __init__(self):
#Worker.__init__(self)
manager = multiprocessing.Manager()
self.result = manager.list()
def getResult(self):
return self.result
def go(self, queue):
self.setQueue(queue)
print "The worker has started doing some work (counting from 0 to 9)"
for i in range(10):
self.queue.put("working on step "+str(i)+"...")
proportion = (float(i)+1)/10
slp = random.uniform(0.50, 3.50)
time.sleep(slp)
self.queue.put(proportion)
self.queue.put("done.")
self.queue.put(1.0)
self.queue.put(True)
print "The worker has finished."
self.result.append("The the work has been finished and the result isn't 42.")
class ProgressBar(gobject.GObject):
__gsignals__ = {
'updated' : (gobject.SIGNAL_RUN_LAST,
gobject.TYPE_NONE,
(gobject.TYPE_FLOAT,)),
'finished': (gobject.SIGNAL_RUN_LAST,
gobject.TYPE_NONE,
())
}
def __init__(self, observer):
gobject.GObject.__init__(self)
self.observer = observer
self.observer.connect("updated",self.displayUpdate)
self.observer.connect("message_event",self.displayMessage)
self.observer.connect("finished",self.finish)
self.progress = gtk.ProgressBar()
self.progress.connect("destroy", self.destroy)
self.process=None
self.lock = threading.RLock()
def displayUpdate(self, obj, fraction):
self.lock.acquire()
self.progress.set_fraction(fraction)
self.emit("updated", fraction)
self.lock.release()
def displayMessage(self, obj, text):
self.lock.acquire()
self.progress.set_text(text)
self.lock.release()
def finish(self, obj, data=None):
self.emit("finished")
def destroy(self, widget, data=None):
print "Destroying"
if self.process==None:
return
self.queue.put("finished")
self.process.join()
self.thread.join()
gobject.type_register(ProgressBar)
class ProgressBarWindow:
def __init__(self, observer, title):
self.progressBar = ProgressBar(observer)
self.progressBar.connect("finished", self.done)
window = gtk.Window(gtk.WINDOW_TOPLEVEL)
window.set_border_width(10)
window.set_title(title)
window.add(self.progressBar.progress)
window.show_all()
window.connect("destroy", self.destroy)
def main(self):
gtk.main()
def done(self, widget, data=None):
#self.destroy()
print "FINISHED!"
def destroy(self, widget=None, data=None):
gtk.main_quit()
# Test
if __name__ == '__main__':
# n workers
worker = []
process = []
observer =[]
window = []
for i in range(10):
worker.append(WorkerExample())
process.append(Process(worker[i].go, ()))
observer.append(Process[i].getObserver())
combo = CombinedObserver(observer)
window0 = ProgressBarWindow(combo, "Workers' progress")
for i in range(10):
window.append(ProgressBarWindow(observer[i], "Workers {}'s progress".format(str(i))))
for i in range(10):
process[i].start()
gtk.main()
print "finished"
sys.exit()

View File

@@ -0,0 +1 @@
/data_2/Projects/h2inc/multiprocessing_test.py

View File

@@ -0,0 +1,314 @@
#!/usr/bin/env python3.5
# You are free to use and/or change this code for
# your own needs.
# Original code (c)2018 Jan Lerking
# Program to test various scenarios of
# single thread, multi thread, pool and process
import os
import sys
import multiprocessing as mp
import time
import io
import cpuinfo
import queue
import threading
import time
import random
import string
import shutil
from tkinter import Tk, ttk, Label, Button, LabelFrame
from tkinter import filedialog, Entry, Checkbutton
from tkinter import Grid, StringVar, DoubleVar
from tkinter import N, E, S, W
from tkinter import DISABLED, NORMAL
from os import errno
import os
from os.path import expanduser
import platform
class file_progress:
def __init__(self, master):
self.sourcedir = StringVar()
self.destdir = StringVar()
self.addinc = StringVar()
self.cfilevar = DoubleVar()
self.totalvar = 0
self.filecnt = 0
self.infofolder = 'Number of folders: 0'
self.infofile = 'Number of headers: 0'
self.currentfile = 'Current file: '
self.totprogress = 'Total progress: '
self.sourcedir.set('Select source directory!')
self.destdir.set('Select destination directory!')
self.master = master
self.master.title('File progress')
self.master.grid_columnconfigure(1, weight=1)
self.frame = LabelFrame(master, text='Progress')
self.frame.grid(row=0, column=0, columnspan=3, sticky=N+S+E+W, padx=5, pady=5)
self.frame.grid_columnconfigure(1, weight=1)
self.totallabel = Label(self.frame, text=self.totprogress)
self.totallabel.grid(row=8, column=0, sticky=W, padx=5, pady=5)
self.totallabel.config(state=DISABLED)
self.totalprogress = ttk.Progressbar(self.frame, orient='horizontal', mode='determinate')
self.totalprogress.grid(row=9, column=0, columnspan=3, sticky=N+S+E+W, padx=5, pady=5)
self.totalprogress.config(variable=self.totalvar, maximum=self.filecnt)
def select_sourcedir(self, sourcedir):
root.directory = os.path.abspath(filedialog.askdirectory())
if root.directory:
sourcedir.set(root.directory)
filecnt = sourcedir_filecnt(root.directory)
if filecnt > 0:
tempstr = 'Number of headers: '+str(filecnt)
temptot = 'Total progress: 0 of '+str(filecnt)
print ('Source directory: ', sourcedir.get())
self.destlabel.config(state=NORMAL)
self.destentry.config(state=NORMAL)
self.destdir_button.config(state=NORMAL)
self.infofiles.config(text=tempstr)
self.totallabel.config(text=temptot)
self.filecnt = filecnt
foldercnt = sourcedir_foldercnt(root.directory)
if foldercnt > 0:
tempstr = 'Number of folders: '+str(foldercnt)
self.infofolders.config(text=tempstr)
def select_destdir(self, destdir):
root.directory = filedialog.askdirectory()
if root.directory:
destdir.set(root.directory)
print ('Destination directory: ', destdir.get())
self.incchkbox.config(state=NORMAL)
self.infofolders.config(state=NORMAL)
self.infofiles.config(state=NORMAL)
self.translate_button.config(state=NORMAL)
self.cfilelabel.config(state=NORMAL)
self.totallabel.config(state=NORMAL)
def translate(self, destdir, sourcedir, addinc):
doinc = addinc.get()
dest = destdir.get()
source = sourcedir.get()
if doinc == 'yes':
dest = dest+'/include'
print(os.path.exists(os.path.dirname(dest)))
if not os.path.exists(os.path.dirname(dest)):
try:
os.makedirs(os.path.dirname(dest))
except OSError as exc: # Guard against race condition
if exc.errno != errno.EEXIST:
raise
destdir.set(dest)
print ('Destination directory: ', destdir.get())
process_files(source, dest)
def cfileprogress_update(cnt):
self.cfilevar = cnt
def currentfile_update(current):
self.currentfile = 'Current file: '+current
root = Tk()
root.update()
#root.minsize(350, 210)
#width = (root.winfo_screenwidth()/2)-(350/2)
#height = (root.winfo_screenheight()/2)-(210/2)
#root.geometry('+%d+%d' % (width, height))
root.resizable(False, False)
f_progress = file_progress(root)
root.mainloop()
cpu_info = cpuinfo.get_cpu_info()
num_cores = mp.cpu_count()
print(num_cores)
filelist = []
exitFlag = 0
queueLock = threading.Lock()
workQueue = queue.Queue(0)
threads = []
class myThread (threading.Thread):
def __init__(self, threadID, name, q):
threading.Thread.__init__(self)
self.threadID = threadID
self.name = name
self.q = q
def run(self):
print ("Starting " + self.name)
process_data(self.name, self.q)
print ("Exiting " + self.name)
def process_data(threadName, q):
while not exitFlag:
queueLock.acquire()
if not workQueue.empty():
data = q.get()
process_file(data)
queueLock.release()
print ("%s processing %s" % (threadName, data))
else:
queueLock.release()
time.sleep(1)
def sourcedir_filecnt(sourcedir):
### Return the number of files, ending with '.h', in sourcedir - including subdirectories ###
cnt = 0
global filelist
for folderName, subfolders, files in os.walk(sourcedir):
for file in files:
if file.lower().endswith('.h'):
cnt += 1
filelist += [folderName+'/'+file]
return cnt
def process_files(sourcedir, destdir):
global filelist
for f in filelist:
process_file(f)
def process_file(data):
outfile = ''
inputfile = data
encodings = ['utf-8', 'latin-1', 'windows-1250', 'windows-1252', 'ascii',
'big5', 'big5hkscs', 'cp037', 'cp273', 'cp424', 'cp437', 'cp500',
'cp720', 'cp737', 'cp775', 'cp850', 'cp852', 'cp855', 'cp856',
'cp857', 'cp858', 'cp860', 'cp861', 'cp862', 'cp863', 'cp864', 'cp865',
'cp866', 'cp869', 'cp874', 'cp875', 'cp932', 'cp949', 'cp950', 'cp1006',
'cp1026', 'cp1125', 'cp1140', 'cp1250', 'cp1251', 'cp1252', 'cp1253', 'cp1254',
'cp1255', 'cp1256', 'cp1257', 'cp1258', 'cp65001', 'euc-jp', 'euc-jis-2004',
'euc-jisx0213', 'euc-kr', 'gb2312', 'gbk', 'gb18030', 'hz', 'iso2022-jp',
'iso2022-jp-1', 'iso2022-jp-2', 'iso2022-jp-2004', 'iso2022-jp-3',
'iso2022-jp-ext', 'iso2022-kr', 'iso8859-2', 'iso8859-3', 'iso8859-4',
'iso8859-5', 'iso8859-6', 'iso8859-7', 'iso8859-8', 'iso8859-9', 'iso8859-10',
'iso8859-11', 'iso8859-13', 'iso8859-14', 'iso8859-15', 'iso8859-16', 'johab',
'koi8-r', 'koi8-t', 'koi8-u', 'kz1048', 'mac-cyrillic', 'mac-greek',
'mac-iceland', 'mac-latin2', 'mac-roman', 'mac-turkish', 'ptcp154',
'shift-jis', 'shift-jis-2004', 'shift-jisx0213', 'utf-32', 'utf-32-be',
'utf-32-le', 'utf-16', 'utf-16-be', 'utf-16-le', 'utf-7', 'utf-8-sig']
for e in encodings:
try:
fh = io.open(data, 'r', encoding=e)
fh.readlines()
fh.seek(0)
except UnicodeDecodeError:
print('got unicode error with %s , trying different encoding' % e)
else:
#print('opening the file with encoding: %s ' % e)
break
#print(os.path.basename(data))
for lines in fh:
outfile = outfile+lines
fh.close()
outputfile = os.path.splitext(inputfile)[0]+'.inc'
outputfile = str(outputfile).replace(sourcedir, destdir)
#print(outputfile)
if not os.path.exists(os.path.dirname(outputfile)):
try:
os.makedirs(os.path.dirname(outputfile))
except OSError as exc: # Guard against race condition
if exc.errno != errno.EEXIST:
raise
newfile = open(outputfile, "w")
newfile.write(outfile)
newfile.close()
def single_thread():
t1 = time.time()
cnt = sourcedir_filecnt(sourcedir)
print('Files in: '+str(cnt))
process_files(sourcedir, destdir)
print('Single thread process time: '+str(time.time()-t1))
def multi_thread():
global filelist
global exitFlag
t1 = time.time()
cnt = sourcedir_filecnt(sourcedir)
threadList = ["thread 1", "thread 2", "thread 3", "thread 4", "thread 5"]
nameList = filelist
threadID = 0
# Create new threads
for tName in threadList:
thread = myThread(threadID, tName, workQueue)
thread.start()
threads.append(thread)
threadID += 1
# Fill the queue
queueLock.acquire()
for word in nameList:
workQueue.put(word)
queueLock.release()
# Wait for queue to empty
while not workQueue.empty():
pass
# Notify threads it's time to exit
exitFlag = 1
# Wait for all threads to complete
for t in threads:
t.join()
print ("Exiting Main Thread")
print('Files in: '+str(cnt))
print('Multi thread process time: '+str(time.time()-t1))
def async_process(num):
t1 = time.time()
cnt = sourcedir_filecnt(sourcedir)
pool = mp.Pool(processes=num)
#[pool.apply_async(process_file, args=(f,)) for f in filelist]
pool.map(process_file, filelist)
#output = [p.get() for p in results]
#print(output)
# Setup a list of processes that we want to run
#processes = [mp.Process(target=rand_string, args=(5, output)) for x in range(8)]
#processes = [mp.Process(target=process_files, args=(sourcedir, destdir)) for x in range(num)]
# Run processes
#for p in processes:
#p.start()
# Exit the completed processes
#for p in processes:
#p.join()
# Get process results from the output queue
#results = [output.get() for p in processes]
#print(results)
print('Files in: '+str(cnt))
print('Multi ('+str(num)+') processes time: '+str(time.time()-t1))
def cleanup(dest):
shutil.rmtree(dest)
#sourcedir = 'C:/Users/dksojlg/Documents/gtk+-3.22.26'
sourcedir = '/usr/include'
#destdir = 'C:/Users/dksojlg/Documents/include'
destdir = '/data_2/include'
#print(cpu_info)
#single_thread() #2543 files - Single thread process time: 1.3732633590698242 sec.
#os.rmdir(destdir)
#multi_thread() #2543 files - Syncronized 5 threads process time: 80.25179100036621 sec.
#os.rmdir(destdir)
async_process(16) #2543 files - 2 processes, process time: 1.2379400730133057 sec.
#cleanup(destdir)
#async_process(4) #2543 files - 2 processes, process time: 2.6622860431671143 sec.
#cleanup(destdir)
#async_process(6) #2543 files - 2 processes, process time: 2.6622860431671143 sec.
#cleanup(destdir)
#async_process(8) #2543 files - 2 processes, process time: 2.6622860431671143 sec.
#cleanup(destdir)

View File

@@ -0,0 +1,20 @@
from multiprocessing import Pool
import time
work = (["A", 5], ["B", 2], ["C", 1], ["D", 3])
def work_log(work_data):
print(" Process %s waiting %s seconds" % (work_data[0], work_data[1]))
time.sleep(int(work_data[1]))
print(" Process %s Finished." % work_data[0])
def pool_handler():
p = Pool(2)
p.map(work_log, work)
if __name__ == '__main__':
pool_handler()

View File

@@ -0,0 +1,57 @@
from multiprocessing import Process, Value, Lock
from tkinter import Tk
from tkinter import Tk, ttk, Label, Button, LabelFrame
from tkinter import Grid, StringVar, DoubleVar
from tkinter import N, E, S, W
from tkinter import DISABLED, NORMAL
import os
import sys
dirname = 'C:\\Users\\dksojlg\\Documents'
platform = sys.platform
print(platform)
class testGUI:
def __init__(self, master, initval=0):
self.progress = Value('i', initval)
self.dircount = Value('i', initval)
self.foldercnt = 'Folders: {}'.format(self.dircount.value)
self.lock = Lock()
self.master = master
self.master.title('Multiprocess progressbar test!')
self.master.grid_columnconfigure(1, weight=1)
self.frame = LabelFrame(master, text='Folder count')
self.frame.grid(row=0, column=0, columnspan=3, sticky=N+S+E+W, padx=5, pady=5)
self.frame.grid_columnconfigure(1, weight=1)
self.sourcelabel = Label(self.frame, text=self.foldercnt)
self.sourcelabel.grid(row=0, column=0, sticky=E, padx=(5, 1), pady=5)
self.sourcedir_button = Button(self.frame, text="Count folders...", command= lambda: get_directories(self))
self.sourcedir_button.grid(row=0, column=2, sticky=W, padx=(3, 5), pady=5)
def increment_dir(self):
with self.lock:
self.dircount.value += 1
self.sourcelabel.config(text=self.foldercnt)
def read_dir(testgui, dirname):
for folderName, subfolders, files in os.walk(dirname):
if subfolders:
for subfolder in subfolders:
read_dir(testgui, subfolder)
#testgui.increment_dir()
def get_directories(testgui):
p = Process(target=read_dir, args=(testgui, dirname,))
p.start()
if __name__ == "__main__":
root = Tk()
root.update()
root.resizable(False, False)
testgui = testGUI(root)
root.mainloop()

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

50
concurrency-futures.py Normal file
View File

@@ -0,0 +1,50 @@
import concurrent.futures
import time
import multiprocessing
num_cores = multiprocessing.cpu_count()
def is_prime(num):
if num <= 1:
return False
elif num <= 3:
return True
elif num%2 == 0 or num%3 == 0:
return False
i = 5
while i*i <= num:
if num%i == 0 or num%(i+2) == 0:
return False
i += 6
return True
def find_sum(num):
sum_of_primes = 0
ix = 2
while ix <= num:
if is_prime(ix):
sum_of_primes += ix
ix += 1
print("{} : Sum = {}".format(num, sum_of_primes))
return sum_of_primes
def sum_primes_thread(nums):
with concurrent.futures.ThreadPoolExecutor(max_workers = num_cores) as executor:
executor.map(find_sum, nums)
def sum_primes_process(nums):
with concurrent.futures.ProcessPoolExecutor(max_workers = num_cores) as executor:
executor.map(find_sum, nums)
if __name__ == '__main__':
nums = [1000000, 900000, 800000, 700000, 600000, 500000, 400000, 300000, 200000, 100000]
print("Using multithreading (num_threads = num_cores):")
start = time.time()
sum_primes_thread(nums)
print("Time taken = {0:.5f}".format(time.time() - start))
print("Using multiprocessing (num_processes = num_cores):")
start = time.time()
sum_primes_process(nums)
print("Time taken = {0:.5f}".format(time.time() - start))

1
h2inc Symbolic link
View File

@@ -0,0 +1 @@
/data_2/Projects/h2inc/h2inc.py

367
h2inc.glade Normal file
View File

@@ -0,0 +1,367 @@
<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.18.3 -->
<interface>
<requires lib="gtk+" version="3.12"/>
<object class="GtkWindow" id="window1">
<property name="can_focus">False</property>
<property name="margin_left">5</property>
<property name="margin_right">5</property>
<property name="margin_top">5</property>
<property name="margin_bottom">5</property>
<child>
<object class="GtkGrid" id="grid1">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="margin_left">5</property>
<property name="margin_right">5</property>
<property name="margin_top">5</property>
<property name="margin_bottom">5</property>
<property name="hexpand">True</property>
<property name="vexpand">True</property>
<child>
<object class="GtkFrame" id="sourceframe">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label_xalign">0</property>
<property name="shadow_type">etched-out</property>
<child>
<object class="GtkAlignment" id="alignment1">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="left_padding">12</property>
<child>
<object class="GtkGrid" id="grid2">
<property name="visible">True</property>
<property name="can_focus">False</property>
<child>
<object class="GtkLabel" id="label2">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="halign">end</property>
<property name="margin_right">5</property>
<property name="margin_top">5</property>
<property name="margin_bottom">5</property>
<property name="hexpand">True</property>
<property name="label" translatable="yes">Source:</property>
<property name="justify">right</property>
</object>
<packing>
<property name="left_attach">0</property>
<property name="top_attach">0</property>
<property name="width">2</property>
</packing>
</child>
<child>
<object class="GtkEntry" id="entry1">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="margin_left">5</property>
<property name="margin_right">5</property>
<property name="margin_top">5</property>
<property name="margin_bottom">5</property>
<property name="text" translatable="yes">Select source directory!</property>
</object>
<packing>
<property name="left_attach">2</property>
<property name="top_attach">0</property>
<property name="width">2</property>
</packing>
</child>
<child>
<object class="GtkButton" id="button1">
<property name="label" translatable="yes">Source directory...</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<property name="halign">start</property>
<property name="margin_left">5</property>
<property name="margin_right">5</property>
<property name="margin_top">5</property>
<property name="margin_bottom">5</property>
</object>
<packing>
<property name="left_attach">4</property>
<property name="top_attach">0</property>
<property name="width">2</property>
</packing>
</child>
<child>
<object class="GtkLabel" id="label3">
<property name="visible">True</property>
<property name="sensitive">False</property>
<property name="can_focus">False</property>
<property name="halign">end</property>
<property name="margin_right">5</property>
<property name="margin_top">5</property>
<property name="margin_bottom">5</property>
<property name="hexpand">True</property>
<property name="label" translatable="yes">Destination:</property>
<property name="justify">right</property>
<property name="ellipsize">start</property>
<property name="angle">2.2351741811588166e-10</property>
</object>
<packing>
<property name="left_attach">0</property>
<property name="top_attach">1</property>
<property name="width">2</property>
</packing>
</child>
<child>
<object class="GtkEntry" id="entry2">
<property name="visible">True</property>
<property name="sensitive">False</property>
<property name="can_focus">True</property>
<property name="margin_left">5</property>
<property name="margin_right">5</property>
<property name="margin_top">5</property>
<property name="margin_bottom">5</property>
<property name="text" translatable="yes">Select destination directory!</property>
</object>
<packing>
<property name="left_attach">2</property>
<property name="top_attach">1</property>
<property name="width">2</property>
</packing>
</child>
<child>
<object class="GtkButton" id="button2">
<property name="label" translatable="yes">Destination directory...</property>
<property name="visible">True</property>
<property name="sensitive">False</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<property name="halign">start</property>
<property name="margin_left">5</property>
<property name="margin_right">5</property>
<property name="margin_top">5</property>
<property name="margin_bottom">5</property>
</object>
<packing>
<property name="left_attach">4</property>
<property name="top_attach">1</property>
<property name="width">2</property>
</packing>
</child>
<child>
<object class="GtkCheckButton" id="checkbutton1">
<property name="label" translatable="yes">Create "include" folder if it does not exist.</property>
<property name="visible">True</property>
<property name="sensitive">False</property>
<property name="can_focus">True</property>
<property name="receives_default">False</property>
<property name="margin_top">5</property>
<property name="margin_bottom">5</property>
<property name="xalign">0</property>
<property name="draw_indicator">True</property>
</object>
<packing>
<property name="left_attach">0</property>
<property name="top_attach">2</property>
<property name="width">6</property>
</packing>
</child>
</object>
</child>
</object>
</child>
<child type="label">
<object class="GtkLabel" id="label1">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">Select folders</property>
</object>
</child>
</object>
<packing>
<property name="left_attach">0</property>
<property name="top_attach">0</property>
<property name="width">5</property>
</packing>
</child>
<child>
<object class="GtkFrame" id="frame1">
<property name="visible">True</property>
<property name="sensitive">False</property>
<property name="can_focus">False</property>
<property name="margin_top">5</property>
<property name="margin_bottom">5</property>
<property name="label_xalign">0</property>
<property name="shadow_type">etched-out</property>
<child>
<object class="GtkAlignment" id="alignment2">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="left_padding">12</property>
<child>
<object class="GtkGrid" id="grid3">
<property name="visible">True</property>
<property name="can_focus">False</property>
<child>
<object class="GtkFrame" id="frame2">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="margin_right">5</property>
<property name="margin_bottom">5</property>
<property name="hexpand">True</property>
<property name="label_xalign">0</property>
<property name="shadow_type">etched-out</property>
<child>
<object class="GtkAlignment" id="alignment3">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="left_padding">12</property>
<child>
<object class="GtkBox" id="box1">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="orientation">vertical</property>
<child>
<object class="GtkLabel" id="label6">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="halign">start</property>
<property name="margin_top">5</property>
<property name="margin_bottom">5</property>
<property name="hexpand">True</property>
<property name="label" translatable="yes">Number of folders: 0</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkLabel" id="label7">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="halign">start</property>
<property name="margin_top">5</property>
<property name="margin_bottom">5</property>
<property name="label" translatable="yes">Number of files: 0</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
</object>
</child>
</object>
</child>
<child type="label">
<object class="GtkLabel" id="label5">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">Source information</property>
</object>
</child>
</object>
<packing>
<property name="left_attach">0</property>
<property name="top_attach">0</property>
</packing>
</child>
<child>
<object class="GtkButton" id="button3">
<property name="label" translatable="yes">Translate!</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<property name="halign">start</property>
<property name="margin_top">5</property>
<property name="margin_bottom">5</property>
</object>
<packing>
<property name="left_attach">0</property>
<property name="top_attach">1</property>
</packing>
</child>
<child>
<object class="GtkFrame" id="frame3">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="margin_right">5</property>
<property name="margin_top">5</property>
<property name="margin_bottom">5</property>
<property name="label_xalign">0</property>
<property name="shadow_type">etched-out</property>
<child>
<object class="GtkAlignment" id="alignment4">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="left_padding">12</property>
<child>
<object class="GtkBox" id="box2">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="orientation">vertical</property>
<child>
<object class="GtkLabel" id="label9">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="halign">start</property>
<property name="margin_top">5</property>
<property name="margin_bottom">5</property>
<property name="label" translatable="yes">Total progress:</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkProgressBar" id="progressbar1">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="margin_right">5</property>
<property name="margin_top">5</property>
<property name="margin_bottom">5</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
</object>
</child>
</object>
</child>
<child type="label">
<object class="GtkLabel" id="label8">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">Progress</property>
</object>
</child>
</object>
<packing>
<property name="left_attach">0</property>
<property name="top_attach">2</property>
</packing>
</child>
</object>
</child>
</object>
</child>
<child type="label">
<object class="GtkLabel" id="label4">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">Translation</property>
</object>
</child>
</object>
<packing>
<property name="left_attach">0</property>
<property name="top_attach">1</property>
<property name="width">5</property>
</packing>
</child>
</object>
</child>
</object>
</interface>

365
h2inc.glade~ Normal file
View File

@@ -0,0 +1,365 @@
<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.18.3 -->
<interface>
<requires lib="gtk+" version="3.12"/>
<object class="GtkWindow" id="window1">
<property name="can_focus">False</property>
<property name="margin_left">5</property>
<property name="margin_right">5</property>
<property name="margin_top">5</property>
<property name="margin_bottom">5</property>
<child>
<object class="GtkGrid" id="grid1">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="margin_left">5</property>
<property name="margin_right">5</property>
<property name="margin_top">5</property>
<property name="margin_bottom">5</property>
<child>
<object class="GtkFrame" id="sourceframe">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label_xalign">0</property>
<property name="shadow_type">etched-out</property>
<child>
<object class="GtkAlignment" id="alignment1">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="left_padding">12</property>
<child>
<object class="GtkGrid" id="grid2">
<property name="visible">True</property>
<property name="can_focus">False</property>
<child>
<object class="GtkLabel" id="label2">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="halign">end</property>
<property name="margin_right">5</property>
<property name="margin_top">5</property>
<property name="margin_bottom">5</property>
<property name="hexpand">True</property>
<property name="label" translatable="yes">Source:</property>
<property name="justify">right</property>
</object>
<packing>
<property name="left_attach">0</property>
<property name="top_attach">0</property>
<property name="width">2</property>
</packing>
</child>
<child>
<object class="GtkEntry" id="entry1">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="margin_left">5</property>
<property name="margin_right">5</property>
<property name="margin_top">5</property>
<property name="margin_bottom">5</property>
<property name="text" translatable="yes">Select source directory!</property>
</object>
<packing>
<property name="left_attach">2</property>
<property name="top_attach">0</property>
<property name="width">2</property>
</packing>
</child>
<child>
<object class="GtkButton" id="button1">
<property name="label" translatable="yes">Source directory...</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<property name="halign">start</property>
<property name="margin_left">5</property>
<property name="margin_right">5</property>
<property name="margin_top">5</property>
<property name="margin_bottom">5</property>
</object>
<packing>
<property name="left_attach">4</property>
<property name="top_attach">0</property>
<property name="width">2</property>
</packing>
</child>
<child>
<object class="GtkLabel" id="label3">
<property name="visible">True</property>
<property name="sensitive">False</property>
<property name="can_focus">False</property>
<property name="halign">end</property>
<property name="margin_right">5</property>
<property name="margin_top">5</property>
<property name="margin_bottom">5</property>
<property name="hexpand">True</property>
<property name="label" translatable="yes">Destination:</property>
<property name="justify">right</property>
<property name="ellipsize">start</property>
<property name="angle">2.2351741811588166e-10</property>
</object>
<packing>
<property name="left_attach">0</property>
<property name="top_attach">1</property>
<property name="width">2</property>
</packing>
</child>
<child>
<object class="GtkEntry" id="entry2">
<property name="visible">True</property>
<property name="sensitive">False</property>
<property name="can_focus">True</property>
<property name="margin_left">5</property>
<property name="margin_right">5</property>
<property name="margin_top">5</property>
<property name="margin_bottom">5</property>
<property name="text" translatable="yes">Select destination directory!</property>
</object>
<packing>
<property name="left_attach">2</property>
<property name="top_attach">1</property>
<property name="width">2</property>
</packing>
</child>
<child>
<object class="GtkButton" id="button2">
<property name="label" translatable="yes">Destination directory...</property>
<property name="visible">True</property>
<property name="sensitive">False</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<property name="halign">start</property>
<property name="margin_left">5</property>
<property name="margin_right">5</property>
<property name="margin_top">5</property>
<property name="margin_bottom">5</property>
</object>
<packing>
<property name="left_attach">4</property>
<property name="top_attach">1</property>
<property name="width">2</property>
</packing>
</child>
<child>
<object class="GtkCheckButton" id="checkbutton1">
<property name="label" translatable="yes">Create "include" folder if it does not exist.</property>
<property name="visible">True</property>
<property name="sensitive">False</property>
<property name="can_focus">True</property>
<property name="receives_default">False</property>
<property name="margin_top">5</property>
<property name="margin_bottom">5</property>
<property name="xalign">0</property>
<property name="draw_indicator">True</property>
</object>
<packing>
<property name="left_attach">0</property>
<property name="top_attach">2</property>
<property name="width">6</property>
</packing>
</child>
</object>
</child>
</object>
</child>
<child type="label">
<object class="GtkLabel" id="label1">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">Select folders</property>
</object>
</child>
</object>
<packing>
<property name="left_attach">0</property>
<property name="top_attach">0</property>
<property name="width">5</property>
</packing>
</child>
<child>
<object class="GtkFrame" id="frame1">
<property name="visible">True</property>
<property name="sensitive">False</property>
<property name="can_focus">False</property>
<property name="margin_top">5</property>
<property name="margin_bottom">5</property>
<property name="label_xalign">0</property>
<property name="shadow_type">etched-out</property>
<child>
<object class="GtkAlignment" id="alignment2">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="left_padding">12</property>
<child>
<object class="GtkGrid" id="grid3">
<property name="visible">True</property>
<property name="can_focus">False</property>
<child>
<object class="GtkFrame" id="frame2">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="margin_right">5</property>
<property name="margin_bottom">5</property>
<property name="hexpand">True</property>
<property name="label_xalign">0</property>
<property name="shadow_type">etched-out</property>
<child>
<object class="GtkAlignment" id="alignment3">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="left_padding">12</property>
<child>
<object class="GtkBox" id="box1">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="orientation">vertical</property>
<child>
<object class="GtkLabel" id="label6">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="halign">start</property>
<property name="margin_top">5</property>
<property name="margin_bottom">5</property>
<property name="hexpand">True</property>
<property name="label" translatable="yes">Number of folders: 0</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkLabel" id="label7">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="halign">start</property>
<property name="margin_top">5</property>
<property name="margin_bottom">5</property>
<property name="label" translatable="yes">Number of files: 0</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
</object>
</child>
</object>
</child>
<child type="label">
<object class="GtkLabel" id="label5">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">Source information</property>
</object>
</child>
</object>
<packing>
<property name="left_attach">0</property>
<property name="top_attach">0</property>
</packing>
</child>
<child>
<object class="GtkButton" id="button3">
<property name="label" translatable="yes">Translate!</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<property name="halign">start</property>
<property name="margin_top">5</property>
<property name="margin_bottom">5</property>
</object>
<packing>
<property name="left_attach">0</property>
<property name="top_attach">1</property>
</packing>
</child>
<child>
<object class="GtkFrame" id="frame3">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="margin_right">5</property>
<property name="margin_top">5</property>
<property name="margin_bottom">5</property>
<property name="label_xalign">0</property>
<property name="shadow_type">etched-out</property>
<child>
<object class="GtkAlignment" id="alignment4">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="left_padding">12</property>
<child>
<object class="GtkBox" id="box2">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="orientation">vertical</property>
<child>
<object class="GtkLabel" id="label9">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="halign">start</property>
<property name="margin_top">5</property>
<property name="margin_bottom">5</property>
<property name="label" translatable="yes">Total progress:</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkProgressBar" id="progressbar1">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="margin_right">5</property>
<property name="margin_top">5</property>
<property name="margin_bottom">5</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
</object>
</child>
</object>
</child>
<child type="label">
<object class="GtkLabel" id="label8">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">Progress</property>
</object>
</child>
</object>
<packing>
<property name="left_attach">0</property>
<property name="top_attach">2</property>
</packing>
</child>
</object>
</child>
</object>
</child>
<child type="label">
<object class="GtkLabel" id="label4">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">Translation</property>
</object>
</child>
</object>
<packing>
<property name="left_attach">0</property>
<property name="top_attach">1</property>
<property name="width">5</property>
</packing>
</child>
</object>
</child>
</object>
</interface>

199
h2inc.py Normal file → Executable file
View File

@@ -1,87 +1,112 @@
# You are free to use and/or change this code for
# your own needs.
# Original code (c)2018 Jan Lerking
# Program to convert C-header (*.h) files to nasm include files (*.inc),
# for direct usage in assembly programming using nasm/yasm.
import os
import sys
from h2inc_parser import parseline, parseparsed
import h2inc_gui
tupline = []
preproc = ()
filelist = []
folderlist = []
cnt = 0
def sourcedir_filecnt(sourcedir):
### Return the number of files, ending with '.h', in sourcedir - including subdirectories ###
cnt = 0
global filelist
for folderName, subfolders, files in os.walk(sourcedir):
for file in files:
if file.lower().endswith('.h'):
cnt += 1
filelist += [folderName+'/'+file]
print(folderName+'/'+file)
#print(filelist)
return cnt
def sourcedir_foldercnt(sourcedir):
### Return the number of folders, if it contains '*.h' files, in sourcedir - including subdirectories ###
global cnt
global folderlist
for folderName, subfolders, files in os.walk(sourcedir):
if subfolders:
for subfolder in subfolders:
sourcedir_foldercnt(subfolder)
tempf = [file for file in files if file.lower().endswith('.h')]
if tempf:
cnt = cnt+1
#print(folderName)
folderlist += [folderName]
#print(folderlist)
#print(len(folderlist))
return cnt
def process_files(sourcedir, destdir, filevar):
#global folderlist
global filelist
global preproc
outfile = ''
cnt = 0
for f in filelist:
current_cnt = 0
inputfile = f
filehandle = open(f, 'r')
current_cnt += 1
filevar = current_cnt
print(os.path.basename(f))
currentfile_update(os.path.basename(f))
for lines in filehandle:
print(lines)
preproc = preproc+tuple([parseline(lines)])
filehandle.close()
current_cnt += 1
filevar = current_cnt
for elements in preproc:
outfile = outfile+parseparsed(elements)
outputfile = os.path.splitext(inputfile)[0]+'.inc'
outputfile = str(outputfile).replace(str(sourcedir.get()), str(destdir.get()))
current_cnt += 1
filevar = current_cnt
print(outputfile)
if not os.path.exists(os.path.dirname(outputfile)):
try:
os.makedirs(os.path.dirname(outputfile))
except OSError as exc: # Guard against race condition
if exc.errno != errno.EEXIST:
raise
newfile = open(outputfile, "w")
newfile.write(outfile)
newfile.close()
current_cnt += 1
filevar = current_cnt
cnt += 1
#!/usr/bin/env python3.5
# You are free to use and/or change this code for
# your own needs.
# Original code (c)2018 Jan Lerking
# Program to convert C-header (*.h) files to nasm include files (*.inc),
# for direct usage in assembly programming using nasm/yasm.
import os
import sys
import multiprocessing as mp
import io
from h2inc_parser import parseline, parseparsed
import h2inc_gui
tupline = []
preproc = ()
filelist = []
folderlist = []
cnt = 0
sourcedir = ''
destdir = ''
num_cores = mp.cpu_count()
def sourcedir_filecnt(sourcedir):
### Return the number of files, ending with '.h', in sourcedir - including subdirectories ###
cnt = 0
global filelist
for folderName, subfolders, files in os.walk(sourcedir):
for file in files:
if file.lower().endswith('.h'):
cnt += 1
filelist += [folderName+'/'+file]
print(folderName+'/'+file)
#print(filelist)
return cnt
def sourcedir_foldercnt(sourcedir):
### Return the number of folders, if it contains '*.h' files, in sourcedir - including subdirectories ###
global cnt
global folderlist
for folderName, subfolders, files in os.walk(sourcedir):
if subfolders:
for subfolder in subfolders:
sourcedir_foldercnt(subfolder)
tempf = [file for file in files if file.lower().endswith('.h')]
if tempf:
cnt = cnt+1
#print(folderName)
folderlist += [folderName]
#print(folderlist)
#print(len(folderlist))
return cnt
def process_files(gui, source, dest):
global sourcedir
global destdir
sourcedir = source
destdir = dest
pool = mp.Pool(processes=num_cores)
pool.map(process_file, filelist)
def process_file(data):
outfile = ''
inputfile = data
encodings = ['utf-8', 'latin-1', 'windows-1250', 'windows-1252', 'ascii',
'big5', 'big5hkscs', 'cp037', 'cp273', 'cp424', 'cp437', 'cp500',
'cp720', 'cp737', 'cp775', 'cp850', 'cp852', 'cp855', 'cp856',
'cp857', 'cp858', 'cp860', 'cp861', 'cp862', 'cp863', 'cp864', 'cp865',
'cp866', 'cp869', 'cp874', 'cp875', 'cp932', 'cp949', 'cp950', 'cp1006',
'cp1026', 'cp1125', 'cp1140', 'cp1250', 'cp1251', 'cp1252', 'cp1253', 'cp1254',
'cp1255', 'cp1256', 'cp1257', 'cp1258', 'cp65001', 'euc-jp', 'euc-jis-2004',
'euc-jisx0213', 'euc-kr', 'gb2312', 'gbk', 'gb18030', 'hz', 'iso2022-jp',
'iso2022-jp-1', 'iso2022-jp-2', 'iso2022-jp-2004', 'iso2022-jp-3',
'iso2022-jp-ext', 'iso2022-kr', 'iso8859-2', 'iso8859-3', 'iso8859-4',
'iso8859-5', 'iso8859-6', 'iso8859-7', 'iso8859-8', 'iso8859-9', 'iso8859-10',
'iso8859-11', 'iso8859-13', 'iso8859-14', 'iso8859-15', 'iso8859-16', 'johab',
'koi8-r', 'koi8-t', 'koi8-u', 'kz1048', 'mac-cyrillic', 'mac-greek',
'mac-iceland', 'mac-latin2', 'mac-roman', 'mac-turkish', 'ptcp154',
'shift-jis', 'shift-jis-2004', 'shift-jisx0213', 'utf-32', 'utf-32-be',
'utf-32-le', 'utf-16', 'utf-16-be', 'utf-16-le', 'utf-7', 'utf-8-sig']
for e in encodings:
try:
fh = io.open(data, 'r', encoding=e)
fh.readlines()
fh.seek(0)
except UnicodeDecodeError:
print('got unicode error with %s , trying different encoding' % e)
else:
#print('opening the file with encoding: %s ' % e)
break
#print(os.path.basename(data))
for lines in fh:
outfile = outfile+lines
fh.close()
outputfile = os.path.splitext(inputfile)[0]+'.inc'
outputfile = str(outputfile).replace(sourcedir, destdir)
#print(outputfile)
if not os.path.exists(os.path.dirname(outputfile)):
try:
os.makedirs(os.path.dirname(outputfile))
except OSError as exc: # Guard against race condition
if exc.errno != errno.EEXIST:
raise
newfile = open(outputfile, "w")
newfile.write(outfile)
newfile.close()
def async_process(num):
pool = mp.Pool(processes=num)
pool.map(process_file, filelist)

View File

@@ -27,7 +27,7 @@ class h2incGUI:
self.filecnt = 0
self.infofolder = 'Number of folders: 0'
self.infofile = 'Number of headers: 0'
self.currentfile = 'Current file: '
#self.currentfile = 'Current file: '
self.totprogress = 'Total progress: '
self.sourcedir.set('Select source directory!')
self.destdir.set('Select destination directory!')
@@ -91,13 +91,13 @@ class h2incGUI:
self.progressframe.grid(row=4, rowspan=2, column=0, columnspan=3, sticky=N+S+E+W, padx=5, pady=5)
self.progressframe.grid_columnconfigure(1, weight=1)
self.cfilelabel = Label(self.progressframe, text=self.currentfile)
self.cfilelabel.grid(row=6, column=0, sticky=W, padx=5, pady=5)
self.cfilelabel.config(state=DISABLED)
#self.cfilelabel = Label(self.progressframe, text=self.currentfile)
#self.cfilelabel.grid(row=6, column=0, sticky=W, padx=5, pady=5)
#self.cfilelabel.config(state=DISABLED)
self.cfileprogress = ttk.Progressbar(self.progressframe, orient='horizontal', mode='determinate')
self.cfileprogress.grid(row=7, column=0, columnspan=3, sticky=N+S+E+W, padx=5, pady=5)
self.cfileprogress.config(variable=self.cfilevar, maximum=3)
#self.cfileprogress = ttk.Progressbar(self.progressframe, orient='horizontal', mode='determinate')
#self.cfileprogress.grid(row=7, column=0, columnspan=3, sticky=N+S+E+W, padx=5, pady=5)
#self.cfileprogress.config(variable=self.cfilevar, maximum=3)
self.totallabel = Label(self.progressframe, text=self.totprogress)
self.totallabel.grid(row=8, column=0, sticky=W, padx=5, pady=5)
@@ -136,12 +136,13 @@ class h2incGUI:
self.infofolders.config(state=NORMAL)
self.infofiles.config(state=NORMAL)
self.translate_button.config(state=NORMAL)
self.cfilelabel.config(state=NORMAL)
#self.cfilelabel.config(state=NORMAL)
self.totallabel.config(state=NORMAL)
def translate(self, destdir, sourcedir, addinc):
doinc = addinc.get()
dest = destdir.get()
source = sourcedir.get()
if doinc == 'yes':
dest = dest+'/include'
print(os.path.exists(os.path.dirname(dest)))
@@ -153,7 +154,7 @@ class h2incGUI:
raise
destdir.set(dest)
print ('Destination directory: ', destdir.get())
process_files(sourcedir, destdir, self.cfilevar)
process_files(self, source, dest)
def cfileprogress_update(cnt):
self.cfilevar = cnt

Binary file not shown.

Binary file not shown.

Binary file not shown.

173
old_h2inc/h2inc_gui_old.py Normal file
View File

@@ -0,0 +1,173 @@
# You are free to use and/or change this code for
# your own needs.
# Original code (c)2018 Jan Lerking
# Program to convert C-header (*.h) files to nasm include files (*.inc),
# for direct usage in assembly programming using nasm/yasm.
from tkinter import Tk, ttk, Label, Button, LabelFrame
from tkinter import filedialog, Entry, Checkbutton
from tkinter import Grid, StringVar, DoubleVar
from tkinter import N, E, S, W
from tkinter import DISABLED, NORMAL
from os import errno
import os
from os.path import expanduser
import platform
import sys
from h2inc_old import sourcedir_filecnt, sourcedir_foldercnt, process_files
class h2incGUI:
def __init__(self, master):
self.sourcedir = StringVar()
self.destdir = StringVar()
self.addinc = StringVar()
self.cfilevar = DoubleVar()
self.totalvar = 0
self.filecnt = 0
self.infofolder = 'Number of folders: 0'
self.infofile = 'Number of headers: 0'
#self.currentfile = 'Current file: '
self.totprogress = 'Total progress: '
self.sourcedir.set('Select source directory!')
self.destdir.set('Select destination directory!')
self.master = master
self.master.title('Translate C-header files to Nasm include files!')
self.master.grid_columnconfigure(1, weight=1)
self.frame = LabelFrame(master, text='Select folders')
self.frame.grid(row=0, column=0, columnspan=3, sticky=N+S+E+W, padx=5, pady=5)
self.frame.grid_columnconfigure(1, weight=1)
self.sourcelabel = Label(self.frame, text='Source: ')
self.sourcelabel.grid(row=0, column=0, sticky=E, padx=(5, 1), pady=5)
self.sourceentry = Entry(self.frame, textvariable=self.sourcedir)
self.sourceentry.grid(row=0, column=1, sticky=E+W, pady=5)
self.sourcedir_button = Button(self.frame, text="Source directory...", command= lambda: self.select_sourcedir(self.sourcedir))
self.sourcedir_button.grid(row=0, column=2, sticky=W, padx=(3, 5), pady=5)
self.destlabel = Label(self.frame, text='Destination: ')
self.destlabel.grid(row=1, column=0, sticky=E, padx=(5, 1), pady=5)
self.destlabel.config(state=DISABLED)
self.destentry = Entry(self.frame, textvariable=self.destdir)
self.destentry.grid(row=1, column=1, sticky=E+W, pady=5)
self.destentry.config(state=DISABLED)
self.destdir_button = Button(self.frame, text="Destination directory...", command= lambda: self.select_destdir(self.destdir))
self.destdir_button.grid(row=1, column=2, sticky=W, padx=(3, 5), pady=5)
self.destdir_button.config(state=DISABLED)
self.incchkbox = Checkbutton(self.frame, text='Create "include" folder if it does not exist.', variable=self.addinc, onvalue='yes', offvalue='no')
self.incchkbox.grid(row=2, column=0, columnspan=2, sticky=W, padx=5, pady=5)
self.incchkbox.config(state=DISABLED)
self.addinc.set('yes')
self.transframe = LabelFrame(master, text='Translation')
self.transframe.grid(row=1, rowspan=2, column=0, columnspan=3, sticky=N+S+E+W, padx=5, pady=5)
self.transframe.grid_columnconfigure(1, weight=1)
self.transframe.grid_rowconfigure(1, weight=1)
self.infoframe = LabelFrame(self.transframe, text='Source information')
self.infoframe.grid(row=1, rowspan=2, column=0, columnspan=3, sticky=N+S+E+W, padx=5, pady=5)
self.infoframe.grid_columnconfigure(1, weight=1)
self.infofolders = Label(self.infoframe, text=self.infofolder)
self.infofolders.grid(row=0, column=0, sticky=W, padx=5, pady=5)
self.infofolders.config(state=DISABLED)
self.infofiles = Label(self.infoframe, text=self.infofile)
self.infofiles.grid(row=1, column=0, sticky=W, padx=5, pady=5)
self.infofiles.config(state=DISABLED)
self.translate_button = Button(self.transframe, text="Translate!", command= lambda: self.translate(self.destdir, self.sourcedir, self.addinc))
self.translate_button.grid(row=3, column=0, sticky=W, padx=5, pady=5)
self.translate_button.config(state=DISABLED)
self.progressframe = LabelFrame(self.transframe, text='Progress')
self.progressframe.grid(row=4, rowspan=2, column=0, columnspan=3, sticky=N+S+E+W, padx=5, pady=5)
self.progressframe.grid_columnconfigure(1, weight=1)
#self.cfilelabel = Label(self.progressframe, text=self.currentfile)
#self.cfilelabel.grid(row=6, column=0, sticky=W, padx=5, pady=5)
#self.cfilelabel.config(state=DISABLED)
#self.cfileprogress = ttk.Progressbar(self.progressframe, orient='horizontal', mode='determinate')
#self.cfileprogress.grid(row=7, column=0, columnspan=3, sticky=N+S+E+W, padx=5, pady=5)
#self.cfileprogress.config(variable=self.cfilevar, maximum=3)
self.totallabel = Label(self.progressframe, text=self.totprogress)
self.totallabel.grid(row=8, column=0, sticky=W, padx=5, pady=5)
self.totallabel.config(state=DISABLED)
self.totalprogress = ttk.Progressbar(self.progressframe, orient='horizontal', mode='determinate')
self.totalprogress.grid(row=9, column=0, columnspan=3, sticky=N+S+E+W, padx=5, pady=5)
self.totalprogress.config(variable=self.totalvar, maximum=self.filecnt)
def select_sourcedir(self, sourcedir):
root.directory = os.path.abspath(filedialog.askdirectory())
if root.directory:
sourcedir.set(root.directory)
filecnt = sourcedir_filecnt(root.directory)
if filecnt > 0:
tempstr = 'Number of headers: '+str(filecnt)
temptot = 'Total progress: 0 of '+str(filecnt)
print ('Source directory: ', sourcedir.get())
self.destlabel.config(state=NORMAL)
self.destentry.config(state=NORMAL)
self.destdir_button.config(state=NORMAL)
self.infofiles.config(text=tempstr)
self.totallabel.config(text=temptot)
self.filecnt = filecnt
foldercnt = sourcedir_foldercnt(root.directory)
if foldercnt > 0:
tempstr = 'Number of folders: '+str(foldercnt)
self.infofolders.config(text=tempstr)
def select_destdir(self, destdir):
root.directory = filedialog.askdirectory()
if root.directory:
destdir.set(root.directory)
print ('Destination directory: ', destdir.get())
self.incchkbox.config(state=NORMAL)
self.infofolders.config(state=NORMAL)
self.infofiles.config(state=NORMAL)
self.translate_button.config(state=NORMAL)
#self.cfilelabel.config(state=NORMAL)
self.totallabel.config(state=NORMAL)
def translate(self, destdir, sourcedir, addinc):
doinc = addinc.get()
dest = destdir.get()
source = sourcedir.get()
if doinc == 'yes':
dest = dest+'/include'
print(os.path.exists(os.path.dirname(dest)))
if not os.path.exists(os.path.dirname(dest)):
try:
os.makedirs(os.path.dirname(dest))
except OSError as exc: # Guard against race condition
if exc.errno != errno.EEXIST:
raise
destdir.set(dest)
print ('Destination directory: ', destdir.get())
process_files(self, source, dest)
def cfileprogress_update(cnt):
self.cfilevar = cnt
def currentfile_update(current):
self.currentfile = 'Current file: '+current
root = Tk()
root.update()
#root.minsize(350, 210)
#width = (root.winfo_screenwidth()/2)-(350/2)
#height = (root.winfo_screenheight()/2)-(210/2)
#root.geometry('+%d+%d' % (width, height))
root.resizable(False, False)
h2incgui = h2incGUI(root)
root.mainloop()

112
old_h2inc/h2inc_old.py Normal file
View File

@@ -0,0 +1,112 @@
#!/usr/bin/env python3.5
# You are free to use and/or change this code for
# your own needs.
# Original code (c)2018 Jan Lerking
# Program to convert C-header (*.h) files to nasm include files (*.inc),
# for direct usage in assembly programming using nasm/yasm.
import os
import sys
import multiprocessing as mp
import io
from h2inc_parser import parseline, parseparsed
import h2inc_gui_old
tupline = []
preproc = ()
filelist = []
folderlist = []
cnt = 0
sourcedir = ''
destdir = ''
num_cores = mp.cpu_count()
def sourcedir_filecnt(sourcedir):
### Return the number of files, ending with '.h', in sourcedir - including subdirectories ###
cnt = 0
global filelist
for folderName, subfolders, files in os.walk(sourcedir):
for file in files:
if file.lower().endswith('.h'):
cnt += 1
filelist += [folderName+'/'+file]
print(folderName+'/'+file)
#print(filelist)
return cnt
def sourcedir_foldercnt(sourcedir):
### Return the number of folders, if it contains '*.h' files, in sourcedir - including subdirectories ###
global cnt
global folderlist
for folderName, subfolders, files in os.walk(sourcedir):
if subfolders:
for subfolder in subfolders:
sourcedir_foldercnt(subfolder)
tempf = [file for file in files if file.lower().endswith('.h')]
if tempf:
cnt = cnt+1
#print(folderName)
folderlist += [folderName]
#print(folderlist)
#print(len(folderlist))
return cnt
def process_files(gui, source, dest):
global sourcedir
global destdir
sourcedir = source
destdir = dest
pool = mp.Pool(processes=num_cores)
pool.map(process_file, filelist)
def process_file(data):
outfile = ''
inputfile = data
encodings = ['utf-8', 'latin-1', 'windows-1250', 'windows-1252', 'ascii',
'big5', 'big5hkscs', 'cp037', 'cp273', 'cp424', 'cp437', 'cp500',
'cp720', 'cp737', 'cp775', 'cp850', 'cp852', 'cp855', 'cp856',
'cp857', 'cp858', 'cp860', 'cp861', 'cp862', 'cp863', 'cp864', 'cp865',
'cp866', 'cp869', 'cp874', 'cp875', 'cp932', 'cp949', 'cp950', 'cp1006',
'cp1026', 'cp1125', 'cp1140', 'cp1250', 'cp1251', 'cp1252', 'cp1253', 'cp1254',
'cp1255', 'cp1256', 'cp1257', 'cp1258', 'cp65001', 'euc-jp', 'euc-jis-2004',
'euc-jisx0213', 'euc-kr', 'gb2312', 'gbk', 'gb18030', 'hz', 'iso2022-jp',
'iso2022-jp-1', 'iso2022-jp-2', 'iso2022-jp-2004', 'iso2022-jp-3',
'iso2022-jp-ext', 'iso2022-kr', 'iso8859-2', 'iso8859-3', 'iso8859-4',
'iso8859-5', 'iso8859-6', 'iso8859-7', 'iso8859-8', 'iso8859-9', 'iso8859-10',
'iso8859-11', 'iso8859-13', 'iso8859-14', 'iso8859-15', 'iso8859-16', 'johab',
'koi8-r', 'koi8-t', 'koi8-u', 'kz1048', 'mac-cyrillic', 'mac-greek',
'mac-iceland', 'mac-latin2', 'mac-roman', 'mac-turkish', 'ptcp154',
'shift-jis', 'shift-jis-2004', 'shift-jisx0213', 'utf-32', 'utf-32-be',
'utf-32-le', 'utf-16', 'utf-16-be', 'utf-16-le', 'utf-7', 'utf-8-sig']
for e in encodings:
try:
fh = io.open(data, 'r', encoding=e)
fh.readlines()
fh.seek(0)
except UnicodeDecodeError:
print('got unicode error with %s , trying different encoding' % e)
else:
#print('opening the file with encoding: %s ' % e)
break
#print(os.path.basename(data))
for lines in fh:
outfile = outfile+lines
fh.close()
outputfile = os.path.splitext(inputfile)[0]+'.inc'
outputfile = str(outputfile).replace(sourcedir, destdir)
#print(outputfile)
if not os.path.exists(os.path.dirname(outputfile)):
try:
os.makedirs(os.path.dirname(outputfile))
except OSError as exc: # Guard against race condition
if exc.errno != errno.EEXIST:
raise
newfile = open(outputfile, "w")
newfile.write(outfile)
newfile.close()
def async_process(num):
pool = mp.Pool(processes=num)
pool.map(process_file, filelist)

View File

@@ -0,0 +1,59 @@
from Queue import Queue
import threading
import time
exitFlag = 0
class myThread (threading.Thread):
def __init__(self, threadID, name, q):
threading.Thread.__init__(self)
self.threadID = threadID
self.name = name
self.q = q
def run(self):
print ("Starting " + self.name)
process_data(self.name, self.q)
print ("Exiting " + self.name)
def process_data(threadName, q):
while not exitFlag:
queueLock.acquire()
if not workQueue.empty():
data = q.get()
queueLock.release()
print ("%s processing %s" % (threadName, data))
else:
queueLock.release()
time.sleep(1)
threadList = ["Thread-1", "Thread-2", "Thread-3"]
nameList = ["One", "Two", "Three", "Four", "Five"]
queueLock = threading.Lock()
workQueue = Queue.Queue(10)
threads = []
threadID = 1
# Create new threads
for tName in threadList:
thread = myThread(threadID, tName, workQueue)
thread.start()
threads.append(thread)
threadID += 1
# Fill the queue
queueLock.acquire()
for word in nameList:
workQueue.put(word)
queueLock.release()
# Wait for queue to empty
while not workQueue.empty():
pass
# Notify threads it's time to exit
exitFlag = 1
# Wait for all threads to complete
for t in threads:
t.join()
print ("Exiting Main Thread")