torsten: utils/testing cleanvcard.py,NONE,1.1

cvs at intevation.de cvs at intevation.de
Tue May 2 17:48:38 CEST 2006


Author: torsten

Update of /kolabrepository/utils/testing
In directory doto:/tmp/cvs-serv21058

Added Files:
	cleanvcard.py 
Log Message:
Added a small python script for removing CHARSET and ENCODING
information from vcards. This is a small and cheap workaround for
issue1155 (vcal export cannot be imported: umlauts break).


--- NEW FILE: cleanvcard.py ---
#!/usr/bin/python
"""\n
This script is a small workaround for importing 2.1 vcards with
included umlauts. It will remove all CHARSET and ENCODING information
from the vcard. The original file will be stored as <filename.orig>

Usage: cleanvcard file 

Options:
    -h / --help     Print this message and exit.

"""
# 20060403 Torsten Irlaender <torsten.irlaender at intevation.de>
#
# This program is free software under the GNU GPL (>=v2)
# Read the file COPYING coming with the software for details.


import sys 
import getopt
import shutil
import re
import os

def usage(code, msg=''):
    print >> sys.stderr, __doc__
    if msg:
        print >> sys.stderr, msg
    sys.exit(code)

def main():
    try:
        opts, args = getopt.getopt(sys.argv[1:], 'h',
        ['help'])
   
    except getopt.error, msg:
        usage(1, msg)
    
    for opt, arg in opts:
        if opt in ('-h', '--help'):
            usage(0)
    if len(args) < 1:
        usage(1)

    # open the vcard and make a backupfile
    filename = args[0]
    backupfilename = filename+".orig"
    try:
        shutil.copyfile(filename,backupfilename)
    except:
        print'Could not open file:',filename
        sys.exit(1)
    
    parsefile(filename)
    

def parsefile(filename):
    
    # opening the vcard 
    f = open(filename)
    f2 = open(filename+".tmp","w+")
    lines = f.readlines()
    
    # Parse each line and get rid if charset and encoding
    for line in lines:
        line = parseline(line)
        f2.write(line)
    f.close()
    f2.close()
    
    # after finishing parsing we move modfied vcard into the original
    # one
    os.rename(filename+".tmp",filename)

def parseline(line):
    search = re.compile('(CHARSET=|ENCODING=)')
    dummy = line.split(':')
    paras = dummy[0]
    if(len(dummy)>1):
        value = dummy[1]
    tokens = paras.split(';')
    i=0
    line=""
    for a in tokens:
        if not re.match(search,a):
            if (i > 0):
                line+=";"
            line += a
        i+=1
    if(len(dummy)>1):
        line+=(":"+value)
    return line

if __name__ == '__main__':
    main()





More information about the commits mailing list