DUNE-DAQ
DUNE Trigger and Data Acquisition software
Loading...
Searching...
No Matches
conffwk.dal.DalBase Class Reference
Inheritance diagram for conffwk.dal.DalBase:
[legend]
Collaboration diagram for conffwk.dal.DalBase:
[legend]

Public Member Functions

 __init__ (self, id, **kwargs)
 __reset_identity__ (self)
 className (self)
 isDalType (self, val)
 oksTypes (self)
 fullName (self)
 copy (self, other)
 rename (self, new_name)
 __repr__ (self)
 __str__ (self)
 __eq__ (self, other)
 __ne__ (self, other)
 __gt__ (self, other)
 __lt__ (self, other)
 __ge__ (self, other)
 __le__ (self, other)
 __hash__ (self)
 __getall__ (self, comp=None)
 get (self, className, idVal=None, lookBaseClasses=False)
 __getattr__ (self, par)
 setattr_nocheck (self, par, val)
 __setattr__ (self, par, val)

Static Public Member Functions

 updated ()
 reset_updated_list ()

Public Attributes

 id = new_name:

Static Protected Attributes

 _updated = set()

Private Attributes

 __class__
list __touched__ = []
str __fullname__ = '%s@%s' % (self.id, self.className())
 __hashvalue__ = hash(self.__fullname__)

Detailed Description

This class is used to represent any DAL object in the system. 

Definition at line 93 of file dal.py.

Constructor & Destructor Documentation

◆ __init__()

conffwk.dal.DalBase.__init__ ( self,
id,
** kwargs )
Constructs an object by setting its id (UID in OKS jargon) at least.

This method will initialize an object of the DalBase type, by setting
its internal properties (with schema cross-checking where it is
possible). The user should at least set the object's id, which at this
moment is not checked for uniqueness.

Keyword arguments:

id -- This is the unique identifier (per database) that the user wants
to assign to this object. This identifier will be used as the OKS
identifier when and if this object is ever serialized in an OKS
database.

**kwargs -- This is a set of attributes and relationships that must
exist in the associated DAL class that inherits from this base.

Definition at line 111 of file dal.py.

111 def __init__(self, id, **kwargs):
112 """Constructs an object by setting its id (UID in OKS jargon) at least.
113
114 This method will initialize an object of the DalBase type, by setting
115 its internal properties (with schema cross-checking where it is
116 possible). The user should at least set the object's id, which at this
117 moment is not checked for uniqueness.
118
119 Keyword arguments:
120
121 id -- This is the unique identifier (per database) that the user wants
122 to assign to this object. This identifier will be used as the OKS
123 identifier when and if this object is ever serialized in an OKS
124 database.
125
126 **kwargs -- This is a set of attributes and relationships that must
127 exist in the associated DAL class that inherits from this base.
128 """
129 from . import dalproperty
130 prop = property(dalproperty._return_attribute('id', self, id),
131 dalproperty._assign_attribute('id'))
132 setattr(self.__class__, 'id', prop)
133 self.__reset_identity__()
134
135 self.__touched__ = [] # optimization
136 for k, v in kwargs.items():
137 setattr(self, k, v)
138

Member Function Documentation

◆ __eq__()

conffwk.dal.DalBase.__eq__ ( self,
other )
True is the 2 objects have the same class and ID.

Definition at line 249 of file dal.py.

249 def __eq__(self, other):
250 """True is the 2 objects have the same class and ID."""
251 return self.__hashvalue__ == hash(other)
252

◆ __ge__()

conffwk.dal.DalBase.__ge__ ( self,
other )
True if the object is greater or equal than the other
alphabetically.

Definition at line 269 of file dal.py.

269 def __ge__(self, other):
270 """True if the object is greater or equal than the other
271 alphabetically.
272 """
273 return (self > other) or (self == other)
274

◆ __getall__()

conffwk.dal.DalBase.__getall__ ( self,
comp = None )
Get all relations, includding a link to myself

Definition at line 283 of file dal.py.

283 def __getall__(self, comp=None):
284 """Get all relations, includding a link to myself"""
285 top = False
286 if not comp:
287 top = True
288 comp = {}
289
290 if self.__fullname__ in comp:
291 return
292
293 comp[self.__fullname__] = self
294 for r in list(self.__schema__['relation'].keys()):
295
296 if not getattr(self, r):
297 continue
298
299 if isinstance(getattr(self, r), list):
300 for k in getattr(self, r):
301 k.__getall__(comp)
302 else:
303 getattr(self, r).__getall__(comp)
304
305 if top:
306 return comp
307

