pykblib package

Module contents

The PyKBLib library provides a simple API for interacting with Keybase.

Submodules

pykblib.keybase module

Defines the core Keybase class.

class pykblib.keybase.Keybase

Bases: object

Provides a high-level interface for interacting with Keybase.

teams

The list of teams to which the active user belongs.

Type:list
username

The username of the active user.

Type:str
create_team(team_name)

Create a team with the specified name.

Parameters:team_name (str) – The name of the team to be created.
Raises:KeybaseException – If the team can’t be created, a KeybaseException is raised.
delete_team(team_name)

Delete the specified team, and all of its sub-teams.

Parameters:team_name (str) – The name of the team to be deleted.
Raises:KeybaseException – If the team isn’t in the Keybase.teams list, or if an APIException is raised when attempting to call the KeybaseAPI.delete_team function, this function will raise a KeybaseException.
ignore_request(team_name, username)

Ignore a user’s access request to the specified team.

Parameters:
  • team_name (str) – The name of the team.
  • username (str) – The name of the user to ignore.
Raises:

KeybaseException – If the request could not be ignored, the function will raise a KeybaseException.

leave_team(team_name)

Leave the specified team.

Parameters:team_name (str) – The name of the team.
Raises:KeybaseException – If the user could not leave the team, the function will raise a KeybaseException.
list_requests(team_name=None)

Retrieve a dictionary of all access requests for the specified team.

If no team is specified, the dictionary will contain all requests for all teams.

Parameters:team_name (str) – The name of the target team. Defaults to None.
Returns:requests – A dict with team names for keys and sets of usernames as values.
Return type:dict
Raises:KeybaseException – If the function couldn’t retrieve the list of access requests, it will raise a KeybaseException.
request_access(team_name)

Request access to the specified team name.

Parameters:team_name (str) – The name of the team.
Raises:KeybaseException – If the request could not be sent, the function will raise a KeybaseException.
team(team_name)

Return a Team class instance for the specified team.

Parameters:team_name (str) – The name of the team to which the Team class should refer.
Returns:If successful, the script will return a Team instance referring to the specified team.
Return type:Team
Raises:KeybaseException – If the team instance could not be created, a KeybaseException is raised.
update_team_list()

Update the list of teams to which the active member belongs.

pykblib.team module

Defines the Team class.

class pykblib.team.Team(team_name, keybase_instance)

Bases: object

The primary point of interaction with Keybase teams.

name

The name of the team.

Type:str
role

The active user’s role within the team.

Type:str
members_by_role

A namedtuple comprising unordered sets of members by specified role. To access the sets, use one of the following:

  • Team.members_by_role.owner
  • Team.members_by_role.admin
  • Team.members_by_role.writer
  • Team.members_by_role.reader
  • Team.members_by_role.deleted
  • Team.members_by_role.reset
Type:namedtuple
add_member(username, role='reader')

Add the specified user to this team.

Note: This function is simply a wrapper for the `Team.add_members` function, where the username_list parameter contains only one item.

Parameters:
  • username (str) – The username of the user to add to the team.
  • role (str) – The role to assign to the new member. This must be either reader, writer, admin, or owner. In order to assign the owner role, the current user must be an owner of the team. (Defaults to reader.)
Raises:

TeamException – If the user could not be added, a TeamException is raised.

add_members(username_list, role='reader')

Add the specified users to this team.

Note: Keybase adds the specified users one at a time, but still returns an error if any of the users cannot be added to the team. For example, if the first two users are successfully added to the team, but the third user fails, the function will raise an error, despite the fact that the first two users were successfully added. As a result, if an attempt to add multiple users fails, it is advised to run the `Team.update()` function to update the member lists, to determine whether any of the specified users were successfully added.

Parameters:
  • usernames (str) – The usernames of the users to add to the team.
  • role (str) – The role to assign to the new members. This must be either reader, writer, admin, or owner. In order to assign the owner role, the current user must be an owner of the team. (Defaults to reader.)
Raises:

TeamException – If the users couldn’t be added, a TeamException is raised.

change_member_role(username, new_role)

Change the specified user’s role within this team.

Parameters:
  • username (str) – The username of the member whose role will be changed.
  • new_role (str) – The role to assign to the member. This must be either reader, writer, admin, or owner. In order to assign the owner role, the current user must be an owner of the team.
Raises:

TeamException – If the user’s role could not be changed, a TeamException is raised.

create_sub_team(sub_team_name)

Create a sub-team within this team.

This function simply calls Keybase.create_team with the appropriate full team name, a concatenation of the parent team and sub-team names, separated by a period.

