Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions pantalaimon/bus_helper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# This new file centralizes the logic for D-Bus selection.

import os
import dbus

from dbus.exceptions import DBusException

def get_bus(bus_choice):
"""
Returns a D-Bus connection based on the user's choice.
Raises RuntimeError if no connection can be made.
"""
if bus_choice == "system":
return dbus.SystemBus()
elif bus_choice == "session":
return dbus.SessionBus()
Comment on lines +13 to +16
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you adjust the README to show these options?

elif bus_choice == "auto":
# First, try to connect to the session bus. This is the default
# behavior for per-user installs.
try:
return dbus.SessionBus()
except DBusException:
pass # Fallback to system bus if session bus fails

# If session bus fails, try system bus.
try:
return dbus.SystemBus()
except dbus.DBusException:
raise RuntimeError("Could not find pantalaimon on session or system bus")
else:
raise ValueError(f"Invalid bus choice: {bus_choice}")
21 changes: 21 additions & 0 deletions pantalaimon/daemon.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
# See the License for the specific language governing permissions and
# limitations under the License.


import argparse
from pantalaimon.bus_helper import get_bus

import asyncio
import json
import os
Expand Down Expand Up @@ -1394,3 +1398,20 @@ async def shutdown(self, _):
if self.default_session:
await self.default_session.close()
self.default_session = None


def main():
parser = argparse.ArgumentParser(description="Pantalaimon Daemon")
parser.add_argument(
"--bus",
choices=["session", "system", "auto"],
default="auto",
help="Choose which D-Bus bus to connect to (default: auto)",
)
args = parser.parse_args()

# Use the helper to get the bus
bus = get_bus(args.bus)

# TODO: Place daemon startup logic here
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this PR ready for review? Seems like we've got a significant TODO here.

print(f"Daemon started with {args.bus} bus: {bus}")
33 changes: 29 additions & 4 deletions pantalaimon/panctl.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@
from prompt_toolkit.completion import Completer, Completion, PathCompleter
from prompt_toolkit.document import Document
from prompt_toolkit.patch_stdout import patch_stdout
from pydbus import SessionBus
from dbus.exceptions import DBusException
from pantalaimon.bus_helper import get_bus


PTK2 = ptk_version.startswith("2.")

Expand Down Expand Up @@ -404,8 +406,31 @@ class PanCtl:

commands = list(command_help.keys())

def __attrs_post_init__(self):
self.bus = SessionBus()
def __attrs_post_init__(self):
try:
self.bus = get_bus("auto")
except RuntimeError as e:
raise DBusException(f"Could not connect to D-Bus: {e}")

self.pan_bus = self.bus.get("org.pantalaimon1")

self.ctl = self.pan_bus["org.pantalaimon1.control"]
self.devices = self.pan_bus["org.pantalaimon1.devices"]

self.own_message_ids = []

self.ctl.Response.connect(self.show_response)
self.ctl.UnverifiedDevices.connect(self.unverified_devices)

self.completer = PanCompleter(self.commands, self.ctl, self.devices)

self.devices.VerificationInvite.connect(self.show_sas_invite)
self.devices.VerificationString.connect(self.show_sas)
self.devices.VerificationDone.connect(self.sas_done)

self.devices.KeyRequest.connect(self.show_key_request)
self.devices.KeyRequestCancel.connect(self.show_key_request_cancel)

self.pan_bus = self.bus.get("org.pantalaimon1")

self.ctl = self.pan_bus["org.pantalaimon1.control"]
Expand Down Expand Up @@ -703,7 +728,7 @@ def main():

try:
panctl = PanCtl()
except GLib.Error as e:
except (DBusException, RuntimeError) as e:
print(f"Error, {e}")
sys.exit(-1)

Expand Down
Loading