示例#1
0
    def to_str(self):
        resource_class = self.database.get_resource_class(self.format)

        if self.version is None:
            lines = ['format:%s\n' % self.format]
        else:
            lines = ['format;version=%s:%s\n' % (self.version, self.format)]
        # Properties are to be sorted by alphabetical order
        properties = self.properties
        names = properties.keys()
        names.sort()

        # Properties
        for name in names:
            property = properties[name]

            # Get the field
            field = resource_class.get_field(name)
            if field is None:
                msg = 'unexpected field "{0}" in resource "{1}" (format "{2}")'
                msg = msg.format(name, self.key, self.format)
                if resource_class.fields_soft:
                    log_warning(msg, domain='itools.database')
                    continue
                raise ValueError, msg

            datatype = field.datatype
            params_schema = field.parameters_schema
            is_empty = datatype.is_empty
            p_type = type(property)
            if p_type is dict:
                languages = property.keys()
                languages.sort()
                lines += [
                    property_to_str(name, property[x], datatype, params_schema)
                    for x in languages if not is_empty(property[x].value)
                ]
            elif p_type is list:
                lines += [
                    property_to_str(name, x, datatype, params_schema)
                    for x in property if not is_empty(x.value)
                ]
            elif property.value is None:
                pass
            elif not is_empty(property.value):
                lines.append(
                    property_to_str(name, property, datatype, params_schema))

        return ''.join(lines)
示例#2
0
文件: agenda.py 项目: staverne/ikaaro
    def to_ical(self, context):
        """Serialize as an ical file, generally named .ics
        """

        lines = ['BEGIN:VCALENDAR\n',
                'VERSION:2.0\n',
                'PRODID:-//itaapy.com/NONSGML ikaaro icalendar V1.0//EN\n']

        # Calendar components
        for event in self.search_resources(cls=Event):
            lines.append('BEGIN:VEVENT\n')
            for ikaaro_name, ics_name in ikaaro_to_ics:
                property = event.get_property(ikaaro_name)
                lang = property.get_parameter('lang')
                if lang:
                    property = Property(property.value, LANGUAGE=lang)
                    p_schema = {'LANGUAGE': String(multiple=False)}
                else:
                    p_schema = None
                datatype = event.get_field(ikaaro_name).datatype
                line = property_to_str(ics_name, property, datatype, p_schema)
                lines.append(line)

            lines.append('END:VEVENT\n')
        lines.append('END:VCALENDAR\n')

        return ''.join(lines)
示例#3
0
    def to_str(self):
        resource_class = self.database.get_resource_class(self.format)

        if self.version is None:
            lines = ['format:%s\n' % self.format]
        else:
            lines = ['format;version=%s:%s\n' % (self.version, self.format)]
        # Properties are to be sorted by alphabetical order
        properties = self.properties
        names = properties.keys()
        names.sort()

        # Properties
        for name in names:
            property = properties[name]

            # Get the field
            field = resource_class.get_field(name)
            if field is None:
                msg = 'unexpected field "%s"' % name
                if resource_class.fields_soft:
                    log_warning(msg, domain='itools.database')
                    continue
                raise ValueError, msg

            datatype = field.datatype
            params_schema = field.parameters_schema
            is_empty = datatype.is_empty
            p_type = type(property)
            if p_type is dict:
                languages = property.keys()
                languages.sort()
                lines += [
                    property_to_str(name, property[x], datatype, params_schema)
                    for x in languages if not is_empty(property[x].value) ]
            elif p_type is list:
                lines += [
                    property_to_str(name, x, datatype, params_schema)
                    for x in property if not is_empty(x.value) ]
            elif property.value is None:
                pass
            elif not is_empty(property.value):
                lines.append(
                    property_to_str(name, property, datatype, params_schema))

        return ''.join(lines)
示例#4
0
    def encode_property(self, name, property_values, encoding='utf-8'):
        if type(property_values) is not list:
            property_values = [property_values]

        datatype = self.get_record_datatype(name)
        return [
            property_to_str(name, x, datatype, {}, encoding)
            for x in property_values ]