Skip to content

Commit

Permalink
Fix linting and formatting
Browse files Browse the repository at this point in the history
fix: address pylint warnings
  • Loading branch information
root authored and OdoctorG committed Jul 4, 2024
1 parent 6ccb1c2 commit fea3f29
Show file tree
Hide file tree
Showing 18 changed files with 581 additions and 594 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/code-qa.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
uses: actions/checkout@v4
- name: Install deps
run: |
pip install mypy==1.8.0 pylint==3.2.5 ruff==0.1.14 remotivelabs-broker>=0.1.8 pytest
pip install mypy==1.8.0 pylint==3.2.5 ruff==0.1.14 pylint-protobuf==0.22.0 remotivelabs-broker>=0.1.8 pytest
- name: Run lint
run: |
cd python
Expand Down
1 change: 1 addition & 0 deletions python/.gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.pyc
.venv
__pycache__
.*
38 changes: 38 additions & 0 deletions python/=0.1.8
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
Requirement already satisfied: mypy==1.8.0 in /mnt/c/Users/Oscar Gren/Documents/RemotiveLabs/lib/python3.10/site-packages (1.8.0)
Requirement already satisfied: pylint==3.2.5 in /mnt/c/Users/Oscar Gren/Documents/RemotiveLabs/lib/python3.10/site-packages (3.2.5)
Requirement already satisfied: ruff==0.1.14 in /mnt/c/Users/Oscar Gren/Documents/RemotiveLabs/lib/python3.10/site-packages (0.1.14)
Requirement already satisfied: pylint-protobuf==0.22.0 in /mnt/c/Users/Oscar Gren/Documents/RemotiveLabs/lib/python3.10/site-packages (0.22.0)
Requirement already satisfied: remotivelabs-broker in /mnt/c/Users/Oscar Gren/Documents/RemotiveLabs/lib/python3.10/site-packages (0.1.26)
Requirement already satisfied: pytest in /mnt/c/Users/Oscar Gren/Documents/RemotiveLabs/lib/python3.10/site-packages (8.2.2)
Requirement already satisfied: tomli>=1.1.0 in /mnt/c/Users/Oscar Gren/Documents/RemotiveLabs/lib/python3.10/site-packages (from mypy==1.8.0) (2.0.1)
Requirement already satisfied: typing-extensions>=4.1.0 in /mnt/c/Users/Oscar Gren/Documents/RemotiveLabs/lib/python3.10/site-packages (from mypy==1.8.0) (4.12.2)
Requirement already satisfied: mypy-extensions>=1.0.0 in /mnt/c/Users/Oscar Gren/Documents/RemotiveLabs/lib/python3.10/site-packages (from mypy==1.8.0) (1.0.0)
Requirement already satisfied: dill>=0.2 in /mnt/c/Users/Oscar Gren/Documents/RemotiveLabs/lib/python3.10/site-packages (from pylint==3.2.5) (0.3.8)
Requirement already satisfied: astroid<=3.3.0-dev0,>=3.2.2 in /mnt/c/Users/Oscar Gren/Documents/RemotiveLabs/lib/python3.10/site-packages (from pylint==3.2.5) (3.2.2)
Requirement already satisfied: platformdirs>=2.2.0 in /mnt/c/Users/Oscar Gren/Documents/RemotiveLabs/lib/python3.10/site-packages (from pylint==3.2.5) (4.2.2)
Requirement already satisfied: mccabe<0.8,>=0.6 in /mnt/c/Users/Oscar Gren/Documents/RemotiveLabs/lib/python3.10/site-packages (from pylint==3.2.5) (0.7.0)
Requirement already satisfied: isort!=5.13.0,<6,>=4.2.5 in /mnt/c/Users/Oscar Gren/Documents/RemotiveLabs/lib/python3.10/site-packages (from pylint==3.2.5) (5.13.2)
Requirement already satisfied: tomlkit>=0.10.1 in /mnt/c/Users/Oscar Gren/Documents/RemotiveLabs/lib/python3.10/site-packages (from pylint==3.2.5) (0.12.5)
Requirement already satisfied: protobuf in /mnt/c/Users/Oscar Gren/Documents/RemotiveLabs/lib/python3.10/site-packages (from pylint-protobuf==0.22.0) (5.27.2)
Requirement already satisfied: requests~=2.21 in /mnt/c/Users/Oscar Gren/Documents/RemotiveLabs/lib/python3.10/site-packages (from remotivelabs-broker) (2.32.3)
Requirement already satisfied: grpc-stubs~=1.53.0.5 in /mnt/c/Users/Oscar Gren/Documents/RemotiveLabs/lib/python3.10/site-packages (from remotivelabs-broker) (1.53.0.5)
Collecting protobuf
Using cached protobuf-3.20.1-cp310-cp310-manylinux_2_12_x86_64.manylinux2010_x86_64.whl (1.1 MB)
Requirement already satisfied: grpcio~=1.44 in /mnt/c/Users/Oscar Gren/Documents/RemotiveLabs/lib/python3.10/site-packages (from remotivelabs-broker) (1.64.1)
Requirement already satisfied: types-protobuf~=4.24.0.20240106 in /mnt/c/Users/Oscar Gren/Documents/RemotiveLabs/lib/python3.10/site-packages (from remotivelabs-broker) (4.24.0.20240408)
Requirement already satisfied: grpc-interceptor~=0.14 in /mnt/c/Users/Oscar Gren/Documents/RemotiveLabs/lib/python3.10/site-packages (from remotivelabs-broker) (0.15.4)
Requirement already satisfied: mypy-protobuf~=3.3.0 in /mnt/c/Users/Oscar Gren/Documents/RemotiveLabs/lib/python3.10/site-packages (from remotivelabs-broker) (3.3.0)
Requirement already satisfied: iniconfig in /mnt/c/Users/Oscar Gren/Documents/RemotiveLabs/lib/python3.10/site-packages (from pytest) (2.0.0)
Requirement already satisfied: exceptiongroup>=1.0.0rc8 in /mnt/c/Users/Oscar Gren/Documents/RemotiveLabs/lib/python3.10/site-packages (from pytest) (1.2.1)
Requirement already satisfied: pluggy<2.0,>=1.5 in /mnt/c/Users/Oscar Gren/Documents/RemotiveLabs/lib/python3.10/site-packages (from pytest) (1.5.0)
Requirement already satisfied: packaging in /mnt/c/Users/Oscar Gren/Documents/RemotiveLabs/lib/python3.10/site-packages (from pytest) (24.1)
Requirement already satisfied: certifi>=2017.4.17 in /mnt/c/Users/Oscar Gren/Documents/RemotiveLabs/lib/python3.10/site-packages (from requests~=2.21->remotivelabs-broker) (2024.6.2)
Requirement already satisfied: idna<4,>=2.5 in /mnt/c/Users/Oscar Gren/Documents/RemotiveLabs/lib/python3.10/site-packages (from requests~=2.21->remotivelabs-broker) (3.7)
Requirement already satisfied: urllib3<3,>=1.21.1 in /mnt/c/Users/Oscar Gren/Documents/RemotiveLabs/lib/python3.10/site-packages (from requests~=2.21->remotivelabs-broker) (2.2.2)
Requirement already satisfied: charset-normalizer<4,>=2 in /mnt/c/Users/Oscar Gren/Documents/RemotiveLabs/lib/python3.10/site-packages (from requests~=2.21->remotivelabs-broker) (3.3.2)
Installing collected packages: protobuf
Attempting uninstall: protobuf
Found existing installation: protobuf 5.27.2
Uninstalling protobuf-5.27.2:
Successfully uninstalled protobuf-5.27.2
Successfully installed protobuf-3.20.1
2 changes: 1 addition & 1 deletion python/cloud-demo/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Python 3 is required

