Skip to main content
This is a pre-production deployment of Warehouse. Changes made here affect the production instance of PyPI (pypi.python.org).
Help us improve Python packaging - Donate today!

Low-level library to interact with keepass databases (supports the v.4 format)

Project Description

This library allows you to write entries to a KeePass database

Simple Example

from pykeepass import PyKeePass

# load database
>>> kp = PyKeePass('db.kdbx', password='somePassw0rd')

# find any group by its name
>>> group = kp.find_groups(name='social', first=True)

# get the entries in a group
>>> group.entries
[Entry: "social/facebook (myusername)", Entry: "social/twitter (myusername)"]

# find any entry by its title
>>> entry = kp.find_entries(title='facebook', first=True)

# retrieve the associated password
>>> entry.password
's3cure_p455w0rd'

# update an entry
>>> entry.notes = 'primary facebook account'

# create a new group
>>> group = kp.add_group(kp.root_group, 'email')

# create a new entry
>>> kp.add_entry(group, 'gmail', 'myusername', 'myPassw0rdXX')
Entry: "email/gmail (myusername)"

# save database
>>> kp.save()

Finding Entries

find_entries (title=None, username=None, password=None, url=None, notes=None, path=None, uuid=None, regex=False, flags=None, tree=None, history=False, first=False)

Returns entries which match all provided parameters, where title, username, password, url, notes, path and uuid are strings. These functions have optional regex boolean and flags string arguments, which means to interpret the string as an XSLT style regular expression with flags.

The path string can be a direct path to an entry, or (when ending in /) the path to the group to recursively search under.

The history (default False) boolean controls whether history entries should be included in the search results.

The first (default False) boolean controls whether to return the first matched item, or a list of matched items.

  • if first=False, the function returns a list of Entry s or [] if there are no matches
  • if first=True, the function returns the first Entry match, or None if there are no matches

entries

a flattened list of all entries in the database

>>> kp.entries
[Entry: "foo_entry (myusername)", Entry: "foobar_entry (myusername)", Entry: "social/gmail (myusername)", Entry: "social/facebook (myusername)"]

>>> kp.find_entries(title='gmail', first=True)
Entry: "social/gmail (myusername)"

>>> kp.find_entries(title='foo.*', regex=True)
[Entry: "foo_entry (myusername)", Entry: "foobar_entry (myusername)"]

>>> entry = kp.find_entries(title='foo.*', url='.*facebook.*', regex=True, first=True)
>>> entry.url
'facebook.com'
>>> entry.title
'foo_entry'

>>> kp.find_groups_by_name('social', first=True).entries
[Entry: "social/gmail (myusername)", Entry: "social/facebook (myusername)"]

For backwards compatibility, the following function are also available:

find_entries_by_title (title, regex=False, flags=None, tree=None, history=False, first=False)

find_entries_by_username (username, regex=False, flags=None, tree=None, history=False, first=False)

find_entries_by_password (password, regex=False, flags=None, tree=None, history=False, first=False)

find_entries_by_url (url, regex=False, flags=None, tree=None, history=False, first=False)

find_entries_by_notes (notes, regex=False, flags=None, tree=None, history=False, first=False)

find_entries_by_path (path, regex=False, flags=None, tree=None, history=False, first=False)

find_entries_by_uuid (uuid, regex=False, flags=None, tree=None, history=False, first=False)

Finding Groups

find_groups (name=None, path=None, uuid=None, tree=None, regex=False, flags=None, first=False)

where name, path and uuid are strings. These functions have optional regex boolean and flags string arguments, which means to interpret the string as an XSLT style regular expression with flags.

The path string must end in /.

The first (default False) boolean controls whether to return the first matched item, or a list of matched items.

  • if first=False, the function returns a list of Group s or [] if there are no matches
  • if first=True, the function returns the first Group match, or None if there are no matches

root_group

the Root group to the database

groups

a flattened list of all groups in the database

>>> kp.groups
[Group: "foo", Group "foobar", Group: "social", Group: "social/foo_subgroup"]

>>> kp.find_groups(name='foo', first=True)
Group: "foo"

>>> kp.find_groups(name='foo.*', regex=True)
[Group: "foo", Group "foobar"]

