From d10ed01d0e0a1afe8d66756d205db31b7dbfc583 Mon Sep 17 00:00:00 2001 From: Fredrik Unger Date: Fri, 6 Apr 2012 07:24:01 +0200 Subject: [PATCH] Adding initial xinclude for converting selected VCARD to docbook. Currently reading a VCARDs file, selecting a VCARD based on the commandline query, and converts it to docbook. Initial tests with a few VCARDs work. If an address has 2 telephone numbers, currently only the first is displayed. If there is only a telephone number and no address, it will not be displayed in this version. --- xinclude/__init__.py | 0 xinclude/contact.py | 136 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 136 insertions(+) create mode 100644 xinclude/__init__.py create mode 100755 xinclude/contact.py diff --git a/xinclude/__init__.py b/xinclude/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xinclude/contact.py b/xinclude/contact.py new file mode 100755 index 0000000..879fd66 --- /dev/null +++ b/xinclude/contact.py @@ -0,0 +1,136 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- + +from vobject import readComponents +import sys + +for arg in sys.argv[1:]: + al = arg.split("=") + if al[0] == "lang": + lang = al[1] + if al[0] == "xptr": + argument = al[1] + +(cards,query) = argument.split('?') +(key, name) = query.split(':') +with open(cards, 'r') as f: + card_data = f.read() +f.closed + +# Scan for the correct card +found = None +out = u'' +for card in readComponents(card_data): +# card.prettyPrint() + if key in card.contents.keys(): + if name.decode('utf-8') == card.contents[key][0].value[0]: + found = card + if key == 'firstname': + if name.decode('utf-8') == card.n.value.given: + found = card + if key == 'surname': + if name.decode('utf-8') == card.n.value.family: + found = card + +if not found: + print query+' failed in '+cards + exit(2) +# when card is found parse it to docbook. +pn = '' + +if 'n' in found.contents.keys(): + n = found.n.value + empty = n.prefix == '' and n.given == '' and \ + n.additional =='' and n.family =='' and n.suffix == '' + if not empty: + pn += '' + if n.prefix != '': + pn += ''+n.prefix+'' + if n.given != '': + pn += ''+n.given+'' + if n.additional != '': + pn += ''+n.additional+'' + if n.family != '': + pn += ''+n.family+'' + if n.suffix != '': + pn += ''+n.suffix+'' + pn += '' + +ad = '' +if 'adr' in found.contents.keys(): + for a, t in zip(found.contents['adr'],found.contents['tel']): + ad += '\n
' + if a.value.street != '': + ad += ''+a.value.street+'' + if a.value.code != '': + ad += ''+a.value.code+'' + if a.value.city != '': + ad += ''+a.value.city+'' + if a.value.country != '': + ad += ''+a.value.country+'' + if t.value != '': + ad += ''+t.value+'' + ad += '
' + +o = '' +if 'org' in found.contents.keys(): + o = ''' + + '''+found.org.value[0]+''' + '''+ad+''' + ''' + + +url = '' +if 'url' in found.contents.keys(): + url += '' +geo = '' +if 'geo' in found.contents.keys(): + (lat,lon) = found.geo.value.split(';') + geo += 'geo:'+lat+','+lon+'' + +# Turn off email for now +email = '' +#if 'email' in found.contents.keys(): +# email += ''+found.email.value+'' + +if empty: + content = pn+o+url+geo+email +else: + if o == '': + content = ''+pn+ad+url+geo+email+'' + else: + content = ''+pn+''+o+''+url+geo+email+ \ + '' +out = ''' + + '''+content+''' + +''' + +ex =''' +
+ Street + Postcode City + Country + +1 123 456 789 +
+ + Occupation + + Organization +
+ Street + Postcode City + Country + +1 123 456 789 +
+
+
+ + +''' + +sys.stdout.write(out.encode('utf-8')) -- 2.30.2