Ejemplo n.º 1
0
    def parseTextParameters(self, txt, data):
        """
        Parse parameters, return string point at value.
        """

        try:
            stripValueSpaces = False    # Fix for AB.app base PHOTO properties that use two spaces at start of line
            while txt:
                if txt[0] == ';':
                    # Parse parameter

                    # Move past delimiter
                    txt = txt[1:]

                    # Get quoted string or token - in iCalendar we only look for "=" here
                    # but for "broken" vCard BASE64 property we need to also terminate on
                    # ":;"
                    parameter_name, txt = stringutils.strduptokenstr(txt, "=:;")
                    if parameter_name is None:
                        raise InvalidProperty("Invalid property", data)

                    if txt[0] != "=":
                        # Deal with parameters without values
                        if ParserContext.VCARD_2_NO_PARAMETER_VALUES == ParserContext.PARSER_RAISE:
                            raise InvalidProperty("Invalid property parameter", data)
                        elif ParserContext.VCARD_2_NO_PARAMETER_VALUES == ParserContext.PARSER_ALLOW:
                            parameter_value = None
                        else: # PARSER_IGNORE and PARSER_FIX
                            parameter_name = None

                        if parameter_name.upper() == "BASE64" and ParserContext.VCARD_2_BASE64 == ParserContext.PARSER_FIX:
                            parameter_name = definitions.Parameter_ENCODING
                            parameter_value = definitions.Parameter_Value_ENCODING_B
                            stripValueSpaces = True
                    else:
                        txt = txt[1:]
                        parameter_value, txt = stringutils.strduptokenstr(txt, ":;,")
                        if parameter_value is None:
                            raise InvalidProperty("Invalid property", data)

                    # Now add parameter value (decode ^-escaping)
                    if parameter_name is not None:
                        attrvalue = Parameter(name=parameter_name, value=decodeParameterValue(parameter_value))
                        self.mParameters.setdefault(parameter_name.upper(), []).append(attrvalue)

                    # Look for additional values
                    while txt[0] == ',':
                        txt = txt[1:]
                        parameter_value2, txt = stringutils.strduptokenstr(txt, ":;,")
                        if parameter_value2 is None:
                            raise InvalidProperty("Invalid property", data)
                        attrvalue.addValue(decodeParameterValue(parameter_value2))
                elif txt[0] == ':':
                    txt = txt[1:]
                    if stripValueSpaces:
                        txt = txt.replace(" ", "")
                    return txt

        except IndexError:
            raise InvalidProperty("Invalid property", data)
Ejemplo n.º 2
0
    def parseTextParameters(self, txt, data):
        """
        Parse parameters, return string point at value.
        """

        try:
            while txt:
                if txt[0] == ';':
                    # Parse parameter

                    # Move past delimiter
                    txt = txt[1:]

                    # Get quoted string or token
                    parameter_name, txt = stringutils.strduptokenstr(txt, "=")
                    if parameter_name is None:
                        raise InvalidProperty("Invalid property: empty parameter name", data)
                    txt = txt[1:]
                    parameter_value, txt = stringutils.strduptokenstr(txt, ":;,")
                    if parameter_value is None:
                        raise InvalidProperty("Invalid property: empty parameter value", data)

                    # Now add parameter value (decode ^-escaping)
                    attrvalue = Parameter(name=parameter_name, value=decodeParameterValue(parameter_value))
                    self.mParameters.setdefault(parameter_name.upper(), []).append(attrvalue)

                    # Look for additional values
                    while txt[0] == ',':
                        txt = txt[1:]
                        parameter_value2, txt = stringutils.strduptokenstr(txt, ":;,")
                        if parameter_value2 is None:
                            raise InvalidProperty("Invalid property: empty parameter multi-value", data)
                        attrvalue.addValue(decodeParameterValue(parameter_value2))
                elif txt[0] == ':':
                    return txt[1:]
                else:
                    # We should never get here but if we do we need to terminate the loop
                    raise InvalidProperty("Invalid property: missing value separator", data)

        except IndexError:
            raise InvalidProperty("Invalid property: 'parameter index error'", data)
Ejemplo n.º 3
0
    def parseTextParameters(self, txt, data):
        """
        Parse parameters, return string point at value.
        """

        try:
            while txt:
                if txt[0] == ';':
                    # Parse parameter

                    # Move past delimiter
                    txt = txt[1:]

                    # Get quoted string or token
                    parameter_name, txt = stringutils.strduptokenstr(txt, "=")
                    if parameter_name is None:
                        raise InvalidProperty("Invalid property", data)
                    txt = txt[1:]
                    parameter_value, txt = stringutils.strduptokenstr(txt, ":;,")
                    if parameter_value is None:
                        raise InvalidProperty("Invalid property", data)

                    # Now add parameter value (decode ^-escaping)
                    attrvalue = Parameter(name=parameter_name, value=decodeParameterValue(parameter_value))
                    self.mParameters.setdefault(parameter_name.upper(), []).append(attrvalue)

                    # Look for additional values
                    while txt[0] == ',':
                        txt = txt[1:]
                        parameter_value2, txt = stringutils.strduptokenstr(txt, ":;,")
                        if parameter_value2 is None:
                            raise InvalidProperty("Invalid property", data)
                        attrvalue.addValue(decodeParameterValue(parameter_value2))
                elif txt[0] == ':':
                    return txt[1:]
                else:
                    # We should never get here but if we do we need to terminate the loop
                    raise InvalidProperty("Invalid property", data)

        except IndexError:
            raise InvalidProperty("Invalid property", data)
