Major updates regarding global variables

This commit is contained in:
2018-05-08 22:03:11 +02:00
parent 4351221d7e
commit f88f5dcd0e
11 changed files with 116 additions and 104 deletions
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
+46
View File
@@ -0,0 +1,46 @@
# 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 pathlib import Path
def init():
home = str(Path.home())
global worker
worker = []
global listener
listener = []
global thread
thread = []
global tupline
tupline = []
global preproc
preproc = ()
global filelist
filelist = []
global folderlist
folderlist = []
global cnt
cnt = 0
global srcdir
srcdir = ''
global destdir
destdir = ''
global fileindex
fileindex = 0
global filecnt
filecnt = 0
global incinc
incinc = ''
global defdir
defdir = False
global defsrc
defsrc = '/usr/include'
global defdest
defdest = home+'/include'
global count
count = 0
+1
View File
@@ -37,6 +37,7 @@
</object>
<object class="GtkApplicationWindow" id="window">
<property name="can_focus">False</property>
<property name="title" translatable="yes">h2inc</property>
<child>
<object class="GtkBox" id="box1">
<property name="visible">True</property>
+2
View File
@@ -37,6 +37,8 @@
</object>
<object class="GtkApplicationWindow" id="window">
<property name="can_focus">False</property>
<property name="title" translatable="yes">H2INC</property>
<property name="hide_titlebar_when_maximized">True</property>
<child>
<object class="GtkBox" id="box1">
<property name="visible">True</property>
+9 -14
View File
@@ -9,27 +9,23 @@ import os
import sys
import io
import time
from h2inc_globals import destdir, defdest, filelist, folderlist, cnt, srcdir
import globvar
def sourcedir_filecnt(sourcedir):
### Return the number of files, ending with '.h', in sourcedir - including subdirectories ###
global cnt
global filelist
global srcdir
srcdir = sourcedir
globvar.srcdir = sourcedir
for folderName, subfolders, files in os.walk(sourcedir):
for file in files:
if file.lower().endswith('.h'):
cnt += 1
filelist += [folderName + '/' + file]
globvar.cnt += 1
globvar.filelist += [folderName + '/' + file]
print(folderName + '/' + file)
# print(filelist)
return cnt
return globvar.cnt
def sourcedir_foldercnt(sourcedir):
### Return the number of folders, if it contains '*.h' files, in sourcedir - including subdirectories ###
cnt = 0
global folderlist
for folderName, subfolders, files in os.walk(sourcedir):
if subfolders:
for subfolder in subfolders:
@@ -38,13 +34,12 @@ def sourcedir_foldercnt(sourcedir):
if tempf:
cnt = cnt + 1
# print(folderName)
folderlist += [folderName]
globvar.folderlist += [folderName]
# print(folderlist)
# print(len(folderlist))
return cnt
def process_file(data):
global count
outfile = ''
inputfile = data
encodings = ['utf-8', 'latin-1', 'windows-1250', 'windows-1252', 'ascii',
@@ -78,9 +73,9 @@ def process_file(data):
outfile = outfile + lines
fh.close()
outputfile = os.path.splitext(inputfile)[0] + '.inc'
outputfile = str(outputfile).replace(srcdir, destdir)
count += 1
print(str(count)+'->'+outputfile)
outputfile = str(outputfile).replace(globvar.srcdir, globvar.destdir)
globvar.count += 1
print(str(globvar.count)+'->'+outputfile)
if not os.path.exists(os.path.dirname(outputfile)):
try:
os.makedirs(os.path.dirname(outputfile))
-25
View File
@@ -1,25 +0,0 @@
# 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.
worker = []
listener = []
thread = []
tupline = []
preproc = ()
filelist = []
folderlist = []
cnt = 0
srcdir = ''
destdir = ''
fileindex = 0
filecnt = 0
incinc = ''
defdir = False
defsrc = '/usr/include'
defdest = '~/include'
count = 0
+44 -50
View File
@@ -12,14 +12,15 @@ import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, Gio, GObject as gobject
#import time
from h2inc_globals import defsrc, destdir, defdest, filelist, cnt, srcdir
from h2inc_mp import Worker, start_workers
import globvar
from h2inc_mp import start_workers
from h2inc_fp import sourcedir_filecnt, sourcedir_foldercnt
globvar.init()
class H2INC:
global app
global destlabel
def __init__(self):
@@ -40,10 +41,11 @@ class H2INC:
self.obj = builder.get_object
self.obj("window").set_application(app)
self.obj("window").set_wmclass("h2inc_gtk","h2inc_gtk")
self.obj("window").set_wmclass("h2inc","h2inc")
self.obj("window").set_title("h2inc - v.0.0.1")
self.obj("window").show_all()
self.obj("source_entry").set_text(defsrc)
self.obj("destination_entry").set_text(defdest)
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)
@@ -93,38 +95,34 @@ class H2INC:
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, srcdir)
Gtk.FileChooser.set_filename(dialog, globvar.srcdir)
response = dialog.run()
if response == Gtk.ResponseType.OK:
self.filecnt = sourcedir_filecnt(dialog.get_filename())
if self.filecnt >0:
print(self.filecnt)
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(self.fileindex, self.filecnt))
self.foldercnt = sourcedir_foldercnt(dialog.get_filename())
if self.foldercnt >0:
self.obj("numfolders_label").set_text(str(self.foldercnt))
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")
dialog.destroy()
def on_destination_button_clicked(self,widget):
global destdir
global incinc
dialog = Gtk.FileChooserDialog("Select destination directory!",
self.obj("window"),
Gtk.FileChooserAction.SELECT_FOLDER,
@@ -134,57 +132,53 @@ class H2INC:
response = dialog.run()
if response == Gtk.ResponseType.OK:
destdir = dialog.get_filename()
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(srcdir)
print(globvar.srcdir)
if self.obj("include_checkbutton").get_active() == True:
incinc = '/include'
destdir = destdir+incinc
print(destdir)
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):
global destdir
global incinc
if self.obj("include_checkbutton").get_active() == True:
incinc = '~/include'
destdir = destdir+incinc
if globvar.destdir.lower().endswith('/include'):
globvar.destdir = globvar.destdir
else:
globvar.destdir = globvar.destdir+'/include'
if self.obj("include_checkbutton").get_active() == False:
incinc = ''
destdir = destdir.replace('~/include', '')
self.obj("destination_entry").set_text(destdir)
print(destdir)
#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):
global defdir
global srcdir
global destdir
global incinc
defdir = self.obj("default_dir_checkbutton").get_active()
if defdir == True:
srcdir = defsrc
self.obj("source_entry").set_text(srcdir)
destdir = defdest
self.obj("destination_entry").set_text(destdir)
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)
self.filecnt = sourcedir_filecnt(srcdir)
if self.filecnt >0:
print(self.filecnt)
self.obj("numfiles_label").set_text(str(self.filecnt))
self.obj("progress_label").set_text("{} of {}".format(self.fileindex, self.filecnt))
self.foldercnt = sourcedir_foldercnt(srcdir)
if self.foldercnt >0:
print(self.foldercnt)
self.obj("numfolders_label").set_text(str(self.foldercnt))
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))
def on_translate_button_clicked(self, widget, data=None):
self.obj("sourceframe").set_sensitive(False)
+14 -15
View File
@@ -9,18 +9,15 @@ import multiprocessing
from queue import Queue
from threading import Thread
import threading
from h2inc_globals import filelist, cnt
from h2inc_fp import process_file
import globvar
import h2inc_fp
num_cores = multiprocessing.cpu_count()
lock = threading.Lock()
queue = 0
class Worker(Thread):
def __init__(self, queue):
Thread.__init__(self)
self.queue = queue
def go(self):
print("The worker has started doing some work (counting from 0 to 9)")
def process_queue():
print("Worker {} working...".format(threading.current_thread().name))
#for i in range(self.filecnt):
#proportion = (float(i+1))/self.filecnt
#self.queue.put((proportion, "working...", i))
@@ -28,12 +25,14 @@ class Worker(Thread):
#process_file(filelist[i])
#self.queue.put((1.0, "finished"))
#print("The worker has finished.")
while True:
cfile = self.queue.get()
process_file(cfile)
self.queue.task_done()
while True:
cfile = queue.get()
print(cfile)
h2inc_fp.process_file(cfile)
queue.task_done()
def start_workers():
global queue
print("Creating shared Queue")
queue = Queue()
@@ -42,11 +41,11 @@ def start_workers():
for n in range(num_cores):
print("Creating Worker", n)
worker = Worker(queue)
worker = threading.Thread(target=process_queue)
worker.deamon = True
worker.start()
for cfile in filelist:
for cfile in globvar.filelist:
print("Queueing {}".format(cfile))
queue.put(cfile)