configurationengine/source/cone/carbon/model.py
changeset 0 2e8eeb919028
child 3 e7e0ae78773e
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 import posixpath
       
    21 import datetime
       
    22 
       
    23 
       
    24 """
       
    25 Base class for Carbon specific elements.
       
    26 Attributes:
       
    27 """
       
    28 from cone.public import api, exceptions, container, utils
       
    29 from cone.confml import model as confmlmodel
       
    30 
       
    31 class ResourceList(object):
       
    32     def __init__(self):
       
    33         self.resources = {}
       
    34 
       
    35     def add_resource(self,resource):
       
    36         self.resources[resource.get_path()] = resource
       
    37 
       
    38     def get_resource(self,path):
       
    39         return self.resources[path]
       
    40 
       
    41     def remove_resource(self,path):
       
    42         del self.resources[path]
       
    43 
       
    44     def list_resources(self):
       
    45         return self.resources.keys()
       
    46 
       
    47     def __len__(self):
       
    48         return len(self.resources)
       
    49 
       
    50     def __getitem__(self, key):
       
    51         return self.resources[key]
       
    52 
       
    53     def __setitem__(self, key, value):
       
    54         self.resources[key] = value
       
    55 
       
    56     def __delitem__( self, key):
       
    57         del self.resources[key]
       
    58 
       
    59     def __iter__(self):
       
    60         return iter(self.resources.values())
       
    61 
       
    62 
       
    63 class ConfigurationResource(object):
       
    64     FILE_EXTENSION = '/root.confml'
       
    65     def __init__(self, **kwargs):
       
    66         self.name          = kwargs.get('configuration_name', None)
       
    67         self.path          = kwargs.get('path', None)
       
    68         self.parent_config = kwargs.get('parent_config', None)
       
    69         self.version       = kwargs.get('version_identifier', None)
       
    70 
       
    71     def get_path(self):
       
    72         path = utils.resourceref.remove_begin_slash(self.path)
       
    73         path = utils.resourceref.remove_end_slash(path)
       
    74         return path + self.FILE_EXTENSION
       
    75 
       
    76     def __str__(self):
       
    77         return "%s = %s : %s:%s" % (self.get_path(),self.path,self.name, self.version)
       
    78 
       
    79 class FeatureListResource(object):
       
    80     CONFML_EXTENSION = '.confml'
       
    81     CARBON_EXTENSION = '.featurelist'
       
    82     def __init__(self, **kwargs):
       
    83         self.path                   = kwargs.get('path', None)
       
    84         self.version_title          = kwargs.get('version_title', None)
       
    85         self.type                   = kwargs.get('type', None)
       
    86         self.list_id                = kwargs.get('list_id', None)
       
    87         self.expanded               = kwargs.get('expanded', None)
       
    88         self.list_version_id        = kwargs.get('list_version_id', None)
       
    89         self.version_identifier     = kwargs.get('version_identifier', None)
       
    90         self.is_latest_version      = kwargs.get('is_latest_version', None)
       
    91         self.can_be_released        = kwargs.get('can_be_released', None)
       
    92         self.has_external_relations = kwargs.get('has_external_relations', None)
       
    93 
       
    94     def get_path(self):
       
    95         path = utils.resourceref.remove_begin_slash(self.version_title)
       
    96         path = utils.resourceref.remove_end_slash(path)
       
    97         return path + self.CONFML_EXTENSION
       
    98 
       
    99     def get_carbon_path(self):
       
   100         path = utils.resourceref.remove_begin_slash(self.version_title)
       
   101         path = utils.resourceref.remove_end_slash(path)
       
   102         return path + self.CARBON_EXTENSION
       
   103 
       
   104     def __str__(self):
       
   105         return "%s = %s : %s" % (self.get_path(),self.path,self.version_title)
       
   106 
       
   107 class CarbonElement(object):
       
   108     pass
       
   109 
       
   110     def _get_mapper(self,modelname):
       
   111         """
       
   112         Return a instance of appropriate mapper for given model.
       
   113         """
       
   114         mapmodule = __import__('cone.carbon.mapping')
       
   115         return mapmodule.carbon.mapping.MAPPERS[modelname]()
       
   116 
       
   117 
       
   118 class CarbonConfiguration(CarbonElement, confmlmodel.ConfmlConfiguration):
       
   119     def __init__(self, ref='', **kwargs):
       
   120         super(CarbonConfiguration, self).__init__(ref, **kwargs)
       
   121         if self.meta == None:
       
   122             self.meta = {}
       
   123         
       
   124         self.name                       = kwargs.get('name') or utils.resourceref.remove_ext(utils.resourceref.psplit_ref(self.path)[-1])
       
   125         self.meta.add('type',kwargs.get('type', 'configurationroot'))
       
   126         self._version_identifier        = kwargs.get('version_identifier', None)
       
   127 
       
   128     @property
       
   129     def version_identifier(self):
       
   130         if self._version_identifier == None:
       
   131             dt = datetime.datetime.today()
       
   132             self._version_identifier = "%dwk%02d" % dt.isocalendar()[0:2]
       
   133         return self._version_identifier
       
   134 
       
   135     @property
       
   136     def type(self):
       
   137         if self.meta and self.meta.get('type'):
       
   138             return self.meta['type']
       
   139         else:
       
   140             return 'configurationroot'
       
   141     
       
   142 class FeatureList(CarbonConfiguration):
       
   143     def __init__(self, ref='', **kwargs):
       
   144         if not kwargs.get('path'):
       
   145             kwargs['path']          = str(kwargs.get('name', '')+'.confml')
       
   146         kwargs['type'] = 'featurelist'
       
   147         super(FeatureList, self).__init__(ref, **kwargs)
       
   148         self.name                = kwargs.get('name', '')
       
   149         self._version_identifier = kwargs.get('version_identifier', 'WORKING')
       
   150 
       
   151 class CarbonFeature(CarbonElement, confmlmodel.ConfmlSetting):
       
   152     def __init__(self, ref,**kwargs):
       
   153         ref = utils.resourceref.to_dottedref(ref)
       
   154         super(CarbonFeature,self).__init__(ref,**kwargs)
       
   155         
       
   156 
       
   157 class CarbonSetting(CarbonFeature, confmlmodel.ConfmlSetting):
       
   158     pass
       
   159 
       
   160 class CarbonIntSetting(CarbonFeature, confmlmodel.ConfmlIntSetting):
       
   161     pass
       
   162 
       
   163 class CarbonBooleanSetting(CarbonFeature, confmlmodel.ConfmlBooleanSetting):
       
   164     pass
       
   165 
       
   166 class CarbonSelectionSetting(CarbonFeature, confmlmodel.ConfmlSelectionSetting):
       
   167     pass
       
   168 
       
   169 class CarbonStringSetting(CarbonFeature, confmlmodel.ConfmlSetting):
       
   170     def __init__(self, ref,**kwargs):
       
   171         super(CarbonStringSetting,self).__init__(ref,**kwargs)
       
   172         self.type = 'string'
       
   173 
       
   174     pass
       
   175 
       
   176 def get_mapper(modelname):
       
   177     """
       
   178     Return a instance of appropriate mapper for given model.
       
   179     """
       
   180     mapmodule = __import__('cone.carbon.mapping')
       
   181     return mapmodule.carbon.mapping.MAPPERS[modelname]()
       
   182