Skip to content

Commit cb768d1

Browse files
committed
fix: remove not thread-safe template compile from parallel loop
and catch exceptions
1 parent 2dfcce2 commit cb768d1

2 files changed

Lines changed: 74 additions & 14 deletions

File tree

src/osw/core.py

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@
1616

1717
import osw.model.entity as model
1818
from osw.model.static import OswBaseModel
19-
from osw.utils.templates import eval_handlebars_template
19+
from osw.utils.templates import (
20+
compile_handlebars_template,
21+
eval_compiled_handlebars_template,
22+
)
2023
from osw.utils.util import parallelize
2124
from osw.utils.wiki import (
2225
get_namespace,
@@ -595,6 +598,9 @@ def store_entity(
595598
meta_category = self.site.get_page(
596599
WtSite.GetPageParam(titles=[param.meta_category_title])
597600
).pages[0]
601+
meta_category_template = meta_category.get_slot_content("schema_template")
602+
if meta_category_template:
603+
meta_category_template = compile_handlebars_template(meta_category_template)
598604

599605
def store_entity_(
600606
entity: model.Entity, namespace_: str = None, index: int = None
@@ -621,14 +627,19 @@ def store_entity_(
621627
"footer", "{{#invoke:Entity|footer}}"
622628
) # required for footer rendering
623629
if namespace_ == "Category":
624-
template = meta_category.get_slot_content("schema_template")
625-
if template:
626-
schema = json.loads(
627-
eval_handlebars_template(
628-
template, jsondata, {"_page_title": entity_title}
630+
if meta_category_template:
631+
try:
632+
schema_str = eval_compiled_handlebars_template(
633+
meta_category_template,
634+
jsondata,
635+
{"_page_title": entity_title},
636+
)
637+
schema = json.loads(schema_str)
638+
page.set_slot_content("jsonschema", schema)
639+
except Exception as e:
640+
print(
641+
f"Schema generation from template failed for {entity}: {e}"
629642
)
630-
)
631-
page.set_slot_content("jsonschema", schema)
632643
page.edit()
633644
if index is None:
634645
print(f"Entity stored at {page.get_url()}.")

src/osw/utils/templates.py

Lines changed: 55 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,56 @@
11
from pybars import Compiler
22

33

4+
def compile_handlebars_template(template):
5+
"""compiles a handlebars template.
6+
WARNING: not thread safe!
7+
8+
Parameters
9+
----------
10+
template
11+
the template string
12+
13+
Returns
14+
-------
15+
the compiled template
16+
"""
17+
compiler = Compiler()
18+
template = compiler.compile(template)
19+
return template
20+
21+
22+
def eval_compiled_handlebars_template(
23+
compiled_template, data, helpers={}, partials={}, add_self_as_partial=True
24+
):
25+
"""evaluates a compiled handlebars template with the given data
26+
27+
Parameters
28+
----------
29+
compiled_template
30+
the compiled template
31+
data
32+
the data dictionary
33+
helpers, optional
34+
helper functions, by default {}
35+
partials, optional
36+
partials, by default {}
37+
add_self_as_partial, optional
38+
if true, add the compiled template as partial 'self', by default True
39+
40+
Returns
41+
-------
42+
the evaluated template as a string
43+
"""
44+
if add_self_as_partial:
45+
partials["self"] = compiled_template
46+
return compiled_template(data, helpers=helpers, partials=partials)
47+
48+
449
def eval_handlebars_template(
550
template, data, helpers={}, partials={}, add_self_as_partial=True
651
):
7-
"""evaluates a handlebars template with the given data
52+
"""evaluates a handlebars template with the given data.
53+
WARNING: not thread safe!
854
955
Parameters
1056
----------
@@ -23,8 +69,11 @@ def eval_handlebars_template(
2369
-------
2470
the evaluated template as a string
2571
"""
26-
compiler = Compiler()
27-
template = compiler.compile(template)
28-
if add_self_as_partial:
29-
partials["self"] = template
30-
return template(data, helpers=helpers, partials=partials)
72+
compiled_template = compile_handlebars_template(template)
73+
return eval_compiled_handlebars_template(
74+
compiled_template,
75+
data,
76+
helpers,
77+
partials,
78+
add_self_as_partial,
79+
)

0 commit comments

Comments
 (0)