pycasbin
pycasbin copied to clipboard
consider support async matcher function
Action
I want author to provide some directions about interface design if he agreed. so I or others can have confident in creating PR.
Rationale
AsyncManagementEnforcer.add_function
accept only sync funciton. there will throw an error. when AsyncManagementEnforcer.enforce
get called.
python stack embraces a lot of async this is an inconvenient to implentment sync match function only for using it with casbin
The Error
explain_index = -1
if not 0 == policy_len:
for i, pvals in enumerate(self.model["p"][ptype].policy):
if len(p_tokens) != len(pvals):
raise RuntimeError("invalid policy size")
p_parameters = dict(zip(p_tokens, pvals))
parameters = dict(r_parameters, **p_parameters)
if exp_has_eval:
rule_names = util.get_eval_value(exp_string)
rules = [util.escape_assertion(p_parameters[rule_name]) for rule_name in rule_names]
exp_with_rule = util.replace_eval(exp_string, rules)
expression = self._get_expression(exp_with_rule, functions)
result = expression.eval(parameters)
if isinstance(result, bool):
if not result:
policy_effects.add(Effector.INDETERMINATE)
continue
elif isinstance(result, float):
if 0 == result:
policy_effects.add(Effector.INDETERMINATE)
continue
else:
> raise RuntimeError("matcher result should be bool, int or float")
E RuntimeError: matcher result should be bool, int or float
.venv/lib/python3.11/site-packages/casbin/core_enforcer.py:469: RuntimeError
=================================================================================== short test summary info ====================================================================================
FAILED tests/test_casbin/test_plan_enforcement.py::test_plan_enforcement - RuntimeError: matcher result should be bool, int or float
Suggestion
-
add_function
could support both sync and async dynamically - create
await enforce.enforce()
async function- might need a separate class or new function name ``
- use
async.gather()
for perfawait enforce.batch_enforce()