feat(builder): use a new content processor
This commit is contained in:
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