>>> kp.find_groups(path='social/', regex=True)
[Group: "social", Group: "social/foo_subgroup"]

>>> kp.find_groups(name='social', first=True).subgroups
[Group: "social/foo_subgroup"]

>>> kp.root_group
Group: "/"

For backwards compatibility, the following functions are also available:

find_groups_by_name (name, tree=None, regex=False, flags=None, first=False)

find_groups_by_path (path, tree=None, regex=False, flags=None, first=False)

find_groups_by_uuid (uuid, tree=None, regex=False, flags=None, first=False)

Adding Entries

add_entry (destination_group, title, username, password, url=None, notes=None, tags=None, expiry_time=None, icon=None, force_creation=False)

delete_entry (entry)

where destination_group is a Group instance. entry is an Entry instance. title, username, password, url, notes, tags, icon are strings. expiry_time is a datetime instance.

If expiry_time is a naive datetime object (i.e. expiry_time.tzinfo is not set), the timezone is retrieved from dateutil.tz.gettz().

# add a new entry to the Root group
>>> kp.add_entry(kp.root_group, 'testing', 'foo_user', 'passw0rd')
Entry: "testing (foo_user)"

# add a new entry to the social group
>>> group = find_groups(name='social', first=True)
>>> entry = kp.add_entry(group, 'testing', 'foo_user', 'passw0rd')
Entry: "testing (foo_user)"

# save the database
>>> kp.save()

# delete an entry
>>> kp.delete_entry(entry)

# save the database
>>> kp.save()

Adding Groups

add_group (destination_group, group_name, icon=None)

delete_group (group)

destination_group and group are instances of Group. group_name is a string

# add a new group to the Root group
>>> group = kp.add_group(kp.root_group, 'social')

# add a new group to the social group
>>> kp.add_group(group, 'gmail')
Group: "social/gmail"

# save the database
>>> kp.save()

# delete a group
>>> kp.delete_group(group)

# save the database
>>> kp.save()

Miscellaneous

read (filename, password=None, keyfile=None)

where filename, password, and keyfile are strings. filename is the path to the database, password is the master password string, and keyfile is the path to the database keyfile. At least one of password and keyfile is required.

save (filename=None)

where filename is the path of the file to save to. If filename is not given, the path given in read will be used.

set_credentials (password=None, keyfile=None)

clear current database credentials and set to the ones given. password and keyfile are strings. At least one of password and keyfile is required

Tests

To run them issue $ python tests/tests.py

Release History

Release History

This version
History Node

2.8.0

History Node

2.7.2

History Node

2.7.1

History Node

2.7.0

History Node

2.6.2

History Node

2.6.1

History Node

2.6.0

History Node

2.5.2

History Node

2.5.1

History Node

2.5.0

History Node

2.4.5

History Node

2.4.4

History Node

2.4.3

History Node

2.4.2

History Node

2.4.1

History Node

2.4.0

History Node

2.3.1

History Node

2.3.0

History Node

2.2.4

History Node

2.2.3

History Node

2.2.2

History Node

2.2.1

History Node

2.2.0

History Node

2.1.1

History Node

2.1.0

History Node

2.0.1

History Node

2.0

History Node

1.5.3

History Node

1.5.2

History Node

1.5.1

History Node

1.5.0

History Node

1.4.2

History Node

1.4.1

History Node

1.4.0

History Node

1.3.0

History Node

1.2.1

History Node

1.2

History Node

1.1

History Node

1.0

Download Files

Download Files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

File Name & Checksum SHA256 Checksum Help Version File Type Upload Date
pykeepass-2.8.0.tar.gz (15.6 kB) Copy SHA256 Checksum SHA256 Source Nov 9, 2017

Supported By

WebFaction WebFaction Technical Writing Elastic Elastic Search Pingdom Pingdom Monitoring Dyn Dyn DNS Sentry Sentry Error Logging CloudAMQP CloudAMQP RabbitMQ Heroku Heroku PaaS Kabu Creative Kabu Creative UX & Design Fastly Fastly CDN DigiCert DigiCert EV Certificate Rackspace Rackspace Cloud Servers DreamHost DreamHost Log Hosting