h2inc_gtk.py split
h2inc_gtk.py split into h2inc_globals.py (global variables), h2inc_mp.py (multiprocessing) and h2inc_fp.py (file processing)
This commit is contained in:
93
Gui test/h2inc_fp.py
Normal file
93
Gui test/h2inc_fp.py
Normal file
@@ -0,0 +1,93 @@
|
||||
# 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 time
|
||||
from h2inc_globals import destdir, defdest, filelist
|
||||
|
||||
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):
|
||||
global count
|
||||
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)
|
||||
count += 1
|
||||
print(str(count)+'->'+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 != FileExistsError:
|
||||
raise
|
||||
newfile = open(outputfile, "w")
|
||||
newfile.write(outfile)
|
||||
newfile.close()
|
||||
|
25
Gui test/h2inc_globals.py
Normal file
25
Gui test/h2inc_globals.py
Normal file
@@ -0,0 +1,25 @@
|
||||
# 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 = '~'
|
||||
count = 0
|
||||
|
@@ -7,160 +7,16 @@
|
||||
# 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, cpu_count
|
||||
import threading
|
||||
#import time
|
||||
from h2inc_globals import defsrc, destdir, defdest, filelist
|
||||
from h2inc_mp import Worker, start_workers
|
||||
from h2inc_fp import sourcedir_filecnt, sourcedir_foldercnt
|
||||
|
||||
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 = ''
|
||||
defdir = False
|
||||
defsrc = 'usr/include'
|
||||
defdest = '~'
|
||||
count = 0
|
||||
|
||||
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):
|
||||
global count
|
||||
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)
|
||||
count += 1
|
||||
print(str(count)+'->'+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 != FileExistsError:
|
||||
raise
|
||||
newfile = open(outputfile, "w")
|
||||
newfile.write(outfile)
|
||||
newfile.close()
|
||||
|
||||
class ExampleApp:
|
||||
class H2INC:
|
||||
|
||||
global app
|
||||
global destlabel
|
||||
@@ -185,10 +41,9 @@ class ExampleApp:
|
||||
self.obj("window").set_application(app)
|
||||
self.obj("window").set_wmclass("h2inc_gtk","h2inc_gtk")
|
||||
self.obj("window").show_all()
|
||||
self.obj("source_entry").set_text(defsrc)
|
||||
self.obj("default_dir_checkbutton").set_active(False)
|
||||
self.obj("default_dir_checkbutton").emit("toggled")
|
||||
self.obj("include_checkbutton").set_active(True)
|
||||
self.obj("include_checkbutton").emit("toggled")
|
||||
|
||||
button = Gtk.Button.new_from_stock(Gtk.STOCK_CANCEL)
|
||||
button.set_property("can-default",True)
|
||||
@@ -330,30 +185,7 @@ class ExampleApp:
|
||||
self.obj("sourceframe").set_sensitive(False)
|
||||
self.obj("translate_button").set_sensitive(False)
|
||||
|
||||
if self.process!=None:
|
||||
return
|
||||
start_workers()
|
||||
|
||||
print("Creating shared Queue")
|
||||
queue = Queue()
|
||||
|
||||
for n in num_cores:
|
||||
print("Creating Worker", n)
|
||||
worker[n] = Worker(queue, self.filecnt, filelist)
|
||||
|
||||
print("Creating Listener", n)
|
||||
listener[n] = Listener(queue)
|
||||
listener[n].connect("updated",self.callbackDisplay)
|
||||
listener[n].connect("finished",self.callbackFinished)
|
||||
|
||||
print("Starting Listener", n)
|
||||
thread[n] = threading.Thread(target=listener[n].go, args=())
|
||||
thread[n].start()
|
||||
|
||||
print("Starting Worker", n)
|
||||
self.process = Process(target=worker[n].go, args=())
|
||||
self.process.start()
|
||||
|
||||
|
||||
|
||||
app = ExampleApp()
|
||||
app = H2INC()
|
||||
app.run(sys.argv)
|
||||
|
55
Gui test/h2inc_mp.py
Normal file
55
Gui test/h2inc_mp.py
Normal file
@@ -0,0 +1,55 @@
|
||||
# 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 multiprocessing
|
||||
from queue import Queue
|
||||
from threading import Thread
|
||||
import threading
|
||||
from h2inc_globals import filelist
|
||||
from h2inc_fp import process_file
|
||||
|
||||
num_cores = multiprocessing.cpu_count()
|
||||
|
||||
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)")
|
||||
#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.")
|
||||
while True:
|
||||
cfile = self.queue.get()
|
||||
process_file(cfile)
|
||||
self.queue.task_done()
|
||||
|
||||
def start_workers():
|
||||
print("Creating shared Queue")
|
||||
queue = Queue()
|
||||
|
||||
print("Number of cores:", num_cores)
|
||||
|
||||
for n in range(num_cores):
|
||||
print("Creating Worker", n)
|
||||
|
||||
worker = Worker(queue)
|
||||
worker.deamon = True
|
||||
worker.start()
|
||||
|
||||
for cfile in filelist:
|
||||
print("Queueing {}".format(cfile))
|
||||
queue.put(cfile)
|
||||
|
||||
queue.join()
|
||||
return
|
||||
|
Reference in New Issue
Block a user