Skip to content

Commit d9d3fa5

Browse files
felixdivohardbyte
authored andcommitted
New location for scripts (hardbyte#370)
* move scripts to can.scripts module * move pytest config into setup.cfg * added scripts to coverage reports * mention CLI tools in Readme * doc improvements for scripts. Renamed scripts section's file in docs * exclude legacy code from coverage reports * allow can.logger and ca.player to be accessed
1 parent 66ce4aa commit d9d3fa5

14 files changed

Lines changed: 139 additions & 49 deletions

.appveyor.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,5 @@ install:
2828
build: off
2929

3030
test_script:
31-
- "pytest -v --timeout=300 --cov=can"
31+
- "pytest"
3232
- "codecov"

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ install:
4949
- travis_retry pip install .[test]
5050

5151
script:
52-
- pytest -v --timeout=300 --cov=can
52+
- pytest
5353
- codecov
5454
# Build Docs with Sphinx
5555
# -a Write all files

README.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,14 @@ Features
3737
--------
3838

3939
- common abstractions for CAN communication
40-
- support for many different backends (see the `docs <https://python-can.readthedocs.io/en/master/interfaces.html>`__)
40+
- support for many different backends (see the `docs <https://python-can.readthedocs.io/en/stable/interfaces.html>`__)
4141
- receiving, sending, and periodically sending messages
4242
- normal and extended arbitration IDs
4343
- limited `CAN FD <https://en.wikipedia.org/wiki/CAN_FD>`__ support
4444
- many different loggers and readers supporting playback: ASC (CANalyzer format), BLF (Binary Logging Format by Vector), CSV, SQLite and Canutils log
4545
- efficient in-kernel or in-hardware filtering of messages on supported interfaces
4646
- bus configuration reading from file or environment variables
47+
- CLI tools for working with CAN busses (see the `docs <https://python-can.readthedocs.io/en/stable/scripts.html>`__)
4748
- more
4849

4950

can/__init__.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,18 @@ class CanError(IOError):
3434
from .util import set_logging_level
3535

3636
from .message import Message
37-
from .bus import BusABC
37+
from .bus import BusABC, BusState
3838
from .thread_safe_bus import ThreadSafeBus
3939
from .notifier import Notifier
4040
from .interfaces import VALID_INTERFACES
4141
from . import interface
4242
from .interface import Bus, detect_available_configs
4343

44-
from can.broadcastmanager import send_periodic, \
44+
from .broadcastmanager import send_periodic, \
4545
CyclicSendTaskABC, \
4646
LimitedDurationCyclicSendTaskABC, \
4747
ModifiableCyclicTaskABC, \
4848
MultiRateCyclicSendTaskABC, \
4949
RestartableCyclicTaskABC
50+
51+
from .scripts import *

can/scripts/__init__.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/usr/bin/env python
2+
# coding: utf-8
3+
4+
"""
5+
This module contains various scripts, like a logfile writer and a logfile player.
6+
7+
.. note::
8+
The scripts reside in here so they can be launched as modules. That makes them
9+
more easily callable on different platforms. But they they can also be called
10+
directly (mostly on Unix systems), because they are also installed as scripts
11+
from ``python-can/scripts/*``.
12+
See PR #370.
13+
14+
"""

can/logger.py renamed to can/scripts/logger.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,23 @@
1717
Dynamic Controls 2010
1818
"""
1919

20-
from __future__ import print_function
20+
from __future__ import absolute_import, print_function
2121

22-
import datetime
2322
import argparse
2423
import socket
24+
from datetime import datetime
2525

2626
import can
27-
from can.bus import BusState
28-
from can.io.logger import Logger
27+
from can import Bus, BusState, Logger
2928

3029

3130
def main():
3231
parser = argparse.ArgumentParser(
3332
"python -m can.logger",
34-
description="Log CAN traffic, printing messages to stdout or to a given file")
33+
description="Log CAN traffic, printing messages to stdout or to a given file.")
3534

3635
parser.add_argument("-f", "--file_name", dest="log_file",
37-
help="""Path and base log filename, extension can be .txt, .asc, .csv, .db, .npz""",
36+
help="""Path and base log filename, for supported types see can.Logger.""",
3837
default=None)
3938

4039
parser.add_argument("-v", action="count", dest="verbosity",
@@ -59,8 +58,10 @@ def main():
5958
help='''Bitrate to use for the CAN bus.''')
6059

6160
group = parser.add_mutually_exclusive_group(required=False)
62-
group.add_argument('--active', action='store_true')
63-
group.add_argument('--passive', action='store_true')
61+
group.add_argument('--active', help="Start the bus as active, this is applied the default.",
62+
action='store_true')
63+
group.add_argument('--passive', help="Start the bus as passive.",
64+
action='store_true')
6465

6566
results = parser.parse_args()
6667

@@ -84,10 +85,10 @@ def main():
8485

8586
config = {"can_filters": can_filters, "single_handle": True}
8687
if results.interface:
87-
config["bustype"] = results.interface
88+
config["interface"] = results.interface
8889
if results.bitrate:
8990
config["bitrate"] = results.bitrate
90-
bus = can.interface.Bus(results.channel, **config)
91+
bus = Bus(results.channel, **config)
9192

9293
if results.active:
9394
bus.state = BusState.ACTIVE
@@ -96,7 +97,7 @@ def main():
9697
bus.state = BusState.PASSIVE
9798

9899
print('Connected to {}: {}'.format(bus.__class__.__name__, bus.channel_info))
99-
print('Can Logger (Started on {})\n'.format(datetime.datetime.now()))
100+
print('Can Logger (Started on {})\n'.format(datetime.now()))
100101
logger = Logger(results.log_file)
101102

102103
try:

can/player.py renamed to can/scripts/player.py

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,22 @@
88
Similar to canplayer in the can-utils package.
99
"""
1010

11-
from __future__ import print_function
11+
from __future__ import absolute_import, print_function
1212

1313
import argparse
14-
import datetime
14+
from datetime import datetime
1515

1616
import can
17-
from can.io.player import LogReader, MessageSync
17+
from can import Bus, LogReader, MessageSync
1818

1919

2020
def main():
2121
parser = argparse.ArgumentParser(
2222
"python -m can.player",
23-
description="Replay CAN traffic")
23+
description="Replay CAN traffic.")
2424

