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