◆ __getattr__()

conffwk.dal.DalBase.__getattr__ ( self,
par )
Returns a given attribute or relationship.

This method returns an attribute or relationship from the current
object, or throws an AttributeError if no such thing exists. It sets
the field touched, so it does not get called twice.

Definition at line 365 of file dal.py.

365 def __getattr__(self, par):
366 """Returns a given attribute or relationship.
367
368 This method returns an attribute or relationship from the current
369 object, or throws an AttributeError if no such thing exists. It sets
370 the field touched, so it does not get called twice.
371 """
372
373 if par in self.__schema__['attribute']:
374 if self.__schema__['attribute'][par]['init-value']:
375 setattr(self, par, self.__schema__[
376 'attribute'][par]['init-value'])
377 else:
378 if self.__schema__['attribute'][par]['multivalue']:
379 setattr(self, par, [])
380 else:
381 return None # in this case, does not set anything
382 return getattr(self, par)
383
384 elif par in self.__schema__['relation']:
385 if self.__schema__['relation'][par]['multivalue']:
386 setattr(self, par, [])
387 else:
388 return None
389 return getattr(self, par)
390
391 raise AttributeError("'%s' object has no attribute/relation '%s'" %
392 (self.className(), par))
393

◆ __gt__()

conffwk.dal.DalBase.__gt__ ( self,
other )
True if the object is greater than the other alphabetically.

Definition at line 257 of file dal.py.

257 def __gt__(self, other):
258 """True if the object is greater than the other alphabetically."""
259 if self.className() == other.className():
260 return self.id > other.id
261 return self.className() > other.className()
262

◆ __hash__()

conffwk.dal.DalBase.__hash__ ( self)
This method is meant to be used to allow DAL objects as map keys.

Definition at line 279 of file dal.py.

279 def __hash__(self):
280 """This method is meant to be used to allow DAL objects as map keys."""
281 return self.__hashvalue__
282

◆ __le__()

conffwk.dal.DalBase.__le__ ( self,
other )
Returns True if the class is smaller or equal than the other. 

Definition at line 275 of file dal.py.

275 def __le__(self, other):
276 """Returns True if the class is smaller or equal than the other. """
277 return (self < other) or (self == other)
278

◆ __lt__()

conffwk.dal.DalBase.__lt__ ( self,
other )
True if the class is smaller than the other.  

Definition at line 263 of file dal.py.

263 def __lt__(self, other):
264 """True if the class is smaller than the other. """
265 if self.className() == other.className():
266 return self.id < other.id
267 return (self.className() < other.className())
268

◆ __ne__()

conffwk.dal.DalBase.__ne__ ( self,
other )
True if the 2 objects *not* have the same class and ID.

Definition at line 253 of file dal.py.

253 def __ne__(self, other):
254 """True if the 2 objects *not* have the same class and ID."""
255 return self.__hashvalue__ != hash(other)
256

◆ __repr__()

conffwk.dal.DalBase.__repr__ ( self)
Returns a nice representation of this object.

Definition at line 208 of file dal.py.

208 def __repr__(self):
209 """Returns a nice representation of this object."""
210 return "<%s>" % (self.__fullname__)
211

◆ __reset_identity__()

conffwk.dal.DalBase.__reset_identity__ ( self)

Definition at line 139 of file dal.py.

139 def __reset_identity__(self):
140 self.__fullname__ = '%s@%s' % (self.id, self.className())
141 self.__hashvalue__ = hash(self.__fullname__)
142

◆ __setattr__()

conffwk.dal.DalBase.__setattr__ ( self,
par,
val )
Sets an object attribute or relationship.

This method overrides the default setattr method, so it can apply
existence and type verification on class attributes. If the attribute
to be set starts with '__', or the passed value is None,
no verification is performed. If the value to set an attribute is a
list, the type verification is performed in every component of that
list.

N.B.: This method takes a reference to the object being passed. It does
not copy the value, so, if you do a.b = c, and then you apply changes
to 'c', these changes will be also applied to 'a.b'.

Parameters:

par -- The name of the parameter (attribute or relationship)

val -- The value that will be attributed to 'par'.

