robotcode-jsonrpc2 0.66.0__tar.gz → 0.67.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: robotcode-jsonrpc2
3
- Version: 0.66.0
3
+ Version: 0.67.0
4
4
  Summary: JSONRPC Server for RobotCode
5
5
  Project-URL: Homepage, https://robotcode.io
6
6
  Project-URL: Donate, https://github.com/sponsors/d-biehl
@@ -25,7 +25,7 @@ Classifier: Programming Language :: Python :: Implementation :: PyPy
25
25
  Classifier: Topic :: Utilities
26
26
  Classifier: Typing :: Typed
27
27
  Requires-Python: >=3.8
28
- Requires-Dist: robotcode-core==0.66.0
28
+ Requires-Dist: robotcode-core==0.67.0
29
29
  Description-Content-Type: text/markdown
30
30
 
31
31
  # robotcode-jsonrpc2
@@ -25,7 +25,7 @@ classifiers = [
25
25
  "Framework :: Robot Framework",
26
26
  "Framework :: Robot Framework :: Tool",
27
27
  ]
28
- dependencies = ["robotcode-core==0.66.0"]
28
+ dependencies = ["robotcode-core==0.67.0"]
29
29
  dynamic = ["version"]
30
30
 
31
31
  [project.urls]
@@ -0,0 +1 @@
1
+ __version__ = "0.67.0"
@@ -144,12 +144,21 @@ class InvalidProtocolVersionError(JsonRPCParseError):
144
144
  pass
145
145
 
146
146
 
147
- class RpcMethodEntry(NamedTuple):
147
+ @dataclass
148
+ class RpcMethodEntry:
148
149
  name: str
149
150
  method: Callable[..., Any]
150
151
  param_type: Optional[Type[Any]]
151
152
  cancelable: bool
152
153
 
154
+ _is_coroutine: Optional[bool] = field(default=None, init=False)
155
+
156
+ @property
157
+ def is_coroutine(self) -> bool:
158
+ if self._is_coroutine is None:
159
+ self._is_coroutine = inspect.iscoroutinefunction(self.method)
160
+ return self._is_coroutine
161
+
153
162
 
154
163
  @runtime_checkable
155
164
  class RpcMethod(Protocol):
@@ -553,10 +562,10 @@ class JsonRPCProtocol(JsonRPCProtocolBase):
553
562
 
554
563
  self._sended_request[id] = SendedRequestEntry(result, return_type_or_converter)
555
564
 
556
- request = JsonRPCRequest(id=id, method=method, params=params)
557
- self.send_message(request)
565
+ request = JsonRPCRequest(id=id, method=method, params=params)
566
+ self.send_message(request)
558
567
 
559
- return result
568
+ return result
560
569
 
561
570
  def send_request_async(
562
571
  self,
@@ -622,7 +631,7 @@ class JsonRPCProtocol(JsonRPCProtocolBase):
622
631
  JsonRPCErrorException(message.error.code, message.error.message, message.error.data)
623
632
  )
624
633
  else:
625
- self.__logger.warning(lambda: f"Response for {message} is already done.")
634
+ self.__logger.warning(lambda: f"Error Response for {message} is already done.")
626
635
 
627
636
  except (SystemExit, KeyboardInterrupt):
628
637
  raise
@@ -707,53 +716,57 @@ class JsonRPCProtocol(JsonRPCProtocolBase):
707
716
 
708
717
  params = self._convert_params(e.method, e.param_type, message.params)
709
718
 
710
- if (
711
- isinstance(e.method, HasThreaded)
712
- and e.method.__threaded__
713
- or inspect.ismethod(e.method)
714
- and isinstance(e.method.__func__, HasThreaded)
715
- and e.method.__func__.__threaded__
716
- ):
717
- task = run_coroutine_in_thread(
718
- ensure_coroutine(cast(Callable[..., Any], e.method)), *params[0], **params[1]
719
- )
719
+ if not e.is_coroutine:
720
+ self.send_response(message.id, e.method(*params[0], **params[1]))
720
721
  else:
721
- task = create_sub_task(
722
- ensure_coroutine(e.method)(*params[0], **params[1]),
723
- name=message.method,
724
- )
722
+ if (
723
+ isinstance(e.method, HasThreaded)
724
+ and e.method.__threaded__
725
+ or inspect.ismethod(e.method)
726
+ and isinstance(e.method.__func__, HasThreaded)
727
+ and e.method.__func__.__threaded__
728
+ ):
729
+ task = run_coroutine_in_thread(
730
+ ensure_coroutine(cast(Callable[..., Any], e.method)), *params[0], **params[1]
731
+ )
732
+ else:
733
+ task = create_sub_task(
734
+ ensure_coroutine(e.method)(*params[0], **params[1]),
735
+ name=message.method,
736
+ )
725
737
 
