Skip to content

Commit

Permalink
Add ruff lint workflow and address linting issues (#6)
Browse files Browse the repository at this point in the history
* Add linting and formatting checks in workflow

* Address linting and formatting issues identified by Ruff
  • Loading branch information
neelam-kushwah committed Jun 4, 2024
1 parent ac112fe commit 0501a77
Show file tree
Hide file tree
Showing 10 changed files with 153 additions and 62 deletions.
37 changes: 37 additions & 0 deletions .github/ruff.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# ruff.toml

line-length = 120

# Exclude specific files or directories.
exclude = [
"build/",
"dist/",
".venv/",
"__pycache__/",
]

[lint]
# Enable all basic PEP 8 checks and other common linting rules.
select = [
"E", # PEP 8 rules (formatting issues)
"F", # Pyflakes rules (undefined names, etc.)
"W", # Additional PEP 8 rules (warning-level issues)
"I", # Import order checker (ensuring imports are ordered correctly)
"ERA", # flake8-eradicate (identifying commented-out code)
"N" # Naming convention rules
]

ignore = [
"F811", # Ignore the error for multimethod function
]

[lint.flake8-annotations]
allow-star-arg-any = true

[format]
quote-style = "preserve"
indent-style = "space"
docstring-code-format = true
docstring-code-line-length = 100


44 changes: 44 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# This workflow uses actions that are not certified by GitHub.
# They are provided by a third-party and are governed by
# separate terms of service, privacy policy, and support
# documentation.

name: Run Linter

on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]

permissions:
contents: read

jobs:
deploy:

runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v3


- name: Set up Python
uses: actions/setup-python@v3
with:
python-version: '3.x'

- name: Install Dependencies
run: |
python -m pip install --upgrade pip
python -m pip install ruff
- name: Lint Python Code with Ruff
run: |
ruff check --config .github/ruff.toml
- name: Check Python Code Formatting with Ruff
run: |
ruff format --check --config .github/ruff.toml
6 changes: 2 additions & 4 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,12 @@

import os

from setuptools import setup, find_packages
from setuptools import find_packages, setup

project_name = "up-client-zenoh-python"

script_directory = os.path.realpath(os.path.dirname(__file__))
REQUIREMENTS = [
i.strip() for i in open(os.path.join("requirements.txt")).readlines()
]
REQUIREMENTS = [i.strip() for i in open(os.path.join("requirements.txt")).readlines()]

