Metadata-Version: 2.1
Name: global-manager
Version: 1.0.2
Summary: Global thread context manager 
Author-email: Pavel Shmakov <shmakovpn@yandex.ru>
License: MIT License
        
        Copyright (c) 2018 Real Python
        
        Permission is hereby granted, free of charge, to any person obtaining a copy
        of this software and associated documentation files (the "Software"), to deal
        in the Software without restriction, including without limitation the rights
        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
        copies of the Software, and to permit persons to whom the Software is
        furnished to do so, subject to the following conditions:
        
        The above copyright notice and this permission notice shall be included in all
        copies or substantial portions of the Software.
        
        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
        SOFTWARE.
        
Project-URL: Homepage, https://github.com/shmakovpn/global-manager
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python
Classifier: Programming Language :: Python :: 3
Requires-Python: >=3.7
Description-Content-Type: text/markdown
License-File: LICENSE
Provides-Extra: dev
Requires-Dist: tomli; python_version < "3.11" and extra == "dev"
Requires-Dist: pip-tools; extra == "dev"
Requires-Dist: pytest; extra == "dev"
Requires-Dist: coverage; extra == "dev"
Requires-Dist: build; extra == "dev"
Requires-Dist: reverse-patch; extra == "dev"

# global-manager

![Tests](https://github.com/shmakovpn/global-manager/actions/workflows/python-package.yml/badge.svg)
[![codecov](https://codecov.io/github/shmakovpn/global-manager/graph/badge.svg?token=744XXMAKOZ)](https://codecov.io/github/shmakovpn/global-manager)
![Mypy](https://github.com/shmakovpn/global-manager/actions/workflows/mypy.yml/badge.svg)
[![pypi](https://img.shields.io/pypi/v/global-manager.svg)](https://pypi.python.org/pypi/global-manager)
[![downloads](https://static.pepy.tech/badge/global-manager/month)](https://pepy.tech/project/global-manager)
[![versions](https://img.shields.io/pypi/pyversions/global-manager.svg)](https://github.com/shmakovpn/global-manager)


Global thread context manager.

## Installation 

```bash
pip install global-manager
```

## Examples

```py
from typing import Optional
import dataclasses
import logging
from global_manager import GlobalManager

logger = logging.getLogger('my_view')

@dataclasses.dataclass
class UserContext:
    """Some context"""
    ip: str
    profile_id: int

class UserContextManager(GlobalManager[UserContext]):
    @classmethod
    def get_current_user_context(cls) -> Optional[UserContext]:
        """a business meaning method name is a good practice"""
        return cls.get_current_context()


def my_func():
    user_context: Optional[UserContext] = UserContextManager.get_current_user_context()
    # retrieve context
    logger.debug('my_view profile_id=%s, ip=%s', user_context.profile_id, user_context.ip)
    # other logic ...


# some where in code, m.b. in web framework
def my_view(request):
    # Imagine that the data is obtained from the request
    user_context = UserContext(ip='127.0.0.1', profile_id=1)
    
    with UserContext(user_context):
        return my_func()
```

Context in context.

```py
with UserContext(uc1):
    UserContext.get_current_user_context()  # uc1 context
    
    with UserContext(uc2):
        UserContext.get_current_user_context() # uc2 context

    UserContext.get_current_user_context()  # uc1 context

UserContext.get_current_user_context()  # None
```

Async notations is supported too.

```py
async with UserContext(uc1):
    UserContext.get_current_user_context()  # uc1 context
    
    async with UserContext(uc2):
        UserContext.get_current_user_context() # uc2 context

    UserContext.get_current_user_context()  # uc1 context

UserContext.get_current_user_context()  # None
```
