kwalletbinding.py 2.36 KB
Newer Older
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
# -*- coding:utf-8 -*-
## src/common/kwalletbinding.py
##
## Copyright (c) 2009 Thorsten Glaser <t.glaser AT tarent.de>
##
## This file is part of Gajim.
##
## Gajim is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published
## by the Free Software Foundation; version 3 only. This file is
## also available under the terms of The MirOS Licence.
##
## Gajim is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with Gajim. If not, see <http://www.gnu.org/licenses/>.
##

__all__ = ['kwallet_available', 'kwallet_get', 'kwallet_put']

import subprocess


def kwallet_available():
28 29 30 31 32 33 34 35 36 37 38
    """
    Return True if kwalletcli can be run, False otherwise
    """
    try:
        p = subprocess.Popen(["kwalletcli", "-qV"])
    except Exception:
        return False
    p.communicate()
    if p.returncode == 0:
        return True
    return False
39 40 41


def kwallet_get(folder, entry):
42 43
    """
    Retrieve a passphrase from the KDE Wallet via kwalletcli
44

45 46 47
    Arguments:
    • folder: The top-level category to use (normally the programme name)
    • entry: The key of the entry to retrieve
48

Yann Leboulanger's avatar
Yann Leboulanger committed
49
    Returns the passphrase, False if it cannot be found,
50 51 52 53 54 55
    or None if an error occured.
    """
    p = subprocess.Popen(["kwalletcli", "-q", "-f", folder.encode('utf-8'),
     "-e", entry.encode('utf-8')], stdout=subprocess.PIPE)
    pw = p.communicate()[0]
    if p.returncode == 0:
56
        return pw
57 58 59 60 61
    if p.returncode == 1 or p.returncode == 4:
        # ENOENT
        return False
    # error
    return None
62 63 64


def kwallet_put(folder, entry, passphrase):
65 66
    """
    Store a passphrase into the KDE Wallet via kwalletcli
67

68 69 70 71
    Arguments:
    • folder: The top-level category to use (normally the programme name)
    • entry: The key of the entry to store
    • passphrase: The value to store
72

73 74 75 76 77 78 79 80
    Returns True on success, False otherwise.
    """
    p = subprocess.Popen(["kwalletcli", "-q", "-f", folder.encode('utf-8'),
     "-e", entry.encode('utf-8'), "-P"], stdin=subprocess.PIPE)
    p.communicate(passphrase.encode('utf-8'))
    if p.returncode == 0:
        return True
    return False