[PATCH evemu 12/12] python: provide EventType and EventCode as a special object

Benjamin Tissoires benjamin.tissoires at gmail.com
Mon Jul 28 08:28:23 PDT 2014


On Tue, Jul 22, 2014 at 7:42 PM, Peter Hutterer
<peter.hutterer at who-t.net> wrote:
> Depending on its use it's either an int or a string.
>
> Signed-off-by: Peter Hutterer <peter.hutterer at who-t.net>
> ---
> This one is probably controversial and I'm not 100% sure it's a good idea
> yet either. I just liked being able to print or use the value and have it
> work as expected. It's not like we need the values to be actual integers for
> mathematical operations.
> This would be a case for enums in Python3, but not as long as we support
> Python 2.x as well.
>

I am not a big fan either. This IMO add some more complexity in the
event handling, and by reading the code in InputEvent at first sight,
I did not get what happened. How about just relying on the C functions
evemu_write_event and write_event_desc directly so that the formatting
is not made in Python?

BTW, I ended up applying 01/12 to 10/12. I'll wait for your input on
11/12 and this one before going further.

   81ef21d..7b65861  master -> master

Cheers,
Benjamin

>  python/evemu/__init__.py | 35 ++++++++++++++++++++++++++++++-----
>  1 file changed, 30 insertions(+), 5 deletions(-)
>
> diff --git a/python/evemu/__init__.py b/python/evemu/__init__.py
> index eae8c35..28fcc85 100644
> --- a/python/evemu/__init__.py
> +++ b/python/evemu/__init__.py
> @@ -36,14 +36,39 @@ class InvalidEventStringError(Exception):
>      def __str__(self):
>          return self.s
>
> +class _EventIdentifier(object):
> +    def __str__(self): return self.s
> +    def __unicode__(self): return self.s
> +    def __int__(self): return self.val
> +    def __long__(self): return self.val
> +    def __eq__(self, other):
> +        try:
> +            return self.s == other.s
> +        except AttributeError:
> +            return other == self.s or other == self.val
> +    def __ne__(self, other):
> +        return not self.__eq__(other)
> +    def __cmp__(self, other):
> +        return cmp(self.val, other.val)
> +
> +class EventCode(_EventIdentifier):
> +    def __init__(self, type, code):
> +        self.val = code
> +        self.s = event_names.event_get_code_name(type, code)
> +
> +class EventType(_EventIdentifier):
> +    def __init__(self, type):
> +        self.val = type
> +        self.s = event_names.event_get_type_name(type)
> +
>  class InputEvent(object):
>      __slots__ = 'sec', 'usec', 'type', 'code', 'value'
>
>      def __init__(self, sec, usec, type, code, value):
>          self.sec = sec
>          self.usec = usec
> -        self.type = type
> -        self.code = code
> +        self.type = EventType(type)
> +        self.code = EventCode(type, code)
>          self.value = value
>
>      def __str__(self):
> @@ -52,11 +77,11 @@ class InputEvent(object):
>          s = "E: %d.%d %04x %04x %04d\t" % (self.sec, self.usec, type, code, value)
>          if type == event_names.ev_map["EV_SYN"]:
>              if code == event_names.syn_map["SYN_MT_REPORT"]:
> -                s += "# ++++++++++++ %s (%d) ++++++++++" % (event_names.event_get_code_name(type, code), value)
> +                s += "# ++++++++++++ %s (%d) ++++++++++" % (code, value)
>              else:
> -                s += "# ------------ %s (%d) ----------" % (event_names.event_get_code_name(type, code), value)
> +                s += "# ------------ %s (%d) ----------" % (code, value)
>          else:
> -            s += "# %s / %-20s %d" % (event_names.event_get_type_name(type), event_names.event_get_code_name(type, code), value)
> +            s += "# %s / %-20s %d" % (type, code, value)
>          return s
>
>      @classmethod
> --
> 1.9.3
>
> _______________________________________________
> Input-tools mailing list
> Input-tools at lists.freedesktop.org
> http://lists.freedesktop.org/mailman/listinfo/input-tools


More information about the Input-tools mailing list