-
-
Notifications
You must be signed in to change notification settings - Fork 193
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add interface stubs for async adapters (#335)
* refactor!: change reorganize imports for sane asyncio ext * feat: async adapter interface stubs * chore: reflect pr comments * chore: revert black bump pending decision * refactor: move update adapter interface to persist module * ci: bump tooling and linting versions * ci: fix linting action
- Loading branch information
1 parent
47e5ef5
commit d557189
Showing
23 changed files
with
360 additions
and
238 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,107 +1,7 @@ | ||
# Copyright 2021 The casbin Authors. All Rights Reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# NOTE: this file exists as a backwards compatible alias. please directly | ||
# use FilteredFileAdapter from `casbin.persist.adapters.filtered_file_adapter` instead. | ||
|
||
from casbin import persist | ||
from .file_adapter import FileAdapter | ||
import os | ||
from .filtered_file_adapter import Filter | ||
from .filtered_file_adapter import FilteredFileAdapter as FilteredAdapter | ||
|
||
|
||
class Filter: | ||
# P,G are string [] | ||
P = [] | ||
G = [] | ||
|
||
|
||
class FilteredAdapter(FileAdapter, persist.FilteredAdapter): | ||
filtered = False | ||
_file_path = "" | ||
filter = Filter() | ||
# new_filtered_adapte is the constructor for FilteredAdapter. | ||
def __init__(self, file_path): | ||
self.filtered = True | ||
self._file_path = file_path | ||
|
||
def load_policy(self, model): | ||
if not os.path.isfile(self._file_path): | ||
raise RuntimeError("invalid file path, file path cannot be empty") | ||
self.filtered = False | ||
self._load_policy_file(model) | ||
|
||
# load_filtered_policy loads only policy rules that match the filter. | ||
def load_filtered_policy(self, model, filter): | ||
if filter == None: | ||
return self.load_policy(model) | ||
|
||
if not os.path.isfile(self._file_path): | ||
raise RuntimeError("invalid file path, file path cannot be empty") | ||
|
||
try: | ||
filter_value = [filter.__dict__["P"]] + [filter.__dict__["G"]] | ||
except: | ||
raise RuntimeError("invalid filter type") | ||
|
||
self.load_filtered_policy_file(model, filter_value, persist.load_policy_line) | ||
self.filtered = True | ||
|
||
def load_filtered_policy_file(self, model, filter, hanlder): | ||
with open(self._file_path, "rb") as file: | ||
while True: | ||
line = file.readline() | ||
line = line.decode().strip() | ||
if line == "\n": | ||
continue | ||
if not line: | ||
break | ||
if filter_line(line, filter): | ||
continue | ||
|
||
hanlder(line, model) | ||
|
||
# is_filtered returns true if the loaded policy has been filtered. | ||
def is_filtered(self): | ||
return self.filtered | ||
|
||
def save_policy(self, model): | ||
if self.filtered: | ||
raise RuntimeError("cannot save a filtered policy") | ||
|
||
self._save_policy_file(model) | ||
|
||
|
||
def filter_line(line, filter): | ||
if filter == None: | ||
return False | ||
|
||
p = line.split(",") | ||
if len(p) == 0: | ||
return True | ||
filter_slice = [] | ||
|
||
if p[0].strip() == "p": | ||
filter_slice = filter[0] | ||
elif p[0].strip() == "g": | ||
filter_slice = filter[1] | ||
return filter_words(p, filter_slice) | ||
|
||
|
||
def filter_words(line, filter): | ||
if len(line) < len(filter) + 1: | ||
return True | ||
skip_line = False | ||
for i, v in enumerate(filter): | ||
if len(v) > 0 and (v.strip() != line[i + 1].strip()): | ||
skip_line = True | ||
break | ||
|
||
return skip_line | ||
__all__ = ["Filter", "FilteredAdapter"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
from .adapter import AsyncAdapter | ||
from .adapter_filtered import AsyncFilteredAdapter | ||
from .batch_adapter import AsyncBatchAdapter | ||
from .file_adapter import AsyncFileAdapter | ||
from .update_adapter import AsyncUpdateAdapter | ||
|
||
__all__ = [ | ||
"AsyncAdapter", | ||
"AsyncFilteredAdapter", | ||
"AsyncBatchAdapter", | ||
"AsyncFileAdapter", | ||
"AsyncUpdateAdapter", | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
from abc import ABCMeta, abstractmethod | ||
|
||
|
||
class AsyncAdapter(metaclass=ABCMeta): | ||
"""The interface for async Casbin adapters.""" | ||
|
||
@abstractmethod | ||
async def load_policy(self, model): | ||
"""loads all policy rules from the storage.""" | ||
pass | ||
|
||
@abstractmethod | ||
async def save_policy(self, model): | ||
"""saves all policy rules to the storage.""" | ||
pass | ||
|
||
@abstractmethod | ||
async def add_policy(self, sec, ptype, rule): | ||
"""adds a policy rule to the storage.""" | ||
pass | ||
|
||
@abstractmethod | ||
async def remove_policy(self, sec, ptype, rule): | ||
"""removes a policy rule from the storage.""" | ||
pass | ||
|
||
@abstractmethod | ||
async def remove_filtered_policy(self, sec, ptype, field_index, *field_values): | ||
"""removes policy rules that match the filter from the storage. | ||
This is part of the Auto-Save feature. | ||
""" | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
from abc import ABCMeta, abstractmethod | ||
|
||
|
||
class AsyncFilteredAdapter(metaclass=ABCMeta): | ||
"""AsyncFilteredAdapter is the interface for async Casbin adapters supporting filtered policies.""" | ||
|
||
@abstractmethod | ||
async def is_filtered(self): | ||
"""IsFiltered returns true if the loaded policy has been filtered | ||
Marks if the loaded policy is filtered or not | ||
""" | ||
pass | ||
|
||
@abstractmethod | ||
async def load_filtered_policy(self, model, filter): | ||
"""Loads policy rules that match the filter from the storage.""" | ||
pass |
Oops, something went wrong.