Skip to content

Commit

Permalink
Annotation cleanup ebroecker#323 (ebroecker#396)
Browse files Browse the repository at this point in the history
  • Loading branch information
Funth0mas authored and ebroecker committed Sep 17, 2019
1 parent 2974476 commit d11c0d3
Show file tree
Hide file tree
Showing 13 changed files with 93 additions and 91 deletions.
34 changes: 17 additions & 17 deletions src/canmatrix/cancluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,23 @@
from __future__ import absolute_import
import typing

import canmatrix.canmatrix as cm
import canmatrix


class CanCluster(dict):

def __init__(self, *arg, **kw):
super(CanCluster, self).__init__(*arg, **kw)
self._frames = [] # type: typing.List[cm.Frame]
self._signals = [] # type: typing.List[cm.Signal]
self._ecus = [] # type: typing.List[cm.Ecu]
self._frames = [] # type: typing.List[canmatrix.Frame]
self._signals = [] # type: typing.List[canmatrix.Signal]
self._ecus = [] # type: typing.List[canmatrix.Ecu]
self.update()

def update_frames(self): # type: () -> typing.MutableSequence[cm.Frame]
frames = [] # type: typing.List[cm.Frame]
def update_frames(self): # type: () -> typing.MutableSequence[canmatrix.Frame]
frames = [] # type: typing.List[canmatrix.Frame]
frame_names = [] # type: typing.List[str]
for matrixName in self:
for frame in self[matrixName].frames: # type: cm.Frame
for frame in self[matrixName].frames: # type: canmatrix.Frame
if frame.name not in frame_names:
frame_names.append(frame.name)
frames.append(frame)
Expand All @@ -31,12 +31,12 @@ def update_frames(self): # type: () -> typing.MutableSequence[cm.Frame]
self._frames = frames
return frames

def update_signals(self): # type: () -> typing.MutableSequence[cm.Signal]
signals = [] # type: typing.List[cm.Signal]
def update_signals(self): # type: () -> typing.MutableSequence[canmatrix.Signal]
signals = [] # type: typing.List[canmatrix.Signal]
signal_names = [] # type: typing.List[str]
for matrixName in self:
for frame in self[matrixName].frames:
for signal in frame.signals: # type: cm.Signal
for frame in self[matrixName].frames: # type: canmatrix.Frame
for signal in frame.signals:
if signal.name not in signal_names:
signal_names.append(signal.name)
signals.append(signal)
Expand All @@ -47,11 +47,11 @@ def update_signals(self): # type: () -> typing.MutableSequence[cm.Signal]
self._signals = signals
return signals

def update_ecus(self): # type: () -> typing.MutableSequence[cm.Ecu]
ecus = [] # type: typing.List[cm.Ecu]
def update_ecus(self): # type: () -> typing.MutableSequence[canmatrix.Ecu]
ecus = [] # type: typing.List[canmatrix.Ecu]
ecu_names = [] # type: typing.List[str]
for matrixName in self:
for ecu in self[matrixName].ecus: # type: cm.Ecu
for ecu in self[matrixName].ecus: # type: canmatrix.Ecu
if ecu.name not in ecu_names:
ecu_names.append(ecu.name)
ecus.append(ecu)
Expand All @@ -64,19 +64,19 @@ def update(self):
self.update_ecus()

@property
def ecus(self): # type: () -> typing.MutableSequence[cm.Ecu]
def ecus(self): # type: () -> typing.MutableSequence[canmatrix.Ecu]
if not self._ecus:
self.update_ecus()
return self._ecus

@property
def frames(self): # type: () -> typing.MutableSequence[cm.Frame]
def frames(self): # type: () -> typing.MutableSequence[canmatrix.Frame]
if not self._frames:
self.update_frames()
return self._frames

@property
def signals(self): # type: () -> typing.MutableSequence[cm.Signal]
def signals(self): # type: () -> typing.MutableSequence[canmatrix.Signal]
if not self._signals:
self.update_signals()
return self._signals
26 changes: 9 additions & 17 deletions src/canmatrix/canmatrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -813,7 +813,7 @@ def pgn(self, value): # type: (int) -> None
@property
def priority(self): # type: () -> int
"""Get J1939 priority."""
return self.arbitration_id.j1939_prio
return self.arbitration_id.j1939_priority

@priority.setter
def priority(self, value): # type: (int) -> None
Expand Down Expand Up @@ -1294,15 +1294,13 @@ def decode(self, data):

if self.is_complex_multiplexed:
decoded_values = dict()
filtered_signals = []
filtered_signals += self._filter_signals_for_multiplexer(None, None)
filtered_signals = self._filter_signals_for_multiplexer(None, None)

