Re: egroupware et import calendrier iCal

トップ ページ

このメッセージに返信
著者: Jerome Kieffer
日付:  
To: Jerome Kieffer
CC: guilde
題目: Re: egroupware et import calendrier iCal
Comme on est jamais mieux servi que par soi même ...

Un bout de code «gruick» à souhait, qui transforme un export OutLook
(format CSV) en un iCal importable dans eGroupWare.

Ca marche chez moi mais rien n'est garanti, par contre cela peut
donner des idées, surtout qu'il y a rien sur le grand 'Ternet.
A++

--
Jérôme KIEFFER : http://www.terre-adelie.org
À vélo, prendre une rue à contre-sens est moins dangeureux
que prendre un boulevard dans le sens légal. À qui la faute ?
#!/usr/bin/python
import sys,os,time
from datetime import datetime
from icalendar import Calendar, Event,UTC, vCalAddress, vText

def latin1_to_ascii (unicrap):
    """This takes a UNICODE string and replaces Latin-1 characters with
        something equivalent in 7-bit ASCII. It returns a plain ASCII string. 
        This function makes a best effort to convert Latin-1 characters into 
        ASCII equivalents. It does not just strip out the Latin-1 characters.
        All characters in the standard 7-bit ASCII range are preserved. 
        In the 8th bit range all the Latin-1 accented letters are converted 
        to unaccented equivalents. Most symbol characters are converted to 
        something meaningful. Anything not converted is deleted.
    """
    xlate={0xc0:'A', 0xc1:'A', 0xc2:'A', 0xc3:'A', 0xc4:'A', 0xc5:'A',
        0xc6:'Ae', 0xc7:'C',
        0xc8:'E', 0xc9:'E', 0xca:'E', 0xcb:'E',
        0xcc:'I', 0xcd:'I', 0xce:'I', 0xcf:'I',
        0xd0:'Th', 0xd1:'N',
        0xd2:'O', 0xd3:'O', 0xd4:'O', 0xd5:'O', 0xd6:'O', 0xd8:'O',
        0xd9:'U', 0xda:'U', 0xdb:'U', 0xdc:'U',
        0xdd:'Y', 0xde:'th', 0xdf:'ss',
        0xe0:'a', 0xe1:'a', 0xe2:'a', 0xe3:'a', 0xe4:'a', 0xe5:'a',
        0xe6:'ae', 0xe7:'c',
        0xe8:'e', 0xe9:'e', 0xea:'e', 0xeb:'e',
        0xec:'i', 0xed:'i', 0xee:'i', 0xef:'i',
        0xf0:'th', 0xf1:'n',
        0xf2:'o', 0xf3:'o', 0xf4:'o', 0xf5:'o', 0xf6:'o', 0xf8:'o',
        0xf9:'u', 0xfa:'u', 0xfb:'u', 0xfc:'u',
        0xfd:'y', 0xfe:'th', 0xff:'y',
        0xa1:'!', 0xa2:'{cent}', 0xa3:'{pound}', 0xa4:'{currency}',
        0xa5:'{yen}', 0xa6:'|', 0xa7:'{section}', 0xa8:'{umlaut}',
        0xa9:'{C}', 0xaa:'{^a}', 0xab:'<<', 0xac:'{not}',
        0xad:'-', 0xae:'{R}', 0xaf:'_', 0xb0:'{degrees}',
        0xb1:'{+/-}', 0xb2:'{^2}', 0xb3:'{^3}', 0xb4:"'",
        0xb5:'{micro}', 0xb6:'{paragraph}', 0xb7:'*', 0xb8:'{cedilla}',
        0xb9:'{^1}', 0xba:'{^o}', 0xbb:'>>', 
        0xbc:'{1/4}', 0xbd:'{1/2}', 0xbe:'{3/4}', 0xbf:'?',
        0xd7:'*', 0xf7:'/'
        }


    r=[]
    for i in unicrap:
        if xlate.has_key(ord(i)):
            r.append(xlate[ord(i)])
        elif ord(i) >= 0x80:
            pass
        else:
            r.append(str(i))
    return "".join(r)
a=open(sys.argv[1]).read()
head,body=a.split('\r\n',1)
head=[ i.replace('"','') for i in head.split(",")]
lenlin=len(head)
data=[]
ligne=[]
mot=""
opened=False
for i in body:
    if not opened and i==",":
        ligne.append(mot)
        mot=""
    elif not opened and i =="\n":
        ligne.append(mot)
        mot=""
        if len(ligne)==lenlin:
            data.append(ligne)
            ligne=[]
    elif i=='"':
        opened=not opened

    
    else:
        mot+=i


cal = Calendar()
cal.add('prodid', '-//My calendar product//mxm.dk//')
cal.add('version', '2.0')

CurDate=time.strftime("%Y%m%dT%H%M%S",time.localtime())

for line in data: 
    event = Event()
    print line[0]
    evt=time.strftime("%Y%m%dT%H%M%S",(time.strptime(line[1]+"T"+line[2],"%d/%m/%YT%H:%M:%S")))
    if CurDate>evt:
        print "\t\t Skipped !!!"
        continue
    event.add('summary', latin1_to_ascii(line[0].strip()))
    event.add('dtstart', datetime(*time.strptime(line[1]+"T"+line[2],"%d/%m/%YT%H:%M:%S")[:-2]+(None,)))
    event.add('dtend', datetime(*time.strptime(line[3]+"T"+line[4],"%d/%m/%YT%H:%M:%S")[:-2]+(None,)))
    event.add('priority', 5)
    event.add("sequence",0)
    organizer = vCalAddress()
    organizer.params['cn']=line[9]
    event.add('organizer',organizer,encode=0)
    for part in line[10].split(";"):
        attendee = vCalAddress()
        attendee.params['cn'] = vText(part)
        attendee.params['ROLE'] = vText('REQ-PARTICIPANT')
        event.add('attendee', attendee, encode=0)


    for part in line[11].split(";"):
        attendee = vCalAddress()
        attendee.params['cn'] = vText(part)
        attendee.params['ROLE'] = vText('OPT-PARTICIPANT')
        event.add('attendee', attendee, encode=0)

    
    event.add("description",latin1_to_ascii(line[16].strip()))
    cal.add_component(event)



if len(sys.argv)>2:
    out=sys.argv[3]
else:
    out=os.path.splitext(sys.argv[1])[0]+".ics"


f = open(out, 'wb')
f.write(cal.as_string())
f.close()