Skip to content

Instantly share code, notes, and snippets.

@juba
Created October 9, 2012 12:11
Show Gist options
  • Save juba/3858437 to your computer and use it in GitHub Desktop.
Save juba/3858437 to your computer and use it in GitHub Desktop.
Python script to convert pwsafe exportdb output to a KeePassX Xml file
#!/usr/bin/python
import csv
import datetime
import sys
import StringIO
from genshi.template import MarkupTemplate
class Bunch(object):
def __init__(self, **d):
self.__dict__ = d
TEMPLATE = """\
<!DOCTYPE KEEPASSX_DATABASE>
<database xmlns:py="http://genshi.edgewall.org/">
<group>
<title>Passwords</title>
<icon>1</icon>
<entry py:for="entry in entries">
<title>${entry.group}.${entry.title}</title>
<username>${entry.username}</username>
<password>${entry.password}</password>
<url></url>
<comment>${entry.notes}</comment>
<icon>1</icon>
<creation>${now}</creation>
<lastaccess>${now}</lastaccess>
<lastmod>${now}</lastmod>
<expire>Never</expire>
</entry>
</group>
</database>
"""
lines = [l for l in sys.stdin.readlines() if not l.startswith("#")][1:]
dialect = csv.Sniffer().sniff(''.join(lines))
entries = []
for (_id, group, title, username, password, notes) in csv.reader(lines, dialect=dialect):
entry = Bunch(group=group, username=username, title=title, password=password, notes=notes)
entries.append(entry)
now = datetime.datetime.now()
output = MarkupTemplate(TEMPLATE).generate(entries=entries, now=now).render()
print output
@juba
Copy link
Author

juba commented Oct 9, 2012

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment