Python icalender在ORGANIZER中的变音符号上窒息

时间:2013-04-17 09:53:51

标签: python character-encoding icalendar

我正在尝试修复我从部落获得的文件(导入后的两个小时后重复发生的事件)。所以我写了下面的脚本,它在谣言文件的“ORGANIZER”标签中对变音符号进行扼流:

#!/usr/bin/python

import sys
from icalendar import Calendar, Event
from datetime import timedelta

# reccurring events are off by two hours
reccurrence_timedelta=timedelta(hours=2)

# default
ical_file="test.ical"

# input file 
try:
    ical_file=sys.argv[1]
except IndexError:
    pass

cal = Calendar.from_ical(open(ical_file,'rb').read())
for component in cal.walk():
    if component.name == "VEVENT":
        try:
            component['rrule']
            dtstart = component.decoded('dtstart')
            dtend = component.decoded('dtend')
            new_dtstart=dtstart + reccurrence_timedelta
            new_dtend =dtend + reccurrence_timedelta
            new_cal=Calendar()
            new_cal.add('dtstart',new_dtstart)
            new_cal.add('dtend',new_dtend)
            component['dtstart']=new_cal['dtstart']
            component['dtend']=new_cal['dtend']
        except KeyError:
            pass

new_ics_filename = "corrected_" + ical_file
newics_file = open(new_ics_filename, 'wb')
cal_as_ical=cal.to_ical()
newics_file.write(cal_as_ical)
newics_file.close()

以utf-8编码的ical文件是

BEGIN:VCALENDAR
VERSION:2.0
X-WR-CALNAME:Kalender von acme\, admin
PRODID:-//The Horde Project//Horde_iCalendar Library\, Horde 3.3.5//EN
METHOD:PUBLISH
BEGIN:VEVENT
DTSTART:20130416T100000Z
DTEND:20130416T110000Z
DTSTAMP:20130416T092616Z
UID:20130416112341.10064jz0k4j7uem8@acmenet.de
CREATED:20130416T092341Z
LAST-MODIFIED:20130416T092341Z
SUMMARY:wichtiger termin 1
ORGANIZER;CN="acme, ädmin":mailto:adm-acme@mydomain.de
LOCATION:im büro
CLASS:PUBLIC
STATUS:CONFIRMED
TRANSP:OPAQUE
END:VEVENT
END:VCALENDAR

,错误是

No handlers could be found for logger "icalendar"
Traceback (most recent call last):
   File "./fix_horde_ical.py", line 48, in <module>
    cal_as_ical=cal.to_ical()
  File "/usr/local/lib/python2.7/dist-packages/icalendar-3.3-py2.7.egg/icalendar    /cal.py", line 485, in to_ical
    return self.content_lines().to_ical()
  File "/usr/local/lib/python2.7/dist-packages/icalendar-3.3-py2.7.egg/icalendar/cal.py", line 480, in content_lines
    contentlines.append(Contentline.from_parts((name, params, values)))
  File "/usr/local/lib/python2.7/dist-packages/icalendar-3.3-py2.7.egg/icalendar  /parser.py", line 510, in from_parts
    % (name, params, values))
ValueError: Property: 'ORGANIZER' Wrong values "Parameters({'CN': 'acme, \xc3\xa4dmin'})" or "'mailto:adm-acme@mydomain.de'"

“LOCATION”标签中的变音符号似乎没有问题,但“ORGANIZER”与变音符号有问题。

从错误中可以看出,我使用的是python 2.7和icalendar 3.3。我是否需要遍历每个ORGANIZER并删除非ascii字符?或者是否有更通用,更简单的方法?

2 个答案:

答案 0 :(得分:1)

我想我修好了:

--- parser.py.orig  2013-04-17 21:29:47.253235481 +0200
+++ parser.py   2013-04-17 22:04:44.323634292 +0200
@@ -296,7 +296,7 @@
     items = self.items()
     items.sort()  # To make doctests work
     for key, value in items:
-            value = paramVal(value)
+            value = paramVal(unicode(value, DEFAULT_ENCODING))
         result.append('%s=%s' % (key.upper(),
                                  value.encode(DEFAULT_ENCODING)))
     return ';'.join(result)

另见github上的问题: https://github.com/collective/icalendar/issues/101?source=c

答案 1 :(得分:0)

这可能是由于icalendar中的错误而发生的。

据我所知,icalendar最近刚刚被重构为处理unicode中的东西。它有一个编码测试套件,但它显然不是很全面。具体而言,它不会测试ORGANIZER;CN样式中指定的编码(请参阅https://github.com/collective/icalendar/blob/master/src/icalendar/tests/encoding.ics)。

我建议filing an issue,看看它是如何解决的。