Exemplo n.º 1
0
    def normalize(data):
        """Given the specified data, this static methods normalize its representation
        using Netzob types.

        :parameter data: the data to normalize
        :type data: :class:`object`
        :return: an abstractType which value is data
        :rtype: :class:`netzob.Common.Models.Types.AbstractType.AbstractType`

        >>> from netzob.all import *
        >>> normalizedData = AbstractType.normalize("netzob")
        >>> print normalizedData.__class__
        <class 'netzob.Common.Models.Types.ASCII.ASCII'>
        >>> print normalizedData.value
        bitarray('011011100110010101110100011110100110111101100010')
        """

        if data is None:
            raise TypeError("Cannot normalize None data")

        if isinstance(data, AbstractType):
            return data
        if isinstance(data, int):
            from netzob.Common.Models.Types.Integer import Integer
            return Integer(value=data)
        if isinstance(data, str):
            try:
                from netzob.Common.Models.Types.ASCII import ASCII
                normalizedData = ASCII(value=data)
            except:
                from netzob.Common.Models.Types.Raw import Raw
                normalizedData = Raw(value=data)
            return normalizedData

        raise TypeError("Not a valid data ({0}), impossible to normalize it.", type(data))