예제 #1
0
    def get_attr(
        self,
        name: Text,
        *,
        data_type: AllowedTypesType = str,
        optional: bool = False,
        can_be_str: bool = True,
    ) -> Optional[Union[AllowedValueType, List['Entity'], ]]:
        """
        Access an attribute of the entity.

        See :ref:meth:`launch.frontend.Entity.get_attr`.
        `launch_yaml` does not apply type coercion,
        it only checks if the read value is of the correct type.
        """
        if name not in self.__element:
            if not optional:
                raise AttributeError(
                    'Can not find attribute {} in Entity {}'.format(
                        name, self.type_name))
            else:
                return None
        self.__read_keys.add(name)
        data = self.__element[name]
        if check_is_list_entity(data_type):
            if isinstance(data, list) and isinstance(data[0], dict):
                return [Entity(child, name) for child in data]
            raise TypeError(
                'Attribute {} of Entity {} expected to be a list of dictionaries.'
                .format(name, self.type_name))
        if not is_instance_of(data, data_type, can_be_str=can_be_str):
            raise TypeError(
                'Attribute {} of Entity {} expected to be of type {}, got {}'.
                format(name, self.type_name, data_type, type(data)))
        return data
예제 #2
0
 def get_attr(
     self,
     name: Text,
     *,
     data_type: Any = str,
     optional: bool = False
 ) -> Optional[Union[List[Union[int, str, float, bool]], Union[
         int, str, float, bool], List['Entity']]]:
     """Access an attribute of the entity."""
     if name not in self.__element:
         if not optional:
             raise AttributeError(
                 'Can not find attribute {} in Entity {}'.format(
                     name, self.type_name))
         else:
             return None
     data = self.__element[name]
     if check_is_list_entity(data_type):
         if isinstance(data, list) and isinstance(data[0], dict):
             return [Entity(child, name) for child in data]
         raise TypeError(
             'Attribute {} of Entity {} expected to be a list of dictionaries.'
             .format(name, self.type_name))
     if not check_type(data, data_type):
         raise TypeError(
             'Attribute {} of Entity {} expected to be of type {}, got {}'.
             format(name, self.type_name, data_type, type(data)))
     return data
예제 #3
0
    def get_attr(
        self,
        name: Text,
        *,
        data_type: AllowedTypesType = str,
        optional: bool = False,
        can_be_str: bool = True,
    ) -> Optional[Union[AllowedValueType, List['Entity'], ]]:
        """
        Access an attribute of the entity.

        See :ref:meth:`launch.frontend.Entity.get_attr`.
        `launch_xml` uses type coercion.
        If coercion fails, `ValueError` will be raised.
        """
        attr_error = AttributeError(
            'Attribute {} of type {} not found in Entity {}'.format(
                name, data_type, self.type_name))
        if check_is_list_entity(data_type):
            return_list = [x for x in self.__xml_element if x.tag == name]
            if not return_list:
                if optional:
                    return None
                else:
                    raise attr_error
            self.__read_children.add(return_list[0].tag)
            return [Entity(item) for item in return_list]
        value = None
        if name in self.__xml_element.attrib:
            name_sep = name + '-sep'
            if name_sep not in self.__xml_element.attrib:
                value = self.__xml_element.attrib[name]
            else:
                self.__read_attributes.add(name_sep)
                sep = self.__xml_element.attrib[name_sep]
                value = self.__xml_element.attrib[name].split(sep)
            self.__read_attributes.add(name)
        if value is None:
            if not optional:
                raise attr_error
            else:
                return None
        try:
            value = get_typed_value(value, data_type, can_be_str=can_be_str)
        except ValueError:
            raise TypeError(
                'Attribute {} of Entity {} expected to be of type {}.'
                '`{}` can not be converted to one of those types'.format(
                    name, self.type_name, data_type, value))
        return value
예제 #4
0
파일: entity.py 프로젝트: sousou1/launch
 def get_attr(
     self,
     name,
     *,
     data_type = str,
     optional = False
 ):
     """Access an attribute of the entity."""
     attr_error = AttributeError(
         'Attribute {} of type {} not found in Entity {}'.format(
             name, data_type, self.type_name
         )
     )
     if check_is_list_entity(data_type):
         return_list = filter(lambda x: x.tag == name, self.__xml_element)
         if not return_list:
             if optional:
                 return None
             else:
                 raise attr_error
         return [Entity(item) for item in return_list]
     value = None
     if name in self.__xml_element.attrib:
         name_sep = name + '-sep'
         if name_sep not in self.__xml_element.attrib:
             value = self.__xml_element.attrib[name]
         else:
             sep = self.__xml_element.attrib[name_sep]
             value = self.__xml_element.attrib[name].split(sep)
     if value is None:
         if not optional:
             raise attr_error
         else:
             return None
     try:
         value = get_typed_value(value, data_type)
     except ValueError:
         raise TypeError(
             'Attribute {} of Entity {} expected to be of type {}.'
             '`{}` can not be converted to one of those types'.format(
                 name, self.type_name, data_type, value
             )
         )
     return value