feat(server): adapt the content processor for server
This commit is contained in:
@@ -12,19 +12,50 @@ class Builder:
|
|||||||
self.operator_names = operator_names
|
self.operator_names = operator_names
|
||||||
self.config = config
|
self.config = config
|
||||||
self.rebuild = rebuild
|
self.rebuild = rebuild
|
||||||
|
self.content_processor = None
|
||||||
|
|
||||||
def start(self):
|
def start(self):
|
||||||
if "all" in self.operator_names:
|
if "all" in self.operator_names:
|
||||||
self.operator_names = [operator_name for operator_name in self.config["operators"]]
|
self.operator_names = [operator_name for operator_name in self.config["operators"]]
|
||||||
for operator_name in self.operator_names:
|
for operator_name in self.operator_names:
|
||||||
self.build_assets(operator_name)
|
self.build(operator_name)
|
||||||
self.__release_file(operator_name)
|
self.__release_file(operator_name)
|
||||||
return
|
return
|
||||||
|
|
||||||
def stop(self):
|
def stop(self):
|
||||||
return
|
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"]
|
use_skel = self.config["operators"][operator_name]["use_skel"]
|
||||||
source_path = self.config["operators"][operator_name]["source_folder"].format(name=operator_name)
|
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")
|
operator_file = pathlib.Path.cwd().joinpath(target_path, "..", "operator_assets.js")
|
||||||
if operator_file.exists() is False or self.rebuild is True:
|
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()
|
alpha_composite_threads = list()
|
||||||
png_to_base64_threads = list()
|
png_to_base64_threads = list()
|
||||||
@@ -142,9 +173,9 @@ class Builder:
|
|||||||
with open(operator_file, "w") as f:
|
with open(operator_file, "w") as f:
|
||||||
f.write(jsonContent)
|
f.write(jsonContent)
|
||||||
|
|
||||||
print("Finished building operaotr data for {}.".format(operator_name))
|
print("Finished building operator data for {}.".format(operator_name))
|
||||||
else:
|
else:
|
||||||
print("Operaotr data for {} has been built.".format(operator_name))
|
print("Operator data for {} has been built.".format(operator_name))
|
||||||
return
|
return
|
||||||
|
|
||||||
def __json_to_base64(self, path, dict=None, key=None):
|
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"])
|
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), "..")
|
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"])
|
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:
|
if operator_release_path.exists() is True:
|
||||||
shutil.rmtree(operator_release_path)
|
shutil.rmtree(operator_release_path)
|
||||||
@@ -205,13 +235,17 @@ class Builder:
|
|||||||
else:
|
else:
|
||||||
file_path = pathlib.Path.cwd().joinpath(release_operator_assets_path, filename)
|
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():
|
for file in template_path.iterdir():
|
||||||
if file.is_file() is True:
|
if file.is_file() is True:
|
||||||
file_path = pathlib.Path.cwd().joinpath(operator_release_path, file.name)
|
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:
|
elif file.is_dir() is True:
|
||||||
file_path = pathlib.Path.cwd().joinpath(operator_release_path, file.name)
|
file_path = pathlib.Path.cwd().joinpath(operator_release_path, file.name)
|
||||||
|
|
||||||
|
|||||||
@@ -13,9 +13,14 @@ class ContentProcessor:
|
|||||||
self.__process_value()
|
self.__process_value()
|
||||||
|
|
||||||
def process(self, file_path):
|
def process(self, file_path):
|
||||||
with open(pathlib.Path.cwd().joinpath(file_path), "r") as f:
|
file_path = pathlib.Path.cwd().joinpath(file_path)
|
||||||
content = Formatter(f.read(), "{# ", " #}")
|
with open(file_path, "r") as f:
|
||||||
return content.format(**self.settings[file_path.name])
|
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):
|
def build(self, source_path, target_path):
|
||||||
if source_path.name in self.file_to_process:
|
if source_path.name in self.file_to_process:
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ from http.server import SimpleHTTPRequestHandler
|
|||||||
from socketserver import TCPServer
|
from socketserver import TCPServer
|
||||||
|
|
||||||
from lib.builder import Builder
|
from lib.builder import Builder
|
||||||
from lib.html_processor import HtmlProcessor
|
from lib.content_processor import ContentProcessor
|
||||||
class Server:
|
class Server:
|
||||||
|
|
||||||
def __init__(self, port, operator, config, rebuild) -> None:
|
def __init__(self, port, operator, config, rebuild) -> None:
|
||||||
@@ -15,7 +15,7 @@ class Server:
|
|||||||
|
|
||||||
def start(self):
|
def start(self):
|
||||||
# build assets first
|
# 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))
|
print("Server is up at 0.0.0.0:{port}".format(port=self.port))
|
||||||
self.httpd.serve_forever()
|
self.httpd.serve_forever()
|
||||||
return
|
return
|
||||||
@@ -30,7 +30,7 @@ class httpd(SimpleHTTPRequestHandler):
|
|||||||
self.config = config["server"]
|
self.config = config["server"]
|
||||||
self.operator = operator
|
self.operator = operator
|
||||||
self.template_path = directory
|
self.template_path = directory
|
||||||
self.html_processor = HtmlProcessor(config)
|
self.content_processor = ContentProcessor(config, operator)
|
||||||
|
|
||||||
def __call__(self, *args, **kwds):
|
def __call__(self, *args, **kwds):
|
||||||
super().__init__(*args, directory=self.template_path, **kwds)
|
super().__init__(*args, directory=self.template_path, **kwds)
|
||||||
@@ -48,7 +48,7 @@ class httpd(SimpleHTTPRequestHandler):
|
|||||||
self.send_response(200)
|
self.send_response(200)
|
||||||
self.send_header("Content-type", "text/html")
|
self.send_header("Content-type", "text/html")
|
||||||
self.end_headers()
|
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"))
|
self.wfile.write(bytes(html, "utf8"))
|
||||||
return
|
return
|
||||||
elif access_path == "./assets/":
|
elif access_path == "./assets/":
|
||||||
|
|||||||
Reference in New Issue
Block a user