setup(
name=project_name,
Expand Down
15 changes: 7 additions & 8 deletions up_client_zenoh/examples/common_uuri.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@
from enum import Enum

import zenoh
from uprotocol.proto.uri_pb2 import UAuthority
from uprotocol.proto.uri_pb2 import UEntity
from uprotocol.proto.uri_pb2 import UResource
from uprotocol.proto.uri_pb2 import UAuthority, UEntity, UResource
from uprotocol.uri.factory.uresource_builder import UResourceBuilder

# Configure the logging
Expand All @@ -38,8 +36,12 @@ def authority() -> UAuthority:


def entity(example_type: ExampleType) -> UEntity:
mapping = {ExampleType.PUBLISHER : ("publisher", 1), ExampleType.SUBSCRIBER: ("subscriber", 2),
ExampleType.RPC_SERVER: ("rpc_server", 3), ExampleType.RPC_CLIENT: ("rpc_client", 4)}
mapping = {
ExampleType.PUBLISHER: ("publisher", 1),
ExampleType.SUBSCRIBER: ("subscriber", 2),
ExampleType.RPC_SERVER: ("rpc_server", 3),
ExampleType.RPC_CLIENT: ("rpc_client", 4),
}
name, id = mapping[example_type]
return UEntity(name=name, id=1, version_major=id)

Expand Down Expand Up @@ -70,7 +72,4 @@ def get_zenoh_default_config():
# Create a Zenoh configuration object with default settings
config = zenoh.Config()

# # Set the mode to Peer (or Router, Client depending on your use case)
# config = "peer"

return config
9 changes: 4 additions & 5 deletions up_client_zenoh/examples/publish.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,18 @@

from uprotocol.proto.uattributes_pb2 import UPriority
from uprotocol.proto.umessage_pb2 import UMessage
from uprotocol.proto.upayload_pb2 import UPayloadFormat, UPayload
from uprotocol.proto.upayload_pb2 import UPayload, UPayloadFormat
from uprotocol.proto.uri_pb2 import UUri
from uprotocol.transport.builder.uattributesbuilder import UAttributesBuilder

from up_client_zenoh.examples import common_uuri
from up_client_zenoh.examples.common_uuri import authority, entity, ExampleType, pub_resource, \
get_zenoh_default_config
from up_client_zenoh.examples.common_uuri import ExampleType, authority, entity, get_zenoh_default_config, pub_resource
from up_client_zenoh.upclientzenoh import UPClientZenoh

publisher = UPClientZenoh(get_zenoh_default_config(), authority(), entity(ExampleType.PUBLISHER))


def publishtoZenoh():
def publish_to_zenoh():
# create uuri
uuri = UUri(entity=entity(ExampleType.PUBLISHER), resource=pub_resource())
cnt = 0
Expand All @@ -44,4 +43,4 @@ def publishtoZenoh():


if __name__ == '__main__':
publishtoZenoh()
publish_to_zenoh()
2 changes: 1 addition & 1 deletion up_client_zenoh/examples/rpc_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from uprotocol.proto.uri_pb2 import UUri

from up_client_zenoh.examples import common_uuri
from up_client_zenoh.examples.common_uuri import authority, entity, ExampleType, rpc_resource, get_zenoh_default_config
from up_client_zenoh.examples.common_uuri import ExampleType, authority, entity, get_zenoh_default_config, rpc_resource
from up_client_zenoh.upclientzenoh import UPClientZenoh

rpc_client = UPClientZenoh(get_zenoh_default_config(), authority(), entity(ExampleType.RPC_CLIENT))
Expand Down
7 changes: 2 additions & 5 deletions up_client_zenoh/examples/rpc_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,20 @@
from datetime import datetime

from uprotocol.proto.umessage_pb2 import UMessage
from uprotocol.proto.upayload_pb2 import UPayload
from uprotocol.proto.upayload_pb2 import UPayloadFormat
from uprotocol.proto.upayload_pb2 import UPayload, UPayloadFormat
from uprotocol.proto.uri_pb2 import UUri
from uprotocol.proto.ustatus_pb2 import UStatus
from uprotocol.transport.builder.uattributesbuilder import UAttributesBuilder
from uprotocol.transport.ulistener import UListener

from up_client_zenoh.examples import common_uuri
from up_client_zenoh.examples.common_uuri import authority, entity, ExampleType, rpc_resource, \
get_zenoh_default_config
from up_client_zenoh.examples.common_uuri import ExampleType, authority, entity, get_zenoh_default_config, rpc_resource
from up_client_zenoh.upclientzenoh import UPClientZenoh

rpc_server = UPClientZenoh(get_zenoh_default_config(), authority(), entity(ExampleType.RPC_SERVER))


class RPCRequestListener(UListener):

def on_receive(self, msg: UMessage) -> UStatus:
attributes = msg.attributes
payload = msg.payload
Expand Down
8 changes: 3 additions & 5 deletions up_client_zenoh/examples/subscribe.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,11 @@
from uprotocol.transport.ulistener import UListener

from up_client_zenoh.examples import common_uuri
from up_client_zenoh.examples.common_uuri import authority, entity, ExampleType, pub_resource, \
get_zenoh_default_config
from up_client_zenoh.examples.common_uuri import ExampleType, authority, entity, get_zenoh_default_config, pub_resource
from up_client_zenoh.upclientzenoh import UPClientZenoh


class MyListener(UListener):

def on_receive(self, msg: UMessage) -> UStatus:
common_uuri.logging.debug('on receive called')
common_uuri.logging.debug(msg.payload.value)
Expand All @@ -37,13 +35,13 @@ def on_receive(self, msg: UMessage) -> UStatus:
client = UPClientZenoh(get_zenoh_default_config(), authority(), entity(ExampleType.SUBSCRIBER))


def subscribeToZenoh():
def subscribe_to_zenoh():
# create uuri
uuri = UUri(entity=entity(ExampleType.PUBLISHER), resource=pub_resource())
client.register_listener(uuri, MyListener())


if __name__ == '__main__':
subscribeToZenoh()
subscribe_to_zenoh()
while True:
time.sleep(1)
62 changes: 39 additions & 23 deletions up_client_zenoh/upclientzenoh.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,20 @@
from typing import Dict, Tuple

import zenoh
from uprotocol.proto.uattributes_pb2 import CallOptions
from uprotocol.proto.uattributes_pb2 import UAttributes, UMessageType
from uprotocol.proto.uattributes_pb2 import UPriority
from uprotocol.proto.uattributes_pb2 import CallOptions, UAttributes, UMessageType, UPriority
from uprotocol.proto.umessage_pb2 import UMessage
from uprotocol.proto.upayload_pb2 import UPayloadFormat, UPayload
from uprotocol.proto.uri_pb2 import UUri, UAuthority, UEntity
from uprotocol.proto.ustatus_pb2 import UStatus, UCode
from uprotocol.proto.upayload_pb2 import UPayload, UPayloadFormat
from uprotocol.proto.uri_pb2 import UAuthority, UEntity, UUri
from uprotocol.proto.ustatus_pb2 import UCode, UStatus
from uprotocol.rpc.rpcclient import RpcClient
from uprotocol.transport.builder.uattributesbuilder import UAttributesBuilder
from uprotocol.transport.ulistener import UListener
from uprotocol.transport.utransport import UTransport
from uprotocol.transport.validate.uattributesvalidator import Validators
from uprotocol.uri.factory.uresource_builder import UResourceBuilder
from uprotocol.uri.validator.urivalidator import UriValidator
from zenoh import open as zenoh_open, Subscriber, Encoding, Config, Query, Sample, Value, Queryable
from zenoh import Config, Encoding, Query, Queryable, Sample, Subscriber, Value
from zenoh import open as zenoh_open

from up_client_zenoh.zenohutils import ZenohUtils

Expand All @@ -48,7 +47,6 @@


class UPClientZenoh(UTransport, RpcClient):

def __init__(self, config: Config, uauthority: UAuthority, uentity: UEntity):
self.session = zenoh_open(config)
self.subscriber_map: Dict[Tuple[str, UListener], Subscriber] = {}
Expand Down Expand Up @@ -97,8 +95,7 @@ def send_publish_notification(self, zenoh_key: str, payload: UPayload, attribute
logging.debug(f"Attachment: {attachment}")
encoding = Encoding.APP_CUSTOM().with_suffix(str(payload.format))

put_builder = self.session.put(keyexpr=zenoh_key, encoding=encoding, value=buf, attachment=attachment,
priority=priority)
self.session.put(keyexpr=zenoh_key, encoding=encoding, value=buf, attachment=attachment, priority=priority)

msg = "Successfully sent data to Zenoh"
logging.debug(f"SUCCESS:{msg}")
Expand Down Expand Up @@ -156,7 +153,9 @@ def zenoh_callback(reply: Query.reply) -> None:
logging.debug(msg)
return UStatus(code=UCode.INTERNAL, message=msg)
# Create UMessage
msg = UMessage(attributes=u_attribute, payload=UPayload(length=0, format=encoding, value=sample.payload))
msg = UMessage(
attributes=u_attribute, payload=UPayload(length=0, format=encoding, value=sample.payload)
)
resp_callback.on_receive(msg)
else:
msg = f"Error while parsing Zenoh reply: {reply.error}"
Expand All @@ -167,8 +166,13 @@ def zenoh_callback(reply: Query.reply) -> None:
ttl = attributes.ttl / 1000 if attributes.ttl is not None else 1000

value = Value(payload.value, encoding=Encoding.APP_CUSTOM().with_suffix(str(payload.format)))
self.session.get(zenoh_key, lambda reply: zenoh_callback(reply), target=zenoh.QueryTarget.BEST_MATCHING(),
value=value, timeout=ttl)
self.session.get(
zenoh_key,
lambda reply: zenoh_callback(reply),
target=zenoh.QueryTarget.BEST_MATCHING(),
value=value,
timeout=ttl,
)
msg = "Successfully sent rpc request to Zenoh"
logging.debug(f"SUCCESS:{msg}")
return UStatus(code=UCode.OK, message=msg)
Expand Down Expand Up @@ -229,7 +233,6 @@ def callback(sample: Sample) -> None:

# Create Zenoh subscriber
try:

subscriber = self.session.declare_subscriber(zenoh_key, callback)
if subscriber:
with self.subscriber_lock:
Expand All @@ -239,7 +242,7 @@ def callback(sample: Sample) -> None:
msg = "Unable to register callback with Zenoh"
logging.debug(msg)
return UStatus(code=UCode.INTERNAL, message=msg)
except Exception as e:
except Exception:
msg = "Unable to register callback with Zenoh"
logging.debug(msg)
return UStatus(code=UCode.INTERNAL, message=msg)
Expand Down Expand Up @@ -285,7 +288,7 @@ def callback(query: Query) -> None:
queryable = self.session.declare_queryable(zenoh_key, callback)
with self.queryable_lock:
self.queryable_map[(topic.SerializeToString(), listener)] = queryable
except Exception as e:
except Exception:
msg = "Unable to register callback with Zenoh"
logging.debug(msg)
return UStatus(code=UCode.INTERNAL, message=msg)
Expand Down Expand Up @@ -393,7 +396,12 @@ def _remove_request_listener(self, topic: UUri, listener: UListener) -> None:
if self.queryable_map.pop((topic.SerializeToString(), listener), None) is None:
raise ValueError("RPC request listener doesn't exist")

def invoke_method(self, topic: UUri, payload: UPayload, options: CallOptions, ) -> Future:
def invoke_method(
self,
topic: UUri,
payload: UPayload,
options: CallOptions,
) -> Future:
try:
# Validate UUri
if not UriValidator.validate(topic):
Expand All @@ -411,8 +419,9 @@ def invoke_method(self, topic: UUri, payload: UPayload, options: CallOptions, )
zenoh_key = zenoh_key_result

# Create UAttributes and put into Zenoh user attachment
uattributes = UAttributesBuilder.request(self.get_response_uuri(), topic, UPriority.UPRIORITY_CS4,
options.ttl).build()
uattributes = UAttributesBuilder.request(
self.get_response_uuri(), topic, UPriority.UPRIORITY_CS4, options.ttl
).build()

attachment = ZenohUtils.uattributes_to_attachment(uattributes)

Expand All @@ -426,19 +435,26 @@ def invoke_method(self, topic: UUri, payload: UPayload, options: CallOptions, )
value = Value(buf, encoding=Encoding.APP_CUSTOM().with_suffix(str(payload.format)))

# Send the query
get_builder = self.session.get(zenoh_key, zenoh.Queue(), target=zenoh.QueryTarget.BEST_MATCHING(), value=value,
attachment=attachment, timeout=options.ttl / 1000)
get_builder = self.session.get(
zenoh_key,
zenoh.Queue(),
target=zenoh.QueryTarget.BEST_MATCHING(),
value=value,
attachment=attachment,
timeout=options.ttl / 1000,
)

for reply in get_builder.receiver:

if reply.is_ok:
encoding = ZenohUtils.to_upayload_format(reply.ok.encoding)
if not encoding:
msg = "Error while parsing Zenoh encoding"
logging.debug(f"{msg}")
raise UStatus(code=UCode.INTERNAL, message=msg)

umessage = UMessage(attributes=uattributes, payload=UPayload(format=encoding, value=reply.ok.payload))
umessage = UMessage(
attributes=uattributes, payload=UPayload(format=encoding, value=reply.ok.payload)
)
future = Future()
future.set_result(umessage)
return future
Expand Down
Loading

0 comments on commit 0501a77

Please sign in to comment.