Skip to content

Commit

Permalink
fix: Lint files with latest lint config
Browse files Browse the repository at this point in the history
  • Loading branch information
Shivansh-yadav13 committed Apr 26, 2022
1 parent 326a204 commit 0e1a1ed
Show file tree
Hide file tree
Showing 24 changed files with 114 additions and 348 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ jobs:
VALIDATE_PYTHON_BLACK: true
DEFAULT_BRANCH: master
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
LINTER_RULES_PATH: /casbin
LINTER_RULES_PATH: /
PYTHON_BLACK_CONFIG_FILE: pyproject.toml

coveralls:
Expand Down
4 changes: 1 addition & 3 deletions casbin/config/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,7 @@ def _write(self, section, line_num, b):

if len(option_val) != 2:
raise RuntimeError(
"parse the content error : line {} , {} = ?".format(
line_num, option_val[0]
)
"parse the content error : line {} , {} = ?".format(line_num, option_val[0])
)

option = option_val[0].strip()
Expand Down
14 changes: 3 additions & 11 deletions casbin/core_enforcer.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,7 @@ def init_with_adapter(self, model_path, adapter=None):
def init_with_model_and_adapter(self, m, adapter=None):
"""initializes an enforcer with a model and a database adapter."""

if (
not isinstance(m, Model)
or adapter is not None
and not isinstance(adapter, Adapter)
):
if not isinstance(m, Model) or adapter is not None and not isinstance(adapter, Adapter):
raise RuntimeError("Invalid parameters for enforcer.")

self.adapter = adapter
Expand Down Expand Up @@ -388,8 +384,7 @@ def enforce_ex(self, *rvals):
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
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)
Expand Down Expand Up @@ -419,10 +414,7 @@ def enforce_ex(self, *rvals):
else:
policy_effects.add(Effector.ALLOW)

if (
self.eft.intermediate_effect(policy_effects)
!= Effector.INDETERMINATE
):
if self.eft.intermediate_effect(policy_effects) != Effector.INDETERMINATE:
explain_index = i
break

Expand Down
24 changes: 6 additions & 18 deletions casbin/distributed_enforcer.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,7 @@ def add_policy_self(self, should_persist, sec, ptype, rules):

if sec == "g":
try:
self.build_incremental_role_links(
PolicyOp.Policy_add, ptype, no_exists_policy
)
self.build_incremental_role_links(PolicyOp.Policy_add, ptype, no_exists_policy)
except Exception as e:
self.logger.log("An exception occurred: " + e)
return no_exists_policy
Expand Down Expand Up @@ -81,19 +79,15 @@ def remove_policy_self(self, should_persist, sec, ptype, rules):

return effected

def remove_filtered_policy_self(
self, should_persist, sec, ptype, field_index, *field_values
):
def remove_filtered_policy_self(self, should_persist, sec, ptype, field_index, *field_values):
"""
remove_filtered_policy_self provides a method for dispatcher to remove an authorization
rule from the current policy,field filters can be specified.
The function returns the rules affected and error.
"""
if should_persist:
try:
self.adapter.remove_filtered_policy(
sec, ptype, field_index, field_values
)
self.adapter.remove_filtered_policy(sec, ptype, field_index, field_values)
except Exception as e:
self.logger.log("An exception occurred: " + e)

