Skip to content

Instantly share code, notes, and snippets.

@roorz
Created July 24, 2012 18:53
Show Gist options
  • Select an option

  • Save roorz/3171833 to your computer and use it in GitHub Desktop.

Select an option

Save roorz/3171833 to your computer and use it in GitHub Desktop.
Kee Pass to Safe In Cloud XML
from StringIO import StringIO
from lxml import etree
import os
import sys
if (len(sys.argv) > 1):
srcFile = os.path.abspath(sys.argv[1])
if (not os.path.exists(srcFile)):
print "File '" + srcFile + "' not found."
sys.exit(0)
else:
dstFile = os.path.splitext(srcFile)[0] + ".safeincloud.xml"
xslt_root = etree.XML('''\
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/database">
<database><lf/>
<xsl:apply-templates select="group" />
</database>
</xsl:template>
<xsl:template match="group">
<xsl:if test="title != 'Backup'">
<xsl:apply-templates select="entry" />
</xsl:if>
<xsl:apply-templates select="group" />
</xsl:template>
<xsl:template match="entry">
<card>
<xsl:attribute name="title">
<xsl:value-of select="translate(title, '@', ' ')" />
</xsl:attribute>
<xsl:attribute name="id">
<xsl:value-of select="619950000 + position()" />
</xsl:attribute>
<xsl:element name="lf" />
<field name="Login" type="login">
<xsl:value-of select="username" />
</field><lf/>
<field name="Password" type="password">
<xsl:value-of select="password" />
</field><lf/>
<field name="Website" type="website">
<xsl:value-of select="url" />
</field><lf/>
<xsl:if test="comment">
<notes>
<xsl:value-of select="comment" />
</notes><lf/>
</xsl:if>
</card><lf/>
</xsl:template>
</xsl:stylesheet>''')
transform = etree.XSLT(xslt_root)
root = etree.parse(srcFile)
result = transform(root)
xml = str(result)
xml = xml.replace('<lf/>','\n')
with open(dstFile, 'w') as f:
f.write(xml)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment