2
2
The ldclient module contains the most common top-level entry points for the SDK.
3
3
"""
4
4
5
- import logging
6
-
7
5
from ldclient .rwlock import ReadWriteLock
8
6
from ldclient .version import VERSION
9
7
from .client import *
20
18
start_wait = 5
21
19
22
20
__client = None
23
- __config = Config ()
21
+ __config = None
24
22
__lock = ReadWriteLock ()
25
23
26
24
27
- def set_config (config ):
25
+ def set_config (config : Config ):
28
26
"""Sets the configuration for the shared SDK client instance.
29
27
30
28
If this is called prior to :func:`ldclient.get()`, it stores the configuration that will be used when the
31
29
client is initialized. If it is called after the client has already been initialized, the client will be
32
30
re-initialized with the new configuration (this will result in the next call to :func:`ldclient.get()`
33
31
returning a new client instance).
34
32
35
- :param ldclient.config.Config config: the client configuration
33
+ :param config: the client configuration
36
34
"""
37
35
global __config
38
36
global __client
39
37
global __lock
40
38
try :
41
39
__lock .lock ()
42
40
if __client :
43
- log .info ("Reinitializing LaunchDarkly Client " + version . VERSION + " with new config" )
41
+ log .info ("Reinitializing LaunchDarkly Client " + VERSION + " with new config" )
44
42
new_client = LDClient (config = config , start_wait = start_wait )
45
43
old_client = __client
46
44
__client = new_client
@@ -50,57 +48,15 @@ def set_config(config):
50
48
__lock .unlock ()
51
49
52
50
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 :
93
52
"""Returns the shared SDK client instance, using the current global configuration.
94
53
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.
99
57
100
58
If you need to create multiple client instances with different configurations, instead of this
101
59
singleton approach you can call the :class:`ldclient.client.LDClient` constructor directly instead.
102
-
103
- :rtype: ldclient.client.LDClient
104
60
"""
105
61
global __config
106
62
global __client
@@ -109,13 +65,15 @@ def get():
109
65
__lock .rlock ()
110
66
if __client :
111
67
return __client
68
+ if __config is None :
69
+ raise Exception ("set_config was not called" )
112
70
finally :
113
71
__lock .runlock ()
114
72
115
73
try :
116
74
__lock .lock ()
117
75
if not __client :
118
- log .info ("Initializing LaunchDarkly Client " + version . VERSION )
76
+ log .info ("Initializing LaunchDarkly Client " + VERSION )
119
77
__client = LDClient (config = __config , start_wait = start_wait )
120
78
return __client
121
79
finally :
@@ -136,27 +94,4 @@ def _reset_client():
136
94
c .close ()
137
95
138
96
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