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

Classes

class  DalBase
 
class  DalType
 

Functions

 __strcmp__ (v1, v2)
 
 __recmp__ (pat, v)
 
 prettyprint_cardinality (not_null, multivalue)
 
 prettyprint_range (attr)
 
 prettyprint_doc (entry)
 
 get_classes (m)
 
 generate (configuration, other_dals=[])
 
 module (name, schema, other_dals=[], backend='oksconflibs', db=None)
 

Variables

dict __dal__ = {}
 

Detailed Description

Contains the base class for DAL types and auxiliary methods.

This module defines the PyDALBase class that is used as the base type for all
DAL classes. A few utilities are also available.

Function Documentation

◆ __recmp__()

conffwk.dal.__recmp__ ( pat,
v )

Definition at line 18 of file dal.py.

18def __recmp__(pat, v):
19 return pat.match(v) is not None
20
21

◆ __strcmp__()

conffwk.dal.__strcmp__ ( v1,
v2 )

Definition at line 14 of file dal.py.

14def __strcmp__(v1, v2):
15 return v1 == v2
16
17

◆ generate()

conffwk.dal.generate ( configuration,
other_dals = [] )
Generates the DAL python access layer for the configuration passed.

This method will generate the python DAL access layer for all classes
declared through the conffwk.Configuration object passed. If this file
includes other schemas, the classes for those schemas will also be
generated, unless, classes with matching names are passed through the
"other_dals" parameters.

This method will re-use classes generated in other calls to this method,
either directly (in DAL binding to a python module) or while you created
Configuration type objects. So, you can call this as many times as you want
without incurring in much overhead.

Keyword parameters:

configuration -- The conffwk.Configuration object that you want the prepare
the DAL for.

other_dals -- This is a list of classes that contain other DALs that should
be considered for the inheritance structure of the classes that are going
to be generated here. These classes will not be regenerated. This parameter
can be either a list of modules or classes that won't be regenerated, but
re-used by this generation method.

Returns the DAL classes you asked for.

Definition at line 584 of file dal.py.

584def generate(configuration, other_dals=[]):
585 """Generates the DAL python access layer for the configuration passed.
586
587 This method will generate the python DAL access layer for all classes
588 declared through the conffwk.Configuration object passed. If this file
589 includes other schemas, the classes for those schemas will also be
590 generated, unless, classes with matching names are passed through the
591 "other_dals" parameters.
592
593 This method will re-use classes generated in other calls to this method,
594 either directly (in DAL binding to a python module) or while you created
595 Configuration type objects. So, you can call this as many times as you want
596 without incurring in much overhead.
597
598 Keyword parameters:
599
600 configuration -- The conffwk.Configuration object that you want the prepare
601 the DAL for.
602
603 other_dals -- This is a list of classes that contain other DALs that should
604 be considered for the inheritance structure of the classes that are going
605 to be generated here. These classes will not be regenerated. This parameter
606 can be either a list of modules or classes that won't be regenerated, but
607 re-used by this generation method.
608
609 Returns the DAL classes you asked for.
610 """
611 from types import ModuleType as module
612
613 klasses = []
614
615 other_classes = {}
616 for k in other_dals:
617 if isinstance(k, module):
618 other_classes.update(get_classes(k))
619 else:
620 other_classes[k.pyclassName()] = k
621
622 # we can save a few loops here, we order by number of bases
623 to_generate = {}
624 for k in configuration.classes():
625 if k in other_classes:
626 continue
627
628 N = len(configuration.superclasses(k))
629 if N in to_generate:
630 to_generate[N].append(k)
631 else:
632 to_generate[N] = [k]
633
634 ordered = []
635 run_order = list(to_generate.keys())
636 run_order.sort()
637 for k in run_order:
638 ordered += to_generate[k]
639
640 # generate what we need to
641 while ordered:
642 next = ordered[0] # gets the first one, no matter what it is
643
644 # if I generated this before, just re-use the class,
645 # so python can check the types in the way the user expects
646 if next in __dal__:
647 klasses.append(__dal__[next])
648 other_classes[next] = __dal__[next]
649
650 # else, I need to generate a brand new class here and
651 # add it to my __dal__
652 else:
653 bases = configuration.superclasses(next)
654 bases = [other_classes.get(k, None) for k in bases]
655 bases.append(DalBase)
656 if None in bases: # cannot yet generate for this one, rotate
657 ordered.append(next)
658 else: # we can generate this one now
659 klasses.append(DalType(next, tuple(bases),
660 {'__schema__':
661 configuration.__schema__[next]}))
662 # so we can use this next time
663 other_classes[next] = klasses[-1]
664 klasses[-1].__doc__ = prettyprint_doc(
665 configuration.__schema__[next])
666 __dal__[next] = klasses[-1]
667
668 del ordered[0]
669
670 return klasses
671
672

