Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/huy-dna/pryvacy
Access control decorators for python classes
https://github.com/huy-dna/pryvacy
decorator emulation encapsulation oop poetry-python python
Last synced: 22 days ago
JSON representation
Access control decorators for python classes
- Host: GitHub
- URL: https://github.com/huy-dna/pryvacy
- Owner: Huy-DNA
- License: apache-2.0
- Created: 2024-05-29T05:14:32.000Z (5 months ago)
- Default Branch: main
- Last Pushed: 2024-06-02T16:57:03.000Z (5 months ago)
- Last Synced: 2024-06-02T18:45:52.248Z (5 months ago)
- Topics: decorator, emulation, encapsulation, oop, poetry-python, python
- Language: Python
- Homepage:
- Size: 64.5 KB
- Stars: 3
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# pryvacy - a set of access control decorators for python
[![PyPI Version](https://img.shields.io/pypi/v/pryvacy.svg)](https://pypi.python.org/pypi/pryvacy)
## Philosophy
* Incur as least overhead as possible when using the access control decorators
* Only classes that use the decorators need to be changed, other related classes do not need to -> Can be opted-in easily## Installation
```bash
pip install pryvacy
```## Usage
The package provides 3 access control decorators: `@private`, `@public`, `@protected` that can be used on methods and nested classes (currently only `@private` can be used on nested classes)
```python
from pryvacy import pryvacy, private, public, protected@pryvacy
class Foo():
@public
def public_method(self):
pass@protected
def protected_method(self):
pass@private
def private_method(self):
pass
```Access control rules:
* Methods inside `Foo` are able to access `public_method`, `protected_method`, `private_method`.
* Code outside `Foo` and not inside any `Foo`'s subclass methods can only access `public_method`.
* Methods inside `Foo`'s subclasses (either decorated with `@pryvacy` or not) can access `public_method` and `protected_method`.
* Nested classes methods can access `public_method`, `protected_method`, `private_method`.## Pitfalls & Bugs
Disclaimer: The package has not been tested thoroughly! Use with caution! Any contributions are appreciated~
1. Currently, class-level code cannot access `protected_method` and `private_method`.
Example:
```python
class Foo():
...
class Bar(Foo):
Foo().public_method() # OK!
Foo().protected_method() # Exception!
Foo().private_method() # Exception!
```
2. The `test_back_and_forth` test is failing. This scenario is unlikely to happen but I will try to come up with a performant solution.## Current limitations
* `@private` and `@protected` are not supported on nested classes yet.
* No way to enforce access policy on class and instance attributes.
## Roadmap
* Benchmark decorated classes
* Test comprehensively the decorators interaction with the whole ecosystem
* Implement @private and @protected on nested classes