configurationengine/source/cone/carbon/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
       
    26 
       
    27 """ Carbon to confml model mapping is done in Carbon2confml """
       
    28 from cone.confml import model as confmlmodel
       
    29 
       
    30 class Carbon2confml(object):
       
    31     """
       
    32     Carbon2confml class maps Carbon model object to confml model objects.  
       
    33     """
       
    34     def __init__(self):
       
    35         self.MAPPING_TABLE = {model.CarbonConfiguration : self.configuration,
       
    36                               model.FeatureList : self.configuration,
       
    37                               model.CarbonFeature: self.setting,
       
    38                               model.CarbonSetting: self.setting,
       
    39                               model.CarbonIntSetting: self.setting,
       
    40                               model.CarbonBooleanSetting: self.setting,
       
    41                               model.CarbonStringSetting: self.setting,
       
    42                               model.CarbonSelectionSetting: self.setting}
       
    43         pass
       
    44 
       
    45     def map_object(self, object):
       
    46         """
       
    47         Return a confml model object from Carbon object
       
    48         """
       
    49         try:
       
    50             return self.MAPPING_TABLE[object.__class__](object)
       
    51         except KeyError:
       
    52             return object
       
    53 
       
    54     def configuration(self, object):
       
    55         """
       
    56         Map a CarbonConfiguration object to a ConfmlConfiguration
       
    57         """
       
    58         mapdict = object._dict()
       
    59         mapobj = confmlmodel.ConfmlConfiguration(**mapdict)
       
    60         return mapobj
       
    61 
       
    62     def setting(self, object):
       
    63         """
       
    64         Map a CarbonSetting object to a ConfmlSetting
       
    65         """
       
    66         mapdict = object._dict()
       
    67         if object.__class__ == model.CarbonFeature:
       
    68             mapobj = api.Feature(**mapdict)
       
    69         elif object.__class__ == model.CarbonIntSetting:
       
    70             mapobj = confmlmodel.ConfmlIntSetting(**mapdict)
       
    71         elif object.__class__ == model.CarbonBooleanSetting:
       
    72             mapobj = confmlmodel.ConfmlBooleanSetting(**mapdict)
       
    73         elif object.__class__ == model.CarbonSelectionSetting:
       
    74             mapobj = confmlmodel.ConfmlSelectionSetting(**mapdict)
       
    75         elif object.__class__ == model.CarbonStringSetting:
       
    76             mapobj = confmlmodel.ConfmlSetting(**mapdict)
       
    77         elif object.__class__ == model.CarbonSetting:
       
    78             mapobj = confmlmodel.ConfmlSetting(**mapdict)
       
    79         else:
       
    80             raise exceptions.IncorrectClassError('Cannot find a mapping object for this class %s!' % object.__class__)
       
    81         return mapobj
       
    82 
       
    83 MAPPERS =  \
       
    84 { 'confml' : Carbon2confml,
       
    85   'carbon' : BaseMapper
       
    86 }
       
    87