Beispiel #1
0
    def toUri(self):
        """
        Return a string representation of the exclude values.

        :return: The string representation.
        :rtype: string
        """
        if len(self._entries) == 0:
            return ""

        result = BytesIO()
        didFirst = False
        for entry in self._entries:
            if didFirst:
                # write is required to take a byte buffer.
                result.write(Exclude._comma)

            if entry.getType() == Exclude.ANY:
                # write is required to take a byte buffer.
                result.write(Exclude._star)
            else:
                entry.getComponent().toEscapedString(result)
            didFirst = True

        return Common.getBytesIOString(result)
Beispiel #2
0
    def toHex(self, result = None):
        """
        Return the hex representation of the bytes in array.

        :param BytesIO result: (optional) The BytesIO stream to write to. If
          omitted, return a str with the result.
        :return: The hex string (only if result is omitted).
        :rtype: str
        """
        if result == None:
            if self._array == None:
                return ""

            result = BytesIO()
            self.toHex(result)
            return Common.getBytesIOString(result)

        if self._array == None:
            return

        array = self.buf()
        hexBuffer = bytearray(2)
        for i in range(len(array)):
            # Get the hex string and transfer to hexBuffer for writing.
            hex = "%02x" % array[i]
            hexBuffer[0] = ord(hex[0])
            hexBuffer[1] = ord(hex[1])
            result.write(hexBuffer)
Beispiel #3
0
    def toUri(self):
        """
        Return a string representation of the exclude values.
        
        :return: The string representation.
        :rtype: string
        """
        if len(self._entries) == 0:
            return ""

        result = BytesIO()
        didFirst = False
        for entry in self._entries:
            if didFirst:
                # write is required to take a byte buffer.
                result.write(Exclude._comma)

            if entry.getType() == Exclude.ANY:
                # write is required to take a byte buffer.
                result.write(Exclude._star)
            else:
                entry.getComponent().toEscapedString(result)
            didFirst = True

        return Common.getBytesIOString(result)
Beispiel #4
0
    def toHex(self, result = None):
        """
        Return the hex representation of the bytes in array.

        :param BytesIO result: (optional) The BytesIO stream to write to. If
          omitted, return a str with the result.
        :return: The hex string (only if result is omitted).
        :rtype: str
        """
        if result == None:
            if self._array == None:
                return ""

            result = BytesIO()
            self.toHex(result)
            return Common.getBytesIOString(result)

        if self._array == None:
            return

        array = self.buf()
        hexBuffer = bytearray(2)
        for i in range(len(array)):
            # Get the hex string and transfer to hexBuffer for writing.
            hex = "%02x" % array[i]
            hexBuffer[0] = ord(hex[0])
            hexBuffer[1] = ord(hex[1])
            result.write(hexBuffer)
Beispiel #5
0
    def toEscapedString(value, result = None):
        """
        Convert value to a string, escaping characters according to the NDN URI 
        Scheme. This also adds "..." to a value with zero or more ".".
        
        :param value: The buffer with the value to escape.
        :type value: An array type with int elements
        :param BytesIO result: (optional) The BytesIO stream to write to.  If 
          omitted, return a str with the result.
        :return: The result as a string (only if result is omitted).
        :rtype: str
        """
        if result == None:
            result = BytesIO()
            Name.toEscapedString(value, result)
            return Common.getBytesIOString(result)            
            
        gotNonDot = False
        for i in range(len(value)):
            if value[i] != ord('.'):
                gotNonDot = True
                break

        charBuffer = bytearray(1)
        if not gotNonDot:
            charBuffer[0] = ord('.')
            # Special case for component of zero or more periods. Add 3 periods.
            for i in range(len(value) + 3):
                result.write(charBuffer)
        else:
            hexBuffer = bytearray(3)
            hexBuffer[0] = ord('%')
            for i in range(len(value)):
                x = value[i]
                # Check for 0-9, A-Z, a-z, (+), (-), (.), (_)
                if ((x >= 0x30 and x <= 0x39) or (x >= 0x41 and x <= 0x5a) or
                    (x >= 0x61 and x <= 0x7a) or x == 0x2b or x == 0x2d or
                    x == 0x2e or x == 0x5f):
                    charBuffer[0] = x
                    # write is required to take a byte buffer.
                    result.write(charBuffer)
                else:
                    # Write '%' followed by the hex value.
                    hex = "%02X" % x
                    hexBuffer[1]  = ord(hex[0])
                    hexBuffer[2]  = ord(hex[1])
                    # write is required to take a byte buffer.
                    result.write(hexBuffer)
