created the whole export function minus some todos

This commit is contained in:
vicky 2022-05-07 20:16:14 +02:00
parent db70eeb69e
commit c540c66f6f
1 changed files with 109 additions and 4 deletions

View File

@ -1,9 +1,114 @@
# Module for final generation of the translated website
import rich
from bootstraparse.modules import config, pathresolver
from collections import namedtuple
all_configs = config.ConfigLoader(pathresolver.b_path("configs/"))
all_templates = config.ConfigLoader(pathresolver.b_path("templates/"))
rich.inspect(all_configs)
rich.inspect(all_templates)
"""
Named tuple containing all necessary information to select the appropriate
markup element and pass it over to ExportManager.
"""
ExportRequest = namedtuple("ExportRequest", ["type", "subtype", "optionals", "others"], defaults=[None, None, "", {}])
"""
Class containing all necessary information to proceed with the
final output.
"""
ExportResponse = namedtuple("ExportResponse", ["start", "end"])
class ExportManager:
"""
Transforms ExportRequest tuples to ExportResponse tuples with the config-provided appropriate markup.
"""
def __init__(self, cnoifg, templates):
# self.config = cnoifg TODO: actually import config
# self.templates = templates TODO: actually import templates
self.config = config.ConfigLoader(pathresolver.b_path("configs/"))
self.templates = config.ConfigLoader(pathresolver.b_path("templates/"))
self.advanced_export = {
"header": self.header_transform,
"display": self.display_transform,
"link": self.link_transform,
"t_head": self.t_transform,
"t_row": self.t_transform,
"t_cell": self.t_transform,
}
def __call__(self, export_request):
return self.transform(export_request)
def transform(self, export_request):
"""
Transformation function to magically poof ExportRequest tuples into
ExportResponse tuples using the loaded config.
:param export_request: ExportRequest tuples
:type export_request: ExportRequest
:return: ExportResponse tuples
:rtype: ExportResponse
"""
if export_request.subtype in self.advanced_export:
return self.advanced_export[export_request.subtype](export_request)
# return self.__getattribute__(export_request.subtype + "_transform")() # alternative method
else:
return self.basic_transform(export_request)
def _get_template(self, export_request):
"""
Function for initializing other transform functions.
:return: start, end, optionals
"""
start, end = self.templates["bootstrap"][export_request.type][export_request.subtype]
# TODO: make it so "bootstrap" isn't necessary
# TODO: add helpful error message in case of KeyError
if export_request.optionals != '':
optionals = " " + export_request.optionals
else:
optionals = ''
return start, end, optionals
def basic_transform(self, export_request):
"""
Basic template for ExportResponse objects.
:param export_request: ExportRequest tuples
:type export_request: ExportRequest
:return: ExportResponse tuples
:rtype: ExportResponse
"""
start, end, optionals = self._get_template(export_request)
start = start.format(optionals=optionals)
return ExportResponse(start, end)
def header_transform(self, export_request):
"""
Specific template for headers.
"""
start, end, optionals = self._get_template(export_request)
start = start.format(optionals=optionals, header_level=export_request.others["header_level"])
# TODO: add helpful message if KeyError
end = end.format(header_level=export_request.others["header_level"])
return ExportResponse(start, end)
def display_transform(self, export_request):
"""
Specific template for display.
"""
start, end, optionals = self._get_template(export_request)
start = start.format(optionals=optionals, display_level=export_request.others["display_level"])
return ExportResponse(start, end)
def link_transform(self, export_request):
"""
Specific template for link.
"""
start, end, _ = self._get_template(export_request)
start = start.format(url=export_request.others["url"])
return ExportResponse(start, end)
def t_transform(self, export_request):
"""
Specific template for table content.
"""
start, end, _ = self._get_template(export_request)
start = start.format(col_span=export_request.others["col_span"])
return ExportResponse(start, end)