Main Interaction¶
This part of the documentation covers the primary API for interacting with the keepass database.
Database¶
The database classes provide the primary API for loading and saving KeePass 1.x databases, in addition to creating new groups and entries.
- class keepassdb.db.Database(dbfile=None, password=None, keyfile=None, readonly=False, new=False)[source]¶
This class represents the KeePass 1.x database.
Variables: - root – The group-like virtual root object (not actually part of database).
- groups – The flat list of groups (keepassdb.model.Group) in this database.
- entries – The flat list of entries (keepassdb.model.Entry) in this database.
- readonly – Whether database was opened read-only.
- filepath – The path to the database that is opened or will be written (if specified).
- password – The passphrase to use to encrypt the database.
- keyfile – A path to a keyfile that can be used instead or in combination with passphrase.
- header – The database header struct (keepassdb.structs.HeaderStruct).
- create_default_group()[source]¶
Create a default ‘Internet’ group on an empty database.
Returns: The new ‘Internet’ group. Return type: keepassdb.model.Group
- create_entry(group, **kwargs)[source]¶
Create a new Entry object.
The group which should hold the entry is needed.
image must be an unsigned int >0, group a Group.
Parameters: - group – The associated group.
- title –
- icon –
- url –
- username –
- password –
- notes –
- expires (datetime) – Expiration date (if None, entry will never expire).
Returns: The new entry.
Return type:
- create_group(title, parent=None, icon=1, expires=None)[source]¶
This method creates a new group.
A group title is needed or no group will be created.
If a parent is given, the group will be created as a sub-group.
title must be a string, image an unsigned int >0 and parent a Group.
Returns: The newly created group. Return type: keepassdb.model.Group
- load(dbfile, password=None, keyfile=None, readonly=False)[source]¶
Load the database from file/stream.
Parameters: - dbfile (str or file-like object) – The database file path/stream.
- password (str) – The password for the database.
- keyfile (str or file-like object) – Path to a keyfile (or a stream) that can be used instead of or in conjunction with password for database.
- readonly (bool) – Whether to open the database read-only.
- load_from_buffer(buf, password=None, keyfile=None, readonly=False)[source]¶
Load a database from passed-in buffer (bytes).
Parameters: - buf (str) – A string (bytes) of the database contents.
- password (str) – The password for the database.
- keyfile (str or file-like object) – Path to a keyfile (or a stream) that can be used instead of or in conjunction with password for database.
- readonly (bool) – Whether to open the database read-only.
- move_entry(entry, group, index=None)[source]¶
Move an entry to another group.
Parameters: - entry (keepassdb.model.Entry) – The Entry object to move.
- group (keepassdb.model.Group) – The new parent Group object for the entry.
- index (int) – The 0-based index within the parent (defaults to appending group to end of parent’s children).
- move_group(group, parent, index=None)[source]¶
Move group to be a child of new parent.
Parameters: - group (keepassdb.model.Group) – The group to move.
- parent (keepassdb.model.Group) – The new parent for the group.
- index (int) – The 0-based index within the parent (defaults to appending group to end of parent’s children).
- remove_entry(entry)[source]¶
Remove specified entry.
Parameters: entry (keepassdb.model.Entry) – The Entry object to remove.
- save(dbfile=None, password=None, keyfile=None)[source]¶
Save the database to specified file/stream with password and/or keyfile.
Parameters: - dbfile (The path to the database file or a file-like object.) – The path to the file we wish to save.
- password (str) – The password to use for the database encryption key.
- keyfile (str or file-like object) – The path to keyfile (or a stream) to use instead of or in conjunction with password for encryption key.
Raises keepassdb.exc.ReadOnlyDatabase: If database was opened with readonly flag.
- class keepassdb.db.LockingDatabase(dbfile=None, password=None, keyfile=None, readonly=False, new=False)[source]¶
A convenience subclass that adds automatic file locking (if db not opened read-only).
The lock is only acquired when the filepath is specified to a load() or save() operation. The close() method will also release the lock.
- acquire_lock(force=False)[source]¶
Takes out a lock (creates a <dbname>.lock file) for the database.
Parameters: force (bool) – Whether to force taking “ownership” of the lock file. Raises : keepassdb.exc.DatabaseAlreadyLocked - If the database is already locked (and force not set to True).
Model¶
The model consists of the Group and Entity classes that together make up the contents of the database. These classes are directly related to their keepassdb.structs counterparts; however, they also track the hierarchy of the database, track modification times, and provide convenience methods to perform create and move operations.
- class keepassdb.model.Entry(uuid=None, group_id=None, group=None, icon=None, title=None, url=None, username=None, password=None, notes=None, created=None, modified=None, accessed=None, expires=None, binary_desc=None, binary=None)[source]¶
Entry represents a simple entry of a KeePass 1.x database.
Variables: - uuid – The ID for the entry.
- group_id – The numeric ID for the group.
- group – The group object that this entity is related to.
- icon – The icon identifier.
- title – The title for the entry.
- username – The username.
- password – The password
- url – The entry URL.
- notes – Notes/comment for the entry.
- created – When entry was created (default: now)
- modified – When entry was last modified (default: now)
- accessed – When the entry was last accessed (default: now)
- expires – When the entry (password) expires. Default will be keepassdb.const.NEVER.
- binary_desc – Description/metadata for the binary column.
- binary – Binary contents.
- struct_type¶
alias of EntryStruct
- class keepassdb.model.Group(id=None, title=None, icon=None, level=None, created=None, modified=None, accessed=None, expires=None, flags=None, parent=None, db=None)[source]¶
Represents a single group of a KeePass 1.x database.
Variables: - db – The parent database (keepassdb.db.Database)
- id – The group numeric id (unsigned int)
- title – The group title (string)
- level – The hierarchy level / depth in tree (unsigned int)
- icon – The group icon identifier used in KeePassX (unsigned int)
- parent – The parent group (keepassdb.model.Group)
- children – List of children groups (keepassdb.model.Group)
- entries – List of member entries (keepassdb.model.Entry)
- change_index(index)[source]¶
Move the group to the new 0-based index within the same parent.
Parameters: index (int) – The 0-based index for the new position within parent group. Note that this index will be evaluated after the group has been removed from the list.
- create_entry(**kwargs)[source]¶
This method creates an entry in this group.
Parameters: - title –
- icon –
- url –
- username –
- password –
- notes –
- expires (datetime) – Expiration date (if None, entry will never expire).
- move(parent, index=None)[source]¶
Move this group to a new parent.
Parameters: - parent (keepassdb.model.Group) – The new parent group; if None will be root group.
- index (int) – The 0-based index within the parent (defaults to appending group to end of parent’s children).
- struct_type¶
alias of GroupStruct
- class keepassdb.model.RootGroup[source]¶
A group-like object that serves as the root node of the tree.
This is the root attribute of the keepassdb.db.Database instance.
This object is not written to the database; it simply exists to provide a virtual node from which to make the top-level groups children.
Variables: - title – The title for the group (‘Root Group’)
- level – The hierarchy level / depth in tree; This is -1 for root group.
- children – List of children groups (keepassdb.model.Group)
Export¶
The export package contains classes for exporting the database.
Support for exporting database to KeePassX XML format.
- class keepassdb.export.xml.XmlExporter(include_comment=False, prettyprint=True)[source]¶
Class for exporting database to KeePassX XML format.
Variables: - include_comment – Whether to include a ‘generated-by’ comment in the header.
- prettyprint – Whether to generate pretty-printed XML (indent, etc.).
- export(db)[source]¶
Export the dbnode to KeePassX XML format.
Parameters: db (keepassdb.db.Database) – The database to export.
Errors¶
The exception classes raised by the library.
- exception keepassdb.exc.AuthenticationError[source]¶
Exception raised when (encrypted) contents cannot be verified against signature.
- exception keepassdb.exc.IncorrectKey[source]¶
Exception raised when contents cannot be decrypted with specified key.
- exception keepassdb.exc.InvalidDatabase[source]¶
Error raised when database appears to be invalid. :)
- exception keepassdb.exc.KPError[source]¶
KPError is the base exception class to handle exception raised by keepassdb.
- exception keepassdb.exc.ParseError[source]¶
Exception raised when unable to parse database structure.