726
- with self._received_request_lock:
727
- self._received_request[message.id] = ReceivedRequestEntry(task, message, e.cancelable)
728
-
729
- def done(t: asyncio.Future[Any]) -> None:
730
- try:
731
- if not t.cancelled():
732
- ex = t.exception()
733
- if ex is not None:
734
- self.__logger.exception(ex, exc_info=ex)
735
- raise JsonRPCErrorException(JsonRPCErrors.INTERNAL_ERROR, f"{type(ex).__name__}: {ex}") from ex
736
-
737
- self.send_response(message.id, t.result())
738
- except asyncio.CancelledError:
739
- self.__logger.debug(lambda: f"request message {message!r} canceled")
740
- self.send_error(JsonRPCErrors.REQUEST_CANCELLED, "Request canceled.", id=message.id)
741
- except (SystemExit, KeyboardInterrupt):
742
- raise
743
- except JsonRPCErrorException as e:
744
- self.send_error(e.code, e.message or f"{type(e).__name__}: {e}", id=message.id, data=e.data)
745
- except BaseException as e:
746
- self.__logger.exception(e)
747
- self.send_error(JsonRPCErrors.INTERNAL_ERROR, f"{type(e).__name__}: {e}", id=message.id)
748
- finally:
749
- with self._received_request_lock:
750
- self._received_request.pop(message.id, None)
751
-
752
- task.add_done_callback(done)
753
-
754
- await task
738
+ with self._received_request_lock:
739
+ self._received_request[message.id] = ReceivedRequestEntry(task, message, e.cancelable)
740
+
741
+ def done(t: asyncio.Future[Any]) -> None:
742
+ try:
743
+ if not t.cancelled():
744
+ ex = t.exception()
745
+ if ex is not None:
746
+ self.__logger.exception(ex, exc_info=ex)
747
+ raise JsonRPCErrorException(
748
+ JsonRPCErrors.INTERNAL_ERROR, f"{type(ex).__name__}: {ex}"
749
+ ) from ex
750
+
751
+ self.send_response(message.id, t.result())
752
+ except asyncio.CancelledError:
753
+ self.__logger.debug(lambda: f"request message {message!r} canceled")
754
+ self.send_error(JsonRPCErrors.REQUEST_CANCELLED, "Request canceled.", id=message.id)
755
+ except (SystemExit, KeyboardInterrupt):
756
+ raise
757
+ except JsonRPCErrorException as e:
758
+ self.send_error(e.code, e.message or f"{type(e).__name__}: {e}", id=message.id, data=e.data)
759
+ except BaseException as e:
760
+ self.__logger.exception(e)
761
+ self.send_error(JsonRPCErrors.INTERNAL_ERROR, f"{type(e).__name__}: {e}", id=message.id)
762
+ finally:
763
+ with self._received_request_lock:
764
+ self._received_request.pop(message.id, None)
765
+
766
+ task.add_done_callback(done)
767
+
768
+ await task
755
769
 
756
- @__logger.call
757
770
  def cancel_request(self, id: Union[int, str, None]) -> None:
758
771
  with self._received_request_lock:
759
772
  entry = self._received_request.get(id, None)
@@ -762,8 +775,7 @@ class JsonRPCProtocol(JsonRPCProtocolBase):
762
775
  self.__logger.debug(lambda: f"try to cancel request {entry.request if entry is not None else ''}")
763
776
  entry.future.cancel()
764
777
 
765
- @__logger.call
766
- async def cancel_all_received_request(self) -> None:
778
+ def cancel_all_received_request(self) -> None:
767
779
  for entry in self._received_request.values():
768
780
  if entry is not None and entry.cancelable and entry.future is not None and not entry.future.cancelled():
769
781
  entry.future.cancel()
@@ -778,23 +790,26 @@ class JsonRPCProtocol(JsonRPCProtocolBase):
778
790
  try:
779
791
  params = self._convert_params(e.method, e.param_type, message.params)
780
792
 
781
- if (
782
- isinstance(e.method, HasThreaded)
783
- and e.method.__threaded__
784
- or inspect.ismethod(e.method)
785
- and isinstance(e.method.__func__, HasThreaded)
786
- and e.method.__func__.__threaded__
787
- ):
788
- task = run_coroutine_in_thread(
789
- ensure_coroutine(cast(Callable[..., Any], e.method)), *params[0], **params[1]
790
- )
793
+ if not e.is_coroutine:
794
+ e.method(*params[0], **params[1])
791
795
  else:
792
- task = create_sub_task(
793
- ensure_coroutine(e.method)(*params[0], **params[1]),
794
- name=message.method,
795
- )
796
+ if (
797
+ isinstance(e.method, HasThreaded)
798
+ and e.method.__threaded__
799
+ or inspect.ismethod(e.method)
800
+ and isinstance(e.method.__func__, HasThreaded)
801
+ and e.method.__func__.__threaded__
802
+ ):
803
+ task = run_coroutine_in_thread(
804
+ ensure_coroutine(cast(Callable[..., Any], e.method)), *params[0], **params[1]
805
+ )
806
+ else:
807
+ task = create_sub_task(
808
+ ensure_coroutine(e.method)(*params[0], **params[1]),
809
+ name=message.method,
810
+ )
796
811
 
797
- await task
812
+ await task
798
813
  except asyncio.CancelledError:
799
814
  pass
800
815
  except (SystemExit, KeyboardInterrupt):
@@ -1 +0,0 @@
1
- __version__ = "0.66.0"