2525
parser.add_argument("-f", "--file_name", dest="log_file",
26-
help="""Path and base log filename, extension can be .txt, .asc, .csv, .db, .npz""",
26+
help="""Path and base log filename, for supported types see can.LogReader.""",
2727
default=None)
2828

2929
parser.add_argument("-v", action="count", dest="verbosity",
@@ -44,38 +44,37 @@ def main():
4444
help='''Bitrate to use for the CAN bus.''')
4545

4646
parser.add_argument('--ignore-timestamps', dest='timestamps',
47-
help='''Ignore timestamps (send all frames immediately with minimum gap between
48-
frames)''', action='store_false')
47+
help='''Ignore timestamps (send all frames immediately with minimum gap between frames)''',
48+
action='store_false')
4949

50-
parser.add_argument('-g', '--gap', type=float, help='''<s> minimum time between replayed frames''')
50+
parser.add_argument('-g', '--gap', type=float, help='''<s> minimum time between replayed frames''',
51+
default=0.0001)
5152
parser.add_argument('-s', '--skip', type=float, default=60*60*24,
5253
help='''<s> skip gaps greater than 's' seconds''')
5354

5455
parser.add_argument('infile', metavar='input-file', type=str,
55-
help='The file to replay. Supported types: .db, .blf')
56+
help='The file to replay. For supported types see can.LogReader.')
5657

5758
results = parser.parse_args()
5859

