Package common :: Module kwalletbinding
[hide private]
[frames] | no frames]

Source Code for Module common.kwalletbinding

 1  # -*- coding:utf-8 -*- 
 2  ## src/common/kwalletbinding.py 
 3  ## 
 4  ## Copyright (c) 2009 Thorsten Glaser <t.glaser AT tarent.de> 
 5  ## 
 6  ## This file is part of Gajim. 
 7  ## 
 8  ## Gajim is free software; you can redistribute it and/or modify 
 9  ## it under the terms of the GNU General Public License as published 
10  ## by the Free Software Foundation; version 3 only. This file is 
11  ## also available under the terms of The MirOS Licence. 
12  ## 
13  ## Gajim is distributed in the hope that it will be useful, 
14  ## but WITHOUT ANY WARRANTY; without even the implied warranty of 
15  ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
16  ## GNU General Public License for more details. 
17  ## 
18  ## You should have received a copy of the GNU General Public License 
19  ## along with Gajim. If not, see <http://www.gnu.org/licenses/>. 
20  ## 
21   
22  __all__ = ['kwallet_available', 'kwallet_get', 'kwallet_put'] 
23   
24  import subprocess 
25   
26   
27 -def kwallet_available():
28 """ 29 Return True if kwalletcli can be run, False otherwise 30 """ 31 try: 32 p = subprocess.Popen(["kwalletcli", "-qV"]) 33 except Exception: 34 return False 35 p.communicate() 36 if p.returncode == 0: 37 return True 38 return False
39 40
41 -def kwallet_get(folder, entry):
42 """ 43 Retrieve a passphrase from the KDE Wallet via kwalletcli 44 45 Arguments: 46 • folder: The top-level category to use (normally the programme name) 47 • entry: The key of the entry to retrieve 48 49 Returns the passphrase as unicode, False if it cannot be found, 50 or None if an error occured. 51 """ 52 p = subprocess.Popen(["kwalletcli", "-q", "-f", folder.encode('utf-8'), 53 "-e", entry.encode('utf-8')], stdout=subprocess.PIPE) 54 pw = p.communicate()[0] 55 if p.returncode == 0: 56 return unicode(pw.decode('utf-8')) 57 if p.returncode == 1 or p.returncode == 4: 58 # ENOENT 59 return False 60 # error 61 return None
62 63
64 -def kwallet_put(folder, entry, passphrase):
65 """ 66 Store a passphrase into the KDE Wallet via kwalletcli 67 68 Arguments: 69 • folder: The top-level category to use (normally the programme name) 70 • entry: The key of the entry to store 71 • passphrase: The value to store 72 73 Returns True on success, False otherwise. 74 """ 75 p = subprocess.Popen(["kwalletcli", "-q", "-f", folder.encode('utf-8'), 76 "-e", entry.encode('utf-8'), "-P"], stdin=subprocess.PIPE) 77 p.communicate(passphrase.encode('utf-8')) 78 if p.returncode == 0: 79 return True 80 return False
81