Beispiel #6
0
    def toEscapedString(value, result = None):
        """
        Convert value to a string, escaping characters according to the NDN URI 
        Scheme. This also adds "..." to a value with zero or more ".".
        
        :param value: The buffer with the value to escape.
        :type value: An array type with int elements
        :param BytesIO result: (optional) The BytesIO stream to write to.  If 
          omitted, return a str with the result.
        :return: The result as a string (only if result is omitted).
        :rtype: str
        """
        if result == None:
            result = BytesIO()
            Name.toEscapedString(value, result)
            return Common.getBytesIOString(result)            
            
        gotNonDot = False
        for i in range(len(value)):
            if value[i] != ord('.'):
                gotNonDot = True
                break

        charBuffer = bytearray(1)
        if not gotNonDot:
            charBuffer[0] = ord('.')
            # Special case for component of zero or more periods. Add 3 periods.
            for i in range(len(value) + 3):
                result.write(charBuffer)
        else:
            hexBuffer = bytearray(3)
            hexBuffer[0] = ord('%')
            for i in range(len(value)):
                x = value[i]
                # Check for 0-9, A-Z, a-z, (+), (-), (.), (_)
                if ((x >= 0x30 and x <= 0x39) or (x >= 0x41 and x <= 0x5a) or
                    (x >= 0x61 and x <= 0x7a) or x == 0x2b or x == 0x2d or
                    x == 0x2e or x == 0x5f):
                    charBuffer[0] = x
                    # write is required to take a byte buffer.
                    result.write(charBuffer)
                else:
                    # Write '%' followed by the hex value.
                    hex = "%02X" % x
                    hexBuffer[1]  = ord(hex[0])
                    hexBuffer[2]  = ord(hex[1])
                    # write is required to take a byte buffer.
                    result.write(hexBuffer)
Beispiel #7
0
   def toUri(self):
       """
       Encode this name as a URI according to the NDN URI Scheme.
       
       :return: The encoded URI.
       :rtype: str
       """
       if len(self._components) == 0:
           return "/"
 
       result = BytesIO()
       for component in self._components:
           # write is required to take a byte buffer.
           result.write(Name._slash)
           component.toEscapedString(result)
 
       return Common.getBytesIOString(result)
Beispiel #8
0
   def toUri(self):
       """
       Encode this name as a URI according to the NDN URI Scheme.
       
       :return: The encoded URI.
       :rtype: str
       """
       if len(self._components) == 0:
           return "/"
 
       result = BytesIO()
       for component in self._components:
           # write is required to take a byte buffer.
           result.write(Name._slash)
           component.toEscapedString(result)
 
       return Common.getBytesIOString(result)
Beispiel #9
0
    def toHex(self):
        """
        Return the hex representation of the bytes in array.

        :return: The hex string.
        :rtype: str
        """
        if self._array == None:
            return ""

        array = self.buf()
        result = BytesIO()
        hexBuffer = bytearray(2)
        for i in range(len(array)):
            # Get the hex string and transfer to hexBuffer for writing.
            hex = "%02X" % array[i]
            hexBuffer[0] = ord(hex[0])
            hexBuffer[1] = ord(hex[1])
            result.write(hexBuffer)

        return Common.getBytesIOString(result)
Beispiel #10
0
    def toHex(self):
        """
        Return the hex representation of the bytes in array.

        :return: The hex string.
        :rtype: str
        """
        if self._array == None:
            return ""

        array = self.buf()
        result = BytesIO()
        hexBuffer = bytearray(2)
        for i in range(len(array)):
            # Get the hex string and transfer to hexBuffer for writing.
            hex = "%02x" % array[i]
            hexBuffer[0] = ord(hex[0])
            hexBuffer[1] = ord(hex[1])
            result.write(hexBuffer)

        return Common.getBytesIOString(result)