refactor: address code review feedback - simplify dispatch, add warning log
Co-authored-by: christianlouis <361235+christianlouis@users.noreply.github.com>
This commit is contained in:
@@ -103,6 +103,7 @@ def classify_document_task(
|
|||||||
try:
|
try:
|
||||||
existing_metadata = json.loads(file_record.ai_metadata)
|
existing_metadata = json.loads(file_record.ai_metadata)
|
||||||
except (json.JSONDecodeError, TypeError):
|
except (json.JSONDecodeError, TypeError):
|
||||||
|
logger.warning("Failed to parse ai_metadata for file %s, starting fresh", file_id)
|
||||||
existing_metadata = {}
|
existing_metadata = {}
|
||||||
|
|
||||||
# Load custom rules
|
# Load custom rules
|
||||||
|
|||||||
@@ -257,10 +257,10 @@ def _match_metadata(rule: ClassificationRule, metadata: dict[str, Any] | None) -
|
|||||||
return str(actual).lower() == expected_value.strip().lower()
|
return str(actual).lower() == expected_value.strip().lower()
|
||||||
|
|
||||||
|
|
||||||
_MATCHERS = {
|
_MATCHERS: dict[str, tuple] = {
|
||||||
RULE_TYPE_FILENAME: _match_filename,
|
RULE_TYPE_FILENAME: (_match_filename, "filename"),
|
||||||
RULE_TYPE_CONTENT: _match_content,
|
RULE_TYPE_CONTENT: (_match_content, "text"),
|
||||||
RULE_TYPE_METADATA: _match_metadata,
|
RULE_TYPE_METADATA: (_match_metadata, "metadata"),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -271,19 +271,13 @@ def _evaluate_rule(
|
|||||||
metadata: dict[str, Any] | None,
|
metadata: dict[str, Any] | None,
|
||||||
) -> MatchedRule | None:
|
) -> MatchedRule | None:
|
||||||
"""Evaluate a single rule against the document. Return a :class:`MatchedRule` on match."""
|
"""Evaluate a single rule against the document. Return a :class:`MatchedRule` on match."""
|
||||||
matcher = _MATCHERS.get(rule.rule_type)
|
entry = _MATCHERS.get(rule.rule_type)
|
||||||
if matcher is None:
|
if entry is None:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
# Dispatch to the appropriate matcher based on rule type
|
matcher, arg_key = entry
|
||||||
if rule.rule_type == RULE_TYPE_FILENAME:
|
arg_map = {"filename": filename, "text": text, "metadata": metadata}
|
||||||
matched = matcher(rule, filename)
|
matched = matcher(rule, arg_map[arg_key])
|
||||||
elif rule.rule_type == RULE_TYPE_CONTENT:
|
|
||||||
matched = matcher(rule, text)
|
|
||||||
elif rule.rule_type == RULE_TYPE_METADATA:
|
|
||||||
matched = matcher(rule, metadata)
|
|
||||||
else:
|
|
||||||
matched = False
|
|
||||||
|
|
||||||
if matched:
|
if matched:
|
||||||
return MatchedRule(
|
return MatchedRule(
|
||||||
|
|||||||
Reference in New Issue
Block a user