Esempio n. 1
0
    def testParameterEncodingDecoding(self):

        prop = Property(name="X-FOO", value="Test")
        prop.addParameter(Parameter("X-BAR", '"Check"'))
        self.assertEqual(str(prop), "X-FOO;X-BAR=^'Check^':Test\r\n")

        prop.addParameter(Parameter("X-BAR2", "Check\nThis\tOut\n"))
        self.assertEqual(str(prop), "X-FOO;X-BAR=^'Check^';X-BAR2=Check^nThis\tOut^n:Test\r\n")

        data = "X-FOO;X-BAR=^'Check^':Test"
        prop = Property.parseText(data)
        self.assertEqual(prop.getParameterValue("X-BAR"), '"Check"')

        data = "X-FOO;X-BAR=^'Check^';X-BAR2=Check^nThis\tOut^n:Test"
        prop = Property.parseText(data)
        self.assertEqual(prop.getParameterValue("X-BAR"), '"Check"')
        self.assertEqual(prop.getParameterValue("X-BAR2"), "Check\nThis\tOut\n")
Esempio n. 2
0
    def testHash(self):

        hashes = []
        for item in TestProperty.test_data:
            prop = Property.parseText(item)
            hashes.append(hash(prop))
        hashes.sort()
        for i in range(1, len(hashes)):
            self.assertNotEqual(hashes[i - 1], hashes[i])
Esempio n. 3
0
    def testHash(self):

        hashes = []
        for item in TestProperty.test_data:
            prop = Property.parseText(item)
            hashes.append(hash(prop))
        hashes.sort()
        for i in range(1, len(hashes)):
            self.assertNotEqual(hashes[i - 1], hashes[i])
Esempio n. 4
0
    def testParseGenerate(self):

        for data in TestProperty.test_data:
            prop = Property.parseText(data)
            propstr = str(prop)
            self.assertEqual(
                propstr[:-2], data, "Failed parse/generate: %s to %s" % (
                    data,
                    propstr,
                ))
Esempio n. 5
0
    def testParameterEncodingDecoding(self):

        prop = Property(name="X-FOO", value="Test")
        prop.addParameter(Parameter("X-BAR", "\"Check\""))
        self.assertEqual(str(prop), "X-FOO;X-BAR=^'Check^':Test\r\n")

        prop.addParameter(Parameter("X-BAR2", "Check\nThis\tOut\n"))
        self.assertEqual(
            str(prop),
            "X-FOO;X-BAR=^'Check^';X-BAR2=Check^nThis\tOut^n:Test\r\n")

        data = "X-FOO;X-BAR=^'Check^':Test"
        prop = Property.parseText(data)
        self.assertEqual(prop.getParameterValue("X-BAR"), "\"Check\"")

        data = "X-FOO;X-BAR=^'Check^';X-BAR2=Check^nThis\tOut^n:Test"
        prop = Property.parseText(data)
        self.assertEqual(prop.getParameterValue("X-BAR"), "\"Check\"")
        self.assertEqual(prop.getParameterValue("X-BAR2"),
                         "Check\nThis\tOut\n")
Esempio n. 6
0
    def testParseProperty(self):

        items = (
            ("ORG:", "ORG:"),
            ("ORG:Example", "ORG:Example"),
            ("ORG:Example\, Inc.", "ORG:Example\, Inc."),
            ("ORG:Example\; Inc;Dept. of Silly Walks", "ORG:Example\; Inc;Dept. of Silly Walks"),
            ("ORG;VALUE=TEXT:Example", "ORG:Example"),
        )

        for item, result in items:
            prop = Property.parseText(item)
            test = prop.getText()
            self.assertEqual(test, result + "\r\n", "Failed to parse and re-generate '%s'" % (item,))
