Skip to content

Commit fb4e529

Browse files
committed
add io/logger.py
1 parent cd3f81b commit fb4e529

4 files changed

Lines changed: 32 additions & 26 deletions

File tree

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ jobs:
102102
can/typechecking.py
103103
can/util.py
104104
can/io/generic.py
105+
can/io/logger.py
105106
- stage: linter
106107
name: "Formatting Checks"
107108
python: "3.7"

can/io/generic.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from abc import ABCMeta
88
from typing import Optional, Union, cast
99

10-
from can.typechecking import FileLike, PathLike
10+
from can.typechecking import FileLike, PathLike, AcceptedIOType
1111

1212

1313
class BaseIOHandler(metaclass=ABCMeta):
@@ -20,7 +20,7 @@ class BaseIOHandler(metaclass=ABCMeta):
2020
was opened
2121
"""
2222

23-
def __init__(self, file: Optional[Union[FileLike, PathLike]], mode: str = "rt"):
23+
def __init__(self, file: AcceptedIOType, mode: str = "rt"):
2424
"""
2525
:param file: a path-like object to open a file, a file-like object
2626
to be used as a file or `None` to not use a file at all

can/io/logger.py

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
"""
66

77
import logging
8+
import pathlib
9+
import typing
810

911
from ..listener import Listener
1012
from .generic import BaseIOHandler
@@ -15,6 +17,8 @@
1517
from .sqlite import SqliteWriter
1618
from .printer import Printer
1719

20+
from can.typechecking import PathLike
21+
1822
log = logging.getLogger("can.io.logger")
1923

2024

@@ -28,36 +32,36 @@ class Logger(BaseIOHandler, Listener): # pylint: disable=abstract-method
2832
* .csv: :class:`can.CSVWriter`
2933
* .db: :class:`can.SqliteWriter`
3034
* .log :class:`can.CanutilsLogWriter`
31-
* other: :class:`can.Printer`
35+
36+
The **filename** may also be *None*, to fall back to :class:`can.Printer`.
3237
3338
The log files may be incomplete until `stop()` is called due to buffering.
3439
3540
.. note::
36-
This class itself is just a dispatcher, and any positional an keyword
41+
This class itself is just a dispatcher, and any positional and keyword
3742
arguments are passed on to the returned instance.
3843
"""
3944

4045
@staticmethod
41-
def __new__(cls, filename, *args, **kwargs):
46+
def __new__(cls, filename: typing.Optional[PathLike], *args, **kwargs):
4247
"""
43-
:type filename: str or None or path-like
44-
:param filename: the filename/path the file to write to,
45-
may be a path-like object if the target logger supports
46-
it, and may be None to instantiate a :class:`~can.Printer`
47-
48+
:param filename: the filename/path of the file to write to,
49+
may be a path-like object or None to
50+
instantiate a :class:`~can.Printer`
4851
"""
49-
if filename:
50-
if filename.endswith(".asc"):
51-
return ASCWriter(filename, *args, **kwargs)
52-
elif filename.endswith(".blf"):
53-
return BLFWriter(filename, *args, **kwargs)
54-
elif filename.endswith(".csv"):
55-
return CSVWriter(filename, *args, **kwargs)
56-
elif filename.endswith(".db"):
57-
return SqliteWriter(filename, *args, **kwargs)
58-
elif filename.endswith(".log"):
59-
return CanutilsLogWriter(filename, *args, **kwargs)
60-
61-
# else:
62-
log.warning('unknown file type "%s", falling pack to can.Printer', filename)
63-
return Printer(filename, *args, **kwargs)
52+
if filename is None:
53+
return Printer(*args, **kwargs)
54+
55+
suffix = pathlib.PurePath(filename).suffix
56+
if suffix == ".asc":
57+
return ASCWriter(filename, *args, **kwargs)
58+
if suffix == ".blf":
59+
return BLFWriter(filename, *args, **kwargs)
60+
if suffix == ".csv":
61+
return CSVWriter(filename, *args, **kwargs)
62+
if suffix == ".db":
63+
return SqliteWriter(filename, *args, **kwargs)
64+
if suffix == ".log":
65+
return CanutilsLogWriter(filename, *args, **kwargs)
66+
67+
raise ValueError(f'unknown file type "{filename}"')

can/typechecking.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,5 @@
2323

2424
# Used by the IO module
2525
FileLike = typing.IO[typing.Any]
26-
PathLike = typing.Union[str, bytes, os.PathLike]
26+
PathLike = typing.Union[str, os.PathLike[str]]
27+
AcceptedIOType = typing.Optional[typing.Union[FileLike, PathLike]]

0 commit comments

Comments
 (0)