def _compose_name(path, child_name):
    if path:
        if isinstance(child_name, int) or INSTANT_PATTERN.match(child_name):
            return '{}[{}]'.format(path, child_name)
        return '{}.{}'.format(path, child_name)
    else:
        return child_name
def _parse_child(child_name, child, child_path):
    if 'values' in child:
        return Parameter(child_name, child, child_path)
    elif 'brackets' in child:
        return Scale(child_name, child, child_path)
    elif isinstance(child, dict) and all([INSTANT_PATTERN.match(str(key)) for key in child.keys()]):
        return Parameter(child_name, child, child_path)
    else:
        return ParameterNode(child_name, data = child, file_path = child_path)
def _parse_child(child_name, child, child_path):
    if 'values' in child:
        return Parameter(child_name, child, child_path)
    elif 'brackets' in child:
        return Scale(child_name, child, child_path)
    elif isinstance(child, dict) and all([INSTANT_PATTERN.match(str(key)) for key in child.keys()]):
        return Parameter(child_name, child, child_path)
    else:
        return ParameterNode(child_name, data = child, file_path = child_path)
Exemple #4
0
    def __init__(self, name, data, file_path=None):
        self.name = name
        self.file_path = file_path
        _validate_parameter(self, data, data_type=dict)
        self.description = None
        self.metadata = {}
        self.documentation = None
        self.values_history = self  # Only for backward compatibility

        # Normal parameter declaration: the values are declared under the 'values' key: parse the description and metadata.
        if data.get('values'):
            # 'unit' and 'reference' are only listed here for backward compatibility
            _validate_parameter(self,
                                data,
                                allowed_keys=set([
                                    'values', 'description', 'metadata',
                                    'unit', 'reference', 'documentation'
                                ]))
            self.description = data.get('description')

            _set_backward_compatibility_metadata(self, data)
            self.metadata.update(data.get('metadata', {}))

            _validate_parameter(self, data['values'], data_type=dict)
            values = data['values']

            self.documentation = data.get('documentation')

        else:  # Simplified parameter declaration: only values are provided
            values = data

        instants = sorted(values.keys(),
                          reverse=True)  # sort in reverse chronological order

        values_list = []
        for instant_str in instants:
            if not INSTANT_PATTERN.match(instant_str):
                raise ParameterParsingError(
                    "Invalid property '{}' in '{}'. Properties must be valid YYYY-MM-DD instants, such as 2017-01-15."
                    .format(instant_str, self.name), file_path)

            instant_info = values[instant_str]

            #  Ignore expected values, as they are just metadata
            if instant_info == "expected" or isinstance(
                    instant_info, dict) and instant_info.get("expected"):
                continue

            value_name = _compose_name(name, instant_str)
            value_at_instant = ParameterAtInstant(value_name,
                                                  instant_str,
                                                  data=instant_info,
                                                  file_path=self.file_path,
                                                  metadata=self.metadata)
            values_list.append(value_at_instant)

        self.values_list = values_list
    def __init__(self, name, data, file_path = None):
        self.name = name
        self.file_path = file_path
        _validate_parameter(self, data, data_type = dict)
        self.description = None
        self.metadata = {}
        self.documentation = None
        self.values_history = self  # Only for backward compatibility

        # Normal parameter declaration: the values are declared under the 'values' key: parse the description and metadata.
        if data.get('values'):
            # 'unit' and 'reference' are only listed here for backward compatibility
            _validate_parameter(self, data, allowed_keys = COMMON_KEYS.union({'values'}))
            self.description = data.get('description')

            _set_backward_compatibility_metadata(self, data)
            self.metadata.update(data.get('metadata', {}))

            _validate_parameter(self, data['values'], data_type = dict)
            values = data['values']

            self.documentation = data.get('documentation')

        else:  # Simplified parameter declaration: only values are provided
            values = data

        instants = sorted(values.keys(), reverse = True)  # sort in reverse chronological order

        values_list = []
        for instant_str in instants:
            if not INSTANT_PATTERN.match(instant_str):
                raise ParameterParsingError(
                    "Invalid property '{}' in '{}'. Properties must be valid YYYY-MM-DD instants, such as 2017-01-15."
                    .format(instant_str, self.name),
                    file_path)

            instant_info = values[instant_str]

            #  Ignore expected values, as they are just metadata
            if instant_info == "expected" or isinstance(instant_info, dict) and instant_info.get("expected"):
                continue

            value_name = _compose_name(name, item_name = instant_str)
            value_at_instant = ParameterAtInstant(value_name, instant_str, data = instant_info, file_path = self.file_path, metadata = self.metadata)
            values_list.append(value_at_instant)

        self.values_list = values_list