Skip to content

Commit

Permalink
prepare rule editing
Browse files Browse the repository at this point in the history
  • Loading branch information
cutoffthetop committed Dec 4, 2024
1 parent 6f0c5e5 commit 960394a
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 21 deletions.
1 change: 0 additions & 1 deletion mex/backend/constants.py

This file was deleted.

1 change: 0 additions & 1 deletion mex/backend/graph/connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,6 @@ def _merge_item(
nested_edge_labels=nested_edge_labels,
nested_node_labels=nested_node_labels,
)

return self.commit(
query,
**constraints,
Expand Down
12 changes: 4 additions & 8 deletions mex/backend/merged/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

from pydantic import Field, TypeAdapter, ValidationError

from mex.backend.constants import NUMBER_OF_RULE_TYPES
from mex.backend.fields import MERGEABLE_FIELDS_BY_CLASS_NAME
from mex.backend.graph.connector import GraphConnector
from mex.backend.graph.exceptions import InconsistentGraphError
Expand Down Expand Up @@ -158,18 +157,15 @@ def merge_search_result_item(item: dict[str, Any]) -> AnyMergedModel:
for component in item["components"]
if component["entityType"] in RULE_MODEL_CLASSES_BY_NAME
]
if len(rules_raw) == NUMBER_OF_RULE_TYPES:
rule_set_response = transform_raw_rules_to_rule_set_response(rules_raw)
elif len(rules_raw) == 0:
rule_set_response = None
if rules_raw:
rule_set = transform_raw_rules_to_rule_set_response(rules_raw)
else:
msg = f"Unexpected number of rules found in graph: {len(rules_raw)}"
raise InconsistentGraphError(msg)
rule_set = None

return create_merged_item(
identifier=item["identifier"],
extracted_items=extracted_items,
rule_set=rule_set_response,
rule_set=rule_set,
)


Expand Down
18 changes: 10 additions & 8 deletions mex/backend/rules/helpers.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from collections.abc import Mapping
from typing import Any, Final

from mex.backend.constants import NUMBER_OF_RULE_TYPES
from mex.backend.graph.connector import GraphConnector
from mex.backend.graph.exceptions import InconsistentGraphError
from mex.common.exceptions import MExError
from mex.common.models import (
ADDITIVE_MODEL_CLASSES_BY_NAME,
Expand Down Expand Up @@ -32,9 +32,9 @@ def transform_raw_rules_to_rule_set_response(
response: dict[str, Any] = {}
model: type[AnyRuleModel] | None

if len(raw_rules) != NUMBER_OF_RULE_TYPES:
msg = "inconsistent rule item count"
raise MExError(msg)
if (num_raw_rules := len(raw_rules)) != len(MODEL_CLASS_LOOKUP_BY_FIELD_NAME):
msg = f"inconsistent number of rules found: {num_raw_rules}"
raise InconsistentGraphError(msg)

for rule in raw_rules:
for field_name, model_class_lookup in MODEL_CLASS_LOOKUP_BY_FIELD_NAME.items():
Expand All @@ -45,10 +45,10 @@ def transform_raw_rules_to_rule_set_response(

if len(set(stem_types)) != 1:
msg = "inconsistent rule item stem types"
raise MExError(msg)
raise InconsistentGraphError(msg)
if len(set(stable_target_ids)) != 1:
msg = "inconsistent rule item stableTargetIds"
raise MExError(msg)
raise InconsistentGraphError(msg)

response["stableTargetId"] = stable_target_ids[0]
response_class_name = ensure_postfix(stem_types[0], "RuleSetResponse")
Expand Down Expand Up @@ -83,7 +83,7 @@ def create_and_get_rule_set(

def get_rule_set_from_graph(
stable_target_id: Identifier,
) -> AnyRuleSetResponse:
) -> AnyRuleSetResponse | None:
"""Read a rule set from the graph."""
connector = GraphConnector.get()
graph_result = connector.fetch_rule_items(
Expand All @@ -93,7 +93,9 @@ def get_rule_set_from_graph(
0,
3,
)
return transform_raw_rules_to_rule_set_response(graph_result.one()["items"])
if raw_rules := graph_result.one()["items"]:
return transform_raw_rules_to_rule_set_response(raw_rules)
return None


def update_and_get_rule_set(
Expand Down
6 changes: 4 additions & 2 deletions mex/backend/rules/main.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from fastapi import APIRouter
from fastapi import APIRouter, HTTPException
from starlette import status

from mex.backend.rules.helpers import (
Expand All @@ -21,7 +21,9 @@ def create_rule_set(rule_set: AnyRuleSetRequest) -> AnyRuleSetResponse:
@router.get("/rule-set/{stableTargetId}", tags=["editor"])
def get_rule_set(stableTargetId: Identifier) -> AnyRuleSetResponse:
"""Get a rule set."""
return get_rule_set_from_graph(stableTargetId)
if rule_set := get_rule_set_from_graph(stableTargetId):
return rule_set
raise HTTPException(status.HTTP_404_NOT_FOUND, "no rules found")


@router.put("/rule-set/{stableTargetId}", tags=["editor"])
Expand Down
3 changes: 2 additions & 1 deletion mex/backend/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ class BackendSettings(BaseSettings):
validation_alias="MEX_BACKEND_API_USER_DATABASE",
)
identity_provider: IdentityProvider | BackendIdentityProvider = Field(
IdentityProvider.MEMORY,
# IdentityProvider.MEMORY,
BackendIdentityProvider.GRAPH,
description="Provider to assign stableTargetIds to new model instances.",
validation_alias="MEX_IDENTITY_PROVIDER",
) # type: ignore[assignment]
2 changes: 2 additions & 0 deletions mex/backend/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,6 @@ def reraising(
try:
return fn(*args, **kwargs)
except original_error as error:
if isinstance(reraise_as, type):
reraise_as = reraise_as(*error.args)
raise reraise_as from error

0 comments on commit 960394a

Please sign in to comment.