Skip to content

Commit 6bd39ba

Browse files
Add back PdoBase.export() dependency (canmatrix) (#493)
The canmatrix optional dependency was removed on Oct 10, 2021 with commit c46228f. It is now added back as an optional dependency, using the same name as previously: db_export. To install the dependency: $ python3 -m pip install 'canopen[db_export]' Resolves #488
1 parent 08eba81 commit 6bd39ba

File tree

4 files changed

+68
-31
lines changed

4 files changed

+68
-31
lines changed

.github/workflows/pythonpackage.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ jobs:
1717
fail-fast: false
1818
matrix:
1919
python-version: ['3.x']
20+
features: ['', '[db_export]']
2021

2122
steps:
2223
- uses: actions/checkout@v3
@@ -28,7 +29,7 @@ jobs:
2829
run: |
2930
python -m pip install --upgrade pip
3031
pip install pytest pytest-cov
31-
pip install -e .
32+
pip install -e '.${{ matrix.features }}'
3233
- name: Test with pytest
3334
run: |
3435
pytest -v --cov=canopen --cov-report=xml --cov-branch

canopen/pdo/base.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,14 +78,24 @@ def subscribe(self):
7878
def export(self, filename):
7979
"""Export current configuration to a database file.
8080
81+
.. note::
82+
This API requires the ``db_export`` feature to be installed::
83+
84+
python3 -m pip install 'canopen[db_export]'
85+
8186
:param str filename:
8287
Filename to save to (e.g. DBC, DBF, ARXML, KCD etc)
88+
:raises NotImplementedError:
89+
When the ``canopen[db_export]`` feature is not installed.
8390
8491
:return: The CanMatrix object created
8592
:rtype: canmatrix.canmatrix.CanMatrix
8693
"""
87-
from canmatrix import canmatrix
88-
from canmatrix import formats
94+
try:
95+
from canmatrix import canmatrix
96+
from canmatrix import formats
97+
except ImportError:
98+
raise NotImplementedError("This feature requires the 'canopen[db_export]' feature")
8999

90100
db = canmatrix.CanMatrix()
91101
for pdo_map in self.map.values():

pyproject.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ dependencies = [
2626
]
2727
dynamic = ["version"]
2828

29+
[project.optional-dependencies]
30+
db_export = [
31+
"canmatrix ~= 1.0",
32+
]
33+
2934
[project.urls]
3035
documentation = "https://canopen.readthedocs.io/en/stable/"
3136
repository = "https://github.com/christiansandberg/canopen"

test/test_pdo.py

Lines changed: 49 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,36 +5,41 @@
55

66

77
class TestPDO(unittest.TestCase):
8-
9-
def test_bit_mapping(self):
8+
def setUp(self):
109
node = canopen.Node(1, SAMPLE_EDS)
11-
map = node.pdo.tx[1]
12-
map.add_variable('INTEGER16 value') # 0x2001
13-
map.add_variable('UNSIGNED8 value', length=4) # 0x2002
14-
map.add_variable('INTEGER8 value', length=4) # 0x2003
15-
map.add_variable('INTEGER32 value') # 0x2004
16-
map.add_variable('BOOLEAN value', length=1) # 0x2005
17-
map.add_variable('BOOLEAN value 2', length=1) # 0x2006
10+
pdo = node.pdo.tx[1]
11+
pdo.add_variable('INTEGER16 value') # 0x2001
12+
pdo.add_variable('UNSIGNED8 value', length=4) # 0x2002
13+
pdo.add_variable('INTEGER8 value', length=4) # 0x2003
14+
pdo.add_variable('INTEGER32 value') # 0x2004
15+
pdo.add_variable('BOOLEAN value', length=1) # 0x2005
16+
pdo.add_variable('BOOLEAN value 2', length=1) # 0x2006
1817

1918
# Write some values
20-
map['INTEGER16 value'].raw = -3
21-
map['UNSIGNED8 value'].raw = 0xf
22-
map['INTEGER8 value'].raw = -2
23-
map['INTEGER32 value'].raw = 0x01020304
24-
map['BOOLEAN value'].raw = False
25-
map['BOOLEAN value 2'].raw = True
19+
pdo['INTEGER16 value'].raw = -3
20+
pdo['UNSIGNED8 value'].raw = 0xf
21+
pdo['INTEGER8 value'].raw = -2
22+
pdo['INTEGER32 value'].raw = 0x01020304
23+
pdo['BOOLEAN value'].raw = False
24+
pdo['BOOLEAN value 2'].raw = True
25+
26+
self.pdo = pdo
27+
self.node = node
2628

27-
# Check expected data
28-
self.assertEqual(map.data, b'\xfd\xff\xef\x04\x03\x02\x01\x02')
29+
def test_pdo_map_bit_mapping(self):
30+
self.assertEqual(self.pdo.data, b'\xfd\xff\xef\x04\x03\x02\x01\x02')
2931

30-
# Read values from data
31-
self.assertEqual(map['INTEGER16 value'].raw, -3)
32-
self.assertEqual(map['UNSIGNED8 value'].raw, 0xf)
33-
self.assertEqual(map['INTEGER8 value'].raw, -2)
34-
self.assertEqual(map['INTEGER32 value'].raw, 0x01020304)
35-
self.assertEqual(map['BOOLEAN value'].raw, False)
36-
self.assertEqual(map['BOOLEAN value 2'].raw, True)
32+
def test_pdo_map_getitem(self):
33+
pdo = self.pdo
34+
self.assertEqual(pdo['INTEGER16 value'].raw, -3)
35+
self.assertEqual(pdo['UNSIGNED8 value'].raw, 0xf)
36+
self.assertEqual(pdo['INTEGER8 value'].raw, -2)
37+
self.assertEqual(pdo['INTEGER32 value'].raw, 0x01020304)
38+
self.assertEqual(pdo['BOOLEAN value'].raw, False)
39+
self.assertEqual(pdo['BOOLEAN value 2'].raw, True)
3740

41+
def test_pdo_getitem(self):
42+
node = self.node
3843
self.assertEqual(node.tpdo[1]['INTEGER16 value'].raw, -3)
3944
self.assertEqual(node.tpdo[1]['UNSIGNED8 value'].raw, 0xf)
4045
self.assertEqual(node.tpdo[1]['INTEGER8 value'].raw, -2)
@@ -54,10 +59,26 @@ def test_bit_mapping(self):
5459
self.assertEqual(node.tpdo[0x2002].raw, 0xf)
5560
self.assertEqual(node.pdo[0x1600][0x2002].raw, 0xf)
5661

57-
def test_save_pdo(self):
58-
node = canopen.Node(1, SAMPLE_EDS)
59-
node.tpdo.save()
60-
node.rpdo.save()
62+
def test_pdo_save(self):
63+
self.node.tpdo.save()
64+
self.node.rpdo.save()
65+
66+
def test_pdo_export(self):
67+
import tempfile
68+
try:
69+
import canmatrix
70+
except ImportError:
71+
raise unittest.SkipTest("The PDO export API requires canmatrix")
72+
73+
for pdo in "tpdo", "rpdo":
74+
with tempfile.NamedTemporaryFile(suffix=".csv") as tmp:
75+
fn = tmp.name
76+
with self.subTest(filename=fn, pdo=pdo):
77+
getattr(self.node, pdo).export(fn)
78+
with open(fn) as csv:
79+
header = csv.readline()
80+
self.assertIn("ID", header)
81+
self.assertIn("Frame Name", header)
6182

6283

6384
if __name__ == "__main__":

0 commit comments

Comments
 (0)