Skip to content

Add named attribute access to gnome-monitor.py (New) #1916

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 23 commits into
base: main
Choose a base branch
from

Conversation

tomli380576
Copy link
Contributor

@tomli380576 tomli380576 commented May 9, 2025

Description

This PR adds complete type annotations and named properties to the original gnome-monitors script to hopefully make it more ergonomic to use. It's not something that needs to be urgently merged so don't worry about this if you have a fire to fix xD

Basically turns this:

state = MonitorConfigGnome().get_current_state()
curr_refresh_rates = {} 

for monitor in state[1]:
    for mode in monitor[1]:
        if mode[6].get("is-current", False):
            curr_refresh_rates[monitor[0][0]] = mode[3]

print(curr_refresh_rates)

into this:

state = MonitorConfigGnome().get_current_state()
curr_refresh_rates = {}

for monitor in state.physical_monitors:
    for mode in monitor.modes:
        if mode.is_current:
            curr_refresh_rates[monitor.info.connector] = mode.refresh_rate

print(curr_refresh_rates)

with nice IDE autocomplete.

#1843 could take advantage of this to more directly access resolution numbers instead of strings

Resolved issues

In the original MonitorConfigGnome, accessing deeply nested properties from the GetCurrentState's return value was a bit awkward. If we need to get the 1st refresh rate of the 1st monitor, we need to write state[1][1][0][1][0][3] which looks like magic numbers without looking at the original dbus xml. The original MonitorConfigGnome provided nice ways to get the resolution strings but all the other properties were not included.

This PR circumvents this magic index situation by providing named attribute access for the return value of GetCurrentState, so now we can write state.physical_monitors[0].modes[0].refresh_rate and only use indices in places where there's an actual array.

Documentation

The main reference point is this xml and gnome-randr

None of the new classes (MutterDisplayConfig, PhysicalMonitor, LogicalMonitor, MonitorInfo) should be directly constructed because they assume that the input GLib.Variant has the correct type. Use MonitorConfigGnome.get_current_state() as the entry point since it asks GLib to do a deep type check first before returning.

Tests

Original unit tests

@tomli380576 tomli380576 changed the title Fully typed version of gnome-monitors.py (New) Add named attribute access for gnome-monitors.py (New) May 9, 2025
@tomli380576 tomli380576 force-pushed the typed-gnome-monitors branch from b37882d to 7e759db Compare May 9, 2025 07:28
Copy link

codecov bot commented May 9, 2025

Codecov Report

Attention: Patch coverage is 91.60839% with 12 lines in your changes missing coverage. Please review.

Project coverage is 50.83%. Comparing base (c5c19c7) to head (967fa79).
Report is 10 commits behind head on main.

Files with missing lines Patch % Lines
...box-support/checkbox_support/dbus/gnome_monitor.py 88.67% 9 Missing and 3 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1916      +/-   ##
==========================================
+ Coverage   50.46%   50.83%   +0.37%     
==========================================
  Files         382      383       +1     
  Lines       41039    41423     +384     
  Branches     6892     6957      +65     
==========================================
+ Hits        20709    21058     +349     
- Misses      19585    19607      +22     
- Partials      745      758      +13     
Flag Coverage Δ
checkbox-support 64.37% <91.60%> (+1.85%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@tomli380576 tomli380576 marked this pull request as ready for review May 14, 2025 10:29
@tomli380576 tomli380576 changed the title Add named attribute access for gnome-monitors.py (New) Add named attribute access to gnome-monitor.py (New) May 16, 2025
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.

1 participant