Home | History | Annotate | Download | only in criterion
      1 # Copyright (c) 2014-2015, Intel Corporation
      2 # All rights reserved.
      3 #
      4 # Redistribution and use in source and binary forms, with or without modification,
      5 # are permitted provided that the following conditions are met:
      6 #
      7 # 1. Redistributions of source code must retain the above copyright notice, this
      8 # list of conditions and the following disclaimer.
      9 #
     10 # 2. Redistributions in binary form must reproduce the above copyright notice,
     11 # this list of conditions and the following disclaimer in the documentation and/or
     12 # other materials provided with the distribution.
     13 #
     14 # 3. Neither the name of the copyright holder nor the names of its contributors
     15 # may be used to endorse or promote products derived from this software without
     16 # specific prior written permission.
     17 #
     18 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
     19 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     20 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     21 # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
     22 # ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     23 # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     24 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
     25 # ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     26 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
     27 # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     28 
     29 from clientsimulator.criterion.Criterion import Criterion
     30 from clientsimulator.criterion.ExclusiveCriterion import ExclusiveCriterion
     31 from clientsimulator.criterion.InclusiveCriterion import InclusiveCriterion
     32 
     33 
     34 class CriterionClassFactory:
     35 
     36     def __init__(self, criteriaFileName):
     37         self.__criteriaFileName = criteriaFileName
     38 
     39     def __classFactory(self, name, allowedValues, base):
     40         """
     41             Private function which allows to dynamically create a new Criterion SubClass
     42 
     43             :param name: name of the futur subclass
     44             :type name: string
     45             :param allowedValues: all values the criterion can take
     46             :type allowedValues: string list
     47             :param base: direct mother class of the created criterion subclass
     48                          should be InclusiveCriterion or ExclusiveCriterion
     49             :type base: type
     50 
     51             :return: A criterion subclass
     52             :rtype: type
     53         """
     54 
     55         def __init__(self):
     56             """ Init Function of a Criterion Child Class """
     57             base.__init__(self)
     58 
     59         # Creation of the class with allowed values
     60         # (allows to have only one instance of the list)
     61         return type(
     62             name, (base,), {
     63                 "__init__": __init__, "_allowedValues": allowedValues})
     64 
     65     def generateCriterionClasses(self):
     66         """ Function invoqued to generate Criterion Childs classes from an AudioCriteria file"""
     67 
     68         # Parsing criterions File
     69         with open(self.__criteriaFileName, "r") as criteria:
     70             criteriaLines = criteria.readlines()
     71 
     72         parsedCriteria = [(typeAndName.split(), allowedValues.split())
     73                           for typeAndName, allowedValues
     74                           in (line.split(':') for line in criteriaLines)]
     75 
     76         generatedClasses = {}
     77         # Creation of needed classes thanks to criteria File information,
     78         # such as BaseClass, criterion name and possible values
     79         generatedClasses = [
     80             self.__classFactory(className, allowedValues, globals()[classBase])
     81             for (classBase, className), allowedValues in parsedCriteria
     82         ]
     83 
     84         return generatedClasses
     85