5960
verbosity = results.verbosity
60-
gap = 0.0001 if results.gap is None else results.gap
6161

6262
logging_level_name = ['critical', 'error', 'warning', 'info', 'debug', 'subdebug'][min(5, verbosity)]
6363
can.set_logging_level(logging_level_name)
6464

6565
config = {"single_handle": True}
6666
if results.interface:
67-
config["bustype"] = results.interface
67+
config["interface"] = results.interface
6868
if results.bitrate:
6969
config["bitrate"] = results.bitrate
70-
bus = can.interface.Bus(results.channel, **config)
70+
bus = Bus(results.channel, **config)
7171

72-
player = LogReader(results.infile)
72+
reader = LogReader(results.infile)
7373

74-
in_sync = MessageSync(player, timestamps=results.timestamps,
75-
gap=gap, skip=results.skip)
74+
in_sync = MessageSync(reader, timestamps=results.timestamps,
75+
gap=results.gap, skip=results.skip)
7676

77-
print('Can LogReader (Started on {})'.format(
78-
datetime.datetime.now()))
77+
print('Can LogReader (Started on {})'.format(datetime.now()))
7978

8079
try:
8180
for m in in_sync:
@@ -86,6 +85,7 @@ def main():
8685
pass
8786
finally:
8887
bus.shutdown()
88+
reader.stop()
8989

9090

9191
if __name__ == "__main__":

