|
5 | 5 | import asyncio |
6 | 6 | import logging |
7 | 7 | import uuid |
8 | | -from contextvars import ContextVar |
9 | | -from typing import Optional |
10 | 8 |
|
11 | 9 | from efoli import EdifactFormat, EdifactFormatVersion |
12 | 10 | from sqlalchemy import Index |
|
24 | 22 |
|
25 | 23 |
|
26 | 24 | try: |
27 | | - import inject |
28 | | - from ahbicht.content_evaluation.evaluationdatatypes import EvaluatableData, EvaluatableDataProvider |
29 | | - from ahbicht.content_evaluation.evaluator_factory import create_content_evaluation_result_based_evaluators |
30 | 25 | from ahbicht.content_evaluation.expression_check import is_valid_expression |
31 | | - from ahbicht.content_evaluation.token_logic_provider import SingletonTokenLogicProvider, TokenLogicProvider |
32 | 26 | from ahbicht.expressions.condition_expression_parser import extract_categorized_keys |
33 | | - from ahbicht.models.content_evaluation_result import ContentEvaluationResult |
34 | 27 | from lark.exceptions import VisitError |
35 | 28 | except ImportError as import_error: |
36 | 29 | import_error.msg += "; Did you install fundamend[sqlmodels,ahbicht]?" |
|
39 | 32 |
|
40 | 33 | _logger = logging.getLogger(__name__) |
41 | 34 |
|
42 | | -_content_evaluation_result: ContextVar[Optional[ContentEvaluationResult]] = ContextVar( |
43 | | - "_content_evaluation_result", default=None |
44 | | -) |
45 | | - |
46 | | - |
47 | | -def _get_evaluatable_data() -> EvaluatableData[ContentEvaluationResult]: |
48 | | - """ |
49 | | - returns the _content_evaluation_result context var value wrapped in a EvaluatableData container. |
50 | | - This is the kind of data that the ContentEvaluationResultBased RC/FC Evaluators, HintsProvider and Package Resolver |
51 | | - require. |
52 | | - :return: |
53 | | - """ |
54 | | - cer = _content_evaluation_result.get() |
55 | | - assert cer is not None |
56 | | - return EvaluatableData( |
57 | | - body=cer, |
58 | | - edifact_format=EdifactFormat.UTILMD, # not important, something has to be here |
59 | | - edifact_format_version=EdifactFormatVersion.FV2504, # not important, something has to be here |
60 | | - ) |
61 | | - |
62 | | - |
63 | | -def _setup_weird_ahbicht_dependency_injection() -> None: |
64 | | - def configure(binder: inject.Binder) -> None: |
65 | | - binder.bind( |
66 | | - TokenLogicProvider, |
67 | | - SingletonTokenLogicProvider( |
68 | | - [*create_content_evaluation_result_based_evaluators(EdifactFormat.UTILMD, EdifactFormatVersion.FV2504)] |
69 | | - ), |
70 | | - ) |
71 | | - binder.bind_to_provider(EvaluatableDataProvider, _get_evaluatable_data) |
72 | | - |
73 | | - inject.configure_once(configure) |
74 | | - |
75 | 35 |
|
76 | 36 | def _generate_node_texts(session: Session, expression: str, ahb_pk: uuid.UUID) -> str: |
77 | 37 | categorized_key_extract = asyncio.run(extract_categorized_keys(expression)) |
@@ -117,15 +77,22 @@ def _generate_node_texts(session: Session, expression: str, ahb_pk: uuid.UUID) - |
117 | 77 |
|
118 | 78 |
|
119 | 79 | def _get_validity_node_texts_and_error_message_cpu_intensive( |
120 | | - expression: str, session: Session, anwendungshandbuch_pk: uuid.UUID |
| 80 | + expression: str, |
| 81 | + session: Session, |
| 82 | + anwendungshandbuch_pk: uuid.UUID, |
| 83 | + edifact_format: EdifactFormat, |
| 84 | + edifact_format_version: EdifactFormatVersion, |
121 | 85 | ) -> tuple[bool, str, str | None]: |
| 86 | + is_valid = True # default: assume valid unless proven otherwise |
| 87 | + error_message: str | None = None |
122 | 88 | try: |
123 | | - is_valid, error_message = asyncio.run(is_valid_expression(expression, _content_evaluation_result.set)) |
| 89 | + is_valid, error_message = asyncio.run(is_valid_expression(expression, edifact_format, edifact_format_version)) |
124 | 90 | if is_valid: # we might actually get a meaningful node_texts even for invalid expressions, but I don't like it |
125 | 91 | node_texts = _generate_node_texts(session, expression, anwendungshandbuch_pk) |
126 | 92 | else: |
127 | 93 | node_texts = "" |
128 | 94 | except NotImplementedError: # ahbicht fault/missing feature -> act like it's valid |
| 95 | + is_valid = True |
129 | 96 | node_texts = _generate_node_texts(session, expression, anwendungshandbuch_pk) |
130 | 97 | error_message = None |
131 | 98 | return is_valid, node_texts, error_message |
@@ -157,7 +124,6 @@ def create_and_fill_ahb_expression_table(session: Session, use_cpu_intensive_val |
157 | 124 | outcomes. This leads to only few additional expressions marked as invalid but is very slow. |
158 | 125 | """ |
159 | 126 | rows: list[tuple[EdifactFormatVersion | None, str, str | None, uuid.UUID]] = [] |
160 | | - _setup_weird_ahbicht_dependency_injection() |
161 | 127 | for ahb_status_col in [ |
162 | 128 | AhbHierarchyMaterialized.segmentgroup_ahb_status, |
163 | 129 | AhbHierarchyMaterialized.segment_ahb_status, |
@@ -192,7 +158,7 @@ def create_and_fill_ahb_expression_table(session: Session, use_cpu_intensive_val |
192 | 158 | if use_cpu_intensive_validity_check: |
193 | 159 | # as of 2025-04-15 I have no clue how long this actually takes for all expressions |
194 | 160 | _, node_texts, error_message = _get_validity_node_texts_and_error_message_cpu_intensive( |
195 | | - expression, session, row[3] |
| 161 | + expression, session, row[3], EdifactFormat(row[1]), row[0] |
196 | 162 | ) |
197 | 163 | else: |
198 | 164 | _, node_texts, error_message = _get_validity_node_texts_and_error_message_fast(expression, session, row[3]) |
|
0 commit comments