Expand All @@ -103,9 +97,7 @@ def remove_filtered_policy_self(

if sec == "g":
try:
self.build_incremental_role_links(
PolicyOp.Policy_remove, ptype, effects
)
self.build_incremental_role_links(PolicyOp.Policy_remove, ptype, effects)
except Exception as e:
self.logger.log("An exception occurred: " + e)
return effects
Expand Down Expand Up @@ -143,16 +135,12 @@ def update_policy_self(self, should_persist, sec, ptype, old_rule, new_rule):

if sec == "g":
try:
self.build_incremental_role_links(
PolicyOp.Policy_remove, ptype, [old_rule]
)
self.build_incremental_role_links(PolicyOp.Policy_remove, ptype, [old_rule])
except Exception as e:
return False

try:
self.build_incremental_role_links(
PolicyOp.Policy_add, ptype, [new_rule]
)
self.build_incremental_role_links(PolicyOp.Policy_add, ptype, [new_rule])
except Exception as e:
return False

Expand Down
4 changes: 1 addition & 3 deletions casbin/enforcer.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,9 +152,7 @@ def get_implicit_roles_for_user(self, name, domain=""):

return res

def get_implicit_permissions_for_user(
self, user, domain="", filter_policy_dom=True
):
def get_implicit_permissions_for_user(self, user, domain="", filter_policy_dom=True):
"""
gets implicit permissions for a user or role.
Compared to get_permissions_for_user(), this function retrieves permissions for inherited roles.
Expand Down
30 changes: 6 additions & 24 deletions casbin/internal_enforcer.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,10 @@ def _update_policies(self, sec, ptype, old_rules, new_rules):

return rules_updated

def _update_filtered_policies(
self, sec, ptype, new_rules, field_index, *field_values
):
def _update_filtered_policies(self, sec, ptype, new_rules, field_index, *field_values):
"""_update_filtered_policies deletes old rules and adds new rules."""

old_rules = self.model.get_filtered_policy(
sec, ptype, field_index, *field_values
)
old_rules = self.model.get_filtered_policy(sec, ptype, field_index, *field_values)

if self.adapter and self.auto_save:
try:
Expand Down Expand Up @@ -154,29 +150,20 @@ def _remove_policies(self, sec, ptype, rules):

def _remove_filtered_policy(self, sec, ptype, field_index, *field_values):
"""removes rules based on field filters from the current policy."""
rule_removed = self.model.remove_filtered_policy(
sec, ptype, field_index, *field_values
)
rule_removed = self.model.remove_filtered_policy(sec, ptype, field_index, *field_values)
if not rule_removed:
return rule_removed

if self.adapter and self.auto_save:
if (
self.adapter.remove_filtered_policy(
sec, ptype, field_index, *field_values
)
is False
):
if self.adapter.remove_filtered_policy(sec, ptype, field_index, *field_values) is False:
return False

if self.watcher:
self.watcher.update()

return rule_removed

def _remove_filtered_policy_returns_effects(
self, sec, ptype, field_index, *field_values
):
def _remove_filtered_policy_returns_effects(self, sec, ptype, field_index, *field_values):
"""removes rules based on field filters from the current policy."""
rule_removed = self.model.remove_filtered_policy_returns_effects(
sec, ptype, field_index, *field_values
Expand All @@ -185,12 +172,7 @@ def _remove_filtered_policy_returns_effects(
return rule_removed

if self.adapter and self.auto_save:
if (
self.adapter.remove_filtered_policy(
sec, ptype, field_index, *field_values
)
is False
):
if self.adapter.remove_filtered_policy(sec, ptype, field_index, *field_values) is False:
return False

if self.watcher:
Expand Down
16 changes: 4 additions & 12 deletions casbin/management_enforcer.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,17 +153,11 @@ def update_named_policies(self, ptype, old_rules, new_rules):

def update_filtered_policies(self, new_rules, field_index, *field_values):
"""update_filtered_policies deletes old rules and adds new rules."""
return self.update_filtered_named_policies(
"p", new_rules, field_index, *field_values
)
return self.update_filtered_named_policies("p", new_rules, field_index, *field_values)

def update_filtered_named_policies(
self, ptype, new_rules, field_index, *field_values
):
def update_filtered_named_policies(self, ptype, new_rules, field_index, *field_values):
"""update_filtered_named_policies deletes old rules and adds new rules."""
return self._update_filtered_policies(
"p", ptype, new_rules, field_index, *field_values
)
return self._update_filtered_policies("p", ptype, new_rules, field_index, *field_values)

def remove_policy(self, *params):
"""removes an authorization rule from the current policy."""
Expand Down Expand Up @@ -271,9 +265,7 @@ def remove_grouping_policies(self, rules):

def remove_filtered_grouping_policy(self, field_index, *field_values):
"""removes a role inheritance rule from the current policy, field filters can be specified."""
return self.remove_filtered_named_grouping_policy(
"g", field_index, *field_values
)
return self.remove_filtered_named_grouping_policy("g", field_index, *field_values)

def remove_named_grouping_policy(self, ptype, *params):
"""removes a role inheritance rule from the current named policy."""
Expand Down
12 changes: 3 additions & 9 deletions casbin/model/assertion.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,11 @@ def build_role_links(self, rm):
self.rm = rm
count = self.value.count("_")
if count < 2:
raise RuntimeError(
'the number of "_" in role definition should be at least 2'
)
raise RuntimeError('the number of "_" in role definition should be at least 2')

for rule in self.policy:
if len(rule) < count:
raise RuntimeError(
"grouping policy elements do not meet role definition"
)
raise RuntimeError("grouping policy elements do not meet role definition")
if len(rule) > count:
rule = rule[:count]

Expand All @@ -52,9 +48,7 @@ def build_incremental_role_links(self, rm, op, rules):
self.rm = rm
count = self.value.count("_")
if count < 2:
raise RuntimeError(
'the number of "_" in role definition should be at least 2'
)
raise RuntimeError('the number of "_" in role definition should be at least 2')
for rule in rules:
if len(rule) < count:
raise TypeError("grouping policy elements do not meet role definition")
Expand Down
18 changes: 4 additions & 14 deletions casbin/model/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,7 @@ def sort_policies_by_priority(self):
if assertion.priority_index == -1:
continue

assertion.policy = sorted(
assertion.policy, key=lambda x: x[assertion.priority_index]
)
assertion.policy = sorted(assertion.policy, key=lambda x: x[assertion.priority_index])

for i, policy in enumerate(assertion.policy):
assertion.policy_map[",".join(policy)] = i
Expand All @@ -128,9 +126,7 @@ def sort_policies_by_subject_hierarchy(self):
domain_index = index
break

subject_hierarchy_map = self.get_subject_hierarchy_map(
self["g"]["g"].policy
)
subject_hierarchy_map = self.get_subject_hierarchy_map(self["g"]["g"].policy)

def compare_policy(policy):
domain = DEFAULT_DOMAIN
Expand All @@ -139,9 +135,7 @@ def compare_policy(policy):
name = self.get_name_with_domain(domain, policy[sub_index])
return subject_hierarchy_map[name]

assertion.policy = sorted(
assertion.policy, key=compare_policy, reverse=True
)
assertion.policy = sorted(assertion.policy, key=compare_policy, reverse=True)
for i, policy in enumerate(assertion.policy):
assertion.policy_map[",".join(policy)] = i

Expand Down Expand Up @@ -194,11 +188,7 @@ def to_text(self):
def write_string(sec):
for p_type in self[sec]:
value = self[sec][p_type].value
s.append(
"{} = {}\n".format(
sec, value.replace("p_", "p.").replace("r_", "r.")
)
)
s.append("{} = {}\n".format(sec, value.replace("p_", "p.").replace("r_", "r.")))

s.append("[request_definition]\n")
write_string("r")
Expand Down
8 changes: 2 additions & 6 deletions casbin/model/policy.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,9 +195,7 @@ def update_policies(self, sec, ptype, old_rules, new_rules):
if old_rule[priority_index] == new_rule[priority_index]:
ast.policy[idx] = new_rule
else:
raise Exception(
"New rule should have the same priority with old rule."
)
raise Exception("New rule should have the same priority with old rule.")
else:
for idx, old_rule, new_rule in zip(old_rules_index, old_rules, new_rules):
ast.policy[idx] = new_rule
Expand Down Expand Up @@ -234,9 +232,7 @@ def remove_policies_with_effected(self, sec, ptype, rules):

return effected

def remove_filtered_policy_returns_effects(
self, sec, ptype, field_index, *field_values
):
def remove_filtered_policy_returns_effects(self, sec, ptype, field_index, *field_values):
"""
remove_filtered_policy_returns_effects removes policy rules based on field filters from the model.
"""
Expand Down
4 changes: 1 addition & 3 deletions casbin/persist/adapters/update_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,7 @@ def update_policies(self, sec, ptype, old_rules, new_rules):
"""
pass

def update_filtered_policies(
self, sec, ptype, new_rules, field_index, *field_values
):
def update_filtered_policies(self, sec, ptype, new_rules, field_index, *field_values):
"""
update_filtered_policies deletes old rules and adds new rules.
"""
Expand Down
Loading

0 comments on commit 0e1a1ed

Please sign in to comment.