ahoneybun / keyboard-color-switcher

An application to change the keyboard backlight color on System76 laptops
15 stars 7 forks source link

AttributeError: 'gi.repository.Gtk' object has no attribute 'Container #9

Closed yochananmarqos closed 3 years ago

yochananmarqos commented 3 years ago

I just tried it from the 1.0 tag:

Traceback (most recent call last):
  File "/opt/keyboard-color-switcher/keyboard-color-switcher.py", line 7, in <module>
    from gi.repository import Gtk, Gio
  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
  File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 664, in _load_unlocked
  File "<frozen importlib._bootstrap>", line 627, in _load_backward_compatible
  File "/usr/lib/python3.9/site-packages/gi/importer.py", line 145, in load_module
    dynamic_module = load_overrides(introspection_module)
  File "/usr/lib/python3.9/site-packages/gi/overrides/__init__.py", line 118, in load_overrides
    override_mod = importlib.import_module(override_package_name)
  File "/usr/lib/python3.9/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "/usr/lib/python3.9/site-packages/gi/overrides/Gtk.py", line 168, in <module>
    class Container(Gtk.Container, Widget):
  File "/usr/lib/python3.9/site-packages/gi/module.py", line 123, in __getattr__
    raise AttributeError("%r object has no attribute %r" % (
AttributeError: 'gi.repository.Gtk' object has no attribute 'Container'

Serval WS Manjaro GNOME gtk3 3.24.24 python 3.9.1 python-gobject 3.38.0

ahoneybun commented 3 years ago

What did you run for this to happen? Is it from the build process for PKGBUILD or from running sudo python3 keyboard-color-switcher.py ? What is the hardware as well?

JeffLabonte commented 3 years ago

@ahoneybun it seems like it is unable to import Gtk because it doesn't have Container?

ahoneybun commented 3 years ago

It's just odd that I haven't seen it on my system (oryp4). I'll boot in to my Manjaro to test.

yochananmarqos commented 3 years ago

I'm working on a PKGBUILD with a Polkit policy which worked earlier today. I also tried running sudo python keyboard-color-switcher.py directly from the repo as well. This is on my Serval WS.

ahoneybun commented 3 years ago

I can't recreate it since my other system is a galp3-b which doesn't have the backlights.

ahoneybun commented 3 years ago

I wonder if it is because python was used instead of python3?

JeffLabonte commented 3 years ago

Arch Linux tends to use bleeding edge software and disregard older versions. There could be a breaking change in the version used by @yochananmarqos. It will need more investigation.

You are using the latest version of Manjaro with Gnome @yochananmarqos? Any special repository or using unstable repositories from Manjaro?

yochananmarqos commented 3 years ago

@ahoneybun On Arch, python is Python 3.

On Ubuntu, it's the other way around: python is Python 2.

I'm using the Manjaro unstable branch which syncs with Arch stable multiple times a day.

ahoneybun commented 3 years ago

@JeffLabonte can you help me disable the laptop_model check so I can launch the application?

@yochananmarqos can you share your pacman.conf file for the unstable branch?

yochananmarqos commented 3 years ago

There was just a stable update today, you'll have the same software versions just about.

See here for switching branches: https://wiki.manjaro.org/index.php/Switching_Branches

JeffLabonte commented 3 years ago

@ahoneybun in the file keyboard_backlight.py in folder kcc_cli, you can change self.laptop_model at line 25 for string.

You can set oryp4 if you have 3 backlight path and oryp6 if you have one. Also, you can comment out line 23 in this case since it won't be used.

ahoneybun commented 3 years ago

@yochananmarqos I'm trying to set up Manjaro on my oryp4 but I don't have the /sys/class/leds path and I can't use the key combos.

yochananmarqos commented 3 years ago

@ahoneybun Install system76-oled from the AUR. I have all the driver packages installed, so I'm not sure if any others are required.

local/system76-acpi-dkms 1.0.2+1+g0bc966c-1
    This provides the system76_acpi in-tree driver for systems missing it.
local/system76-dkms 1.0.12-2
    On newer System76 laptops, this driver controls some of the hotkeys and allows for custom
    fan control.
local/system76-driver 20.04.27-1
    Universal driver for System76 computers
local/system76-firmware-daemon 1.0.23-2
    System76 CLI tool for installing firmware updates and systemd service that exposes a DBUS API for handling firmware updates
local/system76-io-dkms 1.0.1-1
    This DKMS driver controls System76 IO devices
local/system76-oled 0.1.4-1
    Control brightness on System76 OLED displays
local/system76-power 1.1.15-1
    System76 Power Management
ahoneybun commented 3 years ago

It looks like it works now! I'll test the AUR package now.

ahoneybun commented 3 years ago

Looks like it built for me but when I tried to run it I get this:

/opt/keyboard-color-switcher/keyboard-color-switcher.py:7: PyGIWarning: Gtk was imported without specifying a version first. Use gi.require_version('Gtk', '3.0') before import to ensure that the right version gets loaded. from gi.repository import Gtk, Gio Traceback (most recent call last): File "/opt/keyboard-color-switcher/keyboard-color-switcher.py", line 9, in from kcc_cli.keyboard_backlight import KeyboardBacklight ModuleNotFoundError: No module named 'kcc_cli'

yochananmarqos commented 3 years ago

I updated my PKGBUILD to install to site-packages instead and included the folders:

/usr/lib/python3.9/site-packages/keyboard-color-switcher
├── kcc
└── kcc_cli
└── keyboard-color-switcher.py

The error output is the same. What am I missing? Does kcc_cli need to be in a separate site-packages folder, kcc_cli.keyboard_backlight? I tried that, but it made no difference.

I tried this as well:

/usr/lib/python3.9/site-packages/kcc
/usr/lib/python3.9/site-packages/kcc_cli
/usr/lib/python3.9/site-packages/keyboard-color-switcher
└── keyboard-color-switcher.py
ahoneybun commented 3 years ago

I did a git pull then built the package again and it works for me.

yochananmarqos commented 3 years ago

Hmm... I had gtk4 installed for testing and just removed it.

/usr/lib/python3.9/site-packages/keyboard-color-switcher/keyboard-color-switcher.py:7: PyGIWarning: Gtk was imported without specifying a version first. Use gi.require_version('Gtk', '3.0') before import to ensure that the right version gets loaded.
  from gi.repository import Gtk, Gio
Traceback (most recent call last):
  File "/usr/lib/python3.9/site-packages/keyboard-color-switcher/keyboard-color-switcher.py", line 110, in <module>
    win = MainWindow()
  File "/usr/lib/python3.9/site-packages/keyboard-color-switcher/keyboard-color-switcher.py", line 27, in __init__
    self.keyboard_backlight = KeyboardBacklight()
  File "/usr/lib/python3.9/site-packages/keyboard-color-switcher/kcc_cli/keyboard_backlight.py", line 30, in __init__
    f"{laptop_model} is not supported by this script"
NameError: name 'laptop_model' is not defined
ahoneybun commented 3 years ago

It looks like it isn't detecting this model as supported. What version of the Serval is this? I think only the serw12 is in the program right now.

yochananmarqos commented 3 years ago

Yes, it's the serw12.

❯ sudo dmidecode -s system-product-name
Serval WS
❯ sudo dmidecode -s system-version
serw12
ahoneybun commented 3 years ago

I'm not sure how @JeffLabonte set it to detect models and if it is using a database to do that. It has to somewhere.

ahoneybun commented 3 years ago

Oh I see it!

yochananmarqos commented 3 years ago

serw12 is missing. I added 'serw12': FOUR_BACKLIGHT_PATH, and now...

/usr/lib/python3.9/site-packages/keyboard-color-switcher/keyboard-color-switcher.py:7: PyGIWarning: Gtk was imported without specifying a version first. Use gi.require_version('Gtk', '3.0') before import to ensure that the right version gets loaded.
  from gi.repository import Gtk, Gio
Traceback (most recent call last):
  File "/usr/lib/python3.9/site-packages/keyboard-color-switcher/keyboard-color-switcher.py", line 110, in <module>
    win = MainWindow()
  File "/usr/lib/python3.9/site-packages/keyboard-color-switcher/keyboard-color-switcher.py", line 29, in __init__
    Gtk.Window.__init__(self)
  File "/usr/lib/python3.9/site-packages/gi/overrides/Gtk.py", line 515, in __init__
    raise RuntimeError(
RuntimeError: Gtk couldn't be initialized. Use Gtk.init_check() if you want to handle this case
ahoneybun commented 3 years ago

Can you share this output?

cat /sys/class/leds/

yochananmarqos commented 3 years ago
❯ ls /sys/class/leds
input30::capslock  input30::numlock input4::numlock     system76::airplane
input30::compose   input30::scrolllock  input4::scrolllock  system76::kbd_backlight
input30::kana      input4::capslock phy0-led
❯ ls /sys/class/leds/system76::kbd_backlight
brightness         color_center  color_left   device      power      trigger
brightness_hw_changed  color_extra   color_right  max_brightness  subsystem  uevent

Only changing color_left does anything.

yochananmarqos commented 3 years ago

I added the following here:

import gi
gi.require_version('Gtk', '3.0') # <--
from gi.repository import Gtk, Gio

This works now, however running GUI applications as root isn't really the greatest idea in the world.

sudo /usr/lib/python3.9/site-packages/keyboard-color-switcher/keyboard-color-switcher.py

Well, so much for the Polkit policy. For some reason that was causing an issue:

❯ pkexec /usr/lib/python3.9/site-packages/keyboard-color-switcher/keyboard-color-switcher.py

Unable to init server: Could not connect: Connection refused
Unable to init server: Could not connect: Connection refused
Traceback (most recent call last):
  File "/usr/lib/python3.9/site-packages/keyboard-color-switcher/keyboard-color-switcher.py", line 111, in <module>
    win = MainWindow()
  File "/usr/lib/python3.9/site-packages/keyboard-color-switcher/keyboard-color-switcher.py", line 30, in __init__
    Gtk.Window.__init__(self)
  File "/usr/lib/python3.9/site-packages/gi/overrides/Gtk.py", line 515, in __init__
    raise RuntimeError(
RuntimeError: Gtk couldn't be initialized. Use Gtk.init_check() if you want to handle this case.

PKGBUILD is updated to use gksu instead.

ahoneybun commented 3 years ago

It looks better with that prompt though I don't know if that works in Wayland or not though that's another task to deal with later I think.

yochananmarqos commented 3 years ago

With zensu (provides gksu) you can use spacefm, kdialog, yad or zenity for the prompt. It will look for them in that order. I have yad installed as a dependency for something else, so it's using that for me.

With pkexec, it uses the system dialog (or whatever it's called) just like when systemctl enable some.service is used, for instance.

ahoneybun commented 3 years ago

@JeffLabonte so it sounds like color_extra is for the backlight on the front of some systems like the serw11 and serw12.

ahoneybun commented 3 years ago

Using zensu certainly works for what we need!

ahoneybun commented 3 years ago

I don't think system76-oled is really needed tbh.

yochananmarqos commented 3 years ago

it sounds like color_extra is for the backlight on the front of some systems like the serw11 and serw12.

You mean on the lid? That color doesn't change. color_left is the whole keyboard, the others don't do anything.

Using zensu certainly works for what we need!

It's not ideal, but it'll work for now.

I don't think system76-oled is really needed tbh.

But you said...

I don't have the /sys/class/leds path and I can't use the key combos.

What did you install that exposed the path, then? You'll need system76-dkms for the hotkeys.

ahoneybun commented 3 years ago

I didn't have the system76-dkms package when I installed the oled package. Then I installed dkms then rebooted and the path was there.

ahoneybun commented 3 years ago

I'm sorry for the extra messages I believe we are getting off course to fix your issue. I'm a bit worried about these messages as well:

Unable to init server: Could not connect: Connection refused Unable to init server: Could not connect: Connection refused

Are you using Xorg or Wayland?

yochananmarqos commented 3 years ago

I believe we are getting off course to fix your issue

It's already fixed, see above. Once #10 is merged and the gi.require_version('Gtk', '3.0') is added, anyway.

Unable to init server: Could not connect: Connection refused Unable to init server: Could not connect: Connection refused

That was from pkexec, don't worry about that.

Are you using Xorg or Wayland?

Xorg because NVIDIA.

yochananmarqos commented 3 years ago

Merge #11 and we're golden!

ahoneybun commented 3 years ago

Is the main issue fixed as well?

yochananmarqos commented 3 years ago

See: #12. Actually, don't merge that yet. I'll see what else I can add to it.

If you mean the issues with using the Polkit policy, no. Still the same output as above.

ahoneybun commented 3 years ago

Alright so I recreated it on my system though I can run the application just fine like this:

keyboard-color-switcher

yochananmarqos commented 3 years ago

Right, so can I. Effectively, the issue is solved.

Part of the issue was not declaring the GTK version. Since I also had Gtk 4 installed, it threw it off.

The other part of the issue was using the Polkit policy which I've put on hold for now. I'll figure that out later.

ahoneybun commented 3 years ago

I'll keep looking into it when I can unless @JeffLabonte has any ideas. There don't seem to be a ton of reports of the behavior.

yochananmarqos commented 3 years ago

Well, no. Not many people would have Gtk 4 installed yet, for one. Either way, that's solved now.

ahoneybun commented 3 years ago

I don't have GTK 4 installed as far as I know of.

yochananmarqos commented 3 years ago

No, you wouldn't as nothing stable requires it yet. I was testing new software.

ahoneybun commented 3 years ago

Well the oddness is that I can recreate the issue without GTK 4.

yochananmarqos commented 3 years ago

You can? After #11 was merged, it doesn't matter what other versions of Gtk are installed.

After today's unstable branch updates including gtk3 3.24.25, I now cannot reproduce the issue with the Polikit policy. My PKGBUILD is updated to install it again.

ahoneybun commented 3 years ago

I haven't run it today with the recent changes.

ahoneybun commented 3 years ago

It sounds like this issue has been fixed correct? I can run it like this with no issue:

sudo /usr/lib/python3.9/site-packages/keyboard-color-switcher/keyboard-color-switcher.py

yochananmarqos commented 3 years ago

If you installed it via my PKGBUILD, don't run it like that. The whole idea was to use pkexec. Just run keyboard-color-switcher.

JeffLabonte commented 3 years ago

@yochananmarqos @ahoneybun I will consider the issue fixed and close this issue! 😄