Adding initial xinclude for converting selected VCARD to docbook.
authorFredrik Unger <fred@tree.se>
Fri, 6 Apr 2012 05:24:01 +0000 (07:24 +0200)
committerFredrik Unger <fred@tree.se>
Fri, 6 Apr 2012 05:24:01 +0000 (07:24 +0200)
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 [new file with mode: 0644]
xinclude/contact.py [new file with mode: 0755]

diff --git a/xinclude/__init__.py b/xinclude/__init__.py
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/xinclude/contact.py b/xinclude/contact.py
new file mode 100755 (executable)
index 0000000..879fd66
--- /dev/null
@@ -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 += '<personname>'
+    if n.prefix != '':
+      pn += '<honorific>'+n.prefix+'</honorific>'
+    if n.given != '':
+      pn += '<firstname>'+n.given+'</firstname>'
+    if n.additional != '':
+      pn += '<othername>'+n.additional+'</othername>'
+    if n.family != '':
+      pn += '<surname>'+n.family+'</surname>'
+    if n.suffix != '':
+      pn += '<lineage>'+n.suffix+'</lineage>'
+    pn += '</personname>'
+
+ad = ''
+if 'adr' in found.contents.keys():
+  for a, t in zip(found.contents['adr'],found.contents['tel']):
+    ad += '\n<address type="'+a.type_param+'">'
+    if a.value.street != '':
+      ad += '<street>'+a.value.street+'</street>'
+    if a.value.code != '':
+      ad += '<postcode>'+a.value.code+'</postcode>'
+    if a.value.city != '':
+      ad += '<city>'+a.value.city+'</city>'
+    if a.value.country != '':
+      ad += '<country>'+a.value.country+'</country>'
+    if t.value != '':
+      ad += '<phone>'+t.value+'</phone>'
+    ad += '</address>'
+
+o = ''
+if 'org' in found.contents.keys():
+  o = '''
+      <org>
+        <orgname>'''+found.org.value[0]+'''</orgname>
+        '''+ad+'''
+        </org>'''
+
+
+url = ''
+if 'url' in found.contents.keys():
+  url += '<uri type="website"><link xlink:href="'+found.url.value+'"/></uri>'
+geo = ''
+if 'geo' in found.contents.keys():
+  (lat,lon) = found.geo.value.split(';')
+  geo += '<uri type="location"><link xlink:href="http://www.openstreetmap.org/'+ \
+      '?mlat='+lat+'&amp;mlon='+lon+'&amp;zoom=18&amp;layers=M">geo:'+lat+','+lon+'</link></uri>'
+
+# Turn off email for now
+email = ''
+#if 'email' in found.contents.keys():
+#  email += '<email>'+found.email.value+'</email>'
+
+if empty:
+  content = pn+o+url+geo+email
+else:
+  if o == '':
+    content = '<person>'+pn+ad+url+geo+email+'</person>'
+  else:
+    content = '<person>'+pn+'<affiliation>'+o+'</affiliation>'+url+geo+email+ \
+        '</person>'
+out = '''
+<para xmlns="http://docbook.org/ns/docbook"
+      xmlns:xlink="http://www.w3.org/1999/xlink">
+  '''+content+'''
+</para>
+'''
+
+ex ='''
+    <address>
+      <street>Street</street>
+      <postcode>Postcode</postcode> <city>City</city>
+      <country>Country</country>
+      <phone>+1 123 456 789</phone>
+    </address>
+    <affiliation>
+      <jobtitle>Occupation</jobtitle>
+      <org>
+        <orgname>Organization</orgname>
+        <address>
+          <street>Street</street>
+          <postcode>Postcode</postcode> <city>City</city>
+          <country>Country</country>
+          <phone>+1 123 456 789</phone>
+        </address>
+      </org>
+    </affiliation>
+  </person>
+</para>
+'''
+
+sys.stdout.write(out.encode('utf-8'))