feat(builder): use a new content processor
This commit is contained in:
@@ -4,14 +4,13 @@ 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
|
||||
from lib.content_processor import ContentProcessor
|
||||
|
||||
class Builder:
|
||||
|
||||
def __init__(self, config, operator_names=list(), rebuild=False) -> None:
|
||||
self.operator_names = operator_names
|
||||
self.config = config
|
||||
self.html_processor = HtmlProcessor(config)
|
||||
self.rebuild = rebuild
|
||||
|
||||
def start(self):
|
||||
@@ -191,6 +190,7 @@ 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,28 +205,15 @@ class Builder:
|
||||
else:
|
||||
file_path = pathlib.Path.cwd().joinpath(release_operator_assets_path, filename)
|
||||
|
||||
shutil.copyfile(
|
||||
file,
|
||||
file_path
|
||||
)
|
||||
content_processor.build(file, file_path)
|
||||
|
||||
for file in template_path.iterdir():
|
||||
if file.is_file() is True:
|
||||
__temp = file.name.split(".")
|
||||
filename = __temp[0]
|
||||
fileext = __temp[1]
|
||||
file_path = pathlib.Path.cwd().joinpath(operator_release_path, file.name)
|
||||
|
||||
if filename == "index" and fileext == "html":
|
||||
self.html_processor.build(operator_name, file, file_path)
|
||||
else:
|
||||
shutil.copyfile(
|
||||
file,
|
||||
file_path
|
||||
)
|
||||
content_processor.build(file, file_path)
|
||||
elif file.is_dir() is True:
|
||||
filename = file.name
|
||||
file_path = pathlib.Path.cwd().joinpath(operator_release_path, filename)
|
||||
file_path = pathlib.Path.cwd().joinpath(operator_release_path, file.name)
|
||||
|
||||
shutil.copytree(
|
||||
file,
|
||||
|
||||
@@ -25,6 +25,7 @@ class Config:
|
||||
preview=str,
|
||||
use_skel=bool,
|
||||
title=str,
|
||||
config=dict,
|
||||
)
|
||||
)
|
||||
self.__read_config()
|
||||
@@ -48,8 +49,8 @@ class Config:
|
||||
for operator_name, operator_content in self.config[key].items():
|
||||
self.__config_check(operator_name, operator_content, self.valid_keys[key])
|
||||
|
||||
with open(self.config_path, 'w') as f:
|
||||
yaml.safe_dump(self.config, f, allow_unicode=True)
|
||||
# with open(self.config_path, 'w') as f:
|
||||
# yaml.safe_dump(self.config, f, allow_unicode=True)
|
||||
|
||||
def __config_check(self, block_name: str, contents: dict, required_keys: dict):
|
||||
checklist = [key for key in required_keys]
|
||||
|
||||
58
lib/content_processor.py
Normal file
58
lib/content_processor.py
Normal file
@@ -0,0 +1,58 @@
|
||||
import pathlib
|
||||
import shutil
|
||||
|
||||
class ContentProcessor:
|
||||
|
||||
def __init__(self, config, operator_name):
|
||||
self.config = config["operators"][operator_name]
|
||||
self.file_to_process = [key for key, value in self.config["config"].items()]
|
||||
self.settings = self.config["config"]
|
||||
self.evalable = [
|
||||
"__get_version"
|
||||
]
|
||||
self.__process_value()
|
||||
|
||||
def process(self, file_path):
|
||||
with open(pathlib.Path.cwd().joinpath(file_path), "r") as f:
|
||||
content = Formatter(f.read(), "{# ", " #}")
|
||||
return content.format(**self.settings[file_path.name])
|
||||
|
||||
def build(self, source_path, target_path):
|
||||
if source_path.name in self.file_to_process:
|
||||
content = self.process(source_path)
|
||||
with open(pathlib.Path.cwd().joinpath(target_path), "w") as f:
|
||||
f.write(content)
|
||||
else:
|
||||
shutil.copyfile(
|
||||
source_path,
|
||||
target_path
|
||||
)
|
||||
|
||||
def __process_value(self):
|
||||
for item_key, item_value in self.settings.items():
|
||||
for key, value in item_value.items():
|
||||
replace_value = value
|
||||
# if value in evalable
|
||||
if value in self.evalable:
|
||||
if value == "__get_version":
|
||||
replace_value = self.__get_version()
|
||||
else:
|
||||
raise Exception("Unsupported function name: {}".format(value))
|
||||
self.settings[item_key][key] = replace_value
|
||||
|
||||
def __get_version(self):
|
||||
with open(pathlib.Path.cwd().joinpath("Version"), "r") as f:
|
||||
version = f.read()
|
||||
return version
|
||||
|
||||
class Formatter:
|
||||
def __init__(self, content, start, end):
|
||||
self.content = content
|
||||
self.start = start
|
||||
self.end = end
|
||||
|
||||
def format(self, **kwargs):
|
||||
for key, value in kwargs.items():
|
||||
identifier = self.start + key + self.end
|
||||
self.content = self.content.replace(identifier, str(value))
|
||||
return self.content
|
||||
Reference in New Issue
Block a user