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 88 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 106 of file dal.py.

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

Member Function Documentation

◆ __eq__()

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

Definition at line 244 of file dal.py.

244 def __eq__(self, other):
245 """True is the 2 objects have the same class and ID."""
246 return self.__hashvalue__ == hash(other)
247

◆ __ge__()

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

Definition at line 264 of file dal.py.

264 def __ge__(self, other):
265 """True if the object is greater or equal than the other
266 alphabetically.
267 """
268 return (self > other) or (self == other)
269

◆ __getall__()

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

Definition at line 278 of file dal.py.

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

◆ __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 360 of file dal.py.

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

◆ __gt__()

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

Definition at line 252 of file dal.py.

252 def __gt__(self, other):
253 """True if the object is greater than the other alphabetically."""
254 if self.className() == other.className():
255 return self.id > other.id
256 return self.className() > other.className()
257

◆ __hash__()

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

Definition at line 274 of file dal.py.

274 def __hash__(self):
275 """This method is meant to be used to allow DAL objects as map keys."""
276 return self.__hashvalue__
277

◆ __le__()

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

Definition at line 270 of file dal.py.

270 def __le__(self, other):
271 """Returns True if the class is smaller or equal than the other. """
272 return (self < other) or (self == other)
273

◆ __lt__()

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

Definition at line 258 of file dal.py.

258 def __lt__(self, other):
259 """True if the class is smaller than the other. """
260 if self.className() == other.className():
261 return self.id < other.id
262 return (self.className() < other.className())
263

◆ __ne__()

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

Definition at line 248 of file dal.py.

248 def __ne__(self, other):
249 """True if the 2 objects *not* have the same class and ID."""
250 return self.__hashvalue__ != hash(other)
251

◆ __repr__()

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

Definition at line 203 of file dal.py.

203 def __repr__(self):
204 """Returns a nice representation of this object."""
205 return "<%s>" % (self.__fullname__)
206

◆ __reset_identity__()

conffwk.dal.DalBase.__reset_identity__ ( self)

Definition at line 134 of file dal.py.

134 def __reset_identity__(self):
135 self.__fullname__ = '%s@%s' % (self.id, self.className())
136 self.__hashvalue__ = hash(self.__fullname__)
137

◆ __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 397 of file dal.py.

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

◆ __str__()

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

Definition at line 207 of file dal.py.

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

◆ className()

conffwk.dal.DalBase.className ( self)

Definition at line 138 of file dal.py.

138 def className(self):
139 return self.__class__.pyclassName()
140

◆ 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 153 of file dal.py.

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

◆ fullName()

conffwk.dal.DalBase.fullName ( self)

Definition at line 150 of file dal.py.

150 def fullName(self):
151 return self.__fullname__
152

◆ 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 303 of file dal.py.

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

◆ isDalType()

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

Definition at line 141 of file dal.py.

141 def isDalType(self, val):
142 cmp = __strcmp__
143 if hasattr(val, 'match'):
144 cmp = __recmp__
145 return True in [cmp(val, k) for k in self.__class__.__okstypes__]
146

◆ oksTypes()

conffwk.dal.DalBase.oksTypes ( self)

Definition at line 147 of file dal.py.

147 def oksTypes(self):
148 return self.__class__.pyoksTypes()
149

◆ 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 179 of file dal.py.

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

◆ reset_updated_list()

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

Definition at line 101 of file dal.py.

101 def reset_updated_list():
102 """Reset the set keeping track of modified DAL objects
103 """
104 DalBase._updated.clear()
105

◆ setattr_nocheck()

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

Definition at line 389 of file dal.py.

389 def setattr_nocheck(self, par, val):
390 """Sets an attribute by-passing the built-in type check."""
391
392 self.__dict__[par] = val
393 if par in list(self.__schema__['relation'].keys()):
394 self.__touched__.append(par)
395 return val
396

◆ updated()

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

Definition at line 95 of file dal.py.

95 def updated():
96 """Returns a set of DAL objects that were modified in this DB session
97 """
98 return set(DalBase._updated)
99

Member Data Documentation

◆ __class__

conffwk.dal.DalBase.__class__
private

Definition at line 127 of file dal.py.

◆ __fullname__

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

Definition at line 135 of file dal.py.

◆ __hashvalue__

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

Definition at line 136 of file dal.py.

◆ __touched__

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

Definition at line 130 of file dal.py.

◆ _updated

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

Definition at line 92 of file dal.py.

◆ id

conffwk.dal.DalBase.id = new_name:

Definition at line 195 of file dal.py.


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