multiplex_name = None
multiplex_value = None

while self._has_sub_multiplexer(multiplex_name):
multiplex_name, multiplex_value = self._get_sub_multiplexer(multiplex_name, multiplex_value,
decoded)
multiplex_name, multiplex_value = self._get_sub_multiplexer(multiplex_name, multiplex_value, decoded)
decoded_values[multiplex_name] = decoded[multiplex_name]
filtered_signals += self._filter_signals_for_multiplexer(multiplex_name, multiplex_value)

Expand Down Expand Up @@ -1343,7 +1341,7 @@ def __init__(self, definition): # type (str) -> None
:param str definition: definition string. Ex: "INT -5 10"
"""
definition = definition.strip()
self.definition = definition # type: str
self.definition = definition
self.type = None # type: typing.Optional[str]
self.defaultValue = None # type: typing.Any

Expand Down Expand Up @@ -1561,40 +1559,34 @@ def add_define_default(self, name, value): # type: (str, typing.Any) -> None
def delete_obsolete_defines(self): # type: () -> None
"""Delete all unused Defines.
Delete them from frameDefines, buDefines and signalDefines.
Delete them from frame_defines, ecu_defines and signal_defines.
"""
defines_to_delete = set() # type: typing.Set[str]
for frameDef in self.frame_defines: # type: str
found = False
for frameDef in self.frame_defines:
for frame in self.frames:
if frameDef in frame.attributes:
found = True
break
if not found:
else:
defines_to_delete.add(frameDef)
for element in defines_to_delete:
del self.frame_defines[element]
defines_to_delete = set()
for ecu_define in self.ecu_defines:
found = False
for ecu in self.ecus:
if ecu_define in ecu.attributes:
found = True
break
if not found:
else:
defines_to_delete.add(ecu_define)
for element in defines_to_delete:
del self.ecu_defines[element]

defines_to_delete = set()
for signal_define in self.signal_defines:
found = False
for frame in self.frames:
for signal in frame.signals:
if signal_define in signal.attributes:
found = True
break
if not found:
else:
defines_to_delete.add(signal_define)
for element in defines_to_delete:
del self.signal_defines[element]
Expand Down
10 changes: 4 additions & 6 deletions src/canmatrix/cli/compare.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,21 @@
import typing
import click

import attr
import canmatrix.compare

logger = logging.getLogger(__name__)


@click.command()
@click.option('-v', '--verbose', 'verbosity', help="Output verbosity", count=True, default=1)
@click.option('-s', '--silent', is_flag=True, default=False, help="don't print status messages to stdout. (only errors)")
@click.option('-f', '--frames', is_flag=True, default=False, help="show list of frames")
@click.option('-c', '--comments', 'check_comments', is_flag=True, default=False, help="look for changed comments")
@click.option('-a', '--attributes', 'check_attributes', is_flag=True, default=False, help="look for changed attributes")
@click.option('-t', '--valueTable', 'ignore_valuetables', is_flag=True, default=False, help="ignore changed valuetables")
@click.argument('matrix1', required=1)
@click.argument('matrix2', required=1)
def cli_compare(matrix1, matrix2, verbosity, silent, check_comments, check_attributes, ignore_valuetables, frames): # type: () -> int
@click.argument('matrix1', required=True)
@click.argument('matrix2', required=True)
def cli_compare(matrix1, matrix2, verbosity, silent, check_comments, check_attributes, ignore_valuetables, frames):
"""
canmatrix.cli.compare [options] matrix1 matrix2
Expand All @@ -53,8 +53,6 @@ def cli_compare(matrix1, matrix2, verbosity, silent, check_comments, check_attri
import canmatrix.log
root_logger = canmatrix.log.setup_logger()



if silent:
# Only print ERROR messages (ignore import warnings)
verbosity = -1
Expand Down
15 changes: 9 additions & 6 deletions src/canmatrix/cli/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,15 @@

import logging
import sys
import typing

import click

import canmatrix.convert
import canmatrix.log

logger = logging.getLogger(__name__)


def get_formats():
input = ""
output = ""
Expand All @@ -43,6 +45,7 @@ def get_formats():
output += suppFormat + "\n"
return (input, output)


@click.command()
# global switches
@click.option('-v', '--verbose', 'verbosity', count=True, default=1)
Expand Down Expand Up @@ -100,10 +103,10 @@ def get_formats():
#sym switches
@click.option('--symExportEncoding', 'symExportEncoding', default="iso-8859-1", help="Export charset of sym format, maybe utf-8\ndefault iso-8859-1")
# in and out file
@click.argument('infile', required=1)
@click.argument('outfile', required=1)

def cli_convert(infile, outfile, silent, verbosity, **options): # type: () -> int
@click.argument('infile', required=True)
@click.argument('outfile', required=True)
#
def cli_convert(infile, outfile, silent, verbosity, **options):
"""
canmatrix.cli.convert [options] import-file export-file
Expand All @@ -114,7 +117,7 @@ def cli_convert(infile, outfile, silent, verbosity, **options): # type: () -> i

root_logger = canmatrix.log.setup_logger()

if silent == True:
if silent is True:
# only print error messages, ignore verbosity flag
verbosity = -1
options["silent"] = True
Expand Down
40 changes: 21 additions & 19 deletions src/canmatrix/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import sys
import typing

import canmatrix.canmatrix as cm
import canmatrix
import canmatrix.copy
import canmatrix.formats
import canmatrix.log
Expand All @@ -42,25 +42,25 @@ def convert(infile, out_file_name, **options): # type: (str, str, **str) -> Non

logger.info("Exporting " + out_file_name + " ... ")

out_dbs = {}
out_dbs = {} # type: typing.Dict[str, canmatrix.CanMatrix]
for name in dbs:
db = None

if 'ecus' in options and options['ecus'] is not None:
ecu_list = options['ecus'].split(',')
db = cm.CanMatrix()
db = canmatrix.CanMatrix()
for ecu in ecu_list:
canmatrix.copy.copy_ecu_with_frames(ecu, dbs[name], db)
if 'frames' in options and options['frames'] is not None:
frame_list = options['frames'].split(',')
db = cm.CanMatrix()
for frame_name in frame_list: # type: str
frame_to_copy = dbs[name].frame_by_name(frame_name) # type: cm.Frame
db = canmatrix.CanMatrix()
for frame_name in frame_list:
frame_to_copy = dbs[name].frame_by_name(frame_name)
canmatrix.copy.copy_frame(frame_to_copy.arbitration_id, dbs[name], db)
if options.get('signals', False):
signal_list = options['signals'].split(',')
db = cm.CanMatrix()
for signal_name in signal_list: # type: str
db = canmatrix.CanMatrix()
for signal_name in signal_list:
canmatrix.copy.copy_signal(signal_name, dbs[name], db)

if db is None:
Expand Down Expand Up @@ -110,8 +110,8 @@ def convert(infile, out_file_name, **options): # type: (str, str, **str) -> Non
for touple in touples:
(frameName, ecu) = touple.split(':')
frames = db.glob_frames(frameName)
for frame in frames: # type: cm.Frame
for signal in frame.signals: # type: cm.Signal
for frame in frames:
for signal in frame.signals:
signal.add_receiver(ecu)
frame.update_receiver()

Expand All @@ -123,7 +123,7 @@ def convert(infile, out_file_name, **options): # type: (str, str, **str) -> Non
change_tuples = options['changeFrameId'].split(',')
for renameTuple in change_tuples:
old, new = renameTuple.split(':')
frame = db.frame_by_id(cm.ArbitrationId(int(old)))
frame = db.frame_by_id(canmatrix.ArbitrationId(int(old)))
if frame is not None:
frame.arbitration_id.id = int(new)
else:
Expand All @@ -144,20 +144,22 @@ def convert(infile, out_file_name, **options): # type: (str, str, **str) -> Non
frame_ptr.del_attribute("VFrameFormat")

if 'skipLongDlc' in options and options['skipLongDlc'] is not None:
delete_frame_list = [] # type: typing.List[cm.Frame]
for frame in db.frames:
if frame.size > int(options['skipLongDlc']):
delete_frame_list.append(frame)
delete_frame_list = [
frame
for frame in db.frames
if frame.size > int(options['skipLongDlc'])
]
for frame in delete_frame_list:
db.del_frame(frame)

if 'cutLongFrames' in options and options['cutLongFrames'] is not None:
for frame in db.frames:
if frame.size > int(options['cutLongFrames']):
delete_signal_list = []
for sig in frame.signals:
if sig.get_startbit() + int(sig.size) > int(options['cutLongFrames'])*8:
delete_signal_list.append(sig)
delete_signal_list = [
sig
for sig in frame.signals
if sig.get_startbit() + int(sig.size) > int(options['cutLongFrames'])*8
]
for sig in delete_signal_list:
frame.signals.remove(sig)
frame.size = 0
Expand Down
Loading

0 comments on commit d11c0d3

Please sign in to comment.