Raises AttributeError if the parameter does not exist.

Raises ValueError if the value you passed cannot be coerced to a
compatible OKS python type for the attribute or relationship you are
trying to set.

Definition at line 402 of file dal.py.

402 def __setattr__(self, par, val):
403 """Sets an object attribute or relationship.
404
405 This method overrides the default setattr method, so it can apply
406 existence and type verification on class attributes. If the attribute
407 to be set starts with '__', or the passed value is None,
408 no verification is performed. If the value to set an attribute is a
409 list, the type verification is performed in every component of that
410 list.
411
412 N.B.: This method takes a reference to the object being passed. It does
413 not copy the value, so, if you do a.b = c, and then you apply changes
414 to 'c', these changes will be also applied to 'a.b'.
415
416 Parameters:
417
418 par -- The name of the parameter (attribute or relationship)
419
420 val -- The value that will be attributed to 'par'.
421
422 Raises AttributeError if the parameter does not exist.
423
424 Raises ValueError if the value you passed cannot be coerced to a
425 compatible OKS python type for the attribute or relationship you are
426 trying to set.
427 """
428 from .schema import coerce, check_relation, check_cardinality
429 from . import dalproperty
430
431 # no checks for control parameters
432 if par[0:2] == '__':
433 self.__dict__[par] = val
434 return
435
436 # and for the id it is special
437 if par == 'id':
438 if isinstance(val, str):
439 prop = getattr(self.__class__, par)
440 prop.__set__(self, val)
441 self.__reset_identity__()
442 return
443 else:
444 raise ValueError(
445 'The "id" attribute of a DAL object must be a string')
446
447 if par in list(self.__schema__['attribute'].keys()):
448
449 # If val is None, skip checks
450 if val is None:
451 self.__dict__[par] = val
452
453 try:
454 if val is not None:
455 check_cardinality(val, self.__schema__['attribute'][par])
456 if self.__schema__['attribute'][par]['multivalue']:
457 result = \
458 [coerce(v, self.__schema__['attribute'][par])
459 for v in val]
460 else:
461 result = coerce(val, self.__schema__['attribute'][par])
462 else:
463 result = val
464
465 try:
466 prop = getattr(self.__class__, par)
467
468 except AttributeError:
469
470 prop = property(dalproperty._return_attribute(par),
471 dalproperty._assign_attribute(par))
472 setattr(self.__class__, par, prop)
473
474 prop.__set__(self, result)
475
476 except ValueError as e:
477 raise ValueError('Problems setting attribute "%s" '
478 'at object %s: %s' %
479 (par, self.fullName(), str(e)))
480
481 elif par in list(self.__schema__['relation'].keys()):
482
483 try:
484 # If val is None, skip checks
485 if val is not None:
486 check_cardinality(val, self.__schema__['relation'][par])
487
488 tmpval = \
489 val if self.__schema__[
490 'relation'][par]['multivalue'] else [val]
491 for v in tmpval:
492 check_relation(v, self.__schema__['relation'][par])
493
494 try:
495 prop = getattr(self.__class__, par)
496
497 except AttributeError:
498
499 multi = self.__schema__['relation'][par]['multivalue']
500 prop = property(
501 dalproperty._return_relation(par, multi=multi),
502 dalproperty._assign_relation(par))
503 setattr(self.__class__, par, prop)
504
505 prop.__set__(self, val)
506 self.__touched__.append(par)
507
508 except ValueError as e:
509 raise ValueError('Problems setting relation "%s" at '
510 'object %s: %s' %
511 (par, self.fullName(), str(e)))
512
513 else:
514 raise AttributeError('Parameter "%s" is not ' % par +
515 'part of class "%s" or any of its '
516 'parent classes' %
517 (self.className()))
518
519

◆ __str__()

conffwk.dal.DalBase.__str__ ( self)
Returns human readable information about the object.

Definition at line 212 of file dal.py.

