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__()

__recmp__ ( pat,
v )

Definition at line 23 of file dal.py.

23def __recmp__(pat, v):
24 return pat.match(v) is not None
25
26

◆ __strcmp__()

__strcmp__ ( v1,
v2 )

Definition at line 19 of file dal.py.

19def __strcmp__(v1, v2):
20 return v1 == v2
21
22

◆ generate()

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

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

◆ get_classes()

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

Definition at line 577 of file dal.py.

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

◆ module()

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

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

◆ prettyprint_cardinality()

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

Definition at line 27 of file dal.py.

27def prettyprint_cardinality(not_null, multivalue):
28 """Returns a nice string representation for an object cardinality"""
29 if not_null:
30 if multivalue:
31 return '1..*'
32 else:
33 return '1..1'
34
35 else:
36 if multivalue:
37 return '0..*'
38 else:
39 return '0..1'
40
41

◆ prettyprint_doc()

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

Definition at line 51 of file dal.py.

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

◆ prettyprint_range()

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

Definition at line 42 of file dal.py.

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

Variable Documentation

◆ __dal__

dict conffwk.dal.__dal__ = {}
private

Definition at line 14 of file dal.py.