Open function to handle three types of input data. If filename of a regular candump log file is provided, this function opens the file and returns the file object. If filename of a gzip compressed candump log file is provided, the required gzip open function is used
(filename)
| 557 | |
| 558 | @staticmethod |
| 559 | def open(filename): |
| 560 | # type: (Union[IO[bytes], str]) -> Tuple[str, _ByteStream] |
| 561 | """Open function to handle three types of input data. |
| 562 | |
| 563 | If filename of a regular candump log file is provided, this function |
| 564 | opens the file and returns the file object. |
| 565 | If filename of a gzip compressed candump log file is provided, the |
| 566 | required gzip open function is used to obtain the necessary file |
| 567 | object, which gets returned. |
| 568 | If a fileobject or ByteIO is provided, the filename is gathered for |
| 569 | internal use. No further steps are performed on this object. |
| 570 | |
| 571 | :param filename: Can be a string, specifying a candump log file or a |
| 572 | gzip compressed candump log file. Also already opened |
| 573 | file objects are allowed. |
| 574 | :return: A opened file object for further use. |
| 575 | """ |
| 576 | """Open (if necessary) filename.""" |
| 577 | if isinstance(filename, str): |
| 578 | try: |
| 579 | fdesc = gzip.open(filename, "rb") # type: _ByteStream |
| 580 | # try read to cause exception |
| 581 | fdesc.read(1) |
| 582 | fdesc.seek(0) |
| 583 | except IOError: |
| 584 | fdesc = open(filename, "rb") |
| 585 | return filename, fdesc |
| 586 | else: |
| 587 | name = getattr(filename, "name", "No name") |
| 588 | return name, filename |
| 589 | |
| 590 | def next(self): |
| 591 | # type: () -> Packet |
no test coverage detected