Parse a little more

This commit is contained in:
Roberto Alsina 2020-02-05 16:58:08 -03:00
parent 0a8f169064
commit 91353d5951

View File

@ -2,6 +2,8 @@
import re
import parse
def _split_by_lines_matching(pattern, lines):
"""Return a list of groups of lines, splitting on lines
@ -29,15 +31,26 @@ class Field:
class Mode:
"""One of the modes for a monitor."""
"""One of the modes for a monitor.
Note:
mode.name is the hex thing, like "0x56", not "1920x1080"
"""
def __init__(self, data):
"""Initialize Mode from xrandr data."""
self.data = data
self.name = data[0].strip().split()[0]
self.header = data[0]
self.name = parse.search("({mode_name})", self.header)["mode_name"]
self.res_x = parse.search("h: width {res_x:d}", data[1])["res_x"]
self.res_y = parse.search("v: height {res_y:d}", data[2])["res_y"]
self.refresh = parse.search("{refresh:f}Hz", data[2])["refresh"]
self.preferred = "+preferred" in self.header
self.current = "*current" in self.header
def __repr__(self):
return self.data[0].strip()
return self.header.strip()
class Monitor:
@ -49,7 +62,8 @@ class Monitor:
data is a list of lines.
"""
_ = data.pop(0)
self.header = data.pop(0)
self.pos_x, self.pos_y = parse.search("+{:d}+{:d}", self.header)
modes_data = _split_by_lines_matching("^ [^ ]", data)
fields_data = _split_by_lines_matching(r"^\t[^ ]", modes_data.pop(0))
@ -60,3 +74,12 @@ class Monitor:
self.fields = {}
for f in (Field(d) for d in fields_data):
self.fields[f.name] = f
def get_current_mode_name(self):
for k, v in self.modes.values():
if v.current:
return k
def set_current_mode(self, mode_name):
for k, v in self.modes.items():
v.current = k == mode_name