Ejemplo n.º 4
0
    def test_decodeParameterValue(self):
        """
        Special cases for decodeParameterValue.
        """

        data = (
            ("^a^bc^", "^a^bc^"),
            ("^^^abc", "^^abc"),
        )

        for value, decoded in data:
            self.assertEqual(decodeParameterValue(value), decoded)
Ejemplo n.º 5
0
    def test_decodeParameterValue(self):
        """
        Special cases for decodeParameterValue.
        """

        data = (
            ("^a^bc^", "^a^bc^"),
            ("^^^abc", "^^abc"),
        )

        for value, decoded in data:
            self.assertEqual(decodeParameterValue(value), decoded)
Ejemplo n.º 6
0
    def test_encodeParameterValue(self):
        """
        Round trip encodeParameterValue and decodeParameterValue.
        """

        data = (
            ("abc", "abc", None),
            ("\"abc\"", "^'abc^'", None),
            ("abc\ndef", "abc^ndef", None),
            ("abc\rdef", "abc^ndef", "abc\ndef"),
            ("abc\r\ndef", "abc^ndef", "abc\ndef"),
            ("abc\n\tdef", "abc^n\tdef", None),
            ("abc^2", "abc^^2", None),
            ("^abc^", "^^abc^^", None),
        )

        for value, encoded, decoded in data:
            if decoded is None:
                decoded = value
            self.assertEqual(encodeParameterValue(value), encoded)
            self.assertEqual(decodeParameterValue(encoded), decoded)
Ejemplo n.º 7
0
    def test_encodeParameterValue(self):
        """
        Round trip encodeParameterValue and decodeParameterValue.
        """

        data = (
            ("abc", "abc", None),
            ("\"abc\"", "^'abc^'", None),
            ("abc\ndef", "abc^ndef", None),
            ("abc\rdef", "abc^ndef", "abc\ndef"),
            ("abc\r\ndef", "abc^ndef", "abc\ndef"),
            ("abc\n\tdef", "abc^n\tdef", None),
            ("abc^2", "abc^^2", None),
            ("^abc^", "^^abc^^", None),
        )

        for value, encoded, decoded in data:
            if decoded is None:
                decoded = value
            self.assertEqual(encodeParameterValue(value), encoded)
            self.assertEqual(decodeParameterValue(encoded), decoded)
Ejemplo n.º 8
0
    def parseTextParameters(self, txt, data):
        """
        Parse parameters, return string point at value.
        """

        try:
            stripValueSpaces = False  # Fix for AB.app base PHOTO properties that use two spaces at start of line
            while txt:
                if txt[0] == ';':
                    # Parse parameter

                    # Move past delimiter
                    txt = txt[1:]

                    # Get quoted string or token - in iCalendar we only look for "=" here
                    # but for "broken" vCard BASE64 property we need to also terminate on
                    # ":;"
                    parameter_name, txt = stringutils.strduptokenstr(
                        txt, "=:;")
                    if parameter_name is None:
                        raise InvalidProperty(
                            "Invalid property: empty parameter name", data)

                    if txt[0] != "=":
                        # Deal with parameters without values
                        if ParserContext.VCARD_2_NO_PARAMETER_VALUES == ParserContext.PARSER_RAISE:
                            raise InvalidProperty("Invalid property parameter",
                                                  data)
                        elif ParserContext.VCARD_2_NO_PARAMETER_VALUES == ParserContext.PARSER_ALLOW:
                            parameter_value = None
                        else:  # PARSER_IGNORE and PARSER_FIX
                            parameter_name = None

                        if parameter_name.upper(
                        ) == "BASE64" and ParserContext.VCARD_2_BASE64 == ParserContext.PARSER_FIX:
                            parameter_name = definitions.Parameter_ENCODING
                            parameter_value = definitions.Parameter_Value_ENCODING_B
                            stripValueSpaces = True
                    else:
                        txt = txt[1:]
                        parameter_value, txt = stringutils.strduptokenstr(
                            txt, ":;,")
                        if parameter_value is None:
                            raise InvalidProperty(
                                "Invalid property: empty parameter name", data)

                    # Now add parameter value (decode ^-escaping)
                    if parameter_name is not None:
                        attrvalue = Parameter(
                            name=parameter_name,
                            value=decodeParameterValue(parameter_value))
                        self.mParameters.setdefault(parameter_name.upper(),
                                                    []).append(attrvalue)

                    # Look for additional values
                    while txt[0] == ',':
                        txt = txt[1:]
                        parameter_value2, txt = stringutils.strduptokenstr(
                            txt, ":;,")
                        if parameter_value2 is None:
                            raise InvalidProperty(
                                "Invalid property: empty parameter multi-value",
                                data)
                        attrvalue.addValue(
                            decodeParameterValue(parameter_value2))
                elif txt[0] == ':':
                    txt = txt[1:]
                    if stripValueSpaces:
                        txt = txt.replace(" ", "")
                    return txt

        except IndexError:
            raise InvalidProperty("Invalid property: index error", data)