casbin / pycasbin

An authorization library that supports access control models like ACL, RBAC, ABAC in Python
https://casbin.org
Apache License 2.0
1.41k stars 195 forks source link

StringAdapter not useable #358

Open Blindfreddy opened 1 month ago

Blindfreddy commented 1 month ago

The class StringAdapter in module casbin.persist.adapters.string_adapter does not appear to be useable because of a missing import statement in casbin.persist.adapters.__init__.py.

Steps to reproduce:

  1. install casbin
  2. run python interpreter
  3. >>> import casbin.persist as p
    >>> dir(p.adapters)
    ['FileAdapter', 'FilteredAdapter', 'FilteredFileAdapter', 'UpdateAdapter', '__all__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__', 'asyncio', 'file_adapter', 'filtered_file_adapter']
    >>> sa = p.adapters.StringAdapter("foo")
    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    AttributeError: module 'casbin.persist.adapters' has no attribute 'StringAdapter'

Note that there is no class StringAdapter despite the presence of the module string_adapter.py

Fix Edit casbin.persist.adapters.__init__.py, adding the import statement for string_adapers.py and adding it to the __all__ variable

# 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.

from .string_adapter import StringAdapter
from .file_adapter import FileAdapter
from .filtered_file_adapter import FilteredFileAdapter
from ..update_adapter import UpdateAdapter

# alias import for backwards compatibility
FilteredAdapter = FilteredFileAdapter

__all__ = ["StringAdapter", "FileAdapter", "FilteredFileAdapter", "FilteredAdapter", "UpdateAdapter"]

Now it works:

>>> import casbin.persist as p
>>> dir(p)
['Adapter', 'BatchAdapter', 'FileAdapter', 'FilteredAdapter', 'FilteredFileAdapter', 'StringAdapter', 'UpdateAdapter', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__', 'adapter', 'adapter_filtered', 'adapters', 'batch_adapter', 'load_policy_line', 'update_adapter']
>>> sa = p.StringAdapter("foo")
>>> sa
<casbin.persist.adapters.string_adapter.StringAdapter object at 0x10266b740>
casbin-bot commented 1 month ago

@techoner @Nekotoxin