diff mbox series

[kms-test,05/10] kmstest: Support specifying property values in percents

Message ID 20220609234031.14803-6-laurent.pinchart@ideasonboard.com (mailing list archive)
State New
Delegated to: Kieran Bingham
Headers show
Series Test plane alpha and zpos control | expand

Commit Message

Laurent Pinchart June 9, 2022, 11:40 p.m. UTC
If the property is a string that ends with a '%' character, treat it as
a percentage of the range reported by the property and convert it to the
corresponding numerical value.

Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
---
 tests/kmstest.py | 27 ++++++++++++++++++++++-----
 1 file changed, 22 insertions(+), 5 deletions(-)

Comments

Kieran Bingham June 29, 2022, 3:39 p.m. UTC | #1
Quoting Laurent Pinchart (2022-06-10 00:40:26)
> If the property is a string that ends with a '%' character, treat it as
> a percentage of the range reported by the property and convert it to the
> corresponding numerical value.
> 

This seems neat, I'm guessing that following patches are about to show
me the use case, so I'll just throw this in now:


Reviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>

> Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
> ---
>  tests/kmstest.py | 27 ++++++++++++++++++++++-----
>  1 file changed, 22 insertions(+), 5 deletions(-)
> 
> diff --git a/tests/kmstest.py b/tests/kmstest.py
> index 2afaa513aa4d..a99bf3b89d34 100755
> --- a/tests/kmstest.py
> +++ b/tests/kmstest.py
> @@ -258,8 +258,25 @@ class AtomicRequest(pykms.AtomicReq):
>          self.__test = test
>          self.__props = {}
>  
> -    def __format_props(self, props):
> -        return {k: v & ((1 << 64) - 1) for k, v in props.items()}
> +    def __format_props(self, obj, props):
> +        out = {}
> +        for k, v in props.items():
> +            if isinstance(v, str):
> +                if v.endswith('%'):
> +                    prop = obj.get_prop(k)
> +                    if prop.type not in (pykms.PropertyType.Range, pykms.PropertyType.SignedRange):
> +                        raise RuntimeError(f'Unsupported property type {prop.type} for value {v}')
> +
> +                    min, max = prop.values
> +                    v = min + int((max - min) * int(v[:-1]) / 100)
> +                else:
> +                    v = int(v)
> +
> +            if not isinstance(v, int):
> +                raise RuntimeError(f'Unsupported value type {type(v)} for property {k}')
> +
> +            out[k] = v & ((1 << 64) - 1)
> +        return out
>  
>      def add(self, obj, *kwargs):
>          if obj.id not in self.__props:
> @@ -267,13 +284,13 @@ class AtomicRequest(pykms.AtomicReq):
>          obj_props = self.__props[obj.id]
>  
>          if len(kwargs) == 1 and isinstance(kwargs[0], collections.abc.Mapping):
> -            props = self.__format_props(kwargs[0])
> +            props = self.__format_props(obj, kwargs[0])
>          elif len(kwargs) == 2:
> -            props = self.__format_props({ kwargs[0]: = kwargs[1] })
> +            props = self.__format_props(obj, { kwargs[0]: kwargs[1] })
>  
>          obj_props.update(props)
>  
> -        super().add(obj, *kwargs)
> +        super().add(obj, props)
>  
>      def commit(self, data=0, allow_modeset=False):
>          ret = super().commit(data, allow_modeset)
> -- 
> Regards,
> 
> Laurent Pinchart
>
diff mbox series

Patch

diff --git a/tests/kmstest.py b/tests/kmstest.py
index 2afaa513aa4d..a99bf3b89d34 100755
--- a/tests/kmstest.py
+++ b/tests/kmstest.py
@@ -258,8 +258,25 @@  class AtomicRequest(pykms.AtomicReq):
         self.__test = test
         self.__props = {}
 
-    def __format_props(self, props):
-        return {k: v & ((1 << 64) - 1) for k, v in props.items()}
+    def __format_props(self, obj, props):
+        out = {}
+        for k, v in props.items():
+            if isinstance(v, str):
+                if v.endswith('%'):
+                    prop = obj.get_prop(k)
+                    if prop.type not in (pykms.PropertyType.Range, pykms.PropertyType.SignedRange):
+                        raise RuntimeError(f'Unsupported property type {prop.type} for value {v}')
+
+                    min, max = prop.values
+                    v = min + int((max - min) * int(v[:-1]) / 100)
+                else:
+                    v = int(v)
+
+            if not isinstance(v, int):
+                raise RuntimeError(f'Unsupported value type {type(v)} for property {k}')
+
+            out[k] = v & ((1 << 64) - 1)
+        return out
 
     def add(self, obj, *kwargs):
         if obj.id not in self.__props:
@@ -267,13 +284,13 @@  class AtomicRequest(pykms.AtomicReq):
         obj_props = self.__props[obj.id]
 
         if len(kwargs) == 1 and isinstance(kwargs[0], collections.abc.Mapping):
-            props = self.__format_props(kwargs[0])
+            props = self.__format_props(obj, kwargs[0])
         elif len(kwargs) == 2:
-            props = self.__format_props({ kwargs[0]: = kwargs[1] })
+            props = self.__format_props(obj, { kwargs[0]: kwargs[1] })
 
         obj_props.update(props)
 
-        super().add(obj, *kwargs)
+        super().add(obj, props)
 
     def commit(self, data=0, allow_modeset=False):
         ret = super().commit(data, allow_modeset)