Esempio n. 7
0
    def testParseProperty(self):

        items = (
            ("ORG:", "ORG:"),
            ("ORG:Example", "ORG:Example"),
            ("ORG:Example\, Inc.", "ORG:Example\, Inc."),
            ("ORG:Example\; Inc;Dept. of Silly Walks",
             "ORG:Example\; Inc;Dept. of Silly Walks"),
            ("ORG;VALUE=TEXT:Example", "ORG:Example"),
        )

        for item, result in items:
            prop = Property.parseText(item)
            test = prop.getText()
            self.assertEqual(test, result + "\r\n",
                             "Failed to parse and re-generate '%s'" % (item, ))
Esempio n. 8
0
    def testParseProperty(self):

        items = (
            ("ADR:", "ADR:;;;;;;"),
            ("ADR:;", "ADR:;;;;;;"),
            ("ADR:;;;;;;", "ADR:;;;;;;"),
            ("ADR:;;123 Main Street;Any Town;CA;91921-1234", "ADR:;;123 Main Street;Any Town;CA;91921-1234;"),
            ("ADR:;;;;;;USA", "ADR:;;;;;;USA"),
            ("ADR:POB1", "ADR:POB1;;;;;;"),
            ("ADR:;EXT", "ADR:;EXT;;;;;"),
            ("ADR;VALUE=TEXT:;;123 Main Street;Any Town;CA;91921-1234", "ADR:;;123 Main Street;Any Town;CA;91921-1234;"),
        )

        for item, result in items:
            prop = Property.parseText(item)
            test = prop.getText()
            self.assertEqual(test, result + "\r\n", "Failed to parse and re-generate '%s'" % (item,))
Esempio n. 9
0
    def testParseProperty(self):

        items = (
            ("N:", "N:;;;;"),
            ("N:;", "N:;;;;"),
            ("N:;;;;", "N:;;;;"),
            ("N:Cyrus;Daboo;;Dr.", "N:Cyrus;Daboo;;Dr.;"),
            ("N:;;;;PhD.", "N:;;;;PhD."),
            ("N:Cyrus", "N:Cyrus;;;;"),
            ("N:;Daboo", "N:;Daboo;;;"),
            ("N;VALUE=TEXT:Cyrus;Daboo;;Dr.", "N:Cyrus;Daboo;;Dr.;"),
        )

        for item, result in items:
            prop = Property.parseText(item)
            test = prop.getText()
            self.assertEqual(test, result + "\r\n", "Failed to parse and re-generate '%s'" % (item,))
Esempio n. 10
0
    def testParseProperty(self):

        items = (
            ("ADR:", "ADR:;;;;;;"),
            ("ADR:;", "ADR:;;;;;;"),
            ("ADR:;;;;;;", "ADR:;;;;;;"),
            ("ADR:;;123 Main Street;Any Town;CA;91921-1234", "ADR:;;123 Main Street;Any Town;CA;91921-1234;"),
            ("ADR:;;;;;;USA", "ADR:;;;;;;USA"),
            ("ADR:POB1", "ADR:POB1;;;;;;"),
            ("ADR:;EXT", "ADR:;EXT;;;;;"),
            ("ADR;VALUE=TEXT:;;123 Main Street;Any Town;CA;91921-1234", "ADR:;;123 Main Street;Any Town;CA;91921-1234;"),
        )

        for item, result in items:
            prop = Property.parseText(item)
            test = prop.getText()
            self.assertEqual(test, result + "\r\n", "Failed to parse and re-generate '%s'" % (item,))
Esempio n. 11
0
    def parseMultipleTextData(cls, ins):

        if isinstance(ins, str):
            ins = StringIO(ins)

        results = []

        card = cls(add_defaults=False)

        LOOK_FOR_VCARD = 0
        GET_PROPERTY = 1

        state = LOOK_FOR_VCARD

        # Get lines looking for start of calendar
        lines = [None, None]

        while readFoldedLine(ins, lines):

            line = lines[0]
            if state == LOOK_FOR_VCARD:

                # Look for start
                if line == card.getBeginDelimiter():
                    # Next state
                    state = GET_PROPERTY

                # Handle blank line
                elif len(line) == 0:
                    # Raise if requested, otherwise just ignore
                    if ParserContext.BLANK_LINES_IN_DATA == ParserContext.PARSER_RAISE:
                        raise InvalidData("vCard data has blank lines")

                # Unrecognized data
                else:
                    raise InvalidData("vCard data not recognized", line)

            elif state == GET_PROPERTY:

                # Look for end of object
                if line == card.getEndDelimiter():

                    # Finalise the current calendar
                    card.finalise()

                    # Validate some things
                    if not card.hasProperty("VERSION"):
                        raise InvalidData("vCard missing VERSION", "")

                    results.append(card)

                    # Change state
                    card = Card(add_defaults=False)
                    state = LOOK_FOR_VCARD

                # Blank line
                elif len(line) == 0:
                    # Raise if requested, otherwise just ignore
                    if ParserContext.BLANK_LINES_IN_DATA == ParserContext.PARSER_RAISE:
                        raise InvalidData("vCard data has blank lines")

                # Must be a property
                else:

                    # Parse parameter/value for top-level calendar item
                    prop = Property.parseText(line)

                    # Check for valid property
                    if not card.validProperty(prop):
                        raise InvalidData("Invalid property", str(prop))
                    else:
                        card.addProperty(prop)

        # Check for truncated data
        if state != LOOK_FOR_VCARD:
            raise InvalidData("vCard data not complete")

        return results
Esempio n. 12
0
    def testEquality(self):

        for data in TestProperty.test_data:
            prop1 = Property.parseText(data)
            prop2 = Property.parseText(data)
            self.assertEqual(prop1, prop2, "Failed equality: %s" % (data,))
Esempio n. 13
0
    def testParseGenerate(self):

        for data in TestProperty.test_data:
            prop = Property.parseText(data)
            propstr = str(prop)
            self.assertEqual(propstr[:-2], data, "Failed parse/generate: %s to %s" % (data, propstr))
Esempio n. 14
0
    def parseMultipleTextData(cls, ins):

        if isinstance(ins, str):
            ins = StringIO(ins)

        results = []

        card = cls(add_defaults=False)

        LOOK_FOR_VCARD = 0
        GET_PROPERTY = 1

        state = LOOK_FOR_VCARD

        # Get lines looking for start of calendar
        lines = [None, None]

        while readFoldedLine(ins, lines):

            line = lines[0]
            if state == LOOK_FOR_VCARD:

                # Look for start
                if line == card.getBeginDelimiter():
                    # Next state
                    state = GET_PROPERTY

                # Handle blank line
                elif len(line) == 0:
                    # Raise if requested, otherwise just ignore
                    if ParserContext.BLANK_LINES_IN_DATA == ParserContext.PARSER_RAISE:
                        raise InvalidData("vCard data has blank lines")

                # Unrecognized data
                else:
                    raise InvalidData("vCard data not recognized", line)

            elif state == GET_PROPERTY:

                # Look for end of object
                if line == card.getEndDelimiter():

                    # Finalise the current calendar
                    card.finalise()

                    # Validate some things
                    if not card.hasProperty("VERSION"):
                        raise InvalidData("vCard missing VERSION", "")

                    results.append(card)

                    # Change state
                    card = Card(add_defaults=False)
                    state = LOOK_FOR_VCARD

                # Blank line
                elif len(line) == 0:
                    # Raise if requested, otherwise just ignore
                    if ParserContext.BLANK_LINES_IN_DATA == ParserContext.PARSER_RAISE:
                        raise InvalidData("vCard data has blank lines")

                # Must be a property
                else:

                    # Parse parameter/value for top-level calendar item
                    prop = Property.parseText(line)

                    # Check for valid property
                    if not card.validProperty(prop):
                        raise InvalidData("Invalid property", str(prop))
                    else:
                        card.addProperty(prop)

        # Check for truncated data
        if state != LOOK_FOR_VCARD:
            raise InvalidData("vCard data not complete")

        return results
Esempio n. 15
0
    def testEquality(self):

        for data in TestProperty.test_data:
            prop1 = Property.parseText(data)
            prop2 = Property.parseText(data)
            self.assertEqual(prop1, prop2, "Failed equality: %s" % (data, ))