This module provides helper functions for managing credential files, such as username/password files and token files. It allows adding, removing, and checking entries.
Comments and blank lines in the files are preserved, and entries can be updated (upserted) if desired.
| Exception | |
Raised when a duplicate entry is found in a credential file. |
| Exception | |
Raised when attempting to add an entry that already exists without explicitly allowing an overwrite. |
| Exception | |
Raised when an entry is not found in a credential file. |
| Function | add |
Add or update a username/password entry in a credential file. |
| Function | add |
Add or update (upsert) an entry in a line-based file, preserving existing entries, comments, and blank lines. |
| Function | add |
Add or update a token entry in a token file. |
| Function | get |
Retrieve the password for a given username from a credential file. |
| Function | has |
Check whether an entry exists in a line-based file. |
| Function | has |
Check whether a token entry exists in a token file. |
| Function | read |
Read a username/password credential file and return a dictionary of entries. |
| Function | read |
Read a token credential file and return a list of tokens. |
| Function | remove |
Remove a username/password entry from a credential file. |
| Function | remove |
Remove an entry from a line-based file, preserving existing entries, comments, and blank lines. |
| Function | remove |
Remove a token entry from a token file. |
| Function | _find |
Find the index of an entry in a list of lines based on a lookup key. |
| Function | _read |
Read all lines from a file, stripping whitespace and ignoring comments and blank lines. |
| Function | _write |
Write lines to a file, ensuring that the file ends with a newline. |
str | Path, username: str, password: str, update: bool = False):
(source)
¶
Add or update a username/password entry in a credential file.
| Parameters | |
filestr | Path | Path to the credential file. |
username:str | The username for the entry. |
password:str | The password for the entry. |
update:bool | If True, perform an upsert — an existing entry is replaced, or a new one added if none exists. If False (default) and the username already exists, an EntryExistsError is raised. |
| Raises | |
EntryExistsError | If the username already exists and update is False. |
str | Path, key: str, newLine: str, keyOf: Callable[ [ str], str] = lambda line: line, update: bool = False):
(source)
¶
Add or update (upsert) an entry in a line-based file, preserving existing entries, comments, and blank lines.
| Parameters | |
filestr | Path | Path to the file. |
key:str | The lookup key for the entry (e.g. username, or the token itself). |
newstr | The full line to write for this entry. |
keyCallable[ | Function that extracts the lookup key from an existing line. |
update:bool | If True, perform an upsert — an existing entry is replaced, or a new one added if none exists. If False (default) and the key already exists, an EntryExistsError is raised. |
| Raises | |
EntryExistsError | If the key already exists and update is False. |
Add or update a token entry in a token file.
| Parameters | |
filestr | Path | Path to the token file. |
token:str | The token for the entry. |
update:bool | If True, perform an upsert — an existing entry is replaced, or a new one added if none exists. If False (default) and the token already exists, an EntryExistsError is raised. |
| Raises | |
EntryExistsError | If the token already exists and update is False. |
Retrieve the password for a given username from a credential file.
| Parameters | |
filestr | Path | Path to the credential file. |
username:str | The username for which to retrieve the password. |
| Returns | |
str | None | The password for the given username, or None if not found. |
str | Path, key: str, keyOf: Callable[ [ str], str] = lambda line: line) -> bool:
(source)
¶
Check whether an entry exists in a line-based file.
| Parameters | |
filestr | Path | Path to the file. |
key:str | The lookup key for the entry. |
keyCallable[ | Function that extracts the lookup key from an existing line. |
| Returns | |
bool | True if an entry with the given key exists, False otherwise. |
Check whether a token entry exists in a token file.
| Parameters | |
filestr | Path | Path to the token file. |
token:str | The token for the entry. |
| Returns | |
bool | True if an entry with the given token exists, False otherwise. |
Read a username/password credential file and return a dictionary of entries.
| Parameters | |
filestr | Path | Path to the credential file. |
| Returns | |
dict[ | A dictionary mapping usernames to passwords. If the file does not exist, returns an empty dictionary. |
| Raises | |
DuplicateEntryError | If there are duplicate usernames in the file. |
Read a token credential file and return a list of tokens.
| Parameters | |
filestr | Path | Path to the token file. |
| Returns | |
list[ | A list of tokens. If the file does not exist, returns an empty list. |
| Raises | |
DuplicateEntryError | If there are duplicate tokens in the file. |
Remove a username/password entry from a credential file.
| Parameters | |
filestr | Path | Path to the credential file. |
username:str | The username for the entry to remove. |
| Raises | |
EntryNotFoundError | If the entry is not found. |
str | Path, key: str, keyOf: Callable[ [ str], str] = lambda line: line):
(source)
¶
Remove an entry from a line-based file, preserving existing entries, comments, and blank lines.
| Parameters | |
filestr | Path | Path to the file. |
key:str | The lookup key for the entry to remove. |
keyCallable[ | Function that extracts the lookup key from an existing line. |
| Raises | |
EntryNotFoundError | If the entry is not found. |
Remove a token entry from a token file.
| Parameters | |
filestr | Path | Path to the token file. |
token:str | The token for the entry to remove. |
| Raises | |
EntryNotFoundError | If the entry is not found. |
list[ str], key: str, keyOf: Callable[ [ str], str]) -> int | None:
(source)
¶
Find the index of an entry in a list of lines based on a lookup key.
| Parameters | |
lines:list[ | List of lines to search. |
key:str | The lookup key for the entry (e.g. username, or the token itself). |
keyCallable[ | Function that extracts the lookup key from an existing line. |
| Returns | |
int | None | The index of the entry if found, or None if not found. |