212 def __str__(self):
213 """Returns human readable information about the object."""
214
215 retval = "%s(id='%s'" % (self.className(), self.id)
216
217 for a, v in self.__schema__['attribute'].items():
218 retval += ',\n %s = ' % a
219 if hasattr(self, a):
220 retval += str(getattr(self, a))
221 else:
222 retval += 'None'
223 if v['init-value']:
224 retval += ", # defaults to '%s'" % v['init-value']
225 else:
226 if v['not-null']:
227 retval += ', # MUST be set, there is not default!'
228
229 for r, v in self.__schema__['relation'].items():
230 retval += ',\n %s = ' % r
231 rel = None
232 if hasattr(self, r):
233 rel = getattr(self, r)
234
235 if rel is None:
236 retval += "<unset>"
237 elif isinstance(rel, list):
238 retval += str([repr(k) for k in getattr(self, r)])
239 retval += ']'
240 else:
241 retval += repr(getattr(self, r))
242
243 if retval[-2:] == ',\n':
244 retval = retval[:-2]
245 retval += ')'
246
247 return retval
248

◆ className()

conffwk.dal.DalBase.className ( self)

Definition at line 143 of file dal.py.

143 def className(self):
144 return self.__class__.pyclassName()
145

◆ copy()

conffwk.dal.DalBase.copy ( self,
other )
Copies attributes and relationships from the other component.

This will copy whatever relevant attributes and relationships from
another component into myself. The implemented algorithm starts by
iterating on my own schema and looking for the counter part on the
other class's schema, only matching values are copied. This is useful
to copy values from base class objects or templated class objects.

Arguments:

other -- This is the other dal object you are trying to copy.

Definition at line 158 of file dal.py.

158 def copy(self, other):
159 """Copies attributes and relationships from the other component.
160
161 This will copy whatever relevant attributes and relationships from
162 another component into myself. The implemented algorithm starts by
163 iterating on my own schema and looking for the counter part on the
164 other class's schema, only matching values are copied. This is useful
165 to copy values from base class objects or templated class objects.
166
167 Arguments:
168
169 other -- This is the other dal object you are trying to copy.
170 """
171 for k, v in self.__schema__['attribute'].items():
172 if k not in other.__schema__['attribute']:
173 continue
174 setattr(self, k, getattr(other, k))
175 for k, v in self.__schema__['relation'].items():
176 if k not in other.__schema__['relation']:
177 continue
178 obj = getattr(other, k)
179 try:
180 setattr(self, k, list(obj))
181 except TypeError:
182 setattr(self, k, obj)
183

◆ fullName()

conffwk.dal.DalBase.fullName ( self)

Definition at line 155 of file dal.py.

155 def fullName(self):
156 return self.__fullname__
157

◆ get()

conffwk.dal.DalBase.get ( self,
className,
idVal = None,
lookBaseClasses = False )
Get components in the object based on class name and/or id.

This method runs trough the components of its relationships and
returns a sorted list (sorting based on class name and object ID)
containing references to all components that match the search criteria.

Keyword Parameters (may be named):

className -- The name of the class to look for. Should be a string

idVal -- The id of the object to look for. If not set (or set to None),
the search will be based only on the class name. If set, it must be
either a string or an object that defines a match() method (such as a
regular expression).

lookBaseClasses -- If True and parameter to be search is a class, the
method will look also through the base classes names, so if value =
Application, for instance the method will return all objects of class
Application or that inherit from the Application class.

Returns a list with all the components that matched the search
criteria, if idVal is not set or is a type that defines a match()
method such as a regular expression. Otherwise (if it is a string)
returns a single object, if any is found following the criterias for
className and a exact idVal match.

Definition at line 308 of file dal.py.

308 def get(self, className, idVal=None, lookBaseClasses=False):
309 """Get components in the object based on class name and/or id.
310
311 This method runs trough the components of its relationships and
312 returns a sorted list (sorting based on class name and object ID)
313 containing references to all components that match the search criteria.
314
315 Keyword Parameters (may be named):
316
317 className -- The name of the class to look for. Should be a string
318
319 idVal -- The id of the object to look for. If not set (or set to None),
320 the search will be based only on the class name. If set, it must be
321 either a string or an object that defines a match() method (such as a
322 regular expression).
323
324 lookBaseClasses -- If True and parameter to be search is a class, the
325 method will look also through the base classes names, so if value =
326 Application, for instance the method will return all objects of class
327 Application or that inherit from the Application class.
328
329 Returns a list with all the components that matched the search
330 criteria, if idVal is not set or is a type that defines a match()
331 method such as a regular expression. Otherwise (if it is a string)
332 returns a single object, if any is found following the criterias for
333 className and a exact idVal match.
334 """
335 retval = []
336
337 cmp_class = __strcmp__
338 if hasattr(className, 'match'):
339 cmp_class = __recmp__
340
341 for v in self.__getall__().values():
342 if cmp_class(className, v.__class__.__name__) or \
343 (lookBaseClasses and v.isDalType(className)):
344 if idVal:
345 if type(idVal) == str:
346 if idVal == v.id:
347 return v
348 else:
349 continue
350
351 # if idVal is set and is not a string,
352 # we just go brute force...
353 if idVal.match(v.id):
354 retval.append(v)
355 else:
356 # if idVal is not set and we are sure the class matched,
357 # just append
358 retval.append(v)
359
360 if isinstance(idVal, str):
361 raise KeyError('Did not find %s@%s under %s' %
362 (idVal, className, self.fullName()))
363 return retval
364

