### Install up-python Package Source: https://github.com/eclipse-uprotocol/up-python/blob/main/README.adoc Installs the up-python library locally using pip. This makes the library's modules and classes accessible for import in your Python projects. ```python python -m pip install ../ ``` -------------------------------- ### Implement UTransport Interface (Python) Source: https://github.com/eclipse-uprotocol/up-python/blob/main/uprotocol/transport/README.adoc Demonstrates how to implement the `UTransport` interface, which defines the methods a transport middleware must provide to be used by the uProtocol library. This includes methods for sending messages, registering/unregistering listeners, and getting the source URI. ```python class HappyUTransport(UTransport): async def close(self) -> None: pass async def send(self, message): return UStatus(code=UCode.INVALID_ARGUMENT if message is None else UCode.OK) async def register_listener(self, source_filter: UUri, listener: UListener, sink_filter: UUri = None) -> UStatus: listener.on_receive(UMessage()) return UStatus(code=UCode.OK) async def unregister_listener(self, source_filter: UUri, listener, sink_filter: UUri = None): return UStatus(code=UCode.OK) def get_source(self): return UUri() ``` -------------------------------- ### Send Notification with Python Notifier Source: https://github.com/eclipse-uprotocol/up-python/blob/main/uprotocol/communication/README.adoc Demonstrates sending a notification using the Notifier interface. This requires the source URI, the destination URI, and a UTransport instance. The notification is sent without a payload in this example. ```python transport = # your UTransport instance #Notification topic uri : UUri = UUri( ue_id=4, ue_version_major=1, resource_id=0x8000) #Destination for the notification destination_uri : UUri = UUri( ue_id=3, ue_version_major=1) notifier: Notifier = UClient(transport) # Send the notification (without payload) await notifier.notify(uri, destination_uri) ``` -------------------------------- ### Clone uProtocol Python Repository Source: https://github.com/eclipse-uprotocol/up-python/blob/main/README.adoc Clones the up-python repository from GitHub, including submodules, which is the first step in setting up the SDK. Ensures all necessary project files and dependencies are downloaded. ```bash git clone --recurse-submodules https://github.com/eclipse-uprotocol/up-python.git cd up-python ``` -------------------------------- ### Build Publish Message using UMessageBuilder (Python) Source: https://github.com/eclipse-uprotocol/up-python/blob/main/uprotocol/transport/README.adoc Shows how to use `UMessageBuilder` to construct a publish message. It covers building a basic publish message and how to include a payload, such as packing a `UUri` object into a `UPayload`. ```python topic : UUri = UUri( ue_id=4, ue_version_major=1, resource_id=0x8000) UMessageBuilder.publish(topic).build() # to add payload, pack your proto message in UPayload # to pack UUri() UMessageBuilder.publish(topic).build_from_upayload(UPayload.pack(UUri())) ``` -------------------------------- ### Create and Validate uProtocol UUID in Python Source: https://github.com/eclipse-uprotocol/up-python/blob/main/uprotocol/uuid/README.adoc Demonstrates the creation of a uProtocol UUID using the provided factories and verifies its properties using utility functions. It also shows serialization and deserialization, along with assertions to confirm the expected behavior and validity of the UUID. ```python uuid = Factories.UPROTOCOL.create() version = UUIDUtils.get_version(uuid) time = UUIDUtils.get_time(uuid) str_uuid = UuidSerializer.serialize(uuid) assertTrue(UUIDUtils.is_uprotocol(uuid)) assertTrue(UUIDUtils.is_uuid(uuid)) assertFalse(UUIDUtils.is_uuidv6(uuid)) assertTrue(version) assertTrue(time) assertFalse(str_uuid.isspace()) uuid1 = UuidSerializer.deserialize(uuid_string) assertNotEqual(uuid1, UUID()) assertEqual(uuid, uuid1) ``` -------------------------------- ### Subscribe and Unsubscribe using USubscriptionClient in Python Source: https://github.com/eclipse-uprotocol/up-python/blob/main/uprotocol/client/README.adoc Demonstrates how to use the USubscriptionClient to subscribe to a topic and unsubscribe from it. It requires a UTransport instance and defines listener classes for handling received messages and subscription state changes. The InMemoryUSubscriptionClient is used for subscription management. ```python transport = # your UTransport instance #subscription topic topic : UUri = UUri( ue_id=4, ue_version_major=1, resource_id=0x8000) #Listener to process incoming events on the topic class MySubscriptionListener(UListener): def on_receive(self, umsg: UMessage) -> None: # Handle receiving published message pass listener = MySubscriptionListener() # Optional handler that is called whenever the SubscriptionState changes for the subscriber class MySubscriptionChangeHandler(SubscriptionChangeHandler) def handle_subscription_change(self, topic: UUri, status: SubscriptionStatus) -> None: # Handle subscription status changes if you're interested like when # the subscription state changes from SUBSCRIBE_PENDING to SUBSCRIBED pass subscriber : InMemoryUSubscriptionClient = InMemoryUSubscriptionClient(transport) response : SubscriptionResponse = subscriber.subscribe(topic, listener, CallOptions.DEFAULT, MySubscriptionChangeHandler()) #UnSubscribe from the topic status : UStatus = subscriber.unsubscribe(topic, listener) ``` -------------------------------- ### Generate Python Proto Bindings Source: https://github.com/eclipse-uprotocol/up-python/blob/main/README.adoc Generates Python protocol buffer files locally by compiling the 'up-spec' protos. This script is essential for having the necessary data models available for use within the Python library. ```python python generate_proto.py ``` -------------------------------- ### Publish Message with Python Publisher Source: https://github.com/eclipse-uprotocol/up-python/blob/main/uprotocol/communication/README.adoc Demonstrates how to publish a message using the Publisher interface from the uProtocol L2 Communication Layer. It requires a UTransport instance and a target UUri. The output is a published message on the specified topic. ```python transport = # your UTransport instance #topic to publish topic : UUri = UUri( ue_id=4, ue_version_major=1, resource_id=0x8000) publisher : Publisher = UClient(transport) #send the publish message publisher.publish(topic) ``` -------------------------------- ### Generate HTML Coverage Report Source: https://github.com/eclipse-uprotocol/up-python/blob/main/README.adoc Generates an HTML report detailing code coverage from the previous test run. The report is saved to 'htmlcov/index.html', providing a visual representation of test coverage. ```bash python -m coverage report python -m coverage html ``` -------------------------------- ### Build Request Message using UMessageBuilder (Python) Source: https://github.com/eclipse-uprotocol/up-python/blob/main/uprotocol/transport/README.adoc Demonstrates how to build a request message using `UMessageBuilder`. This includes defining the source, sink, and a time-to-live (TTL) for the request. ```python source : UUri = UUri( ue_id=4, ue_version_major=1, resource_id=0x8000) sink : UUri = UUri( ue_id=3, ue_version_major=1) #1000 is ttl here UMessageBuilder.request(source, sink, 1000).build() ``` -------------------------------- ### Run Python Tests with Coverage Source: https://github.com/eclipse-uprotocol/up-python/blob/main/README.adoc Executes the project's tests using pytest while measuring code coverage. It targets the 'uprotocol/' directory and generates a report of test execution and code coverage. ```bash python -m coverage run --source uprotocol/ -m pytest ``` -------------------------------- ### Build Notification Message using UMessageBuilder (Python) Source: https://github.com/eclipse-uprotocol/up-python/blob/main/uprotocol/transport/README.adoc Illustrates the process of creating a notification message using `UMessageBuilder`. This involves specifying the topic (source) and the destination URI for the notification. ```python topic : UUri = UUri( ue_id=4, ue_version_major=1, resource_id=0x8000) destination_uri : UUri = UUri( ue_id=3, ue_version_major=1) UMessageBuilder.notification(topic, destination).build() ``` -------------------------------- ### Update Git Submodules Source: https://github.com/eclipse-uprotocol/up-python/blob/main/README.adoc Updates and initializes git submodules for a repository that was already cloned without the `--recurse-submodules` flag. This ensures that nested repositories, like up-spec, are correctly fetched. ```bash git submodule update --init --recursive ``` -------------------------------- ### Build Response Message using UMessageBuilder (Python) Source: https://github.com/eclipse-uprotocol/up-python/blob/main/uprotocol/transport/README.adoc Explains how to construct a response message using `UMessageBuilder`. This requires specifying the source, sink, and a request ID (`reqid`) to correlate with the original request. ```python source : UUri = UUri( ue_id=4, ue_version_major=1, resource_id=0x8000) sink : UUri = UUri( ue_id=3, ue_version_major=1) reqid : UUID = Factories.UPROTOCOL.create() UMessageBuilder.response(source, sink, reqid).build() ``` -------------------------------- ### Register for Notifications with Python Notifier Source: https://github.com/eclipse-uprotocol/up-python/blob/main/uprotocol/communication/README.adoc Shows how to register a listener to receive notifications for a specific topic using the Notifier interface. It requires the topic URI, a UListener implementation, and a UTransport instance. The listener's on_receive method will be called upon receiving a notification. ```python transport = # your UTransport instance #Notification topic uri : UUri = UUri( ue_id=4, ue_version_major=1, resource_id=0x8000) #Listener to process incoming events on the topic class MyListener(UListener): def on_receive(self, umsg: UMessage) -> None: # Handle receiving notifications here pass listener = MyListener() notifier: Notifier = UClient(transport) # Register listener to recieve notifications await notifier.registerNotificationListener(uri, listener) ``` -------------------------------- ### Invoke Method with Python RpcClient Source: https://github.com/eclipse-uprotocol/up-python/blob/main/uprotocol/communication/README.adoc Shows how to invoke a remote method using the RpcClient interface. This involves specifying the target method URI, an optional payload, and CallOptions. It returns an asyncio Future with the response payload or raises an exception on failure. Requires a UTransport instance. ```python transport = # your UTransport instance #URI of the method to be invoked method_uri= UUri(ue_id=10, ue_version_major=1, resource_id=3) payload = UPayload.pack_to_any(UUri()) options = CallOptions(2000, UPriority.UPRIORITY_CS5) rpc_client : RpcClient = UClient(transport) #Returns the asyncio Future with the response payload or raises an exception #with the failure reason as UStatus await rpc_client.invoke_method(method_uri, payload, options) ``` -------------------------------- ### Clean up Project Files Source: https://github.com/eclipse-uprotocol/up-python/blob/main/README.adoc Removes generated files and temporary build artifacts from the project directory. This command helps maintain a clean project state. ```python python clean_project.py ``` -------------------------------- ### Register RPC Request Handler with Python RpcServer Source: https://github.com/eclipse-uprotocol/up-python/blob/main/uprotocol/communication/README.adoc Illustrates how to register a request handler for incoming RPC requests using the RpcServer interface. It requires a target URI and a custom RequestHandler class that implements the handle_request method. The server automatically sends a response. Requires a UTransport instance. ```python transport = # your UTransport instance #URI of the method to be invoked uri= UUri(ue_id=10, ue_version_major=1, resource_id=3) #Handler for processing requests class MyRequestHandler(RequestHandler): def handle_request(self, message: UMessage) -> UPayload: # If your processing of the request was successful, you return the response message like # return UPayload.EMPTY; # If your processing of the request failed, you can raise a UStatusException passing the # appropriate UCode and message such as: # raise UStatusException(UCode.FAILED_PRECONDITION, "Failed to process the request") # For this example, we will return an empty response return UPayload.EMPTY rpc_server: RpcServer = UClient(transport) #Returns the asyncio Future with the response payload or raises an exception #with the failure reason as UStatus await rpc_server.register_request_handler(uri, MyRequestHandler()) ``` -------------------------------- ### Build Response Message for Request using UMessageBuilder (Python) Source: https://github.com/eclipse-uprotocol/up-python/blob/main/uprotocol/transport/README.adoc Shows a convenient way to build a response message that is directly associated with a received request. This method utilizes the attributes from the original request to populate the response. ```python UMessageBuilder.response_for_request(request.attributes).build() ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.