The gi (GObject Introspection) Python package is excellent! It provides Python bindings for GObject-based libraries like GTK, GLib, and Secret Service. It enables you to write native GNOME applications in Python and access system services seamlessly.
First, install these Fedora packages:
[mythcat@fedora ~]# dnf5 install python3-gobject libsecret-devel
...
Package "python3-gobject-3.48.2-3.fc41.x86_64" is already installed.
Package "libsecret-devel-0.21.4-3.fc41.x86_64" is already installed.
...
[mythcat@fedora ~]# dnf5 install gnome-shell gnome-keyring libsecret
...
I used this simple python source code to test the gi python package:
import gi
# Specify the version of Gio and Secret we want to use
gi.require_version('Gio', '2.0')
gi.require_version('Secret', '1')
from gi.repository import Gio, Secret, GLib
def check_schema(schema_name):
try:
Gio.Settings.new(schema_name)
print(f"Schema '{schema_name}' is available")
return True
except GLib.GError as e:
print(f"Schema '{schema_name}' is not installed: {str(e)}")
return False
def store_secret():
schema = Secret.Schema.new("org.example.Password",
Secret.SchemaFlags.NONE,
{
"username": Secret.SchemaAttributeType.STRING,
}
)
Secret.password_store_sync(schema,
{"username": "myuser"},
Secret.COLLECTION_DEFAULT,
"My secret item",
"Hello, World!",
None)
print("Secret stored successfully")
def get_secret():
schema = Secret.Schema.new("org.example.Password",
Secret.SchemaFlags.NONE,
{
"username": Secret.SchemaAttributeType.STRING,
}
)
password = Secret.password_lookup_sync(schema,
{"username": "myuser"},
None)
if password is not None:
print(f"Retrieved secret: {password}")
else:
print("No secret found")
if __name__ == "__main__":
print("Starting secret operations...")
store_secret()
get_secret()
print("Finished secret operations.")
The result is this:
$ python test_001.py
Starting secret operations...
Secret stored successfully
Retrieved secret: Hello, World!
Finished secret operations.