-
Notifications
You must be signed in to change notification settings - Fork 3
/
config_file_access.e
executable file
·393 lines (339 loc) · 10.6 KB
/
config_file_access.e
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
note
component: "Eiffel Object Modelling Framework"
description: "[
Access to a configuration file in ODIN format. Programmatic access uses paths to refer to
any specific item.
]"
keywords: "config, file"
author: "Thomas Beale <[email protected]>"
support: "http://www.openehr.org/issues/browse/AWB"
copyright: "Copyright (c) 2010- The openEHR Foundation <http://www.openEHR.org>"
license: "Apache 2.0 License <http://www.apache.org/licenses/LICENSE-2.0.html>"
class CONFIG_FILE_ACCESS
inherit
SHARED_DT_OBJECT_CONVERTER
export
{NONE} all;
{ANY} deep_twin, is_deep_equal, standard_is_equal
end
SHARED_RESOURCES
export
{NONE} all
end
EXCEPTIONS
export
{NONE} all
end
create
make
feature -- Initialisation
make (an_impl: CONFIG_FILE_ACCESS_I)
do
impl := an_impl
create errors.make
create refresh_listeners.make (0)
end
initialise (a_file_path: STRING)
-- Make with `a_file_path', which doesn't necessarily exist as a file. If it does, it will be read;
-- if not, nothing will be read, and the first save request will create the file new.
do
impl.set_file_path (a_file_path)
load
end
initialise_create (a_file_path: STRING)
-- Make with `a_file_path', which will be created new.
do
impl.set_file_path (a_file_path)
create_default_dt_tree
save
end
feature -- Access
file_path: STRING
-- path to resource file
do
Result := impl.file_path
end
requested_resources: ARRAYED_SET [STRING]
-- paths that the application has requsted so far
do
create Result.make (0)
Result.compare_objects
Result.merge (string_value_cache.current_keys)
Result.merge (string_list_value_cache.current_keys)
Result.merge (boolean_value_cache.current_keys)
Result.merge (integer_value_cache.current_keys)
Result.merge (object_value_cache.current_keys)
end
integer_value (a_path: STRING): INTEGER
-- get the integer value for resource at `a_path'
do
if integer_value_cache.has (a_path) then
Result := integer_value_cache.item (a_path)
elseif has_resource (a_path) and then attached dt_tree as att_dt_tree
and then attached {INTEGER} att_dt_tree.value_at_path (a_path) as int
then
Result := int
integer_value_cache.put (Result, a_path)
end
end
boolean_value (a_path: STRING): BOOLEAN
-- get the boolean value for resource at `a_path'
do
if boolean_value_cache.has (a_path) then
Result := boolean_value_cache.item (a_path)
elseif has_resource (a_path) and then attached dt_tree as att_dt_tree
and then attached {BOOLEAN} att_dt_tree.value_at_path (a_path) as bool
then
Result := bool
boolean_value_cache.put (Result, a_path)
end
end
string_value (a_path: STRING): STRING
-- get the string value for resource at `a_path'; return empty string if nothing found
do
if string_value_cache.has (a_path) then
check attached string_value_cache.item (a_path) as val then
Result := val
end
else
if has_resource (a_path) and then attached dt_tree as att_dt_tree
and then attached {STRING} att_dt_tree.value_at_path (a_path) as str
then
Result := str
else
create Result.make (0)
end
string_value_cache.put (Result, a_path)
end
end
string_list_value (a_path: STRING): ARRAYED_LIST [STRING]
-- List of items specified in file at `a_path'.
do
if string_list_value_cache.has (a_path) then
check attached string_list_value_cache.item (a_path) as val then
Result := val
end
else
create Result.make (0)
if has_resource (a_path) and then attached dt_tree as att_dt_tree then
if attached {ARRAYED_LIST [STRING]} att_dt_tree.value_list_at_path (a_path) as lst_str then
Result := lst_str
-- convert a single string to a string list
elseif attached {STRING} att_dt_tree.value_at_path (a_path) as str then
Result.extend (str)
end
end
string_list_value_cache.put (Result, a_path)
end
end
string_value_env_var_sub (a_path: STRING): STRING
-- get the string value for `a_path', with any env vars of form "$var" substituted
do
if string_value_cache.has (a_path) then
check attached string_value_cache.item (a_path) as val then
Result := val
end
else
if has_resource (a_path) and then attached dt_tree as att_dt_tree and then
attached {STRING} att_dt_tree.value_at_path (a_path) as str
then
Result := substitute_env_vars (str)
else
create Result.make (0)
end
string_value_cache.put (Result, a_path)
end
end
any_value (a_path: STRING): detachable ANY
-- get the value for resource_name
do
if object_value_cache.has (a_path) then
check attached object_value_cache.item (a_path) as val then
Result := val
end
elseif has_resource (a_path) and attached dt_tree as att_dt_tree then
Result := att_dt_tree.value_at_path (a_path)
object_value_cache.put (Result, a_path)
end
end
object_value (a_path: STRING; a_type_name: STRING): detachable ANY
-- get complex object at `a_path'
do
if object_value_cache.has (a_path) then
check attached object_value_cache.item (a_path) as val then
Result := val
end
elseif has_resource (a_path) and attached dt_tree as att_dt_tree then
if attached {DT_COMPLEX_OBJECT} att_dt_tree.node_at_path (a_path) as att_dt_obj then
Result := att_dt_obj.as_object_from_string (a_type_name, Void)
object_value_cache.put (Result, a_path)
end
end
end
errors: ERROR_ACCUMULATOR
feature -- Status Report
is_dirty: BOOLEAN
-- flag to indicate if any content-modifying call has been made since last call to `load' or `save'
has_resource (a_path: STRING): BOOLEAN
-- True if there is a resource at `a_path'
do
Result := attached dt_tree as att_dt_tree and then att_dt_tree.has_path (a_path)
end
feature -- Modification
put_integer_value (a_path: STRING; a_value: INTEGER)
-- put an instance of any ODIN leaf value type
do
put_dt_value (a_path, a_value)
integer_value_cache.force (a_value, a_path)
end
put_boolean_value (a_path: STRING; a_value: BOOLEAN)
-- put an instance of any ODIN leaf value type
do
put_dt_value (a_path, a_value)
boolean_value_cache.force (a_value, a_path)
end
put_string_value (a_path: STRING; a_value: STRING)
-- put an instance of any ODIN leaf value type
do
put_dt_value (a_path, a_value)
string_value_cache.force (a_value, a_path)
end
put_string_list_value (a_path: STRING; a_value: ARRAYED_LIST [STRING])
-- put an instance of any ODIN leaf value type
do
put_dt_value (a_path, a_value)
string_list_value_cache.force (a_value, a_path)
end
put_object (a_path: STRING; a_value: ANY)
-- convert a complex object to Data Tree form and put at `a_path' in current tree
-- FIXME: currently only works for single child paths, i.e. not where the paths ends with xxx[zzz]
local
obj_dt_tree: DT_COMPLEX_OBJECT
do
if not attached dt_tree then
create_default_dt_tree
end
if attached dt_tree as att_dt_tree then
obj_dt_tree := dt_object_converter.object_to_dt (a_value)
if not dt_object_converter.errors.has_errors then
if has_resource (a_path) and then attached att_dt_tree.attribute_node_at_path (a_path) as att_dt_attr then
att_dt_attr.remove_all_children
att_dt_attr.put_child (obj_dt_tree)
else
att_dt_tree.put_object_at_path (obj_dt_tree, a_path)
end
else
raise ("put_object_conversion_failure for type " + a_value.generating_type.name + " to Data Tree form")
end
is_dirty := True
object_value_cache.force (a_value, a_path)
end
end
add_refresh_listener (an_agent: PROCEDURE [ANY, TUPLE])
-- add a listener to be executed on file reload
do
end
feature -- Element Removal
remove_resource (a_path: STRING)
-- remove the resource resource_name
require
Valid_path: has_resource (a_path)
do
if attached dt_tree as att_dt_tree then
if attached att_dt_tree.attribute_node_at_path (a_path) as att_dt_attr and then attached att_dt_attr.parent as att_dt_obj then
att_dt_obj.remove_attribute (att_dt_attr.im_attr_name)
end
is_dirty := True
if string_value_cache.has (a_path) then
string_value_cache.remove (a_path)
elseif string_list_value_cache.has (a_path) then
string_list_value_cache.remove (a_path)
elseif boolean_value_cache.has (a_path) then
boolean_value_cache.remove (a_path)
elseif integer_value_cache.has (a_path) then
integer_value_cache.remove (a_path)
elseif object_value_cache.has (a_path) then
object_value_cache.remove (a_path)
end
end
ensure
Path_removed: not has_resource (a_path)
end
feature -- Commands
load
do
impl.load
if not impl.errors.has_errors then
dt_tree := impl.dt_tree
-- update any refresh listeners
across refresh_listeners as listeners_csr loop
listeners_csr.item.call ([])
end
is_dirty := False
end
end
save
-- serialise to file
do
check attached dt_tree as dtt then
impl.save (dtt)
end
is_dirty := False
end
feature {NONE} -- Implementation
impl: CONFIG_FILE_ACCESS_I
env_var_pattern_matcher: RX_PCRE_REGULAR_EXPRESSION
-- pattern for detecting $NAME in config values
once
create Result.make
Result.compile (env_var_pattern)
end
env_var_pattern: STRING = "\$[a-zA-Z0-9_]+"
dt_tree: detachable DT_COMPLEX_OBJECT
create_default_dt_tree
do
create dt_tree.make_anonymous
end
refresh_listeners: ARRAYED_LIST [PROCEDURE [ANY, TUPLE]]
-- listeners to execute on file refresh
string_value_cache: HASH_TABLE [STRING, STRING]
-- String values already found this session
once
create Result.make (0)
end
string_list_value_cache: HASH_TABLE [ARRAYED_LIST [STRING], STRING]
-- String list values already found this session
once
create Result.make (0)
end
boolean_value_cache: HASH_TABLE [BOOLEAN, STRING]
-- Boolean values already found this session
once
create Result.make (0)
end
integer_value_cache: HASH_TABLE [INTEGER, STRING]
-- Integer values already found this session
once
create Result.make (0)
end
object_value_cache: HASH_TABLE [detachable ANY, STRING]
-- Reference object values already found this session
once
create Result.make (0)
end
put_dt_value (a_path: STRING; a_value: ANY)
-- put an instance of any ODIN leaf value type
do
if not attached dt_tree then
create_default_dt_tree
end
if attached dt_tree as att_dt_tree then
if has_resource (a_path) then
att_dt_tree.set_value_at_path (a_value, a_path)
else
att_dt_tree.put_value_at_path (a_value, a_path)
end
is_dirty := True
end
end
end