configurationengine/source/cone/carbon/persistentjson.py
changeset 3 e7e0ae78773e
parent 0 2e8eeb919028
equal deleted inserted replaced
2:87cfa131b535 3:e7e0ae78773e
    20 import simplejson
    20 import simplejson
    21 
    21 
    22 """ cone specific imports """
    22 """ cone specific imports """
    23 from cone.public import persistence, exceptions, api, utils, container
    23 from cone.public import persistence, exceptions, api, utils, container
    24 from cone.carbon import model
    24 from cone.carbon import model
       
    25 from cone.carbon.resourcemapper import CarbonResourceMapper
    25 
    26 
    26 MODEL                    = model
    27 MODEL                    = model
    27 
    28 
    28 def dumps(obj, indent=True):
    29 def dumps(obj, indent=True):
    29     """ Make sure that the object is mapped to an object in this model """
    30     """ Make sure that the object is mapped to an object in this model """
    34     return dict
    35     return dict
    35 
    36 
    36 def loads(jsonstr):
    37 def loads(jsonstr):
    37     return CarbonReader().loads(jsonstr)
    38     return CarbonReader().loads(jsonstr)
    38 
    39 
    39 
       
    40 class CarbonResourceMapper(object):
       
    41     def __init__(self):
       
    42         self.CARBON_RESOURCE_TYPE_MAP = {'configurationroot' : self.map_carbon_configurationroot,
       
    43                              'configurationlayer' : self.map_carbon_configurationlayer,
       
    44                              'featurelist' : self.map_carbon_featurelist}
       
    45         self.CONFML_RESOURCE_TYPE_MAP = {'configurationroot' : self.map_confml_configurationroot,
       
    46                              'configurationlayer' : self.map_confml_configurationlayer,
       
    47                              'featurelist' : self.map_confml_featurelist}
       
    48 
       
    49     def map_carbon_resource(self, resourcepath):
       
    50         for resourceext in self.CARBON_RESOURCE_TYPE_MAP:
       
    51             if resourcepath.endswith(resourceext):
       
    52                 return self.CARBON_RESOURCE_TYPE_MAP[resourceext](resourcepath)
       
    53         return resourcepath
       
    54 
       
    55     def map_confml_resource(self, resourcetype, resourcepath):
       
    56         return self.CONFML_RESOURCE_TYPE_MAP[resourcetype](resourcepath)
       
    57 
       
    58     def map_carbon_configurationroot(self, resourcepath):
       
    59         return resourcepath.replace('.configurationroot', '.confml')
       
    60 
       
    61     def map_carbon_configurationlayer(self, resourcepath):
       
    62         return resourcepath.replace('.configurationlayer', '/root.confml')
       
    63 
       
    64     def map_carbon_featurelist(self, resourcepath):
       
    65         return "featurelists/%s" % resourcepath.replace('.featurelist', '.confml')
       
    66 
       
    67     def map_confml_configurationroot(self, resourcepath):
       
    68         return resourcepath.replace('.confml', '.configurationroot')
       
    69 
       
    70     def map_confml_configurationlayer(self, resourcepath):
       
    71         return resourcepath.replace('/root.confml', '.configurationlayer')
       
    72 
       
    73     def map_confml_featurelist(self, resourcepath):
       
    74         path = resourcepath.replace('featurelists/','').replace('.confml', '')
       
    75         version_identifier = 'WORKING'
       
    76         m = re.match('^(.*) \((.*)\)', path)
       
    77         # if the resourcepath does not have version information 
       
    78         # use default WORKING
       
    79         if m:
       
    80             path = m.group(1)
       
    81             version_identifier = m.group(2)
       
    82         return '%s (%s).featurelist' % (path, version_identifier)
       
    83 
    40 
    84 class ResourceListReader(persistence.ConeReader):
    41 class ResourceListReader(persistence.ConeReader):
    85     """
    42     """
    86     """ 
    43     """ 
    87     def loads(self, jsonstr):
    44     def loads(self, jsonstr):
   228     def dumps_layer(self, obj):
   185     def dumps_layer(self, obj):
   229         """
   186         """
   230         @param obj: The Configuration object 
   187         @param obj: The Configuration object 
   231         """
   188         """
   232         configuration_dict = {'version_identifier': obj.version_identifier}
   189         configuration_dict = {'version_identifier': obj.version_identifier}
   233 
   190         
   234         datawriter = DataWriter()
   191         datawriter = DataWriter()
   235         data = datawriter.dumps(obj)
   192         data = datawriter.dumps(obj)
   236         configuration_dict['data'] =  data
   193         configuration_dict['data'] =  data
   237         
   194         
   238         return configuration_dict
   195         return configuration_dict
   293     def loads(self, dict):
   250     def loads(self, dict):
   294         """
   251         """
   295         @param obj: The Configuration object 
   252         @param obj: The Configuration object 
   296         """
   253         """
   297         name = dict.get('configuration_name')
   254         name = dict.get('configuration_name')
   298         path = name + ".confml"
   255         path = name + "/root.confml"
   299         conf = model.CarbonConfiguration(dict.get('ref'), path=path, type='configurationlayer')
   256         conf = model.CarbonConfiguration(dict.get('ref'), path=path, type='configurationlayer')
   300         conf.name = name
   257         conf.name = name
   301         
   258         
   302         conf.version = dict.get('version_identifier')
   259         conf.version = dict.get('version_identifier')
   303         
   260         
   304         """ Last read the data of this configuration and add it as a configuration """
   261         """ Last read the data of this configuration and add it as a configuration """
   305         data_reader = DataReader()
   262         data_reader = DataReader()
   306         datacont = data_reader.loads(dict.get('data', {}))
   263         datacont = data_reader.loads(dict.get('data', {}))
   307         proxy = api.ConfigurationProxy(datacont.path)
   264         conf.add_configuration(datacont)
   308         conf.add_configuration(proxy)
       
   309         proxy._set_obj(datacont)
       
   310         
       
   311         return conf
   265         return conf
   312 
   266 
   313 class FeatureListCreateWriter(CarbonWriter):
   267 class FeatureListCreateWriter(CarbonWriter):
   314     @classmethod
   268     @classmethod
   315     def supported_class(cls, classname):
   269     def supported_class(cls, classname):
   453                        'description' : mobj.desc or 'Needs description',
   407                        'description' : mobj.desc or 'Needs description',
   454                        'responsible' : None,
   408                        'responsible' : None,
   455                        'value_type' : self.CONFML_TO_CARBON_TYPE[mobj.type],
   409                        'value_type' : self.CONFML_TO_CARBON_TYPE[mobj.type],
   456                        'children' : []}
   410                        'children' : []}
   457         if featuredict['value_type'] != None:
   411         if featuredict['value_type'] != None:
   458             featuredict['type_object'] = 'carbon_feature_type_normal'
   412             featuredict['type_object'] = 's60_feature'
   459         if mobj.type == 'selection':
   413         if mobj.type == 'selection':
   460             featuredict['options'] = mobj.options.keys() 
   414             featuredict['options'] = mobj.options.keys() 
   461 
   415 
   462         writer = FeatureWriter()
   416         writer = FeatureWriter()
   463         for fearef in mobj.list_features():
   417         for fearef in mobj.list_features():
   502             fea = model.CarbonIntSetting(ref, type=value_type)
   456             fea = model.CarbonIntSetting(ref, type=value_type)
   503         elif value_type == 'string':
   457         elif value_type == 'string':
   504             fea = model.CarbonStringSetting(ref, type=value_type)
   458             fea = model.CarbonStringSetting(ref, type=value_type)
   505         elif value_type == 'selection':
   459         elif value_type == 'selection':
   506             fea = model.CarbonSelectionSetting(ref, type=value_type)
   460             fea = model.CarbonSelectionSetting(ref, type=value_type)
   507             for option in dict.get('options'):
   461             for option_name in dict.get('options'):
   508                 fea.add_option(option,option)
   462                 fea.create_option(option_name, option_name)
   509         elif value_type == '':
   463         elif value_type == '':
   510             fea = model.CarbonFeature(ref, type=value_type)
   464             fea = model.CarbonFeature(ref, type=value_type)
   511         else:
   465         else:
   512             fea = model.CarbonFeature(ref)
   466             fea = model.CarbonFeature(ref)
   513 
   467