Skip to content

Commit

Permalink
ci: code reformatted and linter tests added (#166)
Browse files Browse the repository at this point in the history
* fix: code reformat under PEP-8 using latest black

Signed-off-by: ffyuanda <46557895+ffyuanda@users.noreply.github.com>

* fix: requirements_dev.txt added (for black)

Signed-off-by: ffyuanda <46557895+ffyuanda@users.noreply.github.com>

* ci: Github Actions build.yml file lint test added

- added Run Linters check in build.yml file

- removed checks for pypy3

Signed-off-by: ffyuanda <46557895+ffyuanda@users.noreply.github.com>
  • Loading branch information
ffyuanda authored Jun 14, 2021
1 parent 1fe4cb2 commit fbbc600
Show file tree
Hide file tree
Showing 46 changed files with 1,185 additions and 822 deletions.
29 changes: 21 additions & 8 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
name: build
on:
push:
branches:
- master
branches: [master]
pull_request:
branches: [ master ]
branches: [master]

jobs:
test:
Expand All @@ -14,11 +13,10 @@ jobs:
matrix:
python-version: [3.6, 3.7, 3.8, 3.9]
os: [ubuntu-18.04, macOS-latest, windows-latest]
include:
# pypy3 on Mac OS currently fails trying to compile
# brotlipy. Moving pypy3 to only test linux.
- python-version: pypy3
os: ubuntu-latest
# pypy3 currently fails trying to find the header #include "codecs.h". Removed pypy3 for now.
# include:
# - python-version: pypy3
# os: ubuntu-latest

steps:
- name: Checkout
Expand All @@ -32,6 +30,7 @@ jobs:
- name: Install dependencies
run: |
pip install -r requirements.txt
pip install -r requirements_dev.txt
pip install coveralls
- name: Run tests
Expand All @@ -44,6 +43,20 @@ jobs:
COVERALLS_FLAG_NAME: ${{ matrix.os }} - ${{ matrix.python-version }}
COVERALLS_PARALLEL: true

lint:
name: Run Linters
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2

- name: Super-Linter
uses: github/super-linter@v4.2.2
env:
VALIDATE_PYTHON_BLACK: true
DEFAULT_BRANCH: master
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

coveralls:
name: Indicate completion to coveralls.io
needs: test
Expand Down
2 changes: 1 addition & 1 deletion casbin/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
from . import util
from .persist import *
from .effect import *
from .model import *
from .model import *
2 changes: 1 addition & 1 deletion casbin/config/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
from .config import Config
from .config import Config
40 changes: 24 additions & 16 deletions casbin/config/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ class Config:
"""represents an implementation of the ConfigInterface"""

# DEFAULT_SECTION specifies the name of a section if no name provided
DEFAULT_SECTION = 'default'
DEFAULT_SECTION = "default"
# DEFAULT_COMMENT defines what character(s) indicate a comment `#`
DEFAULT_COMMENT = '#'
DEFAULT_COMMENT = "#"
# DEFAULT_COMMENT_SEM defines what alternate character(s) indicate a comment `;`
DEFAULT_COMMENT_SEM = ';'
DEFAULT_COMMENT_SEM = ";"
# DEFAULT_MULTI_LINE_SEPARATOR defines what character indicates a multi-line content
DEFAULT_MULTI_LINE_SEPARATOR = '\\'
DEFAULT_MULTI_LINE_SEPARATOR = "\\"

_data = dict()

Expand All @@ -32,7 +32,7 @@ def new_config_from_text(text):
return c

def add_config(self, section, option, value):
if section == '':
if section == "":
section = self.DEFAULT_SECTION

if section not in self._data.keys():
Expand All @@ -41,11 +41,11 @@ def add_config(self, section, option, value):
self._data[section][option] = value

def _parse(self, fname):
with open(fname, 'r', encoding='utf-8') as f:
with open(fname, "r", encoding="utf-8") as f:
self._parse_buffer(f)

def _parse_buffer(self, f):
section = ''
section = ""
line_num = 0
buf = []
can_write = False
Expand All @@ -63,19 +63,23 @@ def _parse_buffer(self, f):
break
line = line.strip()

if '' == line or self.DEFAULT_COMMENT == line[0:1] or self.DEFAULT_COMMENT_SEM == line[0:1]:
if (
"" == line
or self.DEFAULT_COMMENT == line[0:1]
or self.DEFAULT_COMMENT_SEM == line[0:1]
):
can_write = True
continue
elif '[' == line[0:1] and ']' == line[-1]:
elif "[" == line[0:1] and "]" == line[-1]:
if len(buf) > 0:
self._write(section, line_num, buf)
can_write = False
section = line[1:-1]
else:
p = ''
p = ""
if self.DEFAULT_MULTI_LINE_SEPARATOR == line[-1]:
p = line[0:-1].strip()
p = p + ' '
p = p + " "
else:
p = line
can_write = True
Expand All @@ -86,10 +90,14 @@ def _write(self, section, line_num, b):
buf = "".join(b)
if len(buf) <= 0:
return
option_val = buf.split('=', 1)
option_val = buf.split("=", 1)

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

option = option_val[0].strip()
value = option_val[1].strip()
Expand Down Expand Up @@ -125,7 +133,7 @@ def set(self, key, value):
if len(key) == 0:
raise RuntimeError("key is empty")

keys = key.lower().split('::')
keys = key.lower().split("::")
if len(keys) >= 2:
section = keys[0]
option = keys[1]
Expand All @@ -137,7 +145,7 @@ def set(self, key, value):
def get(self, key):
"""section.key or key"""

keys = key.lower().split('::')
keys = key.lower().split("::")
if len(keys) >= 2:
section = keys[0]
option = keys[1]
Expand All @@ -148,4 +156,4 @@ def get(self, key):
if section in self._data.keys():
if option in self._data[section].keys():
return self._data[section][option]
return ''
return ""
30 changes: 21 additions & 9 deletions casbin/core_enforcer.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,11 @@ 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 @@ -131,25 +135,25 @@ def set_watcher(self, watcher):

def get_role_manager(self):
"""gets the current role manager."""
return self.rm_map['g']
return self.rm_map["g"]

def set_role_manager(self, rm):
"""sets the current role manager."""
self.rm_map['g'] = rm
self.rm_map["g"] = rm

def set_effector(self, eft):
"""sets the current effector."""

self.eft = eft

def clear_policy(self):
""" clears all policy."""
"""clears all policy."""

self.model.clear_policy()

def init_rm_map(self):
if 'g' in self.model.model.keys():
for ptype in self.model.model['g']:
if "g" in self.model.model.keys():
for ptype in self.model.model["g"]:
self.rm_map[ptype] = default_role_manager.RoleManager(10)

def load_policy(self):
Expand Down Expand Up @@ -296,7 +300,10 @@ def enforce_ex(self, *rvals):

if util.has_eval(exp_string):
rule_names = util.get_eval_value(exp_string)
rules = [util.escape_assertion(p_parameters[rule_name]) for rule_name in rule_names]
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)

