Coverage for icloudpy/utils.py: 84%
32 statements
« prev ^ index » next coverage.py v7.17.2, created at 2026-11-05 06:42 +0000
« prev ^ index » next coverage.py v7.17.2, created at 2026-11-05 06:42 +0000
1"""Utils."""
3import getpass
4from sys import stdout
6import keyring
8from .exceptions import ICloudPyNoStoredPasswordAvailableException
10KEYRING_SYSTEM = "icloudpy://icloud-password"
13def get_password(username, interactive=stdout.isatty() if stdout else False):
14 """Get the password from a username."""
15 try:
16 return get_password_from_keyring(username)
17 except ICloudPyNoStoredPasswordAvailableException:
18 if not interactive:
19 raise
21 return getpass.getpass(f"Enter iCloud password for {username}: ")
24def password_exists_in_keyring(username):
25 """Return true if the password of a username exists in the keyring."""
26 try:
27 get_password_from_keyring(username)
28 except ICloudPyNoStoredPasswordAvailableException:
29 return False
31 return True
34def get_password_from_keyring(username):
35 """Get the password from a username."""
36 result = keyring.get_password(KEYRING_SYSTEM, username)
37 if result is None:
38 raise ICloudPyNoStoredPasswordAvailableException(
39 f"No iCloudPy password for {username} could be found "
40 "in the system keychain. Use the `--store-in-keyring` "
41 "command-line option for storing a password for this "
42 "username.",
43 )
45 return result
48def store_password_in_keyring(username, password):
49 """Store the password of a username."""
50 return keyring.set_password(KEYRING_SYSTEM, username, password)
53def delete_password_in_keyring(username):
54 """Delete the password of a username."""
55 return keyring.delete_password(KEYRING_SYSTEM, username)
58def underscore_to_camelcase(word, initial_capital=False):
59 """Transform a word to camelCase."""
60 words = [x.capitalize() or "_" for x in word.split("_")]
61 if not initial_capital:
62 words[0] = words[0].lower()
64 return "".join(words)