DUNE-DAQ
DUNE Trigger and Data Acquisition software
Loading...
Searching...
No Matches
conffwk.schema Namespace Reference

Classes

class  Cache

Functions

 decode_range (s)
 str2integer (v, t, max)
 to_int (v)
 to_long (v)
 check_range (v, range, range_re, pytype)
 check_relation (v, rel)
 check_cardinality (v, prop)
 coerce (v, attr)
 map_coercion (class_name, schema)

Variables

dict oks_types = {}
 range_regexp

Detailed Description

A set of utilities to simplify OKS instrospection.

Function Documentation

◆ check_cardinality()

check_cardinality ( v,
prop )
Checks the cardinality of a certain attribute or relationship.

Definition at line 130 of file schema.py.

130def check_cardinality(v, prop):
131 """Checks the cardinality of a certain attribute or relationship."""
132
133 # check cardinality
134 if prop['multivalue'] and not isinstance(v, list):
135 raise ValueError('Multivalued properties must be python lists')
136 elif not prop['multivalue'] and isinstance(v, list):
137 raise ValueError(
138 'Single valued properties cannot be set with python lists')
139
140

◆ check_range()

check_range ( v,
range,
range_re,
pytype )
Checks the range of the value 'v' to make sure it is inside.

Definition at line 92 of file schema.py.

92def check_range(v, range, range_re, pytype):
93 """Checks the range of the value 'v' to make sure it is inside."""
94 if isinstance(v, list):
95 for k in v:
96 check_range(k, range, range_re, pytype)
97 return
98
99 in_range = False
100 if type(v) == str and range_re is not None:
101 if range_re.match(v):
102 in_range = True
103 else:
104 for k in range:
105 if isinstance(k, tuple):
106 if v >= k[0] and v <= k[1]:
107 in_range = True
108 else:
109 if v == k:
110 in_range = True
111
112 if not in_range:
113 raise ValueError('Value %s is not in range %s' % (v, range))
114
115

◆ check_relation()

check_relation ( v,
rel )
Checks the value v against the relationship parameters in 'rel'.

Definition at line 116 of file schema.py.

116def check_relation(v, rel):
117 """Checks the value v against the relationship parameters in 'rel'."""
118 from conffwk.dal import DalBase
119
120 if not isinstance(v, DalBase):
121 raise ValueError('Relationships should be DAL objects, but %s is not' %
122 repr(v))
123
124 # check type
125 if rel['type'] not in v.oksTypes():
126 raise ValueError('Object %s is not of type or subtype %s' %
127 (repr(v), rel['type']))
128
129

◆ coerce()

coerce ( v,
attr )
Coerces the input value 'v' in the way the attribute expects.

Definition at line 141 of file schema.py.

141def coerce(v, attr):
142 """Coerces the input value 'v' in the way the attribute expects."""
143
144 # coerce if necessary
145 if type(v) != attr['python_class']:
146 # coerce in this case
147 if attr['python_class'] in [int, int]:
148
149 if isinstance(v, str):
150 if len(v) == 0:
151 raise ValueError(
152 'Integer number cannot be assigned an empty string')
153 if v[0] == '0':
154 try:
155 return attr['python_class'](v, 8)
156 except ValueError:
157 return attr['python_class'](v, 16)
158 else:
159 try:
160 return attr['python_class'](v)
161 except ValueError:
162 return attr['python_class'](v, 16)
163 else:
164 v = attr['python_class'](v)
165
166 elif attr['python_class'] is bool:
167 if isinstance(v, str):
168 if v in ['0', 'false']:
169 v = False
170 else:
171 v = True
172 else:
173 v = attr['python_class'](v)
174
175 else:
176 v = attr['python_class'](v)
177
178 # check the range of each item, if a range was set
179 if attr['range']:
180 check_range(v, attr['range'], attr['range_re'], attr['python_class'])
181
182 # for special types, do a special check
183 if attr['type'] == 'class':
184 # class references must exist in the DAL the time I set it
185 from .dal import __dal__
186 check_range(v, list(__dal__.keys()),
187 attr['range_re'], attr['python_class'])
188
189 elif attr['type'] == 'date':
190 import time
191 try:
192 time.strptime(v, '%Y-%m-%d')
193 except ValueError as e:
194 try:
195 time.strptime(v, '%d/%m/%y')
196 except ValueError as e:
197 try:
198 time.strptime(v, '%Y-%b-%d')
199 except ValueError as e:
200 raise ValueError(
201 'Date types should have the format '
202 'dd/mm/yy or yyyy-mm-dd or yyyy-mon-dd: %s' % v)
203
204 elif attr['type'] == 'time':
205 import time
206 try:
207 time.strptime(v, '%Y-%m-%d %H:%M:%S')
208 except ValueError as e:
209 try:
210 time.strptime(v, '%d/%m/%y %H:%M:%S')
211 except ValueError as e:
212 try:
213 time.strptime(v, '%Y-%b-%d %H:%M:%S')
214 except ValueError as e:
215 raise ValueError(
216 'Time types should have the format dd/mm/yy HH:MM:SS '
217 'or yyyy-mm-dd HH:MM:SS or yyyy-mon-dd HH:MM:SS:'
218 + str(e))
219
220 return v
221
222