◆ isDalType()

conffwk.dal.DalBase.isDalType ( self,
val )

Definition at line 146 of file dal.py.

146 def isDalType(self, val):
147 cmp = __strcmp__
148 if hasattr(val, 'match'):
149 cmp = __recmp__
150 return True in [cmp(val, k) for k in self.__class__.__okstypes__]
151

◆ oksTypes()

conffwk.dal.DalBase.oksTypes ( self)

Definition at line 152 of file dal.py.

152 def oksTypes(self):
153 return self.__class__.pyoksTypes()
154

◆ rename()

conffwk.dal.DalBase.rename ( self,
new_name )
Rename the DAL object to a new name.

This will store the old name in a hidden attribute and when
Configuration.update_dal() is called we use it to check
if there is an existing object with that name in the database.

If yes, we call the underlying ConfigObject.rename() method
transparently. If the old name does not exist in the database,
nothing special is done.

This is the only 'official' way to rename an object on the
DAL level. Just changing the 'id' attribute will not have
the same effect.

Definition at line 184 of file dal.py.

184 def rename(self, new_name):
185 """
186 Rename the DAL object to a new name.
187
188 This will store the old name in a hidden attribute and when
189 Configuration.update_dal() is called we use it to check
190 if there is an existing object with that name in the database.
191
192 If yes, we call the underlying ConfigObject.rename() method
193 transparently. If the old name does not exist in the database,
194 nothing special is done.
195
196 This is the only 'official' way to rename an object on the
197 DAL level. Just changing the 'id' attribute will not have
198 the same effect.
199 """
200 if self.id == new_name:
201 return
202
203 if not hasattr(self, '__old_id'):
204 setattr(self, '__old_id', getattr(self, 'id'))
205
206 self.id = new_name
207

◆ reset_updated_list()

conffwk.dal.DalBase.reset_updated_list ( )
static
Reset the set keeping track of modified DAL objects

Definition at line 106 of file dal.py.

106 def reset_updated_list():
107 """Reset the set keeping track of modified DAL objects
108 """
109 DalBase._updated.clear()
110

◆ setattr_nocheck()

conffwk.dal.DalBase.setattr_nocheck ( self,
par,
val )
Sets an attribute by-passing the built-in type check.

Definition at line 394 of file dal.py.

394 def setattr_nocheck(self, par, val):
395 """Sets an attribute by-passing the built-in type check."""
396
397 self.__dict__[par] = val
398 if par in list(self.__schema__['relation'].keys()):
399 self.__touched__.append(par)
400 return val
401

◆ updated()

conffwk.dal.DalBase.updated ( )
static
Returns a set of DAL objects that were modified in this DB session

Definition at line 100 of file dal.py.

100 def updated():
101 """Returns a set of DAL objects that were modified in this DB session
102 """
103 return set(DalBase._updated)
104

Member Data Documentation

◆ __class__

conffwk.dal.DalBase.__class__
private

Definition at line 132 of file dal.py.

◆ __fullname__

conffwk.dal.DalBase.__fullname__ = '%s@%s' % (self.id, self.className())
private

Definition at line 140 of file dal.py.

◆ __hashvalue__

conffwk.dal.DalBase.__hashvalue__ = hash(self.__fullname__)
private

Definition at line 141 of file dal.py.

◆ __touched__

list conffwk.dal.DalBase.__touched__ = []
private

Definition at line 135 of file dal.py.

◆ _updated

conffwk.dal.DalBase._updated = set()
staticprotected

Definition at line 97 of file dal.py.

◆ id

conffwk.dal.DalBase.id = new_name:

Definition at line 200 of file dal.py.


The documentation for this class was generated from the following file: