Exemplo n.º 1
0
    def testParseValue(self):

        items = (
            ("", "", 1),
            ("Example", "Example", 1),
            ("Example1,Example2", "Example1,Example2", 2),
        )

        for item, result, count in items:
            req = MultiValue(Value.VALUETYPE_TEXT)
            req.parse(item, "icalendar")
            test = req.getText()
            self.assertEqual(test, result, "Failed to parse and re-generate '%s'" % (item,))
            self.assertEqual(len(req.mValues), count, "Failed to parse and re-generate '%s'" % (item,))
Exemplo n.º 2
0
    def testParseValue(self):

        items = (
            ("", "", 1),
            ("Example", "Example", 1),
            ("Example1,Example2", "Example1,Example2", 2),
        )

        for item, result, count in items:
            req = MultiValue(Value.VALUETYPE_TEXT)
            req.parse(item, "icalendar")
            test = req.getText()
            self.assertEqual(test, result,
                             "Failed to parse and re-generate '%s'" % (item, ))
            self.assertEqual(len(req.mValues), count,
                             "Failed to parse and re-generate '%s'" % (item, ))
Exemplo n.º 3
0
    def _init_attr_value_periodlist(self, periodlist):
        # Value
        self.mValue = MultiValue(Value.VALUETYPE_PERIOD)
        for period in periodlist:
            self.mValue.addValue(PeriodValue(period))

        # Parameters
        self.setupValueParameter()
Exemplo n.º 4
0
    def parseText(cls, data):
        """
        Parse the text format data and return a L{Property}

        @param data: text data
        @type data: C{str}
        """

        try:
            prop = cls()

            # Look for parameter or value delimiter
            prop_name, txt = stringutils.strduptokenstr(data, ";:")
            if not prop_name:
                raise InvalidProperty("Invalid property: empty name", data)

            # Get the name
            if prop.sUsesGroup:
                # Check for group prefix
                splits = prop_name.split(".", 1)
                if len(splits) == 2:
                    # We have both group and name
                    prop.mGroup = splits[0]
                    prop.mName = splits[1]
                else:
                    # We have the name
                    prop.mName = prop_name
            else:
                prop.mName = prop_name

            # Get the parameters
            txt = prop.parseTextParameters(txt, data)

            # Tidy first
            prop.mValue = None

            # Get value type from property name
            value_type = prop.determineValueType()

            # Check for multivalued
            if prop.mName.upper() in prop.sMultiValues:
                prop.mValue = MultiValue(value_type)
            else:
                # Create the type
                prop.mValue = Value.createFromType(value_type)

            # Now parse the data
            prop.mValue.parse(txt, prop.sVariant)

            prop._postCreateValue(value_type)

            return prop

        except Exception as e:
            raise InvalidProperty("Invalid property: '{}'".format(e), data)
Exemplo n.º 5
0
    def testSetValue(self):

        req = MultiValue(Value.VALUETYPE_TEXT)
        req.parse("Example1, Example2", "icalendar")
        req.setValue((
            "Example3",
            "Example4",
        ))
        test = req.getText()
        self.assertEqual(test, "Example3,Example4")
Exemplo n.º 6
0
    def testSetValue(self):

        req = MultiValue(Value.VALUETYPE_TEXT)
        req.parse("Example1, Example2", "icalendar")
        req.setValue(("Example3", "Example4",))
        test = req.getText()
        self.assertEqual(test, "Example3,Example4")
Exemplo n.º 7
0
    def _init_attr_value_datetimelist(self, dtl):
        # Value
        date_only = (len(dtl) > 0) and dtl[0].isDateOnly()
        if date_only:
            self.mValue = MultiValue(Value.VALUETYPE_DATE)
        else:
            self.mValue = MultiValue(Value.VALUETYPE_DATETIME)

        for dt in dtl:
            self.mValue.addValue(DateTimeValue(dt))

        # Parameters
        self.setupValueParameter()

        # Look for timezone
        if ((len(dtl) > 0) and not dtl[0].isDateOnly() and dtl[0].local()):
            if definitions.cICalParameter_TZID in self.mParameters:
                del self.mParameters[definitions.cICalParameter_TZID]
            self.mParameters.setdefault(
                definitions.cICalParameter_TZID, []).append(
                    Parameter(name=definitions.cICalParameter_TZID,
                              value=dtl[0].getTimezoneID()))
Exemplo n.º 8
0
    def parseJSON(cls, jobject):
        """
        Parse a JSON property of the form:

        [name, attrs, type, value1, value2, ...]

        @param jobject: a JSON array
        @type jobject: C{list}
        """

        try:
            prop = cls()

            # Get the name
            prop.mName = jobject[0].encode("utf-8").upper()

            # Get the parameters
            if jobject[1]:
                for name, value in jobject[1].items():
                    # Now add parameter value
                    name = name.upper()
                    attrvalue = Parameter(name=name.encode("utf-8"), value=value.encode("utf-8"))
                    prop.mParameters.setdefault(name, []).append(attrvalue)

            # Get default value type from property name and insert a VALUE parameter if current value type is not default
            value_type = cls.sValueTypeMap.get(jobject[2].upper(), Value.VALUETYPE_UNKNOWN)
            default_type = cls.sDefaultValueTypeMap.get(prop.mName.upper(), Value.VALUETYPE_UNKNOWN)
            if default_type != value_type:
                attrvalue = Parameter(name=cls.sValue, value=jobject[2].encode("utf-8").upper())
                prop.mParameters.setdefault(cls.sValue, []).append(attrvalue)

            # Get value type from property name
            value_type = prop.determineValueType()

            # Check for multivalued
            values = jobject[3:]
            if prop.mName.upper() in cls.sMultiValues:
                prop.mValue = MultiValue(value_type)
                prop.mValue.parseJSONValue(values)
            else:
                # Create the type
                prop.mValue = Value.createFromType(value_type)
                prop.mValue.parseJSONValue(values[0])

            # Special post-create for some types
            prop._postCreateValue(value_type)

            return prop

        except Exception as e:
            raise InvalidProperty("Invalid property: '{}'".format(e), jobject)
Exemplo n.º 9
0
    def createValue(self, data):
        # Tidy first
        self.mValue = None

        # Get value type from property name
        value_type = self.determineValueType()

        # Check for multivalued
        if self.mName.upper() in self.sMultiValues:
            self.mValue = MultiValue(value_type)
        else:
            # Create the type
            self.mValue = Value.createFromType(value_type)

        # Now parse the data
        try:
            self.mValue.parse(data, self.sVariant)
        except ValueError as e:
            raise InvalidProperty("Invalid property value: '{}'".format(e), data)

        self._postCreateValue(value_type)
Exemplo n.º 10
0
    def setValue(self, value):
        # Tidy first
        self.mValue = None

        # Get value type from property name
        value_type = self.sDefaultValueTypeMap.get(self.mName.upper(), Value.VALUETYPE_TEXT)

        # Check whether custom value is set
        if self.sValue in self.mParameters:
            value_type = self.sValueTypeMap.get(self.getParameterValue(self.sValue), value_type)

        # Check for multivalued
        if self.mName.upper() in self.sMultiValues:
            self.mValue = MultiValue(value_type)
        else:
            # Create the type
            self.mValue = Value.createFromType(value_type)

        self.mValue.setValue(value)

        # Special post-create for some types
        self._postCreateValue(value_type)