-
Notifications
You must be signed in to change notification settings - Fork 90
Expand file tree
/
Copy pathconfiguration.py
More file actions
287 lines (231 loc) · 10.5 KB
/
configuration.py
File metadata and controls
287 lines (231 loc) · 10.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
#
# Copyright (c) 2016-2026 Deephaven Data Labs and Patent Pending
#
"""This module provides access to the Deephaven Configuration system, allowing users to retrieve configuration
properties as strings, integers, floats, or booleans with optional default values.
"""
from typing import Optional
import jpy
from deephaven import DHError
from deephaven._wrapper import JObjectWrapper
from deephaven.jcompat import j_collection_to_list
_JConfiguration = jpy.get_type("io.deephaven.configuration.Configuration")
class Configuration(JObjectWrapper):
"""A wrapper for the Java Configuration class that provides convenient access to configuration properties.
This class wraps the singleton Configuration.getInstance() by default, but can also wrap a passed in
Configuration instance.
"""
j_object_type = _JConfiguration
def __init__(self, j_configuration: Optional[jpy.JType] = None):
"""Initialize a Configuration wrapper.
Args:
j_configuration (Optional[jpy.JType]): The Java Configuration instance to wrap. If None, uses the
default Configuration.getInstance().
"""
self._j_configuration = (
j_configuration
if j_configuration is not None
else _JConfiguration.getInstance()
)
@property
def j_object(self) -> jpy.JType:
"""The wrapped Java Configuration instance."""
return self._j_configuration
def get_property(
self, property_name: str, default: Optional[str] = None
) -> Optional[str]:
"""Get a configuration property as a string.
Args:
property_name (str): The name of the property to retrieve.
default (Optional[str]): The default value to return if the property is not set. If None and the
property is not set, raises DHError.
Returns:
Optional[str]: The property value as a string, or the default value if provided and the property is not set.
Raises:
DHError: If the property is not set and no default value is provided.
"""
try:
if default is None:
return self._j_configuration.getProperty(property_name)
else:
return self._j_configuration.getStringWithDefault(
property_name, default
)
except Exception as e:
raise DHError(message=f"Failed to get property '{property_name}'") from e
def get_string(
self, property_name: str, default: Optional[str] = None
) -> Optional[str]:
"""Get a configuration property as a string. Alias for get_property.
Args:
property_name (str): The name of the property to retrieve.
default (Optional[str]): The default value to return if the property is not set. If None and the
property is not set, raises DHError.
Returns:
Optional[str]: The property value as a string, or the default value if provided and the property is not set.
Raises:
DHError: If the property is not set and no default value is provided.
"""
return self.get_property(property_name, default)
def get_int(
self, property_name: str, default: Optional[int] = None
) -> Optional[int]:
"""Get a configuration property as an integer.
Args:
property_name (str): The name of the property to retrieve.
default (Optional[int]): The default value to return if the property is not set. If None and the
property is not set, raises DHError.
Returns:
Optional[int]: The property value as an integer, or the default value if provided and the property is not set.
Raises:
DHError: If the property is not set and no default value is provided, or if the property value
cannot be parsed as an integer.
"""
try:
if default is None:
return self._j_configuration.getInteger(property_name)
else:
return self._j_configuration.getIntegerWithDefault(
property_name, default
)
except Exception as e:
raise DHError(
message=f"Failed to get integer property '{property_name}'"
) from e
def get_float(
self, property_name: str, default: Optional[float] = None
) -> Optional[float]:
"""Get a configuration property as a float (double).
Args:
property_name (str): The name of the property to retrieve.
default (Optional[float]): The default value to return if the property is not set. If None and the
property is not set, raises DHError.
Returns:
Optional[float]: The property value as a float, or the default value if provided and the property is not set.
Raises:
DHError: If the property is not set and no default value is provided, or if the property value
cannot be parsed as a float.
"""
try:
if default is None:
return self._j_configuration.getDouble(property_name)
else:
return self._j_configuration.getDoubleWithDefault(
property_name, default
)
except Exception as e:
raise DHError(
message=f"Failed to get float property '{property_name}'"
) from e
def get_bool(
self, property_name: str, default: Optional[bool] = None
) -> Optional[bool]:
"""Get a configuration property as a boolean.
Args:
property_name (str): The name of the property to retrieve.
default (Optional[bool]): The default value to return if the property is not set. If None and the
property is not set, raises DHError.
Returns:
Optional[bool]: The property value as a boolean, or the default value if provided and the property is not set.
Raises:
DHError: If the property is not set and no default value is provided, or if the property value
cannot be parsed as a boolean.
"""
try:
if default is None:
return self._j_configuration.getBoolean(property_name)
else:
return self._j_configuration.getBooleanWithDefault(
property_name, default
)
except Exception as e:
raise DHError(
message=f"Failed to get boolean property '{property_name}'"
) from e
def get_long(
self, property_name: str, default: Optional[int] = None
) -> Optional[int]:
"""Get a configuration property as a long integer.
Args:
property_name (str): The name of the property to retrieve.
default (Optional[int]): The default value to return if the property is not set. If None and the
property is not set, raises DHError.
Returns:
Optional[int]: The property value as a long integer, or the default value if provided and the property is not set.
Raises:
DHError: If the property is not set and no default value is provided, or if the property value
cannot be parsed as a long.
"""
try:
if default is None:
return self._j_configuration.getLong(property_name)
else:
return self._j_configuration.getLongWithDefault(property_name, default)
except Exception as e:
raise DHError(
message=f"Failed to get long property '{property_name}'"
) from e
def has_property(self, property_name: str) -> bool:
"""Check if a configuration property is defined.
Args:
property_name (str): The name of the property to check.
Returns:
bool: True if the property is defined, False otherwise.
"""
try:
return self._j_configuration.hasProperty(property_name)
except Exception as e:
raise DHError(message=f"Failed to check property '{property_name}'") from e
def __getitem__(self, property_name: str) -> str:
"""Get a configuration property as a string using square bracket notation.
This implements the mapping protocol to allow `config[property_name]` syntax.
Args:
property_name (str): The name of the property to retrieve.
Returns:
str: The property value as a string.
Raises:
KeyError: If the property is not set.
"""
try:
return self._j_configuration.getProperty(property_name)
except Exception as e:
raise KeyError(f"Property '{property_name}' not found") from e
def __contains__(self, property_name: str) -> bool:
"""Check if a configuration property is defined using the 'in' operator.
This implements the mapping protocol to allow `property_name in config` syntax.
Args:
property_name (str): The name of the property to check.
Returns:
bool: True if the property is defined, False otherwise.
"""
return self.has_property(property_name)
def __len__(self) -> int:
"""Return the number of configuration properties.
This implements the mapping protocol to allow `len(config)` syntax.
Returns:
int: The number of properties in the configuration.
"""
try:
return self._j_configuration.getProperties().size()
except Exception as e:
raise DHError(message="Failed to get configuration size") from e
def __iter__(self):
"""Return an iterator over the property names.
This implements the mapping protocol to allow iteration over property names.
Returns:
iterator: An iterator over the property names (keys).
"""
try:
# Get the Properties object and convert its keySet to a Python list
properties = self._j_configuration.getProperties()
key_set = properties.keySet()
# Convert Java Set to Python list
return iter(j_collection_to_list(key_set))
except Exception as e:
raise DHError(message="Failed to iterate over configuration keys") from e
def get_configuration() -> Configuration:
"""Get the default Configuration instance.
Returns:
Configuration: The default Configuration instance wrapping Configuration.getInstance().
"""
return Configuration()