194 lines
7.8 KiB
Python
Executable File
194 lines
7.8 KiB
Python
Executable File
#!/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 sys
|
|
import gi
|
|
gi.require_version('Gtk', '3.0')
|
|
from gi.repository import Gtk, Gio, GObject as gobject
|
|
import globvar
|
|
from h2inc_mp import start_workers
|
|
from h2inc_fp import sourcedir_filecnt, sourcedir_foldercnt
|
|
|
|
globvar.init()
|
|
|
|
class H2INC:
|
|
|
|
global app
|
|
|
|
def __init__(self):
|
|
|
|
self.filecnt = 0
|
|
self.foldercnt = 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","h2inc")
|
|
self.obj("window").set_title("h2inc - v.0.1.1")
|
|
self.obj("window").show_all()
|
|
self.obj("source_entry").set_text(globvar.defsrc)
|
|
self.obj("destination_entry").set_text(globvar.defdest)
|
|
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.new_value = self.obj("progressbar").get_fraction()+fraction
|
|
self.obj("progress_label").set_text("{} of {}".format(index+1, globvar.filecnt))
|
|
self.obj("progressbar").set_fraction(self.new_value)
|
|
|
|
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)
|
|
Gtk.FileChooser.set_filename(dialog, globvar.srcdir)
|
|
|
|
response = dialog.run()
|
|
if response == Gtk.ResponseType.OK:
|
|
globvar.filecnt = sourcedir_filecnt(dialog.get_filename())
|
|
if globvar.filecnt >0:
|
|
print(globvar.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(globvar.fileindex, globvar.filecnt))
|
|
globvar.foldercnt = sourcedir_foldercnt(dialog.get_filename())
|
|
if globvar.foldercnt >0:
|
|
self.obj("numfolders_label").set_text(str(globvar.foldercnt))
|
|
elif response == Gtk.ResponseType.CANCEL:
|
|
print("Cancel")
|
|
|
|
globvar.fraction = 1/globvar.filecnt
|
|
dialog.destroy()
|
|
|
|
def on_destination_button_clicked(self,widget):
|
|
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:
|
|
globvar.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(globvar.srcdir)
|
|
if self.obj("include_checkbutton").get_active() == True:
|
|
globvar.incinc = '/include'
|
|
globvar.destdir = globvar.destdir+globvar.incinc
|
|
print(globvar.destdir)
|
|
elif response == Gtk.ResponseType.CANCEL:
|
|
print("Cancel")
|
|
|
|
dialog.destroy()
|
|
|
|
def on_include_checkbutton_toggled(self, widget):
|
|
if self.obj("include_checkbutton").get_active() == True:
|
|
if globvar.destdir.lower().endswith('/include'):
|
|
globvar.destdir = globvar.destdir
|
|
else:
|
|
globvar.destdir = globvar.destdir+'/include'
|
|
if self.obj("include_checkbutton").get_active() == False:
|
|
#globvar.incinc = ''
|
|
globvar.destdir = globvar.destdir.replace('/include', '')
|
|
self.obj("destination_entry").set_text(globvar.destdir)
|
|
print(globvar.destdir)
|
|
|
|
def on_default_dir_checkbutton_toggled(self, widget):
|
|
globvar.defdir = self.obj("default_dir_checkbutton").get_active()
|
|
if globvar.defdir == True:
|
|
globvar.srcdir = globvar.defsrc
|
|
self.obj("source_entry").set_text(globvar.srcdir)
|
|
globvar.destdir = globvar.defdest
|
|
self.obj("destination_entry").set_text(globvar.destdir)
|
|
self.obj("include_checkbutton").set_sensitive(True)
|
|
self.obj("source_label").set_sensitive(False)
|
|
self.obj("source_entry").set_sensitive(False)
|
|
self.obj("source_button").set_sensitive(False)
|
|
self.obj("translation_frame").set_sensitive(True)
|
|
globvar.filecnt = sourcedir_filecnt(globvar.srcdir)
|
|
if globvar.filecnt >0:
|
|
print(globvar.filecnt)
|
|
self.obj("numfiles_label").set_text(str(globvar.filecnt))
|
|
self.obj("progress_label").set_text("{} of {}".format(globvar.fileindex, self.filecnt))
|
|
globvar.foldercnt = sourcedir_foldercnt(globvar.srcdir)
|
|
if globvar.foldercnt >0:
|
|
print(globvar.foldercnt)
|
|
self.obj("numfolders_label").set_text(str(globvar.foldercnt))
|
|
|
|
globvar.fraction = 1/globvar.filecnt
|
|
|
|
def on_translate_button_clicked(self, widget, data=None):
|
|
self.obj("sourceframe").set_sensitive(False)
|
|
self.obj("translate_button").set_sensitive(False)
|
|
|
|
start_workers()
|
|
|
|
app = H2INC()
|
|
app.run(sys.argv)
|