2010-01-27 3 views

Répondre

0

Eh bien je l'ai trouvé un bon exemple à ce sujet dans here, vous devrez également utiliser PyKDE4 non seulement PyQt.

5

Tutoriel dans la ligne de commande Python

D'abord, je vais montrer comment kwallet peut être utilisé à partir de la ligne de commande Python pour lire et écrire un mot de passe:

$ python 

# We import the necessary modules. 
>>> from PyKDE4.kdeui import KWallet 
>>> from PyQt4 import QtGui 

# We create a QApplication. We will not use it, but otherwise 
# we would get a "QEventLoop: Cannot be used without 
# QApplication" error message. 
>>> app = QtGui.QApplication([]) 

# We open the wallet. 
>>> wallet = KWallet.Wallet.openWallet(
       KWallet.Wallet.LocalWallet(), 0) 

# We create a folder in which we will store our password, 
# and set it as current. 
>>> wallet.createFolder('myfolder') 
True 
>>> wallet.hasFolder('myfolder') 
True 
>>> wallet.setFolder('myfolder') 
True 

# We read the password (which does not exist yet), write it, 
# and read it again. 
>>> wallet.readPassword('mykey') 
(0, PyQt4.QtCore.QString(u'')) 
>>> wallet.writePassword('mykey', 'mypassword') 
0 
>>> wallet.readPassword('mykey') 
(0, PyQt4.QtCore.QString(u'mypassword')) 

Tutoriel comme module Python

Habituellement, vous voulez créer des fonctions simples pour enrouler les méthodes de kwallet. Le module Python suivant peut ouvrir le porte-monnaie, obtenir et définir un mot de passe:

#!/usr/bin/python 

from PyKDE4.kdeui import KWallet 
from PyQt4 import QtGui 

def open_wallet(): 
    app = QtGui.QApplication([]) 
    wallet = KWallet.Wallet.openWallet(
       KWallet.Wallet.LocalWallet(), 0) 
    if not wallet.hasFolder('kwallet_example'): 
     wallet.createFolder('kwallet_example') 
    wallet.setFolder('kwallet_example') 
    return wallet 

def get_password(wallet): 
    key, qstr_password = wallet.readPassword('mykey') 

    # converting the password from PyQt4.QtCore.QString to str 
    return str(qstr_password) 

def set_password(wallet, password): 
    wallet.writePassword('mykey', password) 

Il peut être utilisé de la manière suivante:

$ python 
>>> import kwallet_example 
>>> wallet = kwallet_example.open_wallet() 
>>> kwallet_example.set_password(wallet, 'mypass') 
>>> kwallet_example.get_password(wallet)