From e243096962d7ceb954b7a8d9281c693feb5ae665 Mon Sep 17 00:00:00 2001 From: Fierelier Date: Mon, 19 Dec 2022 13:40:59 +0100 Subject: [PATCH] Initial commit --- .gitignore | 1 + LICENSE | 19 ++++++ gawk.py | 45 ++++++++++++++ module/browser/_main.py | 12 ++++ module/browser/addon.py | 13 ++++ module/browser/module.py | 13 ++++ module/browser/module/window/browser.py | 49 +++++++++++++++ module/browser/module/window/main.py | 1 + module/config.py | 53 ++++++++++++++++ module/exception.py | 5 ++ module/py/me/fier/python/__init__.py | 81 +++++++++++++++++++++++++ user/addon/addons.txt | 1 + user/addon/userModules/main.py | 1 + 13 files changed, 294 insertions(+) create mode 100644 .gitignore create mode 100644 LICENSE create mode 100644 gawk.py create mode 100644 module/browser/_main.py create mode 100644 module/browser/addon.py create mode 100644 module/browser/module.py create mode 100644 module/browser/module/window/browser.py create mode 100644 module/browser/module/window/main.py create mode 100644 module/config.py create mode 100644 module/exception.py create mode 100644 module/py/me/fier/python/__init__.py create mode 100644 user/addon/addons.txt create mode 100644 user/addon/userModules/main.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ed8ebf5 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +__pycache__ \ No newline at end of file diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..de7807f --- /dev/null +++ b/LICENSE @@ -0,0 +1,19 @@ +Copyright (c) 2022 + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. \ No newline at end of file diff --git a/gawk.py b/gawk.py new file mode 100644 index 0000000..770f848 --- /dev/null +++ b/gawk.py @@ -0,0 +1,45 @@ +#!/usr/bin/env python3 +def init(): pass +def main(): + mfp.setProgramName("me.fier.gawk") # Your domain/username and the name of your program + + mfp.paths = [mfp.p(mfp.sd,"module","browser")] + mfp.paths + browser = mfp.require("browser") + config = mfp.require("config") + eig = mfp.require("exception").ignore + + print("Loading addons ...") + addonList = eig([],config.readList,eig(None,config.find,mfp.p("addon","addons.txt"))) + for addonName in addonList: + print("> " +addonName) + try: + addonFile = config.find(mfp.p("addon",addonName,"main.py")) + except Exception as e: + print("Could not find " +addonName+ ": " +str(e)) + continue + + addon = mfp.dofile(addonFile) # Add exception handling + browser.addon.add(addonName,addon) + + print("Done, starting browser.") + browser.start() + +def bootstrap(name,modName): + if name in globals(): return + import sys, os, importlib + if getattr(sys, 'frozen', False) and hasattr(sys, '_MEIPASS'): + s = os.path.realpath(sys.executable) + else: + s = os.path.realpath(__file__) + + if not os.path.join(os.path.dirname(s),"module","py") in sys.path: + sys.path = [os.path.join(os.path.dirname(s),"module","py")] + sys.path + + mod = importlib.import_module(modName); modl = mod.Bunch() + mod.init(mod,modl,s,name) + globals()[name] = mod; globals()[name + "l"] = modl + +bootstrap("mfp","me.fier.python") +init() +if __name__ == '__main__': + main() \ No newline at end of file diff --git a/module/browser/_main.py b/module/browser/_main.py new file mode 100644 index 0000000..ba78290 --- /dev/null +++ b/module/browser/_main.py @@ -0,0 +1,12 @@ +name = mfp.programName.rsplit(".",1)[-1] +module = mfp.dofile(mfp.p(mfpl.sd,"module.py")) +addon = mfp.dofile(mfp.p(mfpl.sd,"addon.py")) +windows = [] + +import gi +gi.require_version("Gtk","3.0") +from gi.repository import Gtk + +def start(): + windows.append(mfp.require("browser").module.require(mfp.p("window","browser.py")).create()) + Gtk.main() \ No newline at end of file diff --git a/module/browser/addon.py b/module/browser/addon.py new file mode 100644 index 0000000..0a3e32c --- /dev/null +++ b/module/browser/addon.py @@ -0,0 +1,13 @@ +addons = mfp.Bunch() + +def remove(name): + addons[name].unload() + del addons[name] + +def add(name,glb): + if name in addons: + remove(name) + addons[name] = glb + glb._addon = mfp.Bunch() + glb._addon.name = name + if "init" in glb: glb.init() \ No newline at end of file diff --git a/module/browser/module.py b/module/browser/module.py new file mode 100644 index 0000000..3472895 --- /dev/null +++ b/module/browser/module.py @@ -0,0 +1,13 @@ +import os +paths = [] +paths.append(mfp.p(mfpl.sd,"module")) + +modules = mfp.Bunch() +def require(mod): + if mod in modules: return modules[mod] + for path in paths: + fpath = mfp.p(path,mod) + if os.path.isfile(fpath): + modules[mod] = mfp.dofile(fpath) + return modules[mod] + raise Exception("Module not found: " +mod) \ No newline at end of file diff --git a/module/browser/module/window/browser.py b/module/browser/module/window/browser.py new file mode 100644 index 0000000..610da3f --- /dev/null +++ b/module/browser/module/window/browser.py @@ -0,0 +1,49 @@ +import gi +gi.require_version("Gtk","3.0") +gi.require_version("WebKit2","4.0") +from gi.repository import Gtk,WebKit2 + +browser = mfp.require("browser") + +def onResize(obj,event): + resizeElements(obj,event.width,event.height) + +def resizeElements(obj,width,height): + for webView in obj.cWebViews: + webView.set_size_request(width,height) + +def onClose(obj): + browser.windows.remove(obj.cParent) + for webView in obj.cWebViews: webView.destroy() + obj.destroy() + if len(browser.windows) == 0: + Gtk.main_quit() + +def create(): + import sys + self = mfp.Bunch() + obj = Gtk.Window() + self.obj = obj + + obj.cMainWrapper = Gtk.ScrolledWindow() + obj.add(obj.cMainWrapper) + obj.cMain = Gtk.Fixed() + obj.cMainWrapper.add(obj.cMain) + + obj.cParent = self + obj.set_title(mfp.require("browser").name) + obj.resize(640,480) + obj.connect("configure-event",onResize) + obj.connect("destroy",onClose) + + webView = WebKit2.WebView() + obj.cWebViews = [] + obj.cWebViews.append(webView) + obj.cMain.add(webView) + webView.load_uri(sys.argv[1]) + obj.set_focus(webView) + + resizeElements(obj,640,480) + + obj.show_all() + return self \ No newline at end of file diff --git a/module/browser/module/window/main.py b/module/browser/module/window/main.py new file mode 100644 index 0000000..866c4b9 --- /dev/null +++ b/module/browser/module/window/main.py @@ -0,0 +1 @@ +windows = [] \ No newline at end of file diff --git a/module/config.py b/module/config.py new file mode 100644 index 0000000..d58bb9a --- /dev/null +++ b/module/config.py @@ -0,0 +1,53 @@ +import os +try: + import configparser +except Exception: + import ConfigParser as configparser + +paths = [] + +def find(path,method = os.path.isfile): + for p in paths: + fp = mfp.p(p,path) + if method(fp): return fp + raise Exception("Config file not found: " +path) + +def findAll(path,method = os.path.isfile): + rtn = [] + for p in paths: + fp = mfp.p(p,path) + if method(fp): rtn.append(fp) + return rtn + +def readList(path): + with open(path,encoding="utf-8") as f: + content = f.read().replace("\r","").strip("\n").split("\n") + index = 0 + length = len(content) + while index < length: + line = content[index] + line.strip("\t ") + if line == "": + del content[index] + length -= 1 + continue + content[index] = line + index += 1 + return content + +def readIni(path,config = None): + if config == None: + config = configparser.ConfigParser() + config.optionxform = str + config.read(path) + return config + +def init(): + import sys + if sys.platform == "windows": + paths.append(mfp.p(os.environ["APPDATA"],mfp.programName)) + else: + paths.append(mfp.p(os.environ["HOME"],".config",mfp.programName)) + paths.append(mfp.p(mfp.sd,"user")) + +init() \ No newline at end of file diff --git a/module/exception.py b/module/exception.py new file mode 100644 index 0000000..d17fba4 --- /dev/null +++ b/module/exception.py @@ -0,0 +1,5 @@ +def ignore(rtn,func,*args,**kwargs): + try: + return func(*args,**kwargs) + except Exception: + return rtn \ No newline at end of file diff --git a/module/py/me/fier/python/__init__.py b/module/py/me/fier/python/__init__.py new file mode 100644 index 0000000..0c76b82 --- /dev/null +++ b/module/py/me/fier/python/__init__.py @@ -0,0 +1,81 @@ +import os + +# BUNCH +try: + import munch + Bunch = munch.Munch + bunchify = munch.munchify + unbunchify = munch.unmunchify +except Exception: + import bunch + Bunch = munch.Bunch + bunchify = munch.bunchify + unbunchify = munch.unbunchify + +# GLOBALS +g = Bunch() + +# SCRIPTS +paths = [] +loaded = Bunch() + +def docode(st,name = "Unknown"): + code = compile(st,name,"exec") + glb = Bunch() + glb[distro] = me + glb[distro + "l"] = Bunch() + glb[distro + "l"].s = name + try: + glb[distro + "l"].sd = pUp(name) + except Exception: + pass + glb[distro + "l"].g = glb + exec(code,glb) + return glb + +def dofile(path): + return docode(open(path,encoding="utf-8").read(),path) + +def dorequire(name,*args,**kwargs): + for path in paths: + path = path.replace("?",name) + if os.path.isfile(path): + return dofile(path,*args,**kwargs) + raise Exception("Library " +name+ " not found.") + +def require(name,*args,**kwargs): + if not name in loaded: + loaded[name] = dorequire(name,*args,**kwargs) + return loaded[name] + +# PROGRAM +programName = None +programNameSet = False +def setProgramName(name): + global programName,programNameSet + if programNameSet: return + programNameSet = True + programName = name + +# INIT +inited = False +def init(mod,modl,sp,d): + global inited + if inited: return + global distro,me,p,pUp,programName,programNameSet,s,sd,distro + import sys + inited = True + distro = d + me = mod + + p = os.path.join + pUp = os.path.dirname + s = sp + sd = pUp(sp) + modl.s = s + modl.sd = sd + programName = distro + "." + s.replace(modl.sd + os.path.sep,"",1).rsplit(".",1)[0] + programNameSet = False + + paths.append(p(modl.sd,"module","?.py")) + paths.append(p(modl.sd,"module","?","_main.py")) \ No newline at end of file diff --git a/user/addon/addons.txt b/user/addon/addons.txt new file mode 100644 index 0000000..498ad33 --- /dev/null +++ b/user/addon/addons.txt @@ -0,0 +1 @@ +userModules \ No newline at end of file diff --git a/user/addon/userModules/main.py b/user/addon/userModules/main.py new file mode 100644 index 0000000..69139df --- /dev/null +++ b/user/addon/userModules/main.py @@ -0,0 +1 @@ +mfp.require("browser").module.paths.append(mfp.p(mfp.require("config").paths[0],"module")) \ No newline at end of file