Expand Down Expand Up @@ -324,13 +331,18 @@ 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

else:
if has_eval:
raise RuntimeError("please make sure rule exists in policy when using eval() in matcher")
raise RuntimeError(
"please make sure rule exists in policy when using eval() in matcher"
)

parameters = r_parameters.copy()

Expand Down
37 changes: 25 additions & 12 deletions casbin/distributed_enforcer.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def add_policy_self(self, should_persist, sec, ptype, rules):
AddPolicySelf provides a method for dispatcher to add authorization rules to the current policy.
The function returns the rules affected and error.
"""

no_exists_policy = []
for rule in rules:
if not self.get_model().has_policy(sec, ptype, rule):
Expand All @@ -35,7 +35,9 @@ 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 All @@ -47,9 +49,9 @@ def remove_policy_self(self, should_persist, sec, ptype, rules):
remove_policy_self provides a method for dispatcher to remove policies from current policy.
The function returns the rules affected and error.
"""
if(should_persist):
if should_persist:
try:
if(isinstance(self.adapter, batch_adapter)):
if isinstance(self.adapter, batch_adapter):
self.adapter.remove_policy(sec, ptype, rules)
except Exception as e:
self.logger.log("An exception occurred: " + e)
Expand All @@ -65,23 +67,31 @@ 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)

effects = self.get_model().remove_filtered_policy_returns_effects(sec, ptype, field_index, field_values)
effects = self.get_model().remove_filtered_policy_returns_effects(
sec, ptype, field_index, field_values
)

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 @@ -119,14 +129,17 @@ 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


return True
return True
25 changes: 16 additions & 9 deletions casbin/effect/__init__.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
from .default_effectors import AllowOverrideEffector, DenyOverrideEffector, AllowAndDenyEffector, PriorityEffector
from .default_effectors import (
AllowOverrideEffector,
DenyOverrideEffector,
AllowAndDenyEffector,
PriorityEffector,
)
from .effector import Effector


def get_effector(expr):
''' creates an effector based on the current policy effect expression '''
"""creates an effector based on the current policy effect expression"""

if expr == "some(where (p_eft == allow))":
return AllowOverrideEffector()
return AllowOverrideEffector()
elif expr == "!some(where (p_eft == deny))":
return DenyOverrideEffector()
elif expr == "some(where (p_eft == allow)) && !some(where (p_eft == deny))":
Expand All @@ -15,10 +21,11 @@ def get_effector(expr):
else:
raise RuntimeError("unsupported effect")


def effect_to_bool(effect):
""" """
if effect == Effector.ALLOW:
return True
if effect == Effector.DENY:
return False
raise RuntimeError("effect can't be converted to boolean")
""" """
if effect == Effector.ALLOW:
return True
if effect == Effector.DENY:
return False
raise RuntimeError("effect can't be converted to boolean")
Loading

0 comments on commit fbbc600

Please sign in to comment.