diff mbox series

[09/14] cros_ec_pwm: use RE to search EC backlight PWM

Message ID 20230313094431.507952-10-tzungbi@kernel.org (mailing list archive)
State Handled Elsewhere
Headers show
Series cros-ec-tests: fix some exceptions and clean-ups | expand

Commit Message

Tzung-Bi Shih March 13, 2023, 9:44 a.m. UTC
Use RE to search pattern instead of reading the file line by line.

Signed-off-by: Tzung-Bi Shih <tzungbi@kernel.org>
---
 cros/tests/cros_ec_pwm.py | 24 +++++++-----------------
 1 file changed, 7 insertions(+), 17 deletions(-)
diff mbox series

Patch

diff --git a/cros/tests/cros_ec_pwm.py b/cros/tests/cros_ec_pwm.py
index 2b7b4a3418b1..7158463b115f 100644
--- a/cros/tests/cros_ec_pwm.py
+++ b/cros/tests/cros_ec_pwm.py
@@ -2,6 +2,7 @@ 
 # -*- coding: utf-8 -*-
 
 from cros.helpers.sysfs import *
+import re
 import unittest
 
 
@@ -13,23 +14,12 @@  class TestCrosECPWM(unittest.TestCase):
         """
         if not os.path.exists("/sys/class/backlight/backlight/max_brightness"):
             self.skipTest("No backlight pwm found, skipping")
-        is_ec_pwm = False
-        fd = open("/sys/kernel/debug/pwm", "r")
-        line = fd.readline()
-        while line and not is_ec_pwm:
-            if line[0] != " " and ":ec-pwm" in line:
-                line = fd.readline()
-                while line:
-                    if line[0] == "\n":
-                        is_ec_pwm = False
-                        break
-                    if "backlight" in line:
-                        is_ec_pwm = True
-                        break
-                    line = fd.readline()
-            line = fd.readline()
-        fd.close()
-        if not is_ec_pwm:
+        with open("/sys/kernel/debug/pwm", "r") as fh:
+            pwm = fh.read()
+        for s in pwm.split('\n\n'):
+            if re.match(r'.*:ec-pwm.*backlight', s, re.DOTALL):
+                break
+        else:
             self.skipTest("No EC backlight pwm found, skipping")
         with open("/sys/class/backlight/backlight/max_brightness", "r") as fh:
             brightness = int(int(fh.read()) / 2)