◆ get_classes()

conffwk.dal.get_classes ( m)
Returns a map with classes in a module, the key is the class name.

Definition at line 572 of file dal.py.

572def get_classes(m):
573 """Returns a map with classes in a module, the key is the class name."""
574
575 # assesses all classes from other modules, correlate with names
576 map = {}
577 for k in dir(m):
578 if k.find('__') == 0:
579 continue
580 map[k] = getattr(m, k)
581 return map
582
583

◆ module()

conffwk.dal.module ( name,
schema,
other_dals = [],
backend = 'oksconflibs',
db = None )
Creates a new python module with the OKS schema files passed as
parameter.

This method creates a new module for the user, using the schema files
passed as parameter. Classes from other DALs are not re-created, but just
re-used. This is an example usage:

import conffwk.dal
dal = conffwk.dal.module('dal', 'dal/schema/core.schema.xml')
DFdal = conffwk.dal.module('DFdal', 'DFConfiguration/schema/df.schema.xml',
                          [dal])

This will generate two python dals in the current context. One that binds
everything available in the first schema file and a second one that binds
everything else defined in the DF OKS schema file.

Keyword parameters:

name -- The name of the python module to create. It should match the name
of the variable you are attributing to, but it is not strictly required by
the python interpreter, just a good practice.

schema -- This is a list of OKS schema files that should be considered. You
can also pass OKS datafiles to this one, which actually includes the schema
files you want to have a DAL for. It will just work.

other_dals -- This is a list of other DAL modules that I'll not regenerate,
and which classes will *not* make part of the returned module. In fact,
this parameter is only used to restrict the amount of output classes since
once class is generated internally, it is not regenerated a second time.
In other words creating twice the same DAL implies in almost no overhead.

backend -- This is the OKS backend to use when retrieving the schemas. By
default it is set to 'oksconflibs', which is what we

Definition at line 673 of file dal.py.

673def module(name, schema, other_dals=[], backend='oksconflibs', db=None):
674 """Creates a new python module with the OKS schema files passed as
675 parameter.
676
677 This method creates a new module for the user, using the schema files
678 passed as parameter. Classes from other DALs are not re-created, but just
679 re-used. This is an example usage:
680
681 import conffwk.dal
682 dal = conffwk.dal.module('dal', 'dal/schema/core.schema.xml')
683 DFdal = conffwk.dal.module('DFdal', 'DFConfiguration/schema/df.schema.xml',
684 [dal])
685
686 This will generate two python dals in the current context. One that binds
687 everything available in the first schema file and a second one that binds
688 everything else defined in the DF OKS schema file.
689
690 Keyword parameters:
691
692 name -- The name of the python module to create. It should match the name
693 of the variable you are attributing to, but it is not strictly required by
694 the python interpreter, just a good practice.
695
696 schema -- This is a list of OKS schema files that should be considered. You
697 can also pass OKS datafiles to this one, which actually includes the schema
698 files you want to have a DAL for. It will just work.
699
700 other_dals -- This is a list of other DAL modules that I'll not regenerate,
701 and which classes will *not* make part of the returned module. In fact,
702 this parameter is only used to restrict the amount of output classes since
703 once class is generated internally, it is not regenerated a second time.
704 In other words creating twice the same DAL implies in almost no overhead.
705
706 backend -- This is the OKS backend to use when retrieving the schemas. By
707 default it is set to 'oksconflibs', which is what we
708 """
709 import types
710 from .Configuration import Configuration
711
712 retval = types.ModuleType(name)
713 if isinstance(schema, str):
714 schema = [schema]
715 for s in schema:
716 if db is None:
717 db = Configuration(backend + ':' + s)
718 else:
719 db.load(s)
720 db.__core_init__()
721
722 for k in generate(db, other_dals):
723 retval.__dict__[k.pyclassName()] = k
724 return retval

