configurationengine/source/cone/public/_plugin_reader.py
changeset 3 e7e0ae78773e
parent 0 2e8eeb919028
equal deleted inserted replaced
2:87cfa131b535 3:e7e0ae78773e
    14 # Description:
    14 # Description:
    15 #
    15 #
    16 
    16 
    17 import copy
    17 import copy
    18 import logging
    18 import logging
    19 import plugin, exceptions, api, utils
    19 from cone.public import exceptions, api, utils
       
    20 import plugin
    20 import cone.confml.model
    21 import cone.confml.model
    21 
    22 
    22 log = logging.getLogger('cone')
    23 log = logging.getLogger('cone')
    23 
    24 
    24 # The XML namespace for common ImplML definitions
    25 # The XML namespace for common ImplML definitions
    31 class TempVariableDefinition(object):
    32 class TempVariableDefinition(object):
    32     """
    33     """
    33     Class representing a temporary variable definition in an implementation file.
    34     Class representing a temporary variable definition in an implementation file.
    34     """
    35     """
    35     
    36     
    36     def __init__(self, ref, type, value):
    37     def __init__(self, ref, type, value, lineno=None):
    37         self.ref = ref
    38         self.ref = ref
    38         self.type = type
    39         self.type = type
    39         self.value = value
    40         self.value = value
       
    41         self.lineno = lineno
    40     
    42     
    41     def create_feature(self, config):
    43     def create_feature(self, config):
    42         """
    44         """
    43         Add a feature based on this temp feature definition to the given configuration.
    45         Add a feature based on this temp feature definition to the given configuration.
    44         """
    46         """
    52         
    54         
    53         mapping = {'string' : cone.confml.model.ConfmlStringSetting,
    55         mapping = {'string' : cone.confml.model.ConfmlStringSetting,
    54                    'int'    : cone.confml.model.ConfmlIntSetting,
    56                    'int'    : cone.confml.model.ConfmlIntSetting,
    55                    'real'   : cone.confml.model.ConfmlRealSetting,
    57                    'real'   : cone.confml.model.ConfmlRealSetting,
    56                    'boolean': cone.confml.model.ConfmlBooleanSetting}
    58                    'boolean': cone.confml.model.ConfmlBooleanSetting}
    57         feature = mapping[self.type](ref)
    59         # Create temp variables always name being also the ref
       
    60         feature = mapping[self.type](ref, name=ref)
    58         setattr(feature, TEMP_FEATURE_MARKER_VARNAME, True)
    61         setattr(feature, TEMP_FEATURE_MARKER_VARNAME, True)
    59         config.add_feature(feature, namespace)
    62         config.add_feature(feature, namespace)
    60         
    63         
    61         value = utils.expand_refs_by_default_view(self.value, config.get_default_view())
    64         value = utils.expand_refs_by_default_view(self.value, config.get_default_view())
    62         config.add_data(api.Data(fqr=self.ref, value=value))
    65         config.add_data(api.Data(fqr=self.ref, value=value))
    79 class TempVariableSequenceDefinition(object):
    82 class TempVariableSequenceDefinition(object):
    80     """
    83     """
    81     Class representing a temporary variable sequence definition in an implementation file.
    84     Class representing a temporary variable sequence definition in an implementation file.
    82     """
    85     """
    83     
    86     
    84     def __init__(self, ref, sub_items):
    87     def __init__(self, ref, sub_items, lineno=None):
    85         self.ref = ref
    88         self.ref = ref
    86         self.sub_items = sub_items
    89         self.sub_items = sub_items
       
    90         self.lineno = lineno
    87     
    91     
    88     def create_feature(self, config):
    92     def create_feature(self, config):
    89         if '.' in self.ref:
    93         if '.' in self.ref:
    90             pos = self.ref.rfind('.')
    94             pos = self.ref.rfind('.')
    91             ref = self.ref[pos + 1:]
    95             ref = self.ref[pos + 1:]
    93         else:
    97         else:
    94             ref = self.ref
    98             ref = self.ref
    95             namespace = ''
    99             namespace = ''
    96         
   100         
    97         # Creature the sequence feature
   101         # Creature the sequence feature
    98         seq_fea = api.FeatureSequence(ref)
   102         # Create temp variables always name being also the ref
       
   103         seq_fea = api.FeatureSequence(ref, name=ref)
    99         setattr(seq_fea, TEMP_FEATURE_MARKER_VARNAME, True)
   104         setattr(seq_fea, TEMP_FEATURE_MARKER_VARNAME, True)
   100         config.add_feature(seq_fea, namespace)
   105         config.add_feature(seq_fea, namespace)
   101         
   106         
   102         # Create the sub-features
   107         # Create the sub-features
   103         mapping = {'string' : cone.confml.model.ConfmlStringSetting,
   108         mapping = {'string' : cone.confml.model.ConfmlStringSetting,
   104                    'int'    : cone.confml.model.ConfmlIntSetting,
   109                    'int'    : cone.confml.model.ConfmlIntSetting,
   105                    'real'   : cone.confml.model.ConfmlRealSetting,
   110                    'real'   : cone.confml.model.ConfmlRealSetting,
   106                    'boolean': cone.confml.model.ConfmlBooleanSetting}
   111                    'boolean': cone.confml.model.ConfmlBooleanSetting}
   107         sub_features = []
   112         sub_features = []
   108         for sub_item in self.sub_items:
   113         for sub_item in self.sub_items:
   109             sub_feature = mapping[sub_item[1]](sub_item[0])
   114             sub_feature = mapping[sub_item[1]](sub_item[0], name=sub_item[0])
   110             seq_fea.add_feature(sub_feature)
   115             seq_fea.add_feature(sub_feature)
   111     
   116     
   112     def __eq__(self, other):
   117     def __eq__(self, other):
   113         if type(self) is type(other):
   118         if type(self) is type(other):
   114             return self.ref == other.ref and self.sub_items == other.sub_items
   119             return self.ref == other.ref and self.sub_items == other.sub_items
   181     
   186     
   182     def extend(self, other):
   187     def extend(self, other):
   183         """
   188         """
   184         Extend this object with the contents of another CommonImplmlData object.
   189         Extend this object with the contents of another CommonImplmlData object.
   185         """
   190         """
       
   191         if other is None:
       
   192             return
       
   193         
   186         if other.phase:
   194         if other.phase:
   187             self.phase = other.phase
   195             self.phase = other.phase
   188         if other.tags:
   196         if other.tags:
   189             self.tags = other.tags
   197             self.tags = other.tags
   190         self.tempvar_defs.extend(other.tempvar_defs)
       
   191         if other.setting_refs_override:
   198         if other.setting_refs_override:
   192             self.setting_refs_override = other.setting_refs_override
   199             self.setting_refs_override = other.setting_refs_override
   193         if other.output_root_dir:
   200         if other.output_root_dir:
   194             self.output_root_dir = other.output_root_dir
   201             self.output_root_dir = other.output_root_dir
   195         if other.output_sub_dir:
   202         if other.output_sub_dir:
   196             self.output_sub_dir = other.output_sub_dir
   203             self.output_sub_dir = other.output_sub_dir
   197     
   204     
   198     def copy(self):
   205     def copy(self):
   199         result = CommonImplmlData()
   206         result = CommonImplmlData()
   200         result.phase = self.phase
   207         result.phase = self.phase
   201         if result.tags is not None:
   208         if self.tags is not None:
   202             result.tags = self.tags.copy()
   209             result.tags = self.tags.copy()
   203         result.tempvar_defs = list(self.tempvar_defs)
   210         result.tempvar_defs = list(self.tempvar_defs)
   204         result.setting_refs_override = copy.deepcopy(self.setting_refs_override)
   211         result.setting_refs_override = copy.deepcopy(self.setting_refs_override)
   205         result.output_root_dir = self.output_root_dir
   212         result.output_root_dir = self.output_root_dir
   206         result.output_sub_dir = self.output_sub_dir
   213         result.output_sub_dir = self.output_sub_dir
   451     
   458     
   452     @classmethod
   459     @classmethod
   453     def read_data(cls, etree):
   460     def read_data(cls, etree):
   454         """
   461         """
   455         Read common ImplML data from the given XML element.
   462         Read common ImplML data from the given XML element.
   456         @return: A CommonImplmlData instance or None if no common namespace
   463         @return: A CommonImplmlData instance.
   457             elements were found.
       
   458         """
   464         """
   459         result = CommonImplmlData()
   465         result = CommonImplmlData()
   460         
   466         
   461         reader_methods = {'phase'                   : cls._read_phase,
   467         reader_methods = {'phase'                   : cls._read_phase,
   462                           'tag'                     : cls._read_tag,
   468                           'tag'                     : cls._read_tag,
   464                           'tempVariableSequence'    : cls._read_tempvarseq,
   470                           'tempVariableSequence'    : cls._read_tempvarseq,
   465                           'settingRefsOverride'     : cls._read_setting_refs_override,
   471                           'settingRefsOverride'     : cls._read_setting_refs_override,
   466                           'outputRootDir'           : cls._read_output_root_dir,
   472                           'outputRootDir'           : cls._read_output_root_dir,
   467                           'outputSubDir'            : cls._read_output_sub_dir}
   473                           'outputSubDir'            : cls._read_output_sub_dir}
   468         
   474         
   469         found = False
       
   470         for elem in etree:
   475         for elem in etree:
   471             ns, tag = utils.xml.split_tag_namespace(elem.tag)
   476             ns, tag = utils.xml.split_tag_namespace(elem.tag)
   472             if ns != COMMON_IMPLML_NAMESPACE:   continue
   477             if ns != COMMON_IMPLML_NAMESPACE:   continue
   473             if tag not in reader_methods:       continue
   478             if tag not in reader_methods:       continue
   474             
   479             
   475             reader_methods[tag](elem, result)
   480             reader_methods[tag](elem, result)
   476             found = True
   481         
   477         
   482         return result
   478         if found:   return result
       
   479         else:       return None
       
   480     
   483     
   481     @classmethod
   484     @classmethod
   482     def _read_phase(cls, elem, result):
   485     def _read_phase(cls, elem, result):
   483         phase = elem.get('name')
   486         phase = elem.get('name')
   484         if phase is None:
   487         if phase is None:
   500     @classmethod
   503     @classmethod
   501     def _read_tempvar(cls, elem, result):
   504     def _read_tempvar(cls, elem, result):
   502         ref = elem.get('ref')
   505         ref = elem.get('ref')
   503         type = elem.get('type', 'string')
   506         type = elem.get('type', 'string')
   504         value = elem.get('value', '')
   507         value = elem.get('value', '')
       
   508         lineno = utils.etree.get_lineno(elem)
   505         
   509         
   506         if ref is None:
   510         if ref is None:
   507             cls._raise_missing_attr(elem, 'ref')
   511             cls._raise_missing_attr(elem, 'ref')
   508         if type not in cls.VALID_TYPES:
   512         if type not in cls.VALID_TYPES:
   509             cls._raise_invalid_type(ref, type)
   513             cls._raise_invalid_type(ref, type)
   510         
   514         
   511         result.tempvar_defs.append(TempVariableDefinition(ref, type, value))
   515         result.tempvar_defs.append(TempVariableDefinition(ref, type, value, lineno))
   512     
   516     
   513     @classmethod
   517     @classmethod
   514     def _read_tempvarseq(cls, elem, result):
   518     def _read_tempvarseq(cls, elem, result):
   515         ref = elem.get('ref')
   519         ref = elem.get('ref')
   516         if ref is None:
   520         if ref is None:
   529             sub_items.append((sub_ref, sub_type))
   533             sub_items.append((sub_ref, sub_type))
   530         
   534         
   531         if not sub_items:
   535         if not sub_items:
   532             raise exceptions.ParseError("Temporary variable sequence '%s' does not have any sub-items" % ref)
   536             raise exceptions.ParseError("Temporary variable sequence '%s' does not have any sub-items" % ref)
   533         
   537         
   534         result.tempvar_defs.append(TempVariableSequenceDefinition(ref, sub_items))
   538         lineno = utils.etree.get_lineno(elem)
       
   539         
       
   540         result.tempvar_defs.append(TempVariableSequenceDefinition(ref, sub_items, lineno))
   535     
   541     
   536     @classmethod
   542     @classmethod
   537     def _read_setting_refs_override(cls, elem, result):
   543     def _read_setting_refs_override(cls, elem, result):
   538         if elem.get('refsIrrelevant', 'false').lower() in ('1', 'true'):
   544         if elem.get('refsIrrelevant', 'false').lower() in ('1', 'true'):
   539             refs = None
   545             refs = None