Skip to content

Commit

Permalink
fix: remove not thread-safe template compile from parallel loop
Browse files Browse the repository at this point in the history
and catch exceptions
  • Loading branch information
simontaurus committed Sep 12, 2023
1 parent 2dfcce2 commit cb768d1
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 14 deletions.
27 changes: 19 additions & 8 deletions src/osw/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@

import osw.model.entity as model
from osw.model.static import OswBaseModel
from osw.utils.templates import eval_handlebars_template
from osw.utils.templates import (
compile_handlebars_template,
eval_compiled_handlebars_template,
)
from osw.utils.util import parallelize
from osw.utils.wiki import (
get_namespace,
Expand Down Expand Up @@ -595,6 +598,9 @@ def store_entity(
meta_category = self.site.get_page(
WtSite.GetPageParam(titles=[param.meta_category_title])
).pages[0]
meta_category_template = meta_category.get_slot_content("schema_template")
if meta_category_template:
meta_category_template = compile_handlebars_template(meta_category_template)

def store_entity_(
entity: model.Entity, namespace_: str = None, index: int = None
Expand All @@ -621,14 +627,19 @@ def store_entity_(
"footer", "{{#invoke:Entity|footer}}"
) # required for footer rendering
if namespace_ == "Category":
template = meta_category.get_slot_content("schema_template")
if template:
schema = json.loads(
eval_handlebars_template(
template, jsondata, {"_page_title": entity_title}
if meta_category_template:
try:
schema_str = eval_compiled_handlebars_template(
meta_category_template,
jsondata,
{"_page_title": entity_title},
)
schema = json.loads(schema_str)
page.set_slot_content("jsonschema", schema)
except Exception as e:
print(
f"Schema generation from template failed for {entity}: {e}"
)
)
page.set_slot_content("jsonschema", schema)
page.edit()
if index is None:
print(f"Entity stored at {page.get_url()}.")
Expand Down
61 changes: 55 additions & 6 deletions src/osw/utils/templates.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,56 @@
from pybars import Compiler


def compile_handlebars_template(template):
"""compiles a handlebars template.
WARNING: not thread safe!
Parameters
----------
template
the template string
Returns
-------
the compiled template
"""
compiler = Compiler()
template = compiler.compile(template)
return template


def eval_compiled_handlebars_template(
compiled_template, data, helpers={}, partials={}, add_self_as_partial=True
):
"""evaluates a compiled handlebars template with the given data
Parameters
----------
compiled_template
the compiled template
data
the data dictionary
helpers, optional
helper functions, by default {}
partials, optional
partials, by default {}
add_self_as_partial, optional
if true, add the compiled template as partial 'self', by default True
Returns
-------
the evaluated template as a string
"""
if add_self_as_partial:
partials["self"] = compiled_template
return compiled_template(data, helpers=helpers, partials=partials)


def eval_handlebars_template(
template, data, helpers={}, partials={}, add_self_as_partial=True
):
"""evaluates a handlebars template with the given data
"""evaluates a handlebars template with the given data.
WARNING: not thread safe!
Parameters
----------
Expand All @@ -23,8 +69,11 @@ def eval_handlebars_template(
-------
the evaluated template as a string
"""
compiler = Compiler()
template = compiler.compile(template)
if add_self_as_partial:
partials["self"] = template
return template(data, helpers=helpers, partials=partials)
compiled_template = compile_handlebars_template(template)
return eval_compiled_handlebars_template(
compiled_template,
data,
helpers,
partials,
add_self_as_partial,
)

0 comments on commit cb768d1

Please sign in to comment.