# Copyright 2016 ARM Limited # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # import struct import datetime import os class ReventParser(object): """ Parses revent binary recording files so they can be easily read within python. """ int32_struct = struct.Struct("= 30: raise ValueError("path length too long. corrupt file") self.device_paths.append(f.read(path_length)) while f.tell() < os.path.getsize(path): device_id, sec, usec, typ, code, value = _read_struct(f, self.event_struct) yield (device_id, datetime.datetime.fromtimestamp(sec + float(usec) / 1000000), typ, code, value) @staticmethod def check_revent_file(path): """ Checks whether a file starts with "REVENT" """ with open(path, "rb") as f: magic, file_version = _read_struct(f, ReventParser.header_struct) if magic != "REVENT": msg = "'{}' isn't an revent file, are you using an old recording?" raise ValueError(msg.format(path)) return file_version @staticmethod def get_revent_duration(path): """ Takes an ReventParser and returns the duration of the revent recording in seconds. """ revent_parser = ReventParser().parse(path) first = last = next(revent_parser) for last in revent_parser: pass return (last[1] - first[1]).total_seconds() def _read_struct(f, struct_spec): data = f.read(struct_spec.size) return struct_spec.unpack(data)