Skip to content

Conversation

@allhailwesttexas
Copy link
Contributor

Using the CLI requires system-level dbus packages to be installed, which vary by distro and can sometimes cause problems even when installed if trying to run spotifycli in an isolated env (like installing with pipx or uv). To sidestep this issue, switch to using jeepney, a pure python implementation of the dbus interface. Since it is lower level, the logic ends up a little bit more complex. This could be improved, but since the changes are still quite small it didn't feel worthwhile at this point.

Closes #53.

I have written a script to test the output of all the supported CLI endpoints, but writing stable tests would require a bigger refactor, so I won't add it to the commit, I'll just paste it in here (since it doesn't rely on mocking the dbus response and instead requires exact setup).

import io
import sys
from contextlib import redirect_stdout
from unittest.mock import patch

from spotifycli.spotifycli import main

def test_main():

    cases = (
        ("--status", "Tindersticks - Travelling Light\n"),
        ("--statusshort", "Tindersticks - Travelling...\n"),
        ("--statusposition", "Tindersticks - Travelling Light (00:07/04:51)\n"),
        ("--song", "Travelling Light\n"),
        ("--songshort", "Travelling...\n"),
        ("--artist", "Tindersticks\n"),
        ("--artistshort", "Tindersticks\n"),
        ("--album", "Tindersticks\n"),
        ("--position", "(00:07/04:51)\n"),
        ("--playbackstatus", "▮▮\n"),
        ("--lyrics", "lyrics not found\n"),
        ("--arturl", "https://i.scdn.co/image/ab67616d0000b273cdd1aabdf50a42ca4556d31d\n"),
    )

    for flag, expected in cases:
        f = io.StringIO()
        with redirect_stdout(f), patch.object(sys, 'argv', ['spotifycli', '--client', 'spotify', flag]):
            main()
            result = f.getvalue()
            assert result == expected

@allhailwesttexas allhailwesttexas marked this pull request as ready for review December 2, 2024 21:03
Using the CLI requires system-level dbus packages to be installed, which
vary by distro and can sometimes cause problems even when installed if
trying to run `spotifycli` in an isolated env (like installing with pipx
or uv). To sidestep this issue, switch to using `jeepney`, a pure python
implementation of the dbus interface. Since it is lower level, the
logic ends up a little bit more complex. This could be improved, but
since the changes are still quite small it didn't feel worthwhile at
this point.

Closes pwittchen#53.

I have written a script to test the output of all the supported CLI
endpoints, but writing stable tests would require a bigger refactor,
so I won't add it to the commit, I'll just paste it in here (since it
doesn't rely on mocking the dbus response and instead requires exact
setup).

```python
import io
import sys
from contextlib import redirect_stdout
from unittest.mock import patch

from spotifycli.spotifycli import main

def test_main():

    cases = (
        ("--status", "Tindersticks - Travelling Light\n"),
        ("--statusshort", "Tindersticks - Travelling...\n"),
        ("--statusposition", "Tindersticks - Travelling Light (00:07/04:51)\n"),
        ("--song", "Travelling Light\n"),
        ("--songshort", "Travelling...\n"),
        ("--artist", "Tindersticks\n"),
        ("--artistshort", "Tindersticks\n"),
        ("--album", "Tindersticks\n"),
        ("--position", "(00:07/04:51)\n"),
        ("--playbackstatus", "▮▮\n"),
        ("--lyrics", "lyrics not found\n"),
        ("--arturl", "https://i.scdn.co/image/ab67616d0000b273cdd1aabdf50a42ca4556d31d\n"),
    )

    for flag, expected in cases:
        f = io.StringIO()
        with redirect_stdout(f), patch.object(sys, 'argv', ['spotifycli', '--client', 'spotify', flag]):
            main()
            result = f.getvalue()
            assert result == expected
```
@allhailwesttexas allhailwesttexas force-pushed the peter/use-pure-python-dbus-interface branch from 5060d9e to 0697f64 Compare December 2, 2024 21:56
@pwittchen
Copy link
Owner

Thanks for the PR!

@pwittchen pwittchen merged commit 108c4d1 into pwittchen:develop Dec 3, 2024
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

No Module Named Dbus after Installing Dbus

2 participants