feat(server): adapt the content processor for server

This commit is contained in:
Haoyu Xu
2021-08-08 15:41:07 -04:00
parent 896060d740
commit 7099bc8c27
3 changed files with 54 additions and 15 deletions

View File

@@ -12,19 +12,50 @@ class Builder:
self.operator_names = operator_names
self.config = config
self.rebuild = rebuild
self.content_processor = None
def start(self):
if "all" in self.operator_names:
self.operator_names = [operator_name for operator_name in self.config["operators"]]
for operator_name in self.operator_names:
self.build_assets(operator_name)
self.build(operator_name)
self.__release_file(operator_name)
return
def stop(self):
return
def build_assets(self, operator_name):
def build(self, operator_name):
self.content_processor = ContentProcessor(self.config, operator_name)
assets_thread = threading.Thread(
target=self.__build_assets,
args=(operator_name,),
daemon=True,
)
settings_thread = threading.Thread(
target=self.__build_settings,
args=(operator_name,),
daemon=True,
)
assets_thread.start()
settings_thread.start()
assets_thread.join()
settings_thread.join()
# use content processor to generate operator settings
def __build_settings(self, operator_name):
source_path = pathlib.Path.cwd().joinpath("operator", operator_name, "config")
target_path = pathlib.Path.cwd().joinpath("operator", operator_name)
for file in source_path.iterdir():
if file.is_file() is True:
file_path = pathlib.Path.cwd().joinpath(target_path, file.name)
self.content_processor.build(
file,
file_path
)
return
def __build_assets(self, operator_name):
use_skel = self.config["operators"][operator_name]["use_skel"]
source_path = self.config["operators"][operator_name]["source_folder"].format(name=operator_name)
@@ -39,7 +70,7 @@ class Builder:
operator_file = pathlib.Path.cwd().joinpath(target_path, "..", "operator_assets.js")
if operator_file.exists() is False or self.rebuild is True:
print("Building operaotr data for {}...".format(operator_name))
print("Building operator data for {}...".format(operator_name))
alpha_composite_threads = list()
png_to_base64_threads = list()
@@ -142,9 +173,9 @@ class Builder:
with open(operator_file, "w") as f:
f.write(jsonContent)
print("Finished building operaotr data for {}.".format(operator_name))
print("Finished building operator data for {}.".format(operator_name))
else:
print("Operaotr data for {} has been built.".format(operator_name))
print("Operator data for {} has been built.".format(operator_name))
return
def __json_to_base64(self, path, dict=None, key=None):
@@ -190,7 +221,6 @@ class Builder:
release_operator_assets_path = pathlib.Path.cwd().joinpath(operator_release_path, 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(self.config["server"]["template_folder"])
content_processor = ContentProcessor(self.config, operator_name)
if operator_release_path.exists() is True:
shutil.rmtree(operator_release_path)
@@ -205,13 +235,17 @@ class Builder:
else:
file_path = pathlib.Path.cwd().joinpath(release_operator_assets_path, filename)
content_processor.build(file, file_path)
shutil.copyfile(
file,
file_path
)
# template folder uses content processor to generate files
for file in template_path.iterdir():
if file.is_file() is True:
file_path = pathlib.Path.cwd().joinpath(operator_release_path, file.name)
content_processor.build(file, file_path)
self.content_processor.build(file, file_path)
elif file.is_dir() is True:
file_path = pathlib.Path.cwd().joinpath(operator_release_path, file.name)

View File

@@ -13,9 +13,14 @@ class ContentProcessor:
self.__process_value()
def process(self, file_path):
with open(pathlib.Path.cwd().joinpath(file_path), "r") as f:
content = Formatter(f.read(), "{# ", " #}")
file_path = pathlib.Path.cwd().joinpath(file_path)
with open(file_path, "r") as f:
content = f.read()
if file_path.name in self.file_to_process:
content = Formatter(content, "{# ", " #}")
return content.format(**self.settings[file_path.name])
else:
return content
def build(self, source_path, target_path):
if source_path.name in self.file_to_process:

View File

@@ -3,7 +3,7 @@ from http.server import SimpleHTTPRequestHandler
from socketserver import TCPServer
from lib.builder import Builder
from lib.html_processor import HtmlProcessor
from lib.content_processor import ContentProcessor
class Server:
def __init__(self, port, operator, config, rebuild) -> None:
@@ -15,7 +15,7 @@ class Server:
def start(self):
# build assets first
Builder(self.config, rebuild=self.rebuild).build_assets(self.operator)
Builder(self.config, rebuild=self.rebuild).build(self.operator)
print("Server is up at 0.0.0.0:{port}".format(port=self.port))
self.httpd.serve_forever()
return
@@ -30,7 +30,7 @@ class httpd(SimpleHTTPRequestHandler):
self.config = config["server"]
self.operator = operator
self.template_path = directory
self.html_processor = HtmlProcessor(config)
self.content_processor = ContentProcessor(config, operator)
def __call__(self, *args, **kwds):
super().__init__(*args, directory=self.template_path, **kwds)
@@ -48,7 +48,7 @@ class httpd(SimpleHTTPRequestHandler):
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")
html = self.content_processor.process(self.config["template_folder"] + "index.html")
self.wfile.write(bytes(html, "utf8"))
return
elif access_path == "./assets/":