[PATCH evemu 11/12] python: move event string parsing over to the python bits

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


On Tue, Jul 22, 2014 at 7:42 PM, Peter Hutterer
<peter.hutterer at who-t.net> wrote:
> Signed-off-by: Peter Hutterer <peter.hutterer at who-t.net>
> ---
>  python/evemu/__init__.py        | 55 +++++++++++++++++++++++++++++++++++++++++
>  src/convert-old-dumps-to-1.1.py | 26 ++-----------------
>  2 files changed, 57 insertions(+), 24 deletions(-)
>
> diff --git a/python/evemu/__init__.py b/python/evemu/__init__.py
> index ea0901d..eae8c35 100644
> --- a/python/evemu/__init__.py
> +++ b/python/evemu/__init__.py
> @@ -30,6 +30,12 @@ import evemu.event_names
>  __all__ = ["Device"]
>
>
> +class InvalidEventStringError(Exception):
> +    def __init__(self, s):
> +        self.s = s
> +    def __str__(self):
> +        return self.s
> +
>  class InputEvent(object):
>      __slots__ = 'sec', 'usec', 'type', 'code', 'value'
>
> @@ -40,6 +46,55 @@ class InputEvent(object):
>          self.code = code
>          self.value = value
>
> +    def __str__(self):
> +        type, code, value = self.type, self.code, self.value
> +
> +        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)
> +            else:
> +                s += "# ------------ %s (%d) ----------" % (event_names.event_get_code_name(type, code), value)
> +        else:
> +            s += "# %s / %-20s %d" % (event_names.event_get_type_name(type), event_names.event_get_code_name(type, code), value)
> +        return s
> +
> +    @classmethod
> +    def from_string(cls, s):
> +        m = re.match(r"E: (\d+)\.(\d*) ([a-fA-f0-9]+) ([a-fA-f0-9]+) (-?\d+)", s)
> +        if not m:
> +            raise InvalidEventStringError(s)
> +
> +        sec, usec, type, code, value = m.groups()
> +        sec = int(sec)
> +        usec = int(usec)
> +        type = int(type, 16)
> +        code = int(code, 16)
> +        value = int(value)
> +        return InputEvent(sec, usec, type, code, value)
> +
> +    @classmethod
> +    def events(cls, data):
> +        """
> +        Return a list of events, parsed from the input data
> +
> +        Attributes:
> +        - data  list of strings, string, or file-like object containing the events
> +        """
> +
> +        if type(data) == str:
> +            lines = StringIO.StringIO(data).readlines()
> +        elif hasattr(data, "readlines"):
> +            lines = data.readlines()
> +        else:
> +            lines = data
> +
> +        for line in lines:
> +            try:
> +                yield InputEvent.from_string(line)
> +            except InvalidEventStringError:
> +                pass
> +
>  class Device(object):
>      """
>      Encapsulates a raw kernel input event device, either an existing one as
> diff --git a/src/convert-old-dumps-to-1.1.py b/src/convert-old-dumps-to-1.1.py
> index 4caf4df..7b773af 100755
> --- a/src/convert-old-dumps-to-1.1.py
> +++ b/src/convert-old-dumps-to-1.1.py
> @@ -17,33 +17,10 @@ import sys
>  import evemu
>  import evemu.event_names
>
> -def convert_events(lines):
> -       event_re = re.compile(r"E: (\d+\.\d*) ([a-fA-f0-9]+) ([a-fA-f0-9]+) (-?\d*)\n")
> -
> -       for line in lines:
> -               m = event_re.match(line)
> -               if m:
> -                       t, type, code, value = m.groups()
> -                       type = int(type, 16)
> -                       code = int(code, 16)
> -                       value = int(value, 0)
> -                       print("E: %s %04x %04x %04d\t" % (t, type, code, value))
> -                       desc = ""
> -                       if type == event_names.ev_map["EV_SYN"]:
> -                               if code == syn_map["SYN_MT_REPORT"]:
> -                                       print("# ++++++++++++ %s (%d) ++++++++++" % (event_names.event_get_code_name(type, code), value))
> -                               else:
> -                                       print("# ------------ %s (%d) ----------" % (event_names.event_get_code_name(type, code), value))
> -                       else:
> -                               print("# %s / %-20s %d" % (event_names.event_get_type_name(type), event_names.event_get_code_name(type, code), value))
> -               else:
> -                       print(line)
> -
>  def usage(args):
>         print("%s mydev.desc [mydev.events]" % os.path.basename(args[0]))
>         return 1
>
> -
>  if __name__ == "__main__":
>         if len(sys.argv) < 2:
>                 exit(usage(sys.argv))
> @@ -53,4 +30,5 @@ if __name__ == "__main__":
>         d = None
>         if len(sys.argv) > 2:
>                 with open(sys.argv[2]) as f:
> -                       convert_events(f.readlines())
> +                       for e in evemu.InputEvent.events(f):

Why don't you simply drop the regexp part of this patch and just reuse
the wrapper around evemu_read_event introduced in the previous patch?

Ideally, I'd rather drop all of the events formatting/parsing in the
python wrapper so that we do not have to remember to update it while
some modifications happens.

Cheers,
Benjamin

> +                               print(e)
> --
> 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