configurationengine/source/cone/confml/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 
       
    18 """
       
    19 Base class for Confml elements.
       
    20 Attributes:
       
    21  All Confml element attributes become attributes of this instance.
       
    22 """
       
    23 import types
       
    24 from cone.public import api, exceptions, container, utils
       
    25 
       
    26 class ConfmlElement(api.Base):
       
    27     def _get_mapper(self,modelname):
       
    28         """
       
    29         Return a instance of appropriate mapper for given model.
       
    30         """
       
    31         mapmodule = __import__('cone.confml.mapping')
       
    32         return mapmodule.confml.mapping.MAPPERS[modelname]()
       
    33 
       
    34     def get_desc(self): 
       
    35         try:
       
    36             desc = getattr(self,ConfmlDescription.refname)
       
    37             return desc.text
       
    38         except AttributeError:
       
    39             return None
       
    40 
       
    41     def set_desc(self,value): 
       
    42         self._add(ConfmlDescription(value))
       
    43 
       
    44     def del_desc(self): 
       
    45         try:
       
    46             self._remove(ConfmlDescription.refname)
       
    47         except exceptions.NotFound:
       
    48             pass
       
    49     """ The description as a property """
       
    50     desc = property(get_desc,set_desc,del_desc)
       
    51 class ConfmlConfiguration(ConfmlElement, api.Configuration):
       
    52     """
       
    53     Confml configuration class. 
       
    54     """
       
    55     def __init__(self,ref="", **kwargs):
       
    56         super(ConfmlConfiguration,self).__init__(ref, **kwargs)
       
    57         if kwargs.get('meta'):
       
    58             self.meta = kwargs.get('meta')
       
    59         if kwargs.get('desc'):
       
    60             self.desc = kwargs.get('desc')
       
    61 
       
    62 
       
    63     def get_desc(self): 
       
    64         """
       
    65         @return: The description of the Configuration.
       
    66         """
       
    67         try:
       
    68             desc = getattr(self,ConfmlDescription.refname)
       
    69             return desc.text
       
    70         except AttributeError:
       
    71             return None
       
    72 
       
    73     def set_desc(self,value): 
       
    74         self._add(ConfmlDescription(value))
       
    75 
       
    76     def del_desc(self): 
       
    77         try:
       
    78             self._remove(ConfmlDescription.refname)
       
    79         except exceptions.NotFound:
       
    80             pass
       
    81 
       
    82     """ The description as a property """
       
    83     desc = property(get_desc,set_desc,del_desc)
       
    84 
       
    85     def get_meta(self): 
       
    86         """
       
    87         @return: The description of the Configuration.
       
    88         """
       
    89         try:
       
    90             meta = getattr(self,ConfmlMeta.refname)
       
    91             return meta
       
    92         except AttributeError:
       
    93             return None
       
    94 
       
    95     def set_meta(self,value): 
       
    96         self._add(ConfmlMeta(value))
       
    97 
       
    98     def del_meta(self): 
       
    99         try:
       
   100             self._remove(ConfmlMeta.refname)
       
   101         except exceptions.NotFound:
       
   102             pass
       
   103 
       
   104     """ The meta element as a property """
       
   105     meta = property(get_meta,set_meta,del_meta)
       
   106 
       
   107 
       
   108 class ConfmlGroup(ConfmlElement, api.Group):
       
   109     """
       
   110     Confml view.
       
   111     """
       
   112     def __init__(self, ref="", **kwargs):
       
   113         super(ConfmlGroup,self).__init__(ref,**kwargs)
       
   114         if kwargs.get('icon'):
       
   115             self.icon = kwargs.get('icon')
       
   116         if kwargs.get('desc'):
       
   117             self.desc = kwargs.get('desc')
       
   118 
       
   119     def get_icon(self): 
       
   120         try:
       
   121             icon = getattr(self,ConfmlIcon.refname)
       
   122             return icon.href
       
   123         except AttributeError:
       
   124             return None
       
   125     def set_icon(self,value): self._add(ConfmlIcon(value))
       
   126     def del_icon(self): 
       
   127         try:
       
   128             self._remove(ConfmlIcon.refname)
       
   129         except exceptions.NotFound:
       
   130             pass
       
   131     """ The icon as a property """
       
   132     icon = property(get_icon,set_icon,del_icon)
       
   133 
       
   134     def get_desc(self): 
       
   135         try:
       
   136             desc = getattr(self,ConfmlDescription.refname)
       
   137             return desc.text
       
   138         except AttributeError:
       
   139             return None
       
   140     def set_desc(self,value): self._add(ConfmlDescription(value))
       
   141     def del_desc(self): 
       
   142         try:
       
   143             self._remove(ConfmlDescription.refname)
       
   144         except exceptions.NotFound:
       
   145             pass
       
   146     """ The description as a property """
       
   147     desc = property(get_desc,set_desc,del_desc)
       
   148 
       
   149 
       
   150 class ConfmlView(api.View):
       
   151     """
       
   152     Confml view.
       
   153     """
       
   154     def __init__(self, ref="", **kwargs):
       
   155         super(ConfmlView,self).__init__(ref,**kwargs)
       
   156         if kwargs.get('desc'):
       
   157             self.desc = kwargs.get('desc')
       
   158 
       
   159 
       
   160     def get_desc(self): 
       
   161         try:
       
   162             desc = getattr(self,ConfmlDescription.refname)
       
   163             return desc.text
       
   164         except AttributeError:
       
   165             return None
       
   166     def set_desc(self,value): self._add(ConfmlDescription(value))
       
   167     def del_desc(self): 
       
   168         try:
       
   169             self._remove(ConfmlDescription.refname)
       
   170         except exceptions.NotFound:
       
   171             pass
       
   172     """ The description as a property """
       
   173     desc = property(get_desc,set_desc,del_desc)
       
   174 
       
   175 class ConfmlFeature(ConfmlElement, api.Feature):
       
   176     pass
       
   177 
       
   178 class ConfmlSetting(ConfmlElement, api.Feature):
       
   179     """
       
   180     Confml setting class. Attribute 'options' contains options of this setting.
       
   181     """
       
   182     supported_types = ['int',
       
   183                        'string',
       
   184                        'boolean',
       
   185                        'selection']
       
   186     def __init__(self, ref,**kwargs):
       
   187         super(ConfmlSetting,self).__init__(ref,**kwargs)
       
   188         self.type = kwargs.get('type',None)
       
   189         if kwargs.get('desc'):
       
   190             self.desc = kwargs.get('desc')
       
   191         if kwargs.get('minOccurs'):
       
   192             self.minOccurs = kwargs.get('minOccurs')
       
   193         if kwargs.get('maxOccurs'):
       
   194             self.maxOccurs = kwargs.get('maxOccurs')
       
   195         if kwargs.get('maxLength'):
       
   196             self.maxLength = kwargs.get('maxLength')
       
   197         if kwargs.get('minLength'):
       
   198             self.minLength = kwargs.get('minLength')
       
   199         if kwargs.get('mapKey'):
       
   200             self.mapKey = kwargs.get('mapKey')
       
   201         if kwargs.get('mapValue'):
       
   202             self.mapValue = kwargs.get('mapValue')
       
   203         
       
   204         self.readOnly = kwargs.get('readOnly',None)
       
   205         self.constraint = kwargs.get('constraint',None)
       
   206         self.required = kwargs.get('required',None)
       
   207         self.relevant = kwargs.get('relevant',None)
       
   208 
       
   209     def get_valueset(self):
       
   210         """
       
   211         Get the ValueSet object for this feature, that has the list of available values.
       
   212         """
       
   213         return api.ValueRe('.*')
       
   214 
       
   215     def add_property(self, **kwargs):
       
   216         """
       
   217         @param name=str: property name 
       
   218         @param value=str: property value
       
   219         @param unit=str: property unit, e.g. kB
       
   220         """
       
   221         self._add(ConfmlProperty(**kwargs), container.APPEND)
       
   222 
       
   223     def get_property(self, name):
       
   224         """
       
   225         @param name: The name of the property
       
   226         """
       
   227         for property in utils.get_list(self._get(ConfmlProperty.refname)):
       
   228             if property.name == name:
       
   229                 return property
       
   230         raise exceptions.NotFound("ConfmlProperty with name %s not found!" % name)
       
   231 
       
   232     def remove_property(self, name):
       
   233         """
       
   234         remove a given option from this feature by name. 
       
   235         @param name: 
       
   236         """
       
   237         for property in self._get(ConfmlProperty.refname):
       
   238             if property.name == name:
       
   239                 return self._remove(property.get_fullref())
       
   240         raise exceptions.NotFound("ConfmlProperty with name %s not found!" % name)
       
   241 
       
   242     def list_properties(self):
       
   243         """
       
   244         Return a array of all Feature children references under this object.
       
   245         """
       
   246         return [obj.name for obj in utils.get_list(self._get(ConfmlProperty.refname))]
       
   247 
       
   248     def get_maxlength(self): 
       
   249         try:
       
   250             return getattr(self,ConfmlMaxLength.refname).value
       
   251         except AttributeError:
       
   252             return None
       
   253 
       
   254     def set_maxlength(self,value): 
       
   255         self._add(ConfmlMaxLength(value))
       
   256 
       
   257     def del_maxlength(self): 
       
   258         try:
       
   259             self._remove(ConfmlMaxLength.refname)
       
   260         except exceptions.NotFound:
       
   261             pass
       
   262     """ The description as a property """
       
   263     maxLength = property(get_maxlength,set_maxlength,del_maxlength)
       
   264 
       
   265     def get_minlength(self): 
       
   266         try:
       
   267             return getattr(self,ConfmlMinLength.refname).value
       
   268         except AttributeError:
       
   269             return None
       
   270 
       
   271     def set_minlength(self,value): 
       
   272         self._add(ConfmlMinLength(value))
       
   273 
       
   274     def del_minlength(self): 
       
   275         try:
       
   276             self._remove(ConfmlMinLength.refname)
       
   277         except exceptions.NotFound:
       
   278             pass
       
   279     """ The description as a property """
       
   280     minLength = property(get_minlength,set_minlength,del_minlength)
       
   281 
       
   282     def get_minInclusive(self): 
       
   283         try:
       
   284             return getattr(self,ConfmlMinInclusive.refname).value
       
   285         except AttributeError:
       
   286             return None
       
   287 
       
   288     def set_minInclusive(self,value): 
       
   289         self._add(ConfmlMinInclusive(value))
       
   290 
       
   291     def del_minInclusive(self): 
       
   292         try:
       
   293             self._remove(ConfmlMinInclusive.refname)
       
   294         except exceptions.NotFound:
       
   295             pass     
       
   296     """ The minInclusive as a property """
       
   297     minInclusive = property(get_minInclusive,set_minInclusive,del_minInclusive)
       
   298 
       
   299     def get_maxInclusive(self): 
       
   300         try:
       
   301             return getattr(self,ConfmlMaxInclusive.refname).value
       
   302         except AttributeError:
       
   303             return None
       
   304 
       
   305     def set_maxInclusive(self,value): 
       
   306         self._add(ConfmlMaxInclusive(value))
       
   307 
       
   308     def del_maxInclusive(self): 
       
   309         try:
       
   310             self._remove(ConfmlMaxInclusive.refname)
       
   311         except exceptions.NotFound:
       
   312             pass     
       
   313     """ The minInclusive as a property """
       
   314     maxInclusive = property(get_maxInclusive,set_maxInclusive,del_maxInclusive)
       
   315 
       
   316     def get_minExclusive(self): 
       
   317         try:
       
   318             return getattr(self,ConfmlMinExclusive.refname).value
       
   319         except AttributeError:
       
   320             return None
       
   321 
       
   322     def set_minExclusive(self,value): 
       
   323         self._add(ConfmlMinExclusive(value))
       
   324 
       
   325     def del_minExclusive(self): 
       
   326         try:
       
   327             self._remove(ConfmlMinExclusive.refname)
       
   328         except exceptions.NotFound:
       
   329             pass     
       
   330     """ The minExclusive as a property """
       
   331     minExclusive = property(get_minExclusive,set_minExclusive,del_minExclusive)
       
   332 
       
   333     def get_maxExclusive(self): 
       
   334         try:
       
   335             return getattr(self,ConfmlMaxExclusive.refname).value
       
   336         except AttributeError:
       
   337             return None
       
   338 
       
   339     def set_maxExclusive(self,value): 
       
   340         self._add(ConfmlMaxExclusive(value))
       
   341 
       
   342     def del_maxExclusive(self): 
       
   343         try:
       
   344             self._remove(ConfmlMaxExclusive.refname)
       
   345         except exceptions.NotFound:
       
   346             pass     
       
   347     """ The maxExclusive as a property """
       
   348     maxExclusive = property(get_maxExclusive,set_maxExclusive,del_maxExclusive)
       
   349 
       
   350     def get_pattern(self): 
       
   351         try:
       
   352             return getattr(self,ConfmlPattern.refname).value
       
   353         except AttributeError:
       
   354             return None
       
   355 
       
   356     def set_pattern(self,value): 
       
   357         self._add(ConfmlPattern(value))
       
   358 
       
   359     def del_pattern(self): 
       
   360         try:
       
   361             self._remove(ConfmlPattern.refname)
       
   362         except exceptions.NotFound:
       
   363             pass     
       
   364     """ The pattern as a property """
       
   365     pattern = property(get_pattern,set_pattern,del_pattern)
       
   366 
       
   367     def get_totalDigits(self): 
       
   368         try:
       
   369             return getattr(self,ConfmlTotalDigits.refname).value
       
   370         except AttributeError:
       
   371             return None
       
   372 
       
   373     def set_totalDigits(self,value): 
       
   374         self._add(ConfmlTotalDigits(value))
       
   375 
       
   376     def del_totalDigits(self): 
       
   377         try:
       
   378             self._remove(ConfmlTotalDigits.refname)
       
   379         except exceptions.NotFound:
       
   380             pass     
       
   381     """ The totalDigits as a property """
       
   382     totalDigits = property(get_totalDigits,set_totalDigits,del_totalDigits)
       
   383 
       
   384     @property
       
   385     def options(self):
       
   386         optdict = {}
       
   387         for opt in self._objects(type=api.Option):
       
   388             optdict[opt.value] = opt
       
   389         return  optdict
       
   390 
       
   391     @property
       
   392     def properties(self):
       
   393         dict = {}
       
   394         for property in utils.get_list(self._get(ConfmlProperty.refname)):
       
   395             dict[property.name] = property
       
   396         return  dict
       
   397 
       
   398     def get_rfs(self,):
       
   399         return super(ConfmlSetting,self).get_value('rfs')
       
   400 
       
   401     def set_rfs(self, value):
       
   402         super(ConfmlSetting,self).set_value('rfs',value)
       
   403 
       
   404     def del_rfs(self):
       
   405         super(ConfmlSetting,self).del_value('rfs')
       
   406 
       
   407     rfs = property(get_rfs,set_rfs,del_rfs)
       
   408 
       
   409     def get_value_cast(self, value, attr=None):
       
   410         """
       
   411         A function to perform the value type casting in get operation  
       
   412         @param value: the value to cast 
       
   413         @param attr: the attribute which is fetched from model (normally in confml either None='data' or 'rfs')
       
   414         """
       
   415         if not attr or attr == 'data':
       
   416             return self.get_data_cast(value)
       
   417         elif attr == 'rfs':
       
   418             return self.get_rfs_cast(value)
       
   419         else:
       
   420             return value
       
   421     
       
   422     def set_value_cast(self, value, attr=None):
       
   423         """
       
   424         A function to perform the value type casting in the set operation  
       
   425         @param value: the value to cast 
       
   426         @param attr: the attribute which is fetched from model (normally in confml either None='data' or 'rfs')
       
   427         """
       
   428         if not attr or attr == 'data':
       
   429             return self.set_data_cast(value)
       
   430         elif attr == 'rfs':
       
   431             return self.set_rfs_cast(value)
       
   432         else:
       
   433             return value
       
   434 
       
   435     def get_data_cast(self, value):
       
   436         """
       
   437         A function to perform the data type casting in get operation  
       
   438         @param value: the value to cast 
       
   439         """
       
   440         return value
       
   441     
       
   442     def set_data_cast(self, value):
       
   443         """
       
   444         A function to perform the data type casting in the set operation  
       
   445         @param value: the value to cast 
       
   446         """
       
   447         return value
       
   448 
       
   449     def get_rfs_cast(self, value):
       
   450         """
       
   451         A function to perform the rfs type casting in get operation  
       
   452         @param value: the value to cast 
       
   453         """
       
   454         if value == 'true':
       
   455             return True
       
   456         elif value == 'false':
       
   457             return False
       
   458         else: # otherwise this is an invalid rfs value. Should it report an error?
       
   459             return value
       
   460     
       
   461     def set_rfs_cast(self, value):
       
   462         """
       
   463         A function to perform the rfs type casting in the set operation  
       
   464         @param value: the value to cast 
       
   465         """
       
   466         if value:
       
   467             return 'true'
       
   468         else: 
       
   469             return 'false'
       
   470 
       
   471 
       
   472 class ConfmlStringSetting(ConfmlSetting):
       
   473     """
       
   474     Confml setting class for integer type.
       
   475     """
       
   476     def __init__(self, ref,**kwargs):
       
   477         kwargs['type'] = 'string'
       
   478         ConfmlSetting.__init__(self,ref,**kwargs)
       
   479 
       
   480 
       
   481 class ConfmlIntSetting(ConfmlSetting):
       
   482     """
       
   483     Confml setting class for integer type.
       
   484     """
       
   485     def __init__(self, ref,**kwargs):
       
   486         kwargs['type'] = 'int'
       
   487         ConfmlSetting.__init__(self,ref,**kwargs)
       
   488 
       
   489     def get_valueset(self):
       
   490         """
       
   491         Get the ValueSet object for this feature, that has the list of available values.
       
   492         """
       
   493         return api.ValueRange(0,sys.maxint)
       
   494 
       
   495     def get_data_cast(self, value):
       
   496         """
       
   497         A function to perform the value type casting in get operation  
       
   498         """
       
   499         if value:
       
   500             try:
       
   501                 return int(value)
       
   502             except ValueError:
       
   503                 return int(value, 16)
       
   504         else:
       
   505             return value
       
   506     
       
   507     def set_data_cast(self, value):
       
   508         """
       
   509         A function to perform the value type casting in the set operation  
       
   510         """
       
   511         return str(int(value))
       
   512 
       
   513 
       
   514 class ConfmlRealSetting(ConfmlSetting):
       
   515     """
       
   516     Confml setting class for real type.
       
   517     """
       
   518     def __init__(self, ref,**kwargs):
       
   519         kwargs['type'] = 'real'
       
   520         ConfmlSetting.__init__(self,ref,**kwargs)
       
   521 
       
   522     def get_valueset(self):
       
   523         """
       
   524         Get the ValueSet object for this feature, that has the list of available values.
       
   525         """
       
   526         return api.ValueRange(0,float(sys.maxint))
       
   527 
       
   528     def get_data_cast(self, value):
       
   529         """
       
   530         A function to perform the value type casting in get operation  
       
   531         """
       
   532         if value:
       
   533             return float(value)
       
   534         else:
       
   535             return value
       
   536     
       
   537     def set_data_cast(self, value):
       
   538         """
       
   539         A function to perform the value type casting in the set operation  
       
   540         """
       
   541         return str(float(value))
       
   542 
       
   543 
       
   544 
       
   545 
       
   546 class ConfmlBooleanSetting(ConfmlSetting):
       
   547     """
       
   548     Confml setting class for boolean type.
       
   549     """
       
   550     def __init__(self, ref,**kwargs):
       
   551         kwargs['type'] = 'boolean'
       
   552         ConfmlSetting.__init__(self,ref,**kwargs)
       
   553 
       
   554     def get_valueset(self):
       
   555         """
       
   556         Get the ValueSet object for this feature, that has the list of available values.
       
   557         """
       
   558         return api.ValueSet([True,False])
       
   559 
       
   560     def get_data_cast(self, value):
       
   561         """
       
   562         A function to perform the value type casting in get operation  
       
   563         """
       
   564         if value:
       
   565             if value in ('true', '1'):
       
   566                 return True
       
   567             else:
       
   568                 return False
       
   569         else:
       
   570             return value
       
   571     
       
   572     def set_data_cast(self, value):
       
   573         """
       
   574         A function to perform the value type casting in the set operation  
       
   575         """
       
   576         if isinstance(value, basestring):
       
   577             if value in ('false', '0'):
       
   578                 return 'false'
       
   579             elif value in ('true', '1'):
       
   580                 return 'true'
       
   581         
       
   582         return str(bool(value)).lower()
       
   583 
       
   584 
       
   585 class ConfmlSelectionSetting(ConfmlSetting):
       
   586     """
       
   587     Confml setting class for boolean type.
       
   588     """
       
   589     def __init__(self, ref,**kwargs):
       
   590         kwargs['type'] = 'selection'
       
   591         ConfmlSetting.__init__(self,ref,**kwargs)
       
   592     
       
   593     def get_valueset(self):
       
   594         """
       
   595         Get the ValueSet object for this feature, that has the list of available values.
       
   596         """
       
   597         return api.Feature.get_valueset(self)
       
   598 
       
   599 class ConfmlMultiSelectionSetting(ConfmlSetting):
       
   600     """
       
   601     Confml setting class for multiSelection type.
       
   602     """
       
   603 
       
   604     def __init__(self, ref,**kwargs):
       
   605         kwargs['type'] = 'multiSelection'
       
   606         ConfmlSetting.__init__(self,ref,**kwargs)
       
   607         
       
   608 
       
   609     def get_valueset(self):
       
   610         """
       
   611         Get the ValueSet object for this feature, that has the list of available values.
       
   612         """
       
   613         return api.Feature.get_valueset(self)
       
   614 
       
   615     def get_data_cast(self, value):
       
   616         """
       
   617         A function to perform the value type casting in get operation  
       
   618         """
       
   619         try:
       
   620             if not isinstance(value, types.ListType):
       
   621                 values = value.split('" "')
       
   622                 for i in range(len(values)):
       
   623                     if values[i].startswith('"'):
       
   624                         values[i] = values[i][1:] 
       
   625                     if values[i].endswith('"'):
       
   626                         values[i] = values[i][:-1]
       
   627                 return values
       
   628             return value
       
   629         except AttributeError:
       
   630             return None
       
   631     
       
   632     def set_data_cast(self, value):
       
   633         """
       
   634         A function to perform the value type casting in the set operation  
       
   635         """
       
   636         
       
   637         if isinstance(value, list):
       
   638             value = " ".join(['"%s"' % elem for elem in value])
       
   639         return value
       
   640     
       
   641     def set_value(self, value):
       
   642         if not isinstance(value, types.ListType):
       
   643             raise ValueError("Only list types are allowed.")
       
   644         self.value = value
       
   645 
       
   646 class ConfmlDateSetting(ConfmlSetting):
       
   647     """
       
   648     Confml setting class for date type.
       
   649     """
       
   650     def __init__(self, ref,**kwargs):
       
   651         kwargs['type'] = 'date'
       
   652         ConfmlSetting.__init__(self,ref,**kwargs)
       
   653 
       
   654 class ConfmlTimeSetting(ConfmlSetting):
       
   655     """
       
   656     Confml setting class for time type.
       
   657     """
       
   658     def __init__(self, ref,**kwargs):
       
   659         kwargs['type'] = 'time'
       
   660         ConfmlSetting.__init__(self,ref,**kwargs)
       
   661 
       
   662 class ConfmlDateTimeSetting(ConfmlSetting):
       
   663     """
       
   664     Confml setting class for date-time type.
       
   665     """
       
   666     def __init__(self, ref,**kwargs):
       
   667         kwargs['type'] = 'dateTime'
       
   668         ConfmlSetting.__init__(self,ref,**kwargs)
       
   669 
       
   670 class ConfmlDurationSetting(ConfmlSetting):
       
   671     """
       
   672     Confml setting class for date type.
       
   673     """
       
   674     def __init__(self, ref,**kwargs):
       
   675         kwargs['type'] = 'duration'
       
   676         ConfmlSetting.__init__(self,ref,**kwargs)
       
   677 
       
   678 class ConfmlSequenceSetting(api.FeatureSequence,ConfmlSetting):
       
   679     """
       
   680     Confml setting class. Attribute 'options' contains options of this setting.
       
   681     """
       
   682     def __init__(self, ref,**kwargs):
       
   683         ConfmlSetting.__init__(self,ref,**kwargs)
       
   684         api.FeatureSequence.__init__(self,ref,**kwargs)
       
   685 
       
   686 class ConfmlFileSetting(ConfmlSetting):
       
   687     """
       
   688     Confml file setting class.
       
   689     """
       
   690     def __init__(self, ref,**kwargs):
       
   691         kwargs['type'] = 'file'
       
   692         ConfmlSetting.__init__(self, ref, **kwargs)
       
   693         """
       
   694         The file element always includes localPath and targetPath child elements.
       
   695         """
       
   696         self.add_feature(ConfmlLocalPath())
       
   697         self.add_feature(ConfmlTargetPath())
       
   698 
       
   699 class ConfmlFolderSetting(ConfmlSetting):
       
   700     """
       
   701     Confml folder setting class.
       
   702     """
       
   703     def __init__(self, ref,**kwargs):
       
   704         kwargs['type'] = 'folder'
       
   705         ConfmlSetting.__init__(self, ref, **kwargs)
       
   706         """
       
   707         The folder element always includes localPath and targetPath child elements.
       
   708         """
       
   709         self.add_feature(ConfmlLocalPath())
       
   710         self.add_feature(ConfmlTargetPath())
       
   711 
       
   712 class ConfmlLocalPath(ConfmlElement, api.Feature):
       
   713     """
       
   714     Confml file class. Attribute setting.
       
   715     """
       
   716     def __init__(self, ref='localPath', **kwargs):
       
   717         kwargs['type'] = 'string'
       
   718         ConfmlElement.__init__(self, **kwargs)
       
   719         api.Feature.__init__(self, ref, **kwargs)
       
   720         self.readOnly = kwargs.get('readOnly', None)
       
   721 
       
   722 
       
   723 class ConfmlTargetPath(ConfmlElement, api.Feature):
       
   724     """
       
   725     Confml file class. Attribute setting.
       
   726     """
       
   727     def __init__(self, ref='targetPath', **kwargs):
       
   728         kwargs['type'] = 'string'
       
   729         ConfmlElement.__init__(self, **kwargs)
       
   730         api.Feature.__init__(self, ref, **kwargs)
       
   731         self.readOnly = kwargs.get('readOnly', None)
       
   732 
       
   733 
       
   734 class ConfmlMeta(api.Base):
       
   735     """
       
   736     Confml meta element
       
   737     """
       
   738     refname = "_meta"
       
   739     def __init__(self, array=None, **kwargs):
       
   740         super(ConfmlMeta,self).__init__(self.refname)
       
   741         self.array  = []
       
   742         if array:            
       
   743             self.array += array
       
   744 
       
   745     def __getitem__(self, key):
       
   746         return self.array[key]
       
   747  
       
   748     def __delitem__(self, key):
       
   749         del self.array[key]
       
   750 
       
   751     def __setitem__(self, key, value):
       
   752         self.array[key] = value
       
   753 
       
   754     def __str__(self):
       
   755         tempstr = "ConfmlMeta object\n"
       
   756         counter = 0
       
   757         for item in self.array:
       
   758             tempstr += "\t%d: %s\n" % (counter, item.__str__())
       
   759             counter += 1
       
   760         return tempstr 
       
   761 
       
   762     def __cmp__(self, other):
       
   763         try:
       
   764             for item in self.array:
       
   765                 if item != other.array[self.array.index(item)]:
       
   766                     return 1
       
   767         except:
       
   768             return 1
       
   769         return 0
       
   770 
       
   771     def append(self, value):
       
   772         self.array.append(value)
       
   773 
       
   774     def add(self, tag, value, ns=None, attributes=None):
       
   775         self.array.append(ConfmlMetaProperty(tag, value, ns, attrs=attributes))
       
   776 
       
   777     def get(self, tag, default=None):
       
   778         """
       
   779         Try to find the element by its tag in the meta elem array.
       
   780         @param tag: the tag that is searched,
       
   781         @param default: return the default value if the element is not found. 
       
   782         @return: the value of the ConfmlMetaProperty object if it is found. Default value 
       
   783         if element with tag is not found.
       
   784         """
       
   785         for item in self.array:
       
   786             if item.tag == tag:
       
   787                 return item.value
       
   788         return default
       
   789 
       
   790     def replace(self, index, tag, value, ns=None, dict=None):
       
   791         self.array[index] = ConfmlMetaProperty(tag, value, ns, attrs=dict)
       
   792 
       
   793     def clear(self, value):
       
   794         self.array = []
       
   795 
       
   796     def clone(self):
       
   797         newMeta = ConfmlMeta()
       
   798         for item in self.array:
       
   799             newProp = ConfmlMetaProperty(item.tag, item.value, item.ns, attrs = item.attrs)
       
   800             newMeta.append(newProp)
       
   801         return newMeta
       
   802 
       
   803     def find_by_tag(self, value):
       
   804         for item in self.array:
       
   805             if item.tag == value:
       
   806                 return self.array.index(item)
       
   807         return -1
       
   808 
       
   809     def find_by_attribute(self, name, value):
       
   810         for item in self.array:
       
   811             if item.attrs.has_key(name) and item.attrs[name] == value: 
       
   812                 return self.array.index(item)
       
   813         return -1
       
   814 
       
   815     def get_property_by_tag(self, tag):
       
   816         """
       
   817         Try to find the element by its tag in the meta elem array.
       
   818         @param tag: the tag that is searched
       
   819         @return: the ConfmlMetaProperty object if it is found. None if element with tag is not found.
       
   820         """
       
   821         for item in self.array:
       
   822             if item.tag == tag:
       
   823                 return item
       
   824         return None
       
   825 
       
   826 
       
   827 class ConfmlDescription(api.Base):
       
   828     """
       
   829     Confml description element
       
   830     """
       
   831     refname = "_desc"
       
   832     def __init__(self, text=None, **kwargs):
       
   833         super(ConfmlDescription,self).__init__(self.refname)
       
   834         self.text = text or ''
       
   835 
       
   836 
       
   837 class ConfmlIcon(api.Base):
       
   838     """
       
   839     Confml icon element
       
   840     """
       
   841     refname = "_icon"
       
   842     def __init__(self, href='', **kwargs):
       
   843         super(ConfmlIcon,self).__init__(self.refname)
       
   844         self.href = href
       
   845 
       
   846 
       
   847 class ConfmlProperty(api.Base):
       
   848     """
       
   849     Confml meta element
       
   850     """
       
   851     refname = "_property"
       
   852     def __init__(self, **kwargs):
       
   853         """
       
   854         @param name=str: name string 
       
   855         @param value=str: value for the property, string 
       
   856         @param unit=str: unit of the property
       
   857         """
       
   858         super(ConfmlProperty,self).__init__(self.refname)
       
   859         self.name = kwargs.get('name',None)
       
   860         self.value = kwargs.get('value',None)
       
   861         self.unit = kwargs.get('unit',None)
       
   862 
       
   863 
       
   864 class ConfmlMetaProperty(api.Base):
       
   865     """
       
   866     Confml meta property element
       
   867     """
       
   868     refname = "_metaproperty"
       
   869     def __init__(self, tag, value = None, ns = None, **kwargs):
       
   870         """
       
   871         """
       
   872         super(ConfmlMetaProperty,self).__init__(self.refname)
       
   873         self.tag = tag
       
   874         self.value = value
       
   875         self.ns = ns
       
   876         if kwargs.has_key("attrs") and kwargs["attrs"] != None:
       
   877             self.attrs = dict(kwargs["attrs"])
       
   878         else:
       
   879             self.attrs = {}
       
   880 
       
   881     def __cmp__(self, other):
       
   882         try:
       
   883             if self.tag != other.tag or self.value != other.value\
       
   884                 or self.ns != other.ns or self.attrs != other.attrs:
       
   885                 return 1
       
   886         except:
       
   887             return 1
       
   888         return 0
       
   889 
       
   890     def __str__(self):
       
   891         return "Tag: %s Value: %s Namespace: %s Attributes: % s" % (self.tag, self.value, self.ns, repr(self.attrs))         
       
   892         
       
   893             
       
   894 
       
   895 class ConfmlLength(api.Base):
       
   896     """
       
   897     Confml length element
       
   898     """
       
   899     refname = "_length"
       
   900     def __init__(self, value, **kwargs):
       
   901         super(ConfmlLength,self).__init__(self.refname)
       
   902         self.value = value
       
   903 
       
   904 class ConfmlMaxLength(api.Base):
       
   905     """
       
   906     Confml max element
       
   907     """
       
   908     refname = "_maxLength"
       
   909     def __init__(self, value, **kwargs):
       
   910         super(ConfmlMaxLength,self).__init__(self.refname)
       
   911         self.value = value
       
   912 
       
   913 class ConfmlMinLength(api.Base):
       
   914     """
       
   915     Confml min element
       
   916     """
       
   917     refname = "_minLength"
       
   918     def __init__(self, value, **kwargs):
       
   919         super(ConfmlMinLength,self).__init__(self.refname)
       
   920         self.value = value
       
   921 
       
   922 class ConfmlMinInclusive(api.Base):
       
   923     """
       
   924     Confml minInclusive element
       
   925     """
       
   926     refname = "_minInclusive"
       
   927     def __init__(self, value, **kwargs):
       
   928         super(ConfmlMinInclusive,self).__init__(self.refname)
       
   929         self.value = value
       
   930 
       
   931 class ConfmlMaxInclusive(api.Base):
       
   932     """
       
   933     Confml minInclusive element
       
   934     """
       
   935     refname = "_maxInclusive"
       
   936     def __init__(self, value, **kwargs):
       
   937         super(ConfmlMaxInclusive,self).__init__(self.refname)
       
   938         self.value = value
       
   939 
       
   940 class ConfmlMinExclusive(api.Base):
       
   941     """
       
   942     Confml minExclusive element
       
   943     """
       
   944     refname = "_minExclusive"
       
   945     def __init__(self, value, **kwargs):
       
   946         super(ConfmlMinExclusive,self).__init__(self.refname)
       
   947         self.value = value
       
   948 
       
   949 class ConfmlMaxExclusive(api.Base):
       
   950     """
       
   951     Confml maxExclusive element
       
   952     """
       
   953     refname = "_maxExclusive"
       
   954     def __init__(self, value, **kwargs):
       
   955         super(ConfmlMaxExclusive,self).__init__(self.refname)
       
   956         self.value = value
       
   957 
       
   958 class ConfmlPattern(api.Base):
       
   959     """
       
   960     Confml pattern element
       
   961     """
       
   962     refname = "_pattern"
       
   963     def __init__(self, value, **kwargs):
       
   964         super(ConfmlPattern,self).__init__(self.refname)
       
   965         self.value = value   
       
   966 
       
   967 class ConfmlTotalDigits(api.Base):
       
   968     """
       
   969     Confml totalDigits element
       
   970     """
       
   971     refname = "_totalDigits"
       
   972     def __init__(self, value, **kwargs):
       
   973         super(ConfmlTotalDigits,self).__init__(self.refname)
       
   974         self.value = value
       
   975 
       
   976 def get_mapper(modelname):
       
   977     """
       
   978     Return a instance of appropriate mapper for given model.
       
   979     """
       
   980     mapmodule = __import__('cone.confml.mapping')
       
   981     return mapmodule.confml.mapping.MAPPERS[modelname]()
       
   982