22The ldclient module contains the most common top-level entry points for the SDK.
33"""
44
5- import logging
6-
75from ldclient .rwlock import ReadWriteLock
86from ldclient .version import VERSION
97from .client import *
2018start_wait = 5
2119
2220__client = None
23- __config = Config ()
21+ __config = None
2422__lock = ReadWriteLock ()
2523
2624
27- def set_config (config ):
25+ def set_config (config : Config ):
2826 """Sets the configuration for the shared SDK client instance.
2927
3028 If this is called prior to :func:`ldclient.get()`, it stores the configuration that will be used when the
3129 client is initialized. If it is called after the client has already been initialized, the client will be
3230 re-initialized with the new configuration (this will result in the next call to :func:`ldclient.get()`
3331 returning a new client instance).
3432
35- :param ldclient.config.Config config: the client configuration
33+ :param config: the client configuration
3634 """
3735 global __config
3836 global __client
3937 global __lock
4038 try :
4139 __lock .lock ()
4240 if __client :
43- log .info ("Reinitializing LaunchDarkly Client " + version . VERSION + " with new config" )
41+ log .info ("Reinitializing LaunchDarkly Client " + VERSION + " with new config" )
4442 new_client = LDClient (config = config , start_wait = start_wait )
4543 old_client = __client
4644 __client = new_client
@@ -50,57 +48,15 @@ def set_config(config):
5048 __lock .unlock ()
5149
5250
53- def set_sdk_key (sdk_key ):
54- """Sets the SDK key for the shared SDK client instance.
55-
56- If this is called prior to :func:`ldclient.get()`, it stores the SDK key that will be used when the client is
57- initialized. If it is called after the client has already been initialized, the client will be
58- re-initialized with the new SDK key (this will result in the next call to :func:`ldclient.get()` returning a
59- new client instance).
60-
61- If you need to set any configuration options other than the SDK key, use :func:`ldclient.set_config()` instead.
62-
63- :param string sdk_key: the new SDK key
64- """
65- global __config
66- global __client
67- global __lock
68- sdk_key_changed = False
69- try :
70- __lock .rlock ()
71- if sdk_key == __config .sdk_key :
72- log .info ("New sdk_key is the same as the existing one. doing nothing." )
73- else :
74- sdk_key_changed = True
75- finally :
76- __lock .runlock ()
77-
78- if sdk_key_changed :
79- try :
80- __lock .lock ()
81- __config = __config .copy_with_new_sdk_key (new_sdk_key = sdk_key )
82- if __client :
83- log .info ("Reinitializing LaunchDarkly Client " + version .VERSION + " with new sdk key" )
84- new_client = LDClient (config = __config , start_wait = start_wait )
85- old_client = __client
86- __client = new_client
87- old_client .close ()
88- finally :
89- __lock .unlock ()
90-
91-
92- def get ():
51+ def get () -> LDClient :
9352 """Returns the shared SDK client instance, using the current global configuration.
9453
95- To use the SDK as a singleton, first make sure you have called :func:`ldclient.set_sdk_key()` or
96- :func:`ldclient.set_config()` at startup time. Then ``get()`` will return the same shared
97- :class:`ldclient.client.LDClient` instance each time. The client will be initialized if it has
98- not been already.
54+ To use the SDK as a singleton, first make sure you have called :func:`ldclient.set_config()`
55+ at startup time. Then ``get()`` will return the same shared :class:`ldclient.client.LDClient`
56+ instance each time. The client will be initialized if it has not been already.
9957
10058 If you need to create multiple client instances with different configurations, instead of this
10159 singleton approach you can call the :class:`ldclient.client.LDClient` constructor directly instead.
102-
103- :rtype: ldclient.client.LDClient
10460 """
10561 global __config
10662 global __client
@@ -109,13 +65,15 @@ def get():
10965 __lock .rlock ()
11066 if __client :
11167 return __client
68+ if __config is None :
69+ raise Exception ("set_config was not called" )
11270 finally :
11371 __lock .runlock ()
11472
11573 try :
11674 __lock .lock ()
11775 if not __client :
118- log .info ("Initializing LaunchDarkly Client " + version . VERSION )
76+ log .info ("Initializing LaunchDarkly Client " + VERSION )
11977 __client = LDClient (config = __config , start_wait = start_wait )
12078 return __client
12179 finally :
@@ -136,27 +94,4 @@ def _reset_client():
13694 c .close ()
13795
13896
139- # currently hidden from documentation - see docs/README.md
140- class NullHandler (logging .Handler ):
141- """A :class:`logging.Handler` implementation that does nothing.
142-
143- .. deprecated:: 6.0.0
144- You should not need to use this class. It was originally used in order to support Python 2.6,
145- which requires that at least one logging handler must always be configured. However, the SDK
146- no longer supports Python 2.6.
147- """
148- def emit (self , record ):
149- pass
150-
151-
152- if not log .handlers :
153- log .addHandler (NullHandler ())
154-
155- try :
156- # noinspection PyUnresolvedReferences
157- unicode
158- except NameError :
159- __BASE_TYPES__ = (str , float , int , bool )
160- else :
161- # noinspection PyUnresolvedReferences
162- __BASE_TYPES__ = (str , float , int , bool , unicode )
97+ __BASE_TYPES__ = (str , float , int , bool )
0 commit comments