feat(html processor): add html processor to process template strings

This commit is contained in:
Halyul
2021-06-01 18:15:36 -04:00
parent 77e5eaeaa4
commit a05bbf7558
6 changed files with 76 additions and 32 deletions

View File

@@ -4,12 +4,14 @@ from lib.skeleton_binary import SkeletonBinary
from lib.alpha_composite import AlphaComposite
from lib.atlas_reader import AtlasReader
from lib.base64_util import *
from lib.html_processor import HtmlProcessor
class Builder:
def __init__(self, config, operator_names=list()) -> None:
self.operator_names = operator_names
self.config = config
self.html_processor = HtmlProcessor(config)
def start(self):
if "all" in self.operator_names:
@@ -175,7 +177,7 @@ class Builder:
operator_release_path = pathlib.Path.cwd().joinpath(target_path, operator_name)
release_operator_assets_path = pathlib.Path.cwd().joinpath(operator_release_path, ".{}".format(self.config["server"]["operator_folder"]))
operator_assets_path = pathlib.Path.cwd().joinpath(self.config["operators"][operator_name]["target_folder"].format(name=operator_name))
template_path = pathlib.Path.cwd().joinpath(".{}".format(self.config["server"]["template_folder"]))
template_path = pathlib.Path.cwd().joinpath(self.config["server"]["template_folder"])
if operator_release_path.exists() is True:
shutil.rmtree(operator_release_path)
@@ -200,11 +202,14 @@ class Builder:
if file.is_file() is True:
filename = file.name
file_path = pathlib.Path.cwd().joinpath(operator_release_path, filename)
shutil.copyfile(
file,
file_path
)
if filename == "index.html":
self.html_processor.build(operator_name, file, file_path)
else:
shutil.copyfile(
file,
file_path
)
elif file.is_dir() is True:
filename = file.name
file_path = pathlib.Path.cwd().joinpath(operator_release_path, filename)

View File

@@ -22,6 +22,7 @@ class Config:
project_json=str,
preview=str,
use_skel=bool,
title=str,
)
)
self.__read_config()

25
lib/html_processor.py Normal file
View File

@@ -0,0 +1,25 @@
import pathlib
class HtmlProcessor:
def __init__(self, config):
self.config = config
pass
def process(self, operator, file_path):
with open(pathlib.Path.cwd().joinpath(file_path), "r") as f:
content = f.read()
return content.format(
title=self.config["operators"][operator]["title"],
version=self.__get_version()
)
def build(self, operator, source_path, target_path):
content = self.process(operator, source_path)
with open(pathlib.Path.cwd().joinpath(target_path), "w") as f:
f.write(content)
def __get_version(self):
with open(pathlib.Path.cwd().joinpath("Version"), "r") as f:
version = f.read()
return version

View File

@@ -1,15 +1,16 @@
from http.server import SimpleHTTPRequestHandler
import pathlib
from http.server import SimpleHTTPRequestHandler
from socketserver import TCPServer
from lib.builder import Builder
from lib.html_processor import HtmlProcessor
class Server:
def __init__(self, port, operator, config) -> None:
self.config = config
self.operator = operator
self.port = port
self.httpd = TCPServer(("", port), httpd(operator, config["server"], directory=str(pathlib.Path.cwd())))
self.httpd = TCPServer(("", port), httpd(operator, config, directory=str(pathlib.Path.cwd())))
def start(self):
# build assets first
@@ -25,19 +26,30 @@ class Server:
class httpd(SimpleHTTPRequestHandler):
def __init__(self, operator, config, directory):
self.config = config
self.config = config["server"]
self.operator = operator
self.template_path = directory
self.html_processor = HtmlProcessor(config)
def __call__(self, *args, **kwds):
super().__init__(*args, directory=self.template_path, **kwds)
def do_GET(self):
# ignore query string
if "?" in self.path:
self.path = self.path.split("?")[0]
split_path = self.path.split("/")
access_path = "/{}/".format(split_path[1])
if self.path == "/":
self.path = self.config["template_folder"] + "index.html"
# self.path = self.config["template_folder"] + "index.html"
self.send_response(200)
self.send_header("Content-type", "text/html")
self.end_headers()
html = self.html_processor.process(self.operator, self.config["template_folder"] + "index.html")
self.wfile.write(bytes(html, "utf8"))
return
elif access_path == "/assets/":
# assets folder
self.path = self.config["template_folder"] + "assets/" + "/".join([i for i in split_path[2:]])