◆ prettyprint_cardinality()

conffwk.dal.prettyprint_cardinality ( not_null,
multivalue )
Returns a nice string representation for an object cardinality

Definition at line 22 of file dal.py.

22def prettyprint_cardinality(not_null, multivalue):
23 """Returns a nice string representation for an object cardinality"""
24 if not_null:
25 if multivalue:
26 return '1..*'
27 else:
28 return '1..1'
29
30 else:
31 if multivalue:
32 return '0..*'
33 else:
34 return '0..1'
35
36

◆ prettyprint_doc()

conffwk.dal.prettyprint_doc ( entry)
Pretty prints a schema Cache entry, to be used by __doc__ strings

Definition at line 46 of file dal.py.

46def prettyprint_doc(entry):
47 """Pretty prints a schema Cache entry, to be used by __doc__ strings"""
48 from .schema import oks_types
49
50 akeys = list(entry['attribute'].keys())
51 akeys.sort()
52 retval = ' Attributes:'
53 if len(akeys) == 0:
54 retval += ' None'
55 retval += '\n'
56 for k in akeys:
57 retval += ' - "' + k + '": ' + \
58 entry['attribute'][k]['description'].strip() + '\n'
59 retval += ' oks-type: ' + entry['attribute'][k]['type'] + '\n'
60 retval += ' cardinality: ' + \
61 prettyprint_cardinality(entry['attribute'][k]['not-null'],
62 entry['attribute'][k]['multivalue']) + '\n'
63 if entry['attribute'][k]['range']:
64 retval += ' range: ' + \
65 prettyprint_range(entry['attribute'][k]) + '\n'
66 if entry['attribute'][k]['init-value']:
67 retval += ' initial value: ' + \
68 str(entry['attribute'][k]['init-value']) + '\n'
69
70 rkeys = list(entry['relation'].keys())
71 rkeys.sort()
72 retval += '\n Relationships:'
73 if len(rkeys) == 0:
74 retval += ' None'
75 retval += '\n'
76 for k in rkeys:
77 retval += ' - "' + k + '": ' + \
78 entry['relation'][k]['description'].strip() + '\n'
79 retval += ' oks-class: ' + entry['relation'][k]['type'] + '\n'
80 retval += ' cardinality: ' + \
81 prettyprint_cardinality(entry['relation'][k]['not-null'],
82 entry['relation'][k]['multivalue']) + '\n'
83 retval += ' aggregated: ' + \
84 str(entry['relation'][k]['aggregation']) + '\n'
85 return retval[:-1]
86
87

◆ prettyprint_range()

conffwk.dal.prettyprint_range ( attr)
Prints the range of an attribute in a nice way

Definition at line 37 of file dal.py.

37def prettyprint_range(attr):
38 """Prints the range of an attribute in a nice way"""
39 to_print = list(attr['range']) # copy
40 for k in range(len(to_print)):
41 if isinstance(to_print[k], tuple):
42 to_print[k] = '..'.join([str(i) for i in to_print[k]])
43 return ', '.join([str(i) for i in to_print])
44
45

Variable Documentation

◆ __dal__

dict conffwk.dal.__dal__ = {}
private

Definition at line 9 of file dal.py.