[PATCH evemu v2 06/10] make-event-names: replace get_code/type_name functions with a unified set

Benjamin Tissoires benjamin.tissoires at gmail.com
Fri Aug 8 07:22:02 PDT 2014


On Fri, Aug 8, 2014 at 1:05 AM, Peter Hutterer <peter.hutterer at who-t.net> wrote:
> Python has default arguments, so we can have a function that does the right
> thing depending on the arguments passed in. And have two functions that
> explicitly fetch the name, or value, for an event.
>
> So now we can do things like
>   t = event_get_value("EV_ABS")
>   c = event_get_value("EV_ABS", "ABS_X")
>   t = event_get_name(0x02)
>   c = event_get_name(0x02, 0x03)
>   c = event_get_name("EV_ABS", 0x03)
>
> Signed-off-by: Peter Hutterer <peter.hutterer at who-t.net>
> ---

I have applied this v2, otherwise I would have to drop 09/10 and 10/10.
But I am still nitpicking for it a little. See inline.

> Changes to v1:
> - explicitly compare event_code to None, otherwise 0-values will be
>   considered false too

Grmbl... this broke 07/10... had to apply it manually... took me 5 min
at least...

>
>  src/make-event-names.py | 32 +++++++++++++++++++++++++-------
>  1 file changed, 25 insertions(+), 7 deletions(-)
>
> diff --git a/src/make-event-names.py b/src/make-event-names.py
> index 5fef9a6..e1a4f3d 100755
> --- a/src/make-event-names.py
> +++ b/src/make-event-names.py
> @@ -101,15 +101,33 @@ def print_python_mapping_table(bits):
>         print_python_map(bits)
>
>         p("""
> -       def event_get_type_name(type):
> -               return ev_map[type]

BTW, I was about to reject this due to the API breakage, but then I
realized that event_names.py is in python/evemu/ only in master, not
in any current release, so I guess it is fine considering it is not
public API.

> +       def event_get_name(event_type, event_code = None):
> +               '''
> +               Returns the event type or code name.
> +               '''
> +               if event_code == None:
> +                       if type(event_type) != int:
> +                               raise TypeError("expected an int")
> +                       return ev_map[event_type]
>
> +               if type(event_code) != int:
> +                       raise TypeError("expected an int")
>
> -       def event_get_code_name(type, code):
> -               try:
> -                       return map[type][code]
> -               except KeyError:
> -                       return 'UNKNOWN'
> +               return map[event_type][event_code]

The original code catches the KeyError exception. I wonder if we
should not catch it too.

Anyway, as mentioned above, patch applied in its current form. Please
submit further changes on top of master:
818b201..385b6e3  master -> master

Cheers,
Benjamin


More information about the Input-tools mailing list