configurationengine/source/cone/confml/mapping.py
changeset 0 2e8eeb919028
equal deleted inserted replaced
-1:000000000000 0:2e8eeb919028
       
     1 #
       
     2 # Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 # All rights reserved.
       
     4 # This component and the accompanying materials are made available
       
     5 # under the terms of "Eclipse Public License v1.0"
       
     6 # which accompanies this distribution, and is available
       
     7 # at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 #
       
     9 # Initial Contributors:
       
    10 # Nokia Corporation - initial contribution.
       
    11 #
       
    12 # Contributors:
       
    13 #
       
    14 # Description:
       
    15 #
       
    16 ## 
       
    17 # @author Teemu Rytkonen
       
    18 
       
    19 
       
    20 """
       
    21 Methods for Mapping Carbon model to other data model objects 
       
    22 """
       
    23 from cone.public import api, exceptions, container, utils
       
    24 from cone.public.mapping import BaseMapper
       
    25 from cone.carbon import model as carbonmodel
       
    26 
       
    27 """ Carbon to confml model mapping is done in Carbon2confml """
       
    28 from cone.confml import model 
       
    29 
       
    30 class Confml2carbon(object):
       
    31     """
       
    32     Carbon2confml class maps Carbon model object to confml model objects.  
       
    33     """
       
    34     def __init__(self):
       
    35         self.MAPPING_TABLE = {model.ConfmlConfiguration: self.configuration,
       
    36                               model.ConfmlFeature: self.setting,
       
    37                               model.ConfmlSetting: self.setting,
       
    38                               model.ConfmlIntSetting: self.setting,
       
    39                               model.ConfmlBooleanSetting: self.setting,
       
    40                               model.ConfmlSelectionSetting: self.setting}
       
    41         pass
       
    42 
       
    43     def map_object(self, object):
       
    44         """
       
    45         Return a confml model object from Carbon object
       
    46         """
       
    47         try:
       
    48             return self.MAPPING_TABLE[object.__class__](object)
       
    49         except KeyError:
       
    50             return object
       
    51 
       
    52     def configuration(self, object):
       
    53         """
       
    54         Map a CarbonConfiguration object to a ConfmlConfiguration
       
    55         """
       
    56         mapdict = object._dict()
       
    57         if object.meta and object.meta.get('type') == 'featurelist':
       
    58             mapobj = object._clone(class_instance=carbonmodel.FeatureList, recursion=True)
       
    59         else:
       
    60             mapobj = object._clone(class_instance=carbonmodel.CarbonConfiguration, recursion=True)
       
    61         return mapobj
       
    62 
       
    63     def setting(self, object):
       
    64         """
       
    65         Map a CarbonSetting object to a ConfmlSetting
       
    66         """
       
    67         mapdict = object._dict()
       
    68         if object.__class__ == model.ConfmlFeature:
       
    69             mapobj = object._clone(class_instance=carbonmodel.CarbonFeature, recursion=True)
       
    70         elif object.__class__ == model.ConfmlIntSetting:
       
    71             mapobj = object._clone(class_instance=carbonmodel.CarbonIntSetting, recursion=True)
       
    72         elif object.__class__ == model.ConfmlBooleanSetting:
       
    73             mapobj = object._clone(class_instance=carbonmodel.CarbonBooleanSetting, recursion=True)
       
    74         elif object.__class__ == model.ConfmlSelectionSetting:
       
    75             mapobj = object._clone(class_instance=carbonmodel.CarbonSelectionSetting, recursion=True)
       
    76         elif object.__class__ == model.ConfmlSetting:
       
    77             mapobj = object._clone(class_instance=carbonmodel.CarbonSetting, recursion=True)
       
    78         else:
       
    79             raise exceptions.IncorrectClassError('Cannot find a mapping object for this class %s!' % object.__class__)
       
    80         return mapobj
       
    81 
       
    82 MAPPERS =  \
       
    83 { 'carbon' : Confml2carbon,
       
    84   'confml' : BaseMapper
       
    85 }
       
    86