mirror of
				https://github.com/ets-labs/python-dependency-injector.git
				synced 2025-11-04 01:47:36 +03:00 
			
		
		
		
	Refactor wiring to reduce complexity
This commit is contained in:
		
							parent
							
								
									c5f799a1ec
								
							
						
					
					
						commit
						63977a73b2
					
				| 
						 | 
					@ -273,41 +273,9 @@ def _is_method(member):
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def _patch_with_injections(fn, injections, closing):
 | 
					def _patch_with_injections(fn, injections, closing):
 | 
				
			||||||
    if inspect.iscoroutinefunction(fn):
 | 
					    if inspect.iscoroutinefunction(fn):
 | 
				
			||||||
        @functools.wraps(fn)
 | 
					        _patched = _get_async_patched(fn, injections, closing)
 | 
				
			||||||
        async def _patched(*args, **kwargs):
 | 
					 | 
				
			||||||
            to_inject = kwargs.copy()
 | 
					 | 
				
			||||||
            for injection, provider in injections.items():
 | 
					 | 
				
			||||||
                if injection not in kwargs:
 | 
					 | 
				
			||||||
                    to_inject[injection] = provider()
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
            result = await fn(*args, **to_inject)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
            for injection, provider in closing.items():
 | 
					 | 
				
			||||||
                if injection in kwargs:
 | 
					 | 
				
			||||||
                    continue
 | 
					 | 
				
			||||||
                if not isinstance(provider, providers.Resource):
 | 
					 | 
				
			||||||
                    continue
 | 
					 | 
				
			||||||
                provider.shutdown()
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
            return result
 | 
					 | 
				
			||||||
    else:
 | 
					    else:
 | 
				
			||||||
        @functools.wraps(fn)
 | 
					        _patched = _get_patched(fn, injections, closing)
 | 
				
			||||||
        def _patched(*args, **kwargs):
 | 
					 | 
				
			||||||
            to_inject = kwargs.copy()
 | 
					 | 
				
			||||||
            for injection, provider in injections.items():
 | 
					 | 
				
			||||||
                if injection not in kwargs:
 | 
					 | 
				
			||||||
                    to_inject[injection] = provider()
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
            result = fn(*args, **to_inject)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
            for injection, provider in closing.items():
 | 
					 | 
				
			||||||
                if injection in kwargs:
 | 
					 | 
				
			||||||
                    continue
 | 
					 | 
				
			||||||
                if not isinstance(provider, providers.Resource):
 | 
					 | 
				
			||||||
                    continue
 | 
					 | 
				
			||||||
                provider.shutdown()
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
            return result
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    _patched.__wired__ = True
 | 
					    _patched.__wired__ = True
 | 
				
			||||||
    _patched.__original__ = fn
 | 
					    _patched.__original__ = fn
 | 
				
			||||||
| 
						 | 
					@ -317,6 +285,47 @@ def _patch_with_injections(fn, injections, closing):
 | 
				
			||||||
    return _patched
 | 
					    return _patched
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					def _get_patched(fn, injections, closing):
 | 
				
			||||||
 | 
					    def _patched(*args, **kwargs):
 | 
				
			||||||
 | 
					        to_inject = kwargs.copy()
 | 
				
			||||||
 | 
					        for injection, provider in injections.items():
 | 
				
			||||||
 | 
					            if injection not in kwargs:
 | 
				
			||||||
 | 
					                to_inject[injection] = provider()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        result = fn(*args, **to_inject)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        for injection, provider in closing.items():
 | 
				
			||||||
 | 
					            if injection in kwargs:
 | 
				
			||||||
 | 
					                continue
 | 
				
			||||||
 | 
					            if not isinstance(provider, providers.Resource):
 | 
				
			||||||
 | 
					                continue
 | 
				
			||||||
 | 
					            provider.shutdown()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        return result
 | 
				
			||||||
 | 
					    return _patched
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					def _get_async_patched(fn, injections, closing):
 | 
				
			||||||
 | 
					    @functools.wraps(fn)
 | 
				
			||||||
 | 
					    async def _patched(*args, **kwargs):
 | 
				
			||||||
 | 
					        to_inject = kwargs.copy()
 | 
				
			||||||
 | 
					        for injection, provider in injections.items():
 | 
				
			||||||
 | 
					            if injection not in kwargs:
 | 
				
			||||||
 | 
					                to_inject[injection] = provider()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        result = await fn(*args, **to_inject)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        for injection, provider in closing.items():
 | 
				
			||||||
 | 
					            if injection in kwargs:
 | 
				
			||||||
 | 
					                continue
 | 
				
			||||||
 | 
					            if not isinstance(provider, providers.Resource):
 | 
				
			||||||
 | 
					                continue
 | 
				
			||||||
 | 
					            provider.shutdown()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        return result
 | 
				
			||||||
 | 
					    return _patched
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def _is_patched(fn):
 | 
					def _is_patched(fn):
 | 
				
			||||||
    return getattr(fn, '__wired__', False) is True
 | 
					    return getattr(fn, '__wired__', False) is True
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue
	
	Block a user