From e9b2653ad0d2e441689b2bcbc7f44ed973114e3a Mon Sep 17 00:00:00 2001 From: Alexander Popov Date: Mon, 4 Dec 2023 20:41:49 +0300 Subject: [PATCH] Update GUI building script --- README.md | 5 +- build_gui.py | 179 +++++++++++++++++++++++++++---------------------- gui/index.html | 2 +- pyproject.toml | 4 ++ 4 files changed, 106 insertions(+), 84 deletions(-) create mode 100644 pyproject.toml diff --git a/README.md b/README.md index eea5739..652b2bd 100644 --- a/README.md +++ b/README.md @@ -1 +1,4 @@ -* [Ubuntu](https://assets.ubuntu.com/v1/0cef8205-ubuntu-font-family-0.83.zip) font family. +## 🧰 Завиисимости для сборки: + +* webui 2.4.2 +* Семейство шрифтов [Ubuntu](https://assets.ubuntu.com/v1/0cef8205-ubuntu-font-family-0.83.zip) diff --git a/build_gui.py b/build_gui.py index 45d3d18..1e43ffa 100644 --- a/build_gui.py +++ b/build_gui.py @@ -6,99 +6,114 @@ from bs4 import BeautifulSoup, Tag from jsmin import jsmin from csscompressor import compress -# html param -html = sys.argv[1] -# target param -target = sys.argv[2] -# path from html param -path = re.sub(r"[^\/]*$", "", html) -# open html file -soup = BeautifulSoup(open(html), 'html.parser') -# find last script as anchorpoint -lastScript = soup.findAll("script", attrs = {"src" : True})[-1] -# get all scripts containing src attribute (= external scripts) -scripts = soup.findAll("script", attrs = {"src" : True}) -# find last style link as anchorpoint -lastStylesheet = soup.findAll("link", attrs = {"rel" : "stylesheet"})[-1] -# get all links to css stylesheets -stylesheets = soup.findAll("link", attrs = {"rel" : "stylesheet"}) +if __name__ == '__main__': + # html input param + html = sys.argv[1] + # target output param + target = sys.argv[2] -# create list of script srcs -print("\nRead Scripts:") -scriptsSrc = deque() -for script in scripts: - scriptsSrc.append(path + script.attrs["src"]) - print("\t" + path + script.attrs["src"]) + # path from html param + path = re.sub(r'[^\/]*$', '', html) -# create list of stylesheets srcs -print("\nRead Stylesheets:") -stylesheetsSrc = deque() -for stylesheet in stylesheets: - stylesheetsSrc.append(path + stylesheet.attrs["href"]) - print("\t" + path + stylesheet.attrs["href"]) + # open html file + print('📂 Open HTML file...', end=' ') + soup = BeautifulSoup(open(html), 'html.parser') + print('OK!') -# merge scripts to temp.js -print("\nMerge Scripts:") -print("\t", end="") -with open("temp.js", "w") as outfileScript: - for fname in scriptsSrc: - # add space every script - if fname != 'gui//webui.js': - outfileScript.write("\n") - print("~", end="") + # find last script as anchorpoint + lastScript = soup.findAll('script', attrs={'src': True})[-1] + # get all scripts containing src attribute (= external scripts) + scripts = soup.findAll('script', attrs={'src': True}) + + # find last style link as anchorpoint + lastStylesheet = soup.findAll('link', attrs={'rel': 'stylesheet'})[-1] + # get all links to css stylesheets + stylesheets = soup.findAll('link', attrs={'rel': 'stylesheet'}) + + # create list of script srcs + print('🔎 Create list of scripts...', end=' ') + scriptsSrc = deque() + for script in scripts: + scriptsSrc.append(path + script.attrs['src']) + print('Complete!') + + # create list of stylesheets srcs + print('🔎 Create list of stylesheets...', end=' ') + stylesheetsSrc = deque() + for stylesheet in stylesheets: + stylesheetsSrc.append(path + stylesheet.attrs['href']) + print('Complete!') + + # merge scripts to .temp.js + print('📥 Merge scripts...', end=' ') + with open('.temp.js', 'w') as outfileScript: + for fname in scriptsSrc: + # add space every script + if fname != 'gui//webui.js': + outfileScript.write('\n') + with open(fname) as infile: + for line in infile: + outfileScript.write(line) + print('Complete!') + + print('📥 Merge stylsheets...', end=' ') + # merge stylsheets to temp.css + with open('.temp.css', 'w') as outfileCSS: + for fname in stylesheetsSrc: + # add space every script + outfileCSS.write('\n') with open(fname) as infile: for line in infile: - outfileScript.write(line) -print("\n"); + outfileCSS.write(line) + print('Complete!') -# merge stylsheets to temp.css -print("Merge Stylesheets:") -print("\t", end="") -with open("temp.css", "w") as outfileCSS: - for fname in stylesheetsSrc: - # add space every script - outfileCSS.write("\n") - print("~", end="") - with open(fname) as infile: - for line in infile: - outfileCSS.write(line) -print("\n"); + # minify javascript + print('🗃️ Minify scripts...', end=' ') + with open('.temp.js') as js: + minified_js = jsmin(js.read()) + print('Complete!') -# minify javascript -print("Minify temp.js\n\t~") -with open("temp.js") as js: - minified_js = jsmin(js.read()) + # minify css + print('🗃️ Minify stylsheets...', end=' ') + with open('.temp.css') as css: + minified_css = compress(css.read()) + print('Complete!') -# minify css -print("\nMinify temp.css\n\t~") -with open("temp.css") as css: - minified_css = compress(css.read()) + # replace scripts with merged and min embed script / css + print('🔄 Embedding script and stylsheets...', end=' ') + tag = soup.new_tag('script') + tag['type'] = 'text/javascript' + tag.append(minified_js) + lastScript.replace_with(tag) -# replace scripts with merged and min embed script / css -print("\nReplacing and deleting\n\t~") -tag = soup.new_tag("script") -tag["type"] = "text/javascript" -tag.append(minified_js) -lastScript.replace_with(tag) + webui_tag = soup.new_tag('script') + webui_tag['type'] = 'text/javascript' + webui_tag['src'] = '/webui.js' + tag.insert_before(webui_tag) -tag = soup.new_tag("style") -tag["type"] = "text/css" -tag.append(minified_css) -lastStylesheet.replace_with(tag) + tag = soup.new_tag('style') + tag['type'] = 'text/css' + tag.append(minified_css) + lastStylesheet.replace_with(tag) + print('Complete!') -#remove script and style tags -for script in scripts: - script.decompose() -for stylesheet in stylesheets: - stylesheet.decompose() + # remove script and style tags + print('🧹 Cleaning...', end=' ') + for script in scripts: + script.decompose() + for stylesheet in stylesheets: + stylesheet.decompose() -#remove temp -os.remove("temp.js") -os.remove("temp.css") + # remove temp files + os.remove('.temp.js') + os.remove('.temp.css') + print('Complete!') -#save html as target -file = open(target,"w") -file.write(soup.prettify()) -file.close() + # save html as target + print('💾 Save builded document...', end=' ') + file = open(target, 'w') + file.write(soup.prettify()) + file.close() + print('Complete!', end='\n\n') -print("\nFIN\n") + print('🏁 Complete') diff --git a/gui/index.html b/gui/index.html index 88d2898..deed424 100644 --- a/gui/index.html +++ b/gui/index.html @@ -7,8 +7,8 @@ - +
diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000..cab6ef7 --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,4 @@ +[tool.black] +skip-string-normalization = true +pycodestyle = true +line-length = 100