U aðfÀã@stddlZddlmZddlmZmZmZGdd„dƒZeƒZed eee efeee efeedœdd„ƒZ dS) éN)Úcontextmanager)ÚAnyÚOptionalÚMappingcsleZdZdZdd„Zeedœdd„Zeedœ‡fdd „ Zed œd d „Z eedœd d„Z ed œdd„Z ‡Z S)ÚContextaâ Context instances can be used to hold state and values during the execution life cycle of a python application without needing to change the existing API of various interacting classes. State/values can be updated in one part of the application and be visible in another part of the application without requiring to pass this state thru the various methods of the call stack. Context also supports maintenance of global and thread specific state which can be very useful in a multi-threaded applications like Flask/Falcon based web applications or the NES Journal reader. State stored at the global level is visible in every concurrently executed thread, while thread specific state is isolated to the corresponding thread of execution. This can useful again in a multi-threaded application like a web-app where each incoming request is processed by a separate thread and things like request headers, authentication user context is thread specific and isolated to the thread handling a particular request. Example usage: -- In the main thread of an application's start up code we might want to inject some global state like so. # In some start-up file called app.py from primordial.context import Context MY_APP_CTX = Context() # Instantiate some shared object jwt_fetcher = JWTFetcher(some_config) MY_APP_CTX.set_global('jwt_fetcher', jwt_fetcher) # In a thread that's handling a particular HTTP request # handle_req.py from app import MY_APP_CTX MY_APP_CTX.user = User() MY_APP_CTX.token = copy_token_from_header() # In a third file somewhere down the line of request processing # some_file_called_by_controller.py from app import MY_APP_CTX def some_func(): # All of these are magically available. # some_func's function signature didn't require to be changed MY_APP_CTX.jwt_fetcher.get() MY_APP_CTX.user.name == 'jDoe' MY_APP_CTX.token.is_valid() cCsi|_t ¡|_dS©N)Ú_globalÚ threadingÚlocalÚ_local)Úself©r úC/opt/nydus/tmp/pip-target-53d1vnqk/lib/python/primordial/context.pyÚ__init__2szContext.__init__)ÚnameÚreturncCsBzt|j|ƒWStk r<||jkr6|j|YS‚YnXdSr)Úgetattrr ÚAttributeErrorr©r rr r rÚ __getattr__6s  zContext.__getattr__)rÚvaluecs(|dkrtƒ ||¡St|j||ƒdS)N)rr )ÚsuperÚ __setattr__Úsetattrr ©r rr©Ú __class__r rr>szContext.__setattr__)rcCs>zt|j|ƒWn(tk r8||jkr,‚|j|=YnXdSr)Údelattrr rrrr r rÚ __delattr__Cs  zContext.__delattr__cCs||j|<dSr)rrr r rÚ set_globalKszContext.set_globalcCs|j |¡dSr)rÚpoprr r rÚ unset_globalNszContext.unset_global) Ú__name__Ú __module__Ú __qualname__Ú__doc__rÚstrrrrrrr!Ú __classcell__r r rrrs+r)Ú local_valsÚ global_valsÚctxc csš|r|nt}|r|ni}|r |ni}| ¡D]\}}t|||ƒq,| ¡D]\}}| ||¡qJz |VW5|D]}t||ƒqp|D]}| |¡q„XdS)a^ Python context-manager for managing the life-cycle of state stored in a context. This context manager allows for state to be both stored in a context and also cleans up this state when the context-manager is exited. Usage: # In some python module file1.py from primordial import Context ctx = Context() # In some python module file2.py from threading import Thread from primordial import make_context from file1 import ctx from file3 import fn3 def fn2(): global_vals = {'v1': 'abc', v2: 'def'} # Set some global values with make_context(global_vals=global_value, ctx): # Kick of fn3 in a new thread t1 = Thread(target=fn3, args=[]) t1.start() t1.join() fn2() # In some python module file3.py from primordial import make_context from file1 import ctx from file4 import fn4 def fn3(): # v2 will shadow the value that was set globally local_vals = {'v3': 'ghi', v2: 'jkl'} # Set some thread specific values # Once this function returns, ctx.v3 and ctx.v2 are not available for access with make_context(local_vals=local_value, ctx): fn4() # We can still access the globally set state here even after the above context manager # has exited. ctx.v1 ctx.v2 # The globally set v2 # In some python module file3.py from file1 import ctx def fn4(): # All of the accesses are valid ctx.v1 ctx.v2 # This accesses the local thread specific v2 ctx.v3 N)ÚCTXÚitemsrrrr!)r(r)r*ÚkÚvr r rÚ make_contextUs;     r/)NNN) r Ú contextlibrÚtypingrrrrr+r&r/r r r rÚs Lþþ