diff --git a/docs/parsers/xrandr.md b/docs/parsers/xrandr.md index 63ebbeec..77832a0e 100644 --- a/docs/parsers/xrandr.md +++ b/docs/parsers/xrandr.md @@ -211,4 +211,4 @@ Returns: ### Parser Information Compatibility: linux, darwin, cygwin, aix, freebsd -Version 1.3 by Kevin Lyter (code (at) lyterk.com) +Version 1.4 by Kevin Lyter (code (at) lyterk.com) diff --git a/jc/parsers/xrandr.py b/jc/parsers/xrandr.py index a785be35..1792509b 100644 --- a/jc/parsers/xrandr.py +++ b/jc/parsers/xrandr.py @@ -26,22 +26,24 @@ Schema: "current_height": integer, "maximum_width": integer, "maximum_height": integer, - "devices": { - "modes": [ - { - "resolution_width": integer, - "resolution_height": integer, - "is_high_resolution": boolean, - "frequencies": [ - { - "frequency": float, - "is_current": boolean, - "is_preferred": boolean - } - ] - } - ] - }, + "devices": [ + { + "modes": [ + { + "resolution_width": integer, + "resolution_height": integer, + "is_high_resolution": boolean, + "frequencies": [ + { + "frequency": float, + "is_current": boolean, + "is_preferred": boolean + } + ] + } + ] + } + ], "is_connected": boolean, "is_primary": boolean, "device_name": string, @@ -73,50 +75,52 @@ Examples: "current_height": 1080, "maximum_width": 32767, "maximum_height": 32767, - "devices": { - "modes": [ - { - "resolution_width": 1920, - "resolution_height": 1080, - "is_high_resolution": false, - "frequencies": [ - { - "frequency": 60.03, - "is_current": true, - "is_preferred": true - }, - { - "frequency": 59.93, - "is_current": false, - "is_preferred": false - } - ] - }, - { - "resolution_width": 1680, - "resolution_height": 1050, - "is_high_resolution": false, - "frequencies": [ - { - "frequency": 59.88, - "is_current": false, - "is_preferred": false - } - ] - } - ], - "is_connected": true, - "is_primary": true, - "device_name": "eDP1", - "resolution_width": 1920, - "resolution_height": 1080, - "offset_width": 0, - "offset_height": 0, - "dimension_width": 310, - "dimension_height": 170, - "rotation": "normal", - "reflection": "normal" - } + "devices": [ + { + "modes": [ + { + "resolution_width": 1920, + "resolution_height": 1080, + "is_high_resolution": false, + "frequencies": [ + { + "frequency": 60.03, + "is_current": true, + "is_preferred": true + }, + { + "frequency": 59.93, + "is_current": false, + "is_preferred": false + } + ] + }, + { + "resolution_width": 1680, + "resolution_height": 1050, + "is_high_resolution": false, + "frequencies": [ + { + "frequency": 59.88, + "is_current": false, + "is_preferred": false + } + ] + } + ], + "is_connected": true, + "is_primary": true, + "device_name": "eDP1", + "resolution_width": 1920, + "resolution_height": 1080, + "offset_width": 0, + "offset_height": 0, + "dimension_width": 310, + "dimension_height": 170, + "rotation": "normal", + "reflection": "normal" + } + ] } ] } @@ -132,53 +136,55 @@ Examples: "current_height": 1080, "maximum_width": 32767, "maximum_height": 32767, - "devices": { - "modes": [ - { - "resolution_width": 1920, - "resolution_height": 1080, - "is_high_resolution": false, - "frequencies": [ - { - "frequency": 60.03, - "is_current": true, - "is_preferred": true - }, - { - "frequency": 59.93, - "is_current": false, - "is_preferred": false - } - ] - }, - { - "resolution_width": 1680, - "resolution_height": 1050, - "is_high_resolution": false, - "frequencies": [ - { - "frequency": 59.88, - "is_current": false, - "is_preferred": false - } - ] - } - ], - "is_connected": true, - "is_primary": true, - "device_name": "eDP1", - "model_name": "ASUS VW193S", - "product_id": "54297", - "serial_number": "78L8021107", - "resolution_width": 1920, - "resolution_height": 1080, - "offset_width": 0, - "offset_height": 0, - "dimension_width": 310, - "dimension_height": 170, - "rotation": "normal", - "reflection": "normal" - } + "devices": [ + { + "modes": [ + { + "resolution_width": 1920, + "resolution_height": 1080, + "is_high_resolution": false, + "frequencies": [ + { + "frequency": 60.03, + "is_current": true, + "is_preferred": true + }, + { + "frequency": 59.93, + "is_current": false, + "is_preferred": false + } + ] + }, + { + "resolution_width": 1680, + "resolution_height": 1050, + "is_high_resolution": false, + "frequencies": [ + { + "frequency": 59.88, + "is_current": false, + "is_preferred": false + } + ] + } + ], + "is_connected": true, + "is_primary": true, + "device_name": "eDP1", + "model_name": "ASUS VW193S", + "product_id": "54297", + "serial_number": "78L8021107", + "resolution_width": 1920, + "resolution_height": 1080, + "offset_width": 0, + "offset_height": 0, + "dimension_width": 310, + "dimension_height": 170, + "rotation": "normal", + "reflection": "normal" + } + ] } ] } @@ -192,7 +198,6 @@ from jc.parsers.pyedid.helpers.edid_helper import EdidHelper class info: """Provides parser metadata (version, author, etc.)""" - version = "1.4" description = "`xrandr` command parser" author = "Kevin Lyter" @@ -206,8 +211,18 @@ class info: __version__ = info.version # keep parsing state so we know which parsers have already tried the line +# Structure is: +# { +# : [ +# +# ] +# } +# +# Where is the xrandr output line to be checked and +# can contain "screen", "device", or "model" parse_state: Dict[str, List] = {} + def _was_parsed(line: str, parser: str) -> bool: """ Check if entered parser has already parsed. If so return True. @@ -522,9 +537,10 @@ def parse(data: str, raw: bool = False, quiet: bool = False) -> Dict: linedata = data.splitlines() linedata.reverse() # For popping - result: Response = {"screens": []} + result: Dict = {} if jc.utils.has_data(data): + result = {"screens": []} while linedata: screen = _parse_screen(linedata) if screen: diff --git a/tests/test_xrandr.py b/tests/test_xrandr.py index 45414105..dc2f578e 100644 --- a/tests/test_xrandr.py +++ b/tests/test_xrandr.py @@ -1,4 +1,3 @@ -import json import re import unittest from typing import Optional @@ -22,8 +21,6 @@ from jc.parsers.xrandr import ( ) import jc.parsers.xrandr -import pprint - class XrandrTests(unittest.TestCase): def setUp(self): @@ -33,7 +30,7 @@ class XrandrTests(unittest.TestCase): """ Test 'xrandr' with no data """ - self.assertEqual(parse("", quiet=True), {"screens": []}) + self.assertEqual(parse("", quiet=True), {}) def test_regexes(self): devices = [