Last active
August 13, 2023 00:02
-
-
Save tmalsburg/9747104 to your computer and use it in GitHub Desktop.
Revisions
-
tmalsburg revised this gist
Feb 2, 2015 . 1 changed file with 2 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -31,8 +31,9 @@ UTF8Writer = codecs.getwriter('utf8') sys.stdout = UTF8Writer(sys.stdout) template = " :%s: %s%s" prefix = "*" indentation = " " def flatten(l): '''Flatten a arbitrarily nested lists and return the result as a single list. -
tmalsburg revised this gist
Mar 29, 2014 . 1 changed file with 5 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -7,6 +7,11 @@ # org-contacts. There is one mandatory argument: the name of the # vCard file. The result is printed to standard out. # Usage: # # python vcard2org-contacts.py contacts.vcf > contacts.org # # Issues: # # The LABEL property in the Forrest Gump example (from Wikipedia) is -
tmalsburg revised this gist
Mar 28, 2014 . 1 changed file with 3 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -3,9 +3,9 @@ # Written by Titus von der Malsburg <[email protected]>, March 2014. # This is a simple script for converting vCard files to # org-contacts. There is one mandatory argument: the name of the # vCard file. The result is printed to standard out. # Issues: # @@ -43,7 +43,7 @@ def flatten(l): if __name__=="__main__": if len(sys.argv) != 2: raise ValueError("Please specify exactly one vCard file.") fname = sys.argv[1] stream = io.open(fname, "r", encoding="utf-8") -
tmalsburg revised this gist
Mar 25, 2014 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
Empty file. -
tmalsburg revised this gist
Mar 25, 2014 . 1 changed file with 11 additions and 5 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -7,6 +7,16 @@ # org-contacts. There is one mandatory argument: the name of the # vcard file. The result is printed to standard out. # Issues: # # The LABEL property in the Forrest Gump example (from Wikipedia) is # not handled correctly: # # LABEL;TYPE=WORK:100 Waters Edge\nBaytown, LA 30314\nUnited States of America # # The part after the comma is missing. That seems to be due to a bug # in vobject. import sys import io import dateutil.parser @@ -16,7 +26,7 @@ UTF8Writer = codecs.getwriter('utf8') sys.stdout = UTF8Writer(sys.stdout) template = " :%s: %s%s" prefix = "*" def flatten(l): @@ -39,9 +49,6 @@ def flatten(l): stream = io.open(fname, "r", encoding="utf-8") vcards = vobject.readComponents(stream) for vcard in vcards: note = "" @@ -88,7 +95,6 @@ def flatten(l): if p.name == "TEL": name = "PHONE" # Collect type attributes: attribs = ", ".join(p.params.get("TYPE", [])) if attribs: -
tmalsburg revised this gist
Mar 25, 2014 . 1 changed file with 3 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -9,6 +9,7 @@ import sys import io import dateutil.parser import vobject import codecs @@ -81,7 +82,8 @@ def flatten(l): name = "ADDRESS" if p.name == "REV": value = dateutil.parser.parse(p.value) value = value.strftime("[%Y-%m-%d %a %H:%M]") if p.name == "TEL": name = "PHONE" -
tmalsburg revised this gist
Mar 25, 2014 . 1 changed file with 1 addition and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -78,15 +78,14 @@ def flatten(l): value = (p.value.street, p.value.code + " " + p.value.city, p.value.region, p.value.country, p.value.extended, p.value.box) value = ", ".join([x for x in value if x!='']) name = "ADDRESS" if p.name == "REV": value = "[%s]" % p.value.split("T")[0] if p.name == "TEL": name = "PHONE" # Collect type attributes: attribs = ", ".join(p.params.get("TYPE", [])) -
tmalsburg created this gist
Mar 24, 2014 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,105 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- # Written by Titus von der Malsburg <[email protected]>, March 2014. # This is a simple script for converting vcard files to # org-contacts. There is one mandatory argument: the name of the # vcard file. The result is printed to standard out. import sys import io import vobject import codecs UTF8Writer = codecs.getwriter('utf8') sys.stdout = UTF8Writer(sys.stdout) indentation = " " prefix = "*" def flatten(l): '''Flatten a arbitrarily nested lists and return the result as a single list. ''' ret = [] for i in l: if isinstance(i, list) or isinstance(i, tuple): ret.extend(flatten(i)) else: ret.append(i) return ret if __name__=="__main__": if len(sys.argv) != 2: raise ValueError("Please specify exactly one vcard file.") fname = sys.argv[1] stream = io.open(fname, "r", encoding="utf-8") vcards = vobject.readComponents(stream) template = indentation + ":%s: %s%s" for vcard in vcards: note = "" print "%s %s" % (prefix, vcard.fn.value) print indentation + ":PROPERTIES:" for p in vcard.getChildren(): if p.name in ("VERSION", "PRODID", "FN") or p.name.startswith("X-"): continue if p.name == "NOTE": note = p.value continue name = p.name value = p.value # Special treatment for some fields: if p.name == "ORG": try: value = ", ".join(flatten(p.value)) except: print p.value if p.name == "N": value = "%s;%s;%s;%s;%s" % (p.value.family, p.value.given, p.value.additional, p.value.prefix, p.value.suffix) if p.name == "ADR": # TODO Make the formatting sensitive to X-ABADR: value = (p.value.street, p.value.code + " " + p.value.city, p.value.region, p.value.country, p.value.extended, p.value.box) value = ", ".join([x for x in value if x!='']) if p.name == "REV": value = "[%s]" % p.value.split("T")[0] if p.name == "TEL": name = "PHONE" if p.name == "ADR": name = "ADDRESS" # Collect type attributes: attribs = ", ".join(p.params.get("TYPE", [])) if attribs: attribs = " (%s)" % attribs # Make sure that there are no newline chars left as that would # break org's property format: value = value.replace("\n", ", ") print template % (name, value, attribs) print indentation + ":END:" if note: print note