◆ decode_range()

decode_range ( s)
Decodes a range string representation, returns a tuple with 2 values.

This is the supported format in regexp representation:
'([-0x]*\\d+)\\D+-?\\d+'

Definition at line 34 of file schema.py.

34def decode_range(s):
35 """Decodes a range string representation, returns a tuple with 2 values.
36
37 This is the supported format in regexp representation:
38 '([-0x]*\\d+)\\D+-?\\d+'
39 """
40 if s.find('..') != -1:
41 # print 'range: %s => %s' % (s, s.split('..'))
42 return s.split('..')
43 k = range_regexp.match(s)
44 if k:
45 # print 'range: %s => %s' % (s, (k.group('s1'), k.group('s2')))
46 return (k.group('s1'), k.group('s2'))
47 # print 'value: %s' % s
48 return s
49
50

◆ map_coercion()

map_coercion ( class_name,
schema )
Given a schema of a class, maps coercion functions from libpyconffwk.

Definition at line 223 of file schema.py.

223def map_coercion(class_name, schema):
224 """Given a schema of a class, maps coercion functions from libpyconffwk."""
225
226 cls = ConfigObject.ConfigObject
227
228 schema['mapping'] = {}
229
230 for k, v in list(schema['attribute'].items()):
231 typename = v['type']
232 getname = v['type']
233 if getname in oks_types['string']:
234 getname = 'string'
235 if v['multivalue']:
236 typename += '_vec'
237 getname += '_vec'
238 v['co_get_method'] = getattr(cls, 'get_' + getname)
239 v['co_set_method'] = getattr(cls, 'set_' + typename)
240
241 else:
242 v['co_get_method'] = getattr(cls, 'get_' + getname)
243 v['co_set_method'] = getattr(cls, 'set_' + typename)
244
245 if v['type'] in oks_types['string']:
246 v['python_class'] = str
247 elif v['type'] in oks_types['bool']:
248 v['python_class'] = bool
249 elif v['type'] in oks_types['integer']:
250 v['python_class'] = to_int
251 elif v['type'] in oks_types['long']:
252 v['python_class'] = to_long
253 elif v['type'] in oks_types['float']:
254 v['python_class'] = float
255
256 # split and coerce ranges
257 v['range_re'] = None
258 if v['range']:
259 if v['type'] == 'string':
260 v['range_re'] = re.compile(v['range'])
261 else:
262 v['range'] = [decode_range(j) for j in v['range'].split(',')]
263 for j in range(len(v['range'])):
264 if isinstance(v['range'][j], str):
265 v['range'][j] = v['python_class'](v['range'][j])
266 elif len(v['range'][j]) == 1:
267 v['range'][j] = v['python_class'](v['range'][j][0])
268 else: # len(v['range'][j]) == 2 (the only other case)
269 v['range'][j] = (v['python_class'](v['range'][j][0]),
270 v['python_class'](v['range'][j][1]))
271
272 # integer numbers have implicit ranges and it is better to check
273 elif v['type'] in oks_types['int-number']:
274 if v['type'] == 's8':
275 v['range'] = [(-2**7, (2**7)-1)]
276 if v['type'] == 'u8':
277 v['range'] = [(0, (2**8)-1)]
278 if v['type'] == 's16':
279 v['range'] = [(-2**15, (2**15)-1)]
280 if v['type'] == 'u16':
281 v['range'] = [(0, (2**16)-1)]
282 if v['type'] == 's32':
283 v['range'] = [(-2**31, (2**31)-1)]
284 if v['type'] == 'u32':
285 v['range'] = [(0, (2**32)-1)]
286 if v['type'] == 's64':
287 v['range'] = [(-2**63, (2**63)-1)]
288 if v['type'] == 'u64':
289 v['range'] = [(0, (2**64)-1)]
290
291 # coerce initial values
292 if v['init-value']:
293 try:
294 if v['type'] in oks_types['string']:
295 pass
296 elif v['multivalue']:
297 v['init-value'] = [coerce(j, v)
298 for j in v['init-value'].split(',')]
299 else:
300 v['init-value'] = coerce(v['init-value'], v)
301 except ValueError as e:
302 logging.warning('Initial value of "%s.%s" could not be '
303 'coerced: %s' %
304 (class_name, k, e))
305
306 # if the type is a date or time type, and there is not default,
307 # set "now"
308 if not v['init-value'] and v['type'] == 'date':
309 import datetime
310 v['init-value'] == datetime.date.today().isoformat()
311
312 # if the type is a date or time type, and there is not default,
313 # set "now"
314 if not v['init-value'] and v['type'] == 'time':
315 import datetime
316 now = datetime.datetime.today()
317 now.replace(microsecond=0)
318 v['init-value'] == now.isoformat(sep=' ')
319
320 for v in list(schema['relation'].values()):
321 if v['multivalue']:
322 v['co_get_method'] = getattr(cls, 'get_objs')
323 v['co_set_method'] = getattr(cls, 'set_objs')
324
325 else:
326 v['co_get_method'] = getattr(cls, 'get_obj')
327 v['co_set_method'] = getattr(cls, 'set_obj')
328
329 return schema
330
331