Parameters:sub_team_name (str) – The name of the sub-team to be created. The final team name will be parent.sub_team_name where parent is this team’s name.
Raises:TeamException – If the sub-team cannot be created, a TeamException is raised.
delete()

Delete this team and all of its sub-teams.

Note: This is simply a wrapper for the Keybase.delete_team function, passing this team’s name as the team to be deleted.

Raises:KeybaseException – If the team cannot be deleted, a KeybaseException is raised.
ignore_request(username)

Ignore the specified user’s request to join this team.

This function is a wrapper for Keybase.ignore_request, passing this team’s name and the specified username as the target.

Raises:KeybaseException – If the request cannot be ignored, a KeybaseException will be raised.
leave()

Leave this team.

This function is a wrapper for Keybase.leave_team, passing this team’s name as the target.

Raises:KeybaseException – If the active user could not leave the team, a KeybaseException is raised.
list_requests()

Retrieve all requests to join this team.

This function is a wrapper for Keybase.list_requests, retrieving and returning only the set of usernames requesting access to this team.

Returns:usernames – A set containing all the users who have requested access to this team.
Return type:set
Raises:KeybaseException – Should an error occur, this function will raise a KeybaseException.
members()

Retrieve a set of the usernames of all members in the team.

Note: This includes users who have deleted or reset their accounts.

Returns:members – A set of the usernames of all members in the team.
Return type:set
purge_deleted()

Purge members whose accounts were deleted.

This function is a wrapper for Team.remove_member, which automatically targets members who have deleted their accounts.

Note: Even if a TeamException is raised, it’s possible that some of the deleted users were successfully removed. The usernames listed in Team.members_by_role.deleted will contain those that were unable to be purged.

Raises:TeamException – If some or all of the deleted users could not be purged, a TeamException will be raised.
purge_reset()

Purge members whose accounts were reset.

This function is a wrapper for Team.remove_member, which automatically targets members who have reset their accounts.

Note: Even if a TeamException is raised, it’s possible that some of the reset users were successfully removed. The usernames listed in Team.members_by_role.reset will contain those that were unable to be purged.

Raises:TeamException – If some or all of the reset users could not be purged, a TeamException will be raised.
remove_member(username)

Remove the specified user from this team.

Parameters:username (str) – The username of the user to remove from the team.
Raises:TeamException – If the user cannot be removed, a TeamException is raised.
rename(new_name)

Rename this team.

Note: This will only work if this team is a sub-team.

Parameters:new_name (str) – The sub-team’s new name.
Raises:TeamException – If the team couldn’t be renamed, a TeamException is raised.
sub_team(sub_team_name)

Return a Team instance referring to the specified sub-team.

Parameters:sub_team_name (str) – The name of the sub-team.
Returns:If successful, the script will return a Team instance referring to the sub-team.
Return type:Team
update()

Update the team’s membership and role information.

Changes in membership and role information that were not instigated by PyKBLib will not automatically be reflected in the Team. This function can be used to update this information.

Raises:TeamException – If an error is received from the API when trying to retrieve the team’s membership information, the TeamException will be raised.

pykblib.api module

Defines the KeybaseAPI class.

class pykblib.api.KeybaseAPI

Bases: object

Provides an interface to the Keybase service.

call_api(service, query)

Execute the specified query on the specified API service.

Parameters:
  • service (str) – The name of the API service, i.e. ‘chat’, ‘team’, or ‘wallet’.
  • query (dict) – The query to be sent to the API.
Returns:

result – The result of the query, converted into a nested namedtuple.

Return type:

namedtuple

Raises:

APIException – If there is an error defined in the return value, this will raise an exception containing the error message.

static delete_team(team_name)

Delete the specified team.

Parameters:team_name (str) – The name of the team to be deleted.
Raises:APIException – If the team cannot be deleted, the APIException will be raised.
static run_command(command)

Execute the specified command, then return the result.

Parameters:command (str) – The command to be run.
Returns:output – The output of the command being run.
Return type:str
Raises:APIException – If there was an error returned by the Keybase application, this will raise an exception containing the error message.

pykblib.exceptions module

Defines the various exceptions that are raised by PyKBLib.

exception pykblib.exceptions.APIException(message)

Bases: pykblib.exceptions.KBLibException

Raised when there’s an error with the Keybase API.

exception pykblib.exceptions.KBLibException(message)

Bases: Exception

The base exception class for all PyKBLib exceptions.

message

The error message sent with the exception.

Type:str
exception pykblib.exceptions.KeybaseException(message)

Bases: pykblib.exceptions.KBLibException

Raised when there’s an error with the Keybase class.

exception pykblib.exceptions.TeamException(message)

Bases: pykblib.exceptions.KBLibException

Raised when there’s an error with the Team class.