2021-03-18 15:22:56 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
import sys
|
|
|
|
|
|
|
|
oldexcepthook = sys.excepthook
|
|
|
|
def newexcepthook(type,value,traceback):
|
|
|
|
oldexcepthook(type,value,traceback)
|
|
|
|
#input("Press ENTER to quit.") # - this causes issues with Qt's input hook
|
|
|
|
sys.excepthook = newexcepthook
|
|
|
|
|
|
|
|
import os
|
|
|
|
p = os.path.join
|
|
|
|
pUp = os.path.dirname
|
|
|
|
s = False
|
|
|
|
if getattr(sys, 'frozen', False) and hasattr(sys, '_MEIPASS'):
|
|
|
|
s = os.path.realpath(sys.executable)
|
|
|
|
else:
|
|
|
|
s = os.path.realpath(__file__)
|
|
|
|
sp = pUp(s)
|
|
|
|
|
|
|
|
# script start
|
|
|
|
import qtpy
|
2021-03-18 16:57:02 +00:00
|
|
|
import qtpy.QtGui as QtGui
|
2021-03-18 15:22:56 +00:00
|
|
|
from qtpy.QtGui import *
|
|
|
|
from qtpy.QtWidgets import *
|
|
|
|
from qtpy.QtCore import *
|
|
|
|
from qtpy.QtMultimedia import QSound
|
|
|
|
import configparser
|
|
|
|
import json
|
2021-03-19 02:18:18 +00:00
|
|
|
import time
|
2021-03-20 17:39:54 +00:00
|
|
|
import html
|
2021-03-20 17:40:34 +00:00
|
|
|
import threading
|
2021-03-18 15:22:56 +00:00
|
|
|
os.chdir(sp)
|
2021-03-20 17:40:34 +00:00
|
|
|
|
2021-03-18 15:22:56 +00:00
|
|
|
distro = os.path.splitext(s.rsplit(os.path.sep)[-1])[0]
|
2021-03-18 18:04:27 +00:00
|
|
|
version = {
|
|
|
|
"major": 0,
|
2021-03-25 19:39:54 +00:00
|
|
|
"minor": 8,
|
2021-03-20 19:02:47 +00:00
|
|
|
"patch": 0,
|
2021-03-18 18:04:27 +00:00
|
|
|
"stage": "dev"
|
|
|
|
}
|
|
|
|
versionString = ".".join(map(str,[version["major"],version["minor"]]))
|
|
|
|
versionStringExt = ".".join(map(str,[version["major"],version["minor"],version["patch"]])) + " " + version["stage"]
|
2021-03-18 15:22:56 +00:00
|
|
|
|
2021-03-20 17:40:34 +00:00
|
|
|
browserWindows = []
|
|
|
|
browserWindowsLock = threading.Lock()
|
|
|
|
|
2021-03-18 15:22:56 +00:00
|
|
|
def runCode(str, lcs = False, description = "loose-code"):
|
|
|
|
if lcs == False: lcs = {}
|
|
|
|
code = compile(str,description,"exec")
|
|
|
|
exec(code,globals(),lcs)
|
|
|
|
return lcs
|
|
|
|
|
|
|
|
def runScript(sf, lcs = False):
|
|
|
|
if lcs == False: lcs = {}
|
|
|
|
with open(sf) as script:
|
|
|
|
runCode(script.read(),lcs,sf)
|
|
|
|
return lcs
|
|
|
|
|
|
|
|
def getDataDir(appName):
|
|
|
|
basePath = False
|
|
|
|
if sys.platform.startswith("win"):
|
2021-03-18 16:55:14 +00:00
|
|
|
if "appdata" in os.environ: basePath = os.environ["appdata"]
|
2021-03-18 15:22:56 +00:00
|
|
|
elif sys.platform.startswith("linux"):
|
2021-03-18 16:55:14 +00:00
|
|
|
if "XDG_DATA_HOME" in os.environ:
|
|
|
|
basePath = os.environ["XDG_DATA_HOME"]
|
|
|
|
else:
|
|
|
|
if "HOME" in os.environ: basePath = p(os.environ["HOME"],".config")
|
2021-03-18 15:22:56 +00:00
|
|
|
elif sys.platform.startswith("darwin"):
|
2021-03-18 16:55:14 +00:00
|
|
|
if "HOME" in os.environ: basePath = p(os.environ["HOME"],"Library","Application Support")
|
2021-03-18 15:22:56 +00:00
|
|
|
|
|
|
|
if not basePath: raise
|
|
|
|
return p(basePath,appName)
|
|
|
|
|
|
|
|
def prettyJson(ls):
|
|
|
|
return json.dumps(ls,indent=2)
|
|
|
|
|
|
|
|
class browserWindow(QMainWindow):
|
|
|
|
def __init__(self,*args,**kwargs):
|
|
|
|
super().__init__(*args,**kwargs)
|
2021-03-19 02:37:41 +00:00
|
|
|
self.cTitle = "Hello World - " +distro+ " " +versionStringExt
|
2021-03-18 15:22:56 +00:00
|
|
|
self.cWidth = 640
|
|
|
|
self.cHeight = 480
|
|
|
|
self.setWindowTitle(self.cTitle)
|
|
|
|
self.resize(self.cWidth,self.cHeight)
|
|
|
|
self.cDocumentInfo = {
|
|
|
|
"url": "",
|
|
|
|
"body": "",
|
|
|
|
"headers": {},
|
|
|
|
# additional info:
|
|
|
|
"baseUrl": ""
|
|
|
|
}
|
2021-03-18 18:04:54 +00:00
|
|
|
self.cUserAgent = config["default"]["useragent"].replace("$OS",sys.platform).replace("$VER",versionString).replace("$DIST",distro)
|
2021-03-20 19:02:05 +00:00
|
|
|
|
|
|
|
self.cDownloadId = 0
|
|
|
|
self.cTasks = []
|
|
|
|
self.cTaskLock = threading.Lock()
|
|
|
|
self.cTaskTimer = QTimer()
|
|
|
|
self.cTaskTimer.setInterval(100)
|
|
|
|
self.cTaskTimer.timeout.connect(self.cRunTasks)
|
|
|
|
self.cTaskTimer.start()
|
|
|
|
|
2021-03-18 15:22:56 +00:00
|
|
|
self.cCreateElements()
|
|
|
|
|
|
|
|
def cCreateElements(self):
|
|
|
|
self.cMenuBar = self.menuBar()
|
2021-03-20 15:01:51 +00:00
|
|
|
self.cFileMenu = self.cMenuBar.addMenu("&File")
|
|
|
|
self.cNewWindowButton = self.cFileMenu.addAction("New &window")
|
|
|
|
self.cNewWindowButton.triggered.connect(self.cNewWindow)
|
|
|
|
self.cViewMenu = self.cMenuBar.addMenu("&View")
|
2021-03-18 15:22:56 +00:00
|
|
|
|
|
|
|
self.cUrlBar = QLineEdit(config["default"]["home"],self)
|
2021-03-20 14:59:11 +00:00
|
|
|
self.cUrlBar.returnPressed.connect(self.cNavigate)
|
2021-03-18 15:22:56 +00:00
|
|
|
self.cButtonGo = QPushButton("Go",self)
|
|
|
|
self.cButtonGo.clicked.connect(self.cNavigate)
|
|
|
|
|
|
|
|
self.cDoc = browserDoc(self)
|
|
|
|
|
2021-03-19 02:18:42 +00:00
|
|
|
self.cStatusBar = QStatusBar(self)
|
2021-03-19 18:12:28 +00:00
|
|
|
self.cStatusBar.showMessage("Please wait...")
|
2021-03-19 02:18:42 +00:00
|
|
|
|
2021-03-18 15:22:56 +00:00
|
|
|
self.cResizeElements()
|
|
|
|
self.show()
|
|
|
|
self.cNavigate()
|
2021-03-20 15:00:27 +00:00
|
|
|
self.cUrlBar.setFocus()
|
2021-03-18 15:22:56 +00:00
|
|
|
|
|
|
|
def cResizeElements(self):
|
|
|
|
barSize = 22
|
|
|
|
mb = self.cMenuBar.height()
|
|
|
|
self.cUrlBar.move(0,mb)
|
|
|
|
self.cUrlBar.resize(self.cWidth - barSize,barSize)
|
|
|
|
self.cButtonGo.move(self.cWidth - barSize,mb)
|
|
|
|
self.cButtonGo.resize(barSize,barSize)
|
|
|
|
|
|
|
|
self.cDoc.move(0,mb + barSize)
|
2021-03-19 02:18:42 +00:00
|
|
|
self.cDoc.resize(self.cWidth,self.cHeight - barSize - mb - mb)
|
|
|
|
|
|
|
|
self.cStatusBar.move(0,self.cHeight - mb)
|
|
|
|
self.cStatusBar.resize(self.cWidth,mb)
|
2021-03-18 15:22:56 +00:00
|
|
|
|
|
|
|
def resizeEvent(self,event):
|
|
|
|
self.cWidth = self.width()
|
|
|
|
self.cHeight = self.height()
|
|
|
|
self.cResizeElements()
|
|
|
|
|
2021-03-20 15:01:51 +00:00
|
|
|
def closeEvent(self,event):
|
2021-03-20 17:40:34 +00:00
|
|
|
browserWindowsLock.acquire()
|
2021-03-20 15:01:51 +00:00
|
|
|
browserWindows.remove(self)
|
2021-03-20 17:40:34 +00:00
|
|
|
self.destroy()
|
|
|
|
browserWindowsLock.release()
|
2021-03-20 15:01:51 +00:00
|
|
|
|
2021-03-20 19:02:05 +00:00
|
|
|
def cRunTasks(self):
|
|
|
|
self.cTaskLock.acquire()
|
|
|
|
for task in self.cTasks:
|
|
|
|
task[0](*task[1],**task[2])
|
|
|
|
self.cTasks = []
|
|
|
|
self.cTaskLock.release()
|
|
|
|
|
2021-03-18 15:22:56 +00:00
|
|
|
def cNavigate(self,event = None):
|
|
|
|
try:
|
2021-03-19 02:18:18 +00:00
|
|
|
#print(prettyJson(parseUrl(self.cUrlBar.text())))
|
2021-03-19 18:12:28 +00:00
|
|
|
self.cStatusBar.showMessage("Downloading...")
|
2021-03-20 19:02:05 +00:00
|
|
|
self.cStatusBar.repaint()
|
2021-03-19 02:18:18 +00:00
|
|
|
|
2021-03-20 19:02:05 +00:00
|
|
|
self.cTaskLock.acquire()
|
|
|
|
self.cDownloadId += 1
|
|
|
|
url = self.cUrlBar.text().replace(" ","%20") # dirty
|
2021-03-20 20:13:42 +00:00
|
|
|
urlParsed = parseUrl(url)
|
|
|
|
if urlParsed["protocol"] == "":
|
|
|
|
while len(url) > 0 and url[0] == "/":
|
|
|
|
url = url[1:]
|
|
|
|
url = config["default"]["defaultProtocol"]+ "://" +url
|
|
|
|
self.cUrlBar.setText(url)
|
2021-03-20 19:02:05 +00:00
|
|
|
threading.Thread(target=downloadPage, args=(self,self.cDownloadId,url,[("User-Agent",self.cUserAgent)])).start()
|
|
|
|
self.cTaskLock.release()
|
2021-03-20 15:00:27 +00:00
|
|
|
self.cDoc.setFocus()
|
2021-03-19 02:18:18 +00:00
|
|
|
#print(prettyJson(response["headers"]))
|
2021-03-18 15:22:56 +00:00
|
|
|
except Exception as e:
|
2021-03-20 17:39:54 +00:00
|
|
|
self.cDoc.cRenderHtml(html.escape(str(e)))
|
2021-03-18 15:22:56 +00:00
|
|
|
raise
|
2021-03-20 15:01:51 +00:00
|
|
|
|
2021-03-20 19:02:05 +00:00
|
|
|
def cHandleResponse(self,response):
|
|
|
|
infoFetcher(response)
|
|
|
|
self.cUrlBar.setText(response["url"])
|
|
|
|
self.cDocumentInfo = response
|
|
|
|
self.cStatusBar.showMessage("Rendering...")
|
|
|
|
self.cStatusBar.repaint()
|
|
|
|
start = time.time()
|
2021-03-25 19:34:16 +00:00
|
|
|
|
|
|
|
htm = response["body"]
|
|
|
|
contentType, contentTypeArguments = getContentType(response["headers"],"text")
|
|
|
|
if not "charset" in contentTypeArguments: contentTypeArguments["charset"] = "utf-8"
|
|
|
|
print("content-type: " +contentType+ "\n" +prettyJson(contentTypeArguments))
|
|
|
|
|
|
|
|
try:
|
|
|
|
htm = htm.decode(contentTypeArguments["charset"],errors="ignore")
|
|
|
|
except Exception as e:
|
|
|
|
print("decoding html as '" +contentTypeArguments["charset"]+ "' failed, trying utf-8...")
|
|
|
|
htm = htm.decode("utf-8",errors="ignore")
|
|
|
|
|
|
|
|
self.cDoc.cRenderHtml(htm,contentType)
|
2021-03-20 19:02:05 +00:00
|
|
|
end = time.time()
|
|
|
|
print("Rendering page: " +str(end - start))
|
|
|
|
self.cStatusBar.showMessage("Ready")
|
|
|
|
|
2021-03-20 15:01:51 +00:00
|
|
|
def cNewWindow(self):
|
|
|
|
w = browserWindow()
|
2021-03-20 17:40:34 +00:00
|
|
|
browserWindowsLock.acquire()
|
2021-03-20 15:01:51 +00:00
|
|
|
browserWindows.append(w)
|
|
|
|
w.show()
|
2021-03-20 17:40:34 +00:00
|
|
|
browserWindowsLock.release()
|
2021-03-18 15:22:56 +00:00
|
|
|
|
|
|
|
def main():
|
|
|
|
# load program default addons
|
|
|
|
addonDir = p(sp,"addons")
|
|
|
|
for root,dirs,files in os.walk(addonDir):
|
|
|
|
for file in files:
|
|
|
|
ffile = p(root,file)
|
|
|
|
lfile = ffile.replace(addonDir + os.path.sep,"",1)
|
|
|
|
if lfile[0] == "-": continue
|
|
|
|
print("Running addon: " +lfile)
|
|
|
|
runScript(ffile)
|
|
|
|
break
|
|
|
|
|
|
|
|
global dataDir
|
|
|
|
dataDir = getDataDir(distro)
|
|
|
|
|
|
|
|
global config
|
|
|
|
config = configparser.ConfigParser()
|
|
|
|
|
|
|
|
# read program default config
|
|
|
|
config.read(distro + ".ini")
|
|
|
|
|
|
|
|
# read user config
|
|
|
|
if os.path.isfile(p(dataDir,"config.ini")):
|
|
|
|
config.read(p(dataDir,"config.ini"))
|
|
|
|
|
|
|
|
app = QApplication(sys.argv)
|
2021-03-18 16:57:02 +00:00
|
|
|
try:
|
|
|
|
app.setWindowIcon(QtGui.QIcon("assets/BirdyNet-XP.ico"))
|
|
|
|
except Exception as e:
|
|
|
|
print("Could not set window icon: " +print(e))
|
2021-03-20 17:40:34 +00:00
|
|
|
browserWindowsLock.acquire()
|
2021-03-20 15:01:51 +00:00
|
|
|
browserWindows.append(browserWindow())
|
2021-03-20 17:40:34 +00:00
|
|
|
browserWindowsLock.release()
|
2021-03-18 15:22:56 +00:00
|
|
|
sys.exit(app.exec_())
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|