◆ str2integer()

str2integer ( v,
t,
max )
Converts a value v to integer, irrespectively of its formatting.

If the number starts with a '0', we convert it using an octal
representation. Else, we try a decimal conversion. If any of these fail,
we try an hexa conversion before throwing a ValueError.

Keyword arguments:

v -- the value to be converted
t -- the python type (int or float) to use in the conversion

Definition at line 51 of file schema.py.

51def str2integer(v, t, max):
52 """Converts a value v to integer, irrespectively of its formatting.
53
54 If the number starts with a '0', we convert it using an octal
55 representation. Else, we try a decimal conversion. If any of these fail,
56 we try an hexa conversion before throwing a ValueError.
57
58 Keyword arguments:
59
60 v -- the value to be converted
61 t -- the python type (int or float) to use in the conversion
62 """
63 if isinstance(v, t):
64 return v
65 if not v:
66 return v
67 if isinstance(v, tuple) or isinstance(v, list):
68 return [str2integer(k, t) for k in v]
69 if isinstance(v, str):
70 if v[0] == '*':
71 return max
72 elif v[0] == '0':
73 try:
74 return t(v, 8)
75 except ValueError:
76 return t(v, 16)
77 else:
78 try:
79 return t(v)
80 except ValueError:
81 return t(v, 16)
82 else:
83 return t(v)
84
85

◆ to_int()

to_int ( v)

Definition at line 86 of file schema.py.

86def to_int(v): return str2integer(v, int, sys.maxsize)
87
88

◆ to_long()

to_long ( v)

Definition at line 89 of file schema.py.

89def to_long(v): return str2integer(v, int, 0xffffffffffffffff)
90
91

Variable Documentation

◆ oks_types

dict conffwk.schema.oks_types = {}

Definition at line 19 of file schema.py.

◆ range_regexp

conffwk.schema.range_regexp
Initial value:
= re.compile(
r'(?P<s1>-?0?x?[\da-fA-F]+(\.\d+)?)-(?P<s2>-?0?x?[\da-fA-F]+(\.\d+)?)')

Definition at line 30 of file schema.py.