doc/development.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ These steps are a guideline on how to add a new backend to python-can.
4040
``can.interfaces.VALID_INTERFACES``.
4141
- Add docs where appropiate, like in ``doc/interfaces.rst`` and add
4242
an entry in ``doc/interface/*``.
43+
Update ``doc/scripts.rst`` accordingly.
4344
- Add tests in ``test/*`` where appropiate.
4445

4546

doc/index.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ Contents:
4343
configuration
4444
api
4545
interfaces
46-
bin
46+
scripts
4747
development
4848
history
4949

doc/bin.rst renamed to doc/scripts.rst

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,28 @@ Scripts
33

44
The following modules are callable from python-can.
55

6+
They can either be called by for example ``python -m can.logger`` or ``can_logger.py`` (if installed by pip).
7+
The scripts are internally placed in the module ``can.scripts.*``,
8+
so they could also be launched by ``python -m can.scripts.logger``.
9+
10+
611
can.logger
712
----------
813

9-
Command line help (``python -m can.logger --help``)::
14+
Command line help, called with ``--help``::
1015

1116
usage: python -m can.logger [-h] [-f LOG_FILE] [-v] [-c CHANNEL]
12-
[-i {iscan,slcan,virtual,socketcan_ctypes,usb2can,ixxat,socketcan_native,kvaser,neovi,vector,nican,pcan,serial,remote,socketcan}]
17+
[-i {pcan,ixxat,socketcan_ctypes,kvaser,virtual,usb2can,vector,slcan,nican,socketcan,iscan,neovi,serial,socketcan_native}]
1318
[--filter ...] [-b BITRATE]
19+
[--active | --passive]
1420

15-
Log CAN traffic, printing messages to stdout or to a given file
21+
Log CAN traffic, printing messages to stdout or to a given file.
1622

1723
optional arguments:
1824
-h, --help show this help message and exit
1925
-f LOG_FILE, --file_name LOG_FILE
20-
Path and base log filename, extension can be .txt,
21-
.asc, .csv, .db, .npz
26+
Path and base log filename, for supported types see
27+
can.Logger.
2228
-v How much information do you want to see at the command
2329
line? You can add several of these e.g., -vv is DEBUG
2430
-c CHANNEL, --channel CHANNEL
@@ -27,7 +33,7 @@ Command line help (``python -m can.logger --help``)::
2733
might be a rfcomm device: "/dev/rfcomm0" With the
2834
socketcan interfaces valid channel examples include:
2935
"can0", "vcan0"
30-
-i {iscan,slcan,virtual,socketcan_ctypes,usb2can,ixxat,socketcan_native,kvaser,neovi,vector,nican,pcan,serial,remote,socketcan}, --interface {iscan,slcan,virtual,socketcan_ctypes,usb2can,ixxat,socketcan_native,kvaser,neovi,vector,nican,pcan,serial,remote,socketcan}
36+
-i {pcan,ixxat,socketcan_ctypes,kvaser,virtual,usb2can,vector,slcan,nican,socketcan,iscan,neovi,serial,socketcan_native}, --interface {pcan,ixxat,socketcan_ctypes,kvaser,virtual,usb2can,vector,slcan,nican,socketcan,iscan,neovi,serial,socketcan_native}
3137
Specify the backend CAN interface to use. If left
3238
blank, fall back to reading from configuration files.
3339
--filter ... Comma separated filters can be specified for the given
@@ -37,29 +43,32 @@ Command line help (``python -m can.logger --help``)::
3743
mask != can_id & mask)
3844
-b BITRATE, --bitrate BITRATE
3945
Bitrate to use for the CAN bus.
46+
--active Start the bus as active, this is applied the default.
47+
--passive Start the bus as passive.
4048

4149

4250
can.player
4351
----------
4452

45-
Command line help (``python -m can.player --help``)::
53+
Command line help, called with ``--help``::
4654

4755
usage: python -m can.player [-h] [-f LOG_FILE] [-v] [-c CHANNEL]
48-
[-i {kvaser,virtual,slcan,nican,neovi,ixxat,serial,usb2can,socketcan_ctypes,remote,socketcan_native,iscan,vector,pcan,socketcan}]
49-
[-b BITRATE] [--ignore-timestamps] [-g GAP]
50-
[-s SKIP]
56+
[-i {pcan,ixxat,socketcan_ctypes,kvaser,virtual,usb2can,vector,slcan,nican,socketcan,iscan,neovi,serial,socketcan_native}]
57+
[-b BITRATE] [--ignore-timestamps]
58+
[-g GAP] [-s SKIP]
5159
input-file
5260

53-
Replay CAN traffic
61+
Replay CAN traffic.
5462

5563
positional arguments:
56-
input-file The file to replay. Supported types: .db, .blf
64+
input-file The file to replay. For supported types see
65+
can.LogReader.
5766

5867
optional arguments:
5968
-h, --help show this help message and exit
6069
-f LOG_FILE, --file_name LOG_FILE
61-
Path and base log filename, extension can be .txt,
62-
.asc, .csv, .db, .npz
70+
Path and base log filename, for supported types see
71+
can.LogReader.
6372
-v Also print can frames to stdout. You can add several
6473
of these to enable debugging
6574
-c CHANNEL, --channel CHANNEL
@@ -68,7 +77,7 @@ Command line help (``python -m can.player --help``)::
6877
might be a rfcomm device: "/dev/rfcomm0" With the
6978
socketcan interfaces valid channel examples include:
7079
"can0", "vcan0"
71-
-i {kvaser,virtual,slcan,nican,neovi,ixxat,serial,usb2can,socketcan_ctypes,remote,socketcan_native,iscan,vector,pcan,socketcan}, --interface {kvaser,virtual,slcan,nican,neovi,ixxat,serial,usb2can,socketcan_ctypes,remote,socketcan_native,iscan,vector,pcan,socketcan}
80+
-i {pcan,ixxat,socketcan_ctypes,kvaser,virtual,usb2can,vector,slcan,nican,socketcan,iscan,neovi,serial,socketcan_native}, --interface {pcan,ixxat,socketcan_ctypes,kvaser,virtual,usb2can,vector,slcan,nican,socketcan,iscan,neovi,serial,socketcan_native}
7281
Specify the backend CAN interface to use. If left
7382
blank, fall back to reading from configuration files.
7483
-b BITRATE, --bitrate BITRATE

0 commit comments

Comments
 (0)