```
pip3 install -r requirements.txt
python3 cloud-demo.py \
python3 cloud_demo.py \
--url <url_from_cloud> \
--api-key <api_key_from_cloud> \
--signals Speed,SteeringWheel_Position,Accelerator_PedalPosition
Expand Down
77 changes: 0 additions & 77 deletions python/cloud-demo/cloud-demo.py

This file was deleted.

78 changes: 78 additions & 0 deletions python/cloud-demo/cloud_demo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import signal as signals
import sys
import threading
from argparse import Namespace
from typing import Any

from lib import arguments
from lib.broker import Broker

# Simple program designed to be used with our cloud demo.
# It is expected that you have followed the steps at
# https://demo.remotivelabs.com and started a broker + uploaded
# a recording.

# Once you complete the stages in our cloud-demo you will get all
# information required to run this program. It will look something like:

# python3 cloud_demo.py \
# --url <broker_url> \
# --api-key <api_key> \
# --signals VehicleSpeed,ChassisAcceleratorPedalposition

expected_available_signals = [
"VehicleSpeed",
"ChassisSteeringwheelAngle",
"ChassisAcceleratorPedalposition",
"VehicleCurrentlocationLongitude",
"VehicleCurrentlocationLatitude",
]


def print_signals(frame: Any) -> None:
for s in frame:
print(s)


def main(argv: Namespace) -> None:
print(f"Connecting to {argv.url}")

broker = Broker(argv.url, argv.api_key, argv.access_token)

print("Listing available signals")
available_signals = broker.list_signal_names()
for signal in available_signals:
print(f" - {signal}")

# Sanity check so we are running against the expected recording
if available_signals != expected_available_signals:
print(
"It does not look like you have started the demo recording in cloud. \n"
"Make sure you play turning-torso-drivecycle.zip on this broker from "
"https://demo.remotivelabs.com/p/demo/brokers"
)
sys.exit(0)

# Start subscribe to selected signals
subscription = broker.subscribe(signals=argv.signals, on_frame=print_signals, changed_values_only=True)

# play demo recording
broker.play(namespace="custom_can", path="turning-torso-drivecycle.zip")

# Wait 20 seconds and then shutdown.
# Remove the lines below to just have it running
sleep(seconds=20)

print("Cancelling subscription (20 secs or ctr-c, you can change this to just keep it running)")
subscription.cancel()


def sleep(seconds: float) -> None:
lock = threading.Event()
signals.signal(signals.SIGINT, lambda signum, frame: lock.set())
lock.wait(timeout=seconds)


if __name__ == "__main__":
args = arguments.parse()
main(args)
Empty file.
8 changes: 5 additions & 3 deletions python/cloud-demo/lib/arguments.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from __future__ import annotations

import argparse


def parse(argv):
def parse() -> argparse.Namespace:
parser = argparse.ArgumentParser(description="Provide address to RemotiveBroker")

parser.add_argument(
Expand All @@ -18,7 +20,7 @@ def parse(argv):
type=str,
help="API key is required when accessing brokers running in the cloud",
required=False,
default=None
default=None,
)

parser.add_argument(
Expand All @@ -36,7 +38,7 @@ def parse(argv):
required=False,
help="Comma separated list of signal names to subscribe on",
default="Speed",
type=lambda s: [item for item in s.split(',')]
type=lambda s: list(s.split(",")),
)

args = parser.parse_args()
Expand Down
69 changes: 33 additions & 36 deletions python/cloud-demo/lib/broker.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
from __future__ import annotations

import binascii
import queue
from threading import Thread
import remotivelabs.broker.sync as br
from typing import Any, Callable

from typing import Optional
import remotivelabs.broker.sync as br
from typing_extensions import Self


# pylint: disable=R0902
class Broker:

def __init__(self, url, api_key:str = Optional[None], access_token:str = Optional[None]):
def __init__(self, url: str, api_key: str | None = None, access_token: str | None = None) -> None:
self.url = url
self.api_key = api_key
self.q = queue.Queue()
self.q: queue.Queue[Any] = queue.Queue()
"""Main function, checking arguments passed to script, setting up stubs, configuration and starting Threads."""
# Setting up stubs and configuration
self.intercept_channel = br.create_channel(url, api_key, access_token)
Expand All @@ -21,37 +24,35 @@ def __init__(self, url, api_key:str = Optional[None], access_token:str = Optiona
self.traffic_stub = br.traffic_api_pb2_grpc.TrafficServiceStub(self.intercept_channel)
self.signal_creator = br.SignalCreator(self.system_stub)

def play(self, namespace: str, path: str):
def play(self, namespace: str, path: str) -> None:
playback_list = [
{
"namespace": namespace,
"path": path,
"mode": br.traffic_api_pb2.Mode.PLAY,
}]
}
]

status = self.traffic_stub.PlayTraffic(
br.traffic_api_pb2.PlaybackInfos(
playbackInfo=list(map(self.__create_playback_config, playback_list))
)
self.traffic_stub.PlayTraffic(
br.traffic_api_pb2.PlaybackInfos(playbackInfo=list(map(self.__create_playback_config, playback_list)))
)

def list_signal_names(self):
def list_signal_names(self) -> list[str]:
# Lists available signals
configuration = self.system_stub.GetConfiguration(br.common_pb2.Empty())

signal_names = []
for networkInfo in configuration.networkInfo:
res = self.system_stub.ListSignals(networkInfo.namespace)
for network_info in configuration.networkInfo:
res = self.system_stub.ListSignals(network_info.namespace)
for finfo in res.frame:
for sinfo in finfo.childInfo:
signal_names.append(sinfo.id.name)
return signal_names

def subscribe(self, signals: list, on_frame, changed_values_only: bool = True):
def subscribe(self, signals: list[br.network_api_pb2.Signal], on_frame: Callable[..., None], changed_values_only: bool = True) -> Any:
client_id = br.common_pb2.ClientId(id="cloud_demo")

signals_to_subscribe_on = \
map(lambda signal: self.signal_creator.signal(signal, "custom_can"), signals)
signals_to_subscribe_on = map(lambda signal: self.signal_creator.signal(signal, "custom_can"), signals)

Thread(
target=br.act_on_signal,
Expand All @@ -65,35 +66,31 @@ def subscribe(self, signals: list, on_frame, changed_values_only: bool = True):
),
).start()
# Wait for subscription
ecu, subscription = self.q.get()
_, subscription = self.q.get()
return subscription

@classmethod
def connect(cls, url, api_key: str = Optional[None], access_token: str = Optional[None]):
return Broker(url, api_key, access_token)
def connect(cls, url: str, api_key: str | None = None, access_token: str | None = None) -> Self:
return Broker(url, api_key, access_token) # type: ignore

def __each_signal(self, signals, callback):
callback(map(lambda s: {
'timestamp_nanos': s.timestamp,
'name': s.id.name,
'value': self.__get_value(s)
}, signals))
def __each_signal(self, signals: br.network_api_pb2.Signals, callback: Callable[..., Any]) -> None:
callback(map(lambda s: {"timestamp_nanos": s.timestamp, "name": s.id.name, "value": self.__get_value(s)}, signals))

@staticmethod
def __get_value(signal):
def __get_value(signal: br.network_api_pb2.Signal) -> Any:
if signal.raw != b"":
return "0x" + binascii.hexlify(signal.raw).decode("ascii")
elif signal.HasField("integer"):
if signal.HasField("integer"):
return signal.integer
elif signal.HasField("double"):
if signal.HasField("double"):
return signal.double
elif signal.HasField("arbitration"):
if signal.HasField("arbitration"):
return signal.arbitration
else:
return "empty"

return "empty"

@staticmethod
def __create_playback_config(item):
def __create_playback_config(item: dict[str, Any]) -> br.traffic_api_pb2.PlaybackInfo:
"""Creating configuration for playback
Parameters
Expand All @@ -107,14 +104,14 @@ def __create_playback_config(item):
Object instance of class
"""
playbackConfig = br.traffic_api_pb2.PlaybackConfig(
playback_config = br.traffic_api_pb2.PlaybackConfig(
fileDescription=br.system_api_pb2.FileDescription(path=item["path"]),
namespace=br.common_pb2.NameSpace(name=item["namespace"]),
)
return br.traffic_api_pb2.PlaybackInfo(
playbackConfig=playbackConfig,
playbackConfig=playback_config,
playbackMode=br.traffic_api_pb2.PlaybackMode(mode=item["mode"]),
)


Broker.connect = classmethod(Broker.connect)
Broker.connect = classmethod(Broker.connect) # type: ignore
Loading

0 comments on commit fea3f29

Please sign in to comment.