Unnecessary dependency on acpi / unnecessary regex pattern matching in battery module
Currently the battery module (in modules/metrics.py) relies on acpi to query battery data and regex to parse the returned data:
https://github.com/Axenide/Ax-Shell/blob/2c5319b7ac2dc97e25646fcde32aaa160c4f14da/modules/metrics.py#L416
In my opinion, this should instead be done with psutil.sensors_battery(), as psutil is already used to query cpu-, ram- and disk-usage in that same file. This would remove the dependency on acpi and make the code more uniform.
This would also remove the expensive regex pattern matching to parse the data. (psutils.sensors_battery().percent supplies the battery usage directly: https://psutil.readthedocs.io/en/latest/index.html#psutil.sensors_battery)
I also am guessing that this would be a performance improvement.
I've already implemented thiswith a new class BatteryProvider, analogous to MetricsProvider, which then supplies the battery statistics in a similar fashion to the MetricsProvider class.
An alternative option would be to expand the MetricsProvider class to include a getBattery() method.
Which would be preferable here?
(Both of these are relatively easy to implement, i just need to know which one would be better here, so i know how to write the code for the pull request i want to open)
If there is an advantage to using acpi that I am not seeing, then I'm sorry for opening this issue :(
My current implementation is similar to this:
class BatteryProvider:
"""
Class responsible for obtaining centralized battery metrics.
It updates periodically so that all widgets querying it display the same values.
"""
def __init__(self):
self.percent = 0.0
self.charging = None
# Updates every 1 second
GLib.timeout_add_seconds(1, self._update)
def _update(self):
# The first call may return None, but subsequent calls will provide consistent values.
battery = psutil.sensors_battery()
if battery is None:
self.percent = 0.0
self.charging = None
else:
self.percent = battery.percent
self.charging = battery.power_plugged
expanding the existing MetricsProvider would look something like this:
class MetricsProvider:
def __init__(self):
self.cpu = 0.0
self.mem = 0.0
self.disk = 0.0
self.bat_percent = 0.0
self.bat_charging = None
# Updates every 1 second
GLib.timeout_add_seconds(1, self._update)
def _update(self):
# Get non-blocking usage percentages (interval=0)
# The first call may return 0, but subsequent calls will provide consistent values.
self.cpu = psutil.cpu_percent(interval=0)
self.mem = psutil.virtual_memory().percent
self.disk = psutil.disk_usage("/").percent
battery = psutil.sensors_battery()
if battery is None:
self.percent = 0.0
self.charging = None
else:
self.percent = battery.percent
self.charging = battery.power_plugged
return True
def get_metrics(self):
return (self.cpu, self.mem, self.disk)
def get_battery(self):
return (self.bat_percent, self.bat_charging)
Hey, that's really good. Do you wanna make a PR? So you can appear as a contributor. :)
yes of course :D I'll make the code pretty and open a pr