summaryrefslogtreecommitdiff
path: root/contrib/keepassx2pass.py
blob: 538c157727525cfd919baa33edfba6655e70c106 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#! /usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright (C) 2012 Juhamatti Niemelä <iiska@iki.fi>. All Rights Reserved.
# This file is licensed under the GPLv2+. Please see COPYING for more information.

import sys

from subprocess import Popen, PIPE
from xml.etree import ElementTree

def path_for(element, path=''):
    """ Generate path name from elements title and current path """
    title = element.find('title').text
    return '/'.join([path, title])

def password_data(element):
    """ Return password data and additional info if available from
    password entry element. """
    ret = element.find('password').text + "\n"
    for field in ['username', 'url', 'comment']:
        fel = element.find(field)
        if fel.text is not None:
            ret = "%s%s: %s\n" % (ret, fel.tag, fel.text)
    return ret

def import_entry(element, path=''):
    """ Import new password entry to password-store using pass insert
    command """
    proc = Popen(['pass', 'insert', '--multiline', '--force',
                  path_for(element, path)],
              stdin=PIPE, stdout=PIPE)
    proc.communicate(password_data(element).encode('utf8'))
    proc.wait()

def import_group(element, path=''):
    """ Import all entries and sub-groups from given group """
    npath = path_for(element, path)
    for group in element.findall('group'):
        import_group(group, npath)
    for entry in element.findall('entry'):
        import_entry(entry, npath)


def main(xml_file):
    """ Parse given KeepassX XML file and import password groups from it """
    with open(xml_file) as xml:
        for group in ElementTree.XML(xml.read()).findall('group'):
            import_group(group)

if __name__ == '__main__':
    main(sys.argv[1])
span title='2007-04-16 22:34:28 +0000'>2007-04-16added ebuild window for Qt-Frontendnecoro4-2/+229 2007-04-16added preferences for Qt-Frontendnecoro9-279/+710 2007-04-13Improved masking display and made most of the Qt-Frontend worknecoro7-35/+334 2007-04-10Some more functionality for the Qt-Frontend (complete emerge)necoro8-217/+347 2007-04-07Some more functionality for the Qt-Frontendnecoro11-88/+463 2007-04-07Added Qt-Terminalnecoro4-4/+213 2007-04-06First qt draftnecoro6-1/+796 2007-04-04showed masked packages unmasked by the user similar to stable marked testing ...necoro5-13/+40 2007-03-31changed changelognecoro1-1/+2 2007-03-31Some small changes for etcproposals 1.1necoro1-2/+2 2007-03-31Some small changes for etcproposals 1.1necoro2-3/+3 2007-03-31Some small changes for etcproposals 1.1necoro1-6/+13 2007-03-31Allowed Plugins to have a menunecoro7-201/+315 2007-03-15Added etc-proposals pluginnecoro7-28/+121 2007-03-10Added USE_EXPAND-supportnecoro5-11/+63 2007-03-10Added plugin-data to about-dialognecoro3-197/+264