openrewrite-remote 0.11.0__py3-none-any.whl → 0.13.0__py3-none-any.whl
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.
- openrewrite_remote-0.13.0.dist-info/METADATA +12 -0
- openrewrite_remote-0.13.0.dist-info/RECORD +14 -0
- {openrewrite_remote-0.11.0.dist-info → openrewrite_remote-0.13.0.dist-info}/WHEEL +2 -1
- openrewrite_remote-0.13.0.dist-info/entry_points.txt +2 -0
- openrewrite_remote-0.13.0.dist-info/top_level.txt +1 -0
- rewrite_remote/__init__.py +12 -0
- rewrite_remote/client.py +70 -0
- {rewrite/remote → rewrite_remote}/event.py +2 -2
- {rewrite/remote → rewrite_remote}/receiver.py +245 -88
- {rewrite/remote → rewrite_remote}/remote_utils.py +78 -40
- {rewrite/remote → rewrite_remote}/remoting.py +116 -44
- {rewrite/remote → rewrite_remote}/sender.py +222 -90
- {rewrite/remote → rewrite_remote}/server.py +100 -36
- rewrite_remote/type_utils.py +109 -0
- openrewrite_remote-0.11.0.dist-info/METADATA +0 -17
- openrewrite_remote-0.11.0.dist-info/RECORD +0 -12
- rewrite/remote/__init__.py +0 -7
- rewrite/remote/client.py +0 -40
- rewrite/remote/type_utils.py +0 -101
@@ -1,6 +1,8 @@
|
|
1
|
+
# type: ignore
|
1
2
|
import importlib
|
2
3
|
import importlib.resources
|
3
4
|
import os
|
5
|
+
import select
|
4
6
|
import socket
|
5
7
|
import sys
|
6
8
|
import time
|
@@ -8,58 +10,99 @@ import traceback
|
|
8
10
|
import zipfile
|
9
11
|
from io import BytesIO, StringIO
|
10
12
|
from pathlib import Path
|
13
|
+
from typing import Any
|
11
14
|
from typing import Optional
|
12
15
|
|
13
16
|
import cbor2
|
14
|
-
import select
|
15
17
|
from cbor2 import dumps
|
16
|
-
from rewrite import
|
18
|
+
from rewrite import (
|
19
|
+
ParserInput,
|
20
|
+
InMemoryExecutionContext,
|
21
|
+
ExecutionContext,
|
22
|
+
ParseError,
|
23
|
+
Recipe,
|
24
|
+
)
|
25
|
+
from rewrite.java import J
|
26
|
+
from rewrite.java.remote.receiver import JavaReceiver
|
27
|
+
from rewrite.java.remote.sender import JavaSender
|
17
28
|
from rewrite.python import Py
|
18
29
|
from rewrite.python.parser import PythonParserBuilder
|
19
|
-
from rewrite.remote import SenderContext, ReceiverContext, RemotingContext, RemotingMessenger, RemotingMessageType, \
|
20
|
-
RemotePrinterFactory, OK, ParseErrorSender
|
21
30
|
from rewrite.python.remote.receiver import PythonReceiver
|
22
31
|
from rewrite.python.remote.sender import PythonSender
|
23
32
|
|
33
|
+
from rewrite_remote.handlers.hello_world_handler import hello_world_handler
|
34
|
+
from rewrite_remote.handlers.recipe_install_handler import recipe_install_handler
|
35
|
+
from rewrite_remote.handlers.run_recipe_load_and_visitor_handler import (
|
36
|
+
run_recipe_load_and_visitor_handler,
|
37
|
+
)
|
38
|
+
from rewrite_remote.handlers.list_projects_handler import list_projects_handler
|
39
|
+
|
40
|
+
from rewrite_remote.receiver import ReceiverContext
|
41
|
+
from rewrite_remote.remoting import (
|
42
|
+
RemotePrinterFactory,
|
43
|
+
RemotingContext,
|
44
|
+
RemotingMessageType,
|
45
|
+
RemotingMessenger,
|
46
|
+
)
|
47
|
+
from rewrite_remote.sender import ParseErrorSender, SenderContext
|
48
|
+
|
24
49
|
INACTIVITY_TIMEOUT = 300 # 5 minutes
|
25
50
|
_OK: int = 0
|
26
51
|
_ERROR: int = 1
|
27
52
|
|
28
53
|
|
29
|
-
def register_remoting_factories():
|
30
|
-
SenderContext.register(ParseError,
|
31
|
-
SenderContext.register(Py,
|
32
|
-
ReceiverContext.register(Py,
|
54
|
+
def register_remoting_factories() -> None:
|
55
|
+
SenderContext.register(ParseError, ParseErrorSender)
|
56
|
+
SenderContext.register(Py, PythonSender)
|
57
|
+
ReceiverContext.register(Py, PythonReceiver)
|
58
|
+
SenderContext.register(J, JavaSender)
|
59
|
+
ReceiverContext.register(J, JavaReceiver)
|
33
60
|
|
34
61
|
|
35
|
-
def find_free_port():
|
62
|
+
def find_free_port() -> Any:
|
36
63
|
"""Find a free port by using the system to allocate a port for us."""
|
37
64
|
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
|
38
|
-
s.bind((
|
65
|
+
s.bind(("", 0))
|
39
66
|
s.listen(1)
|
40
67
|
return s.getsockname()[1]
|
41
68
|
|
42
69
|
|
43
70
|
class Server:
|
71
|
+
"""
|
72
|
+
A server that listens for incoming connections and processes requests from clients.
|
73
|
+
"""
|
74
|
+
|
44
75
|
_port: Optional[int]
|
45
76
|
_path: Optional[str]
|
46
77
|
_remoting_context: RemotingContext
|
47
78
|
_messenger: RemotingMessenger
|
48
79
|
|
49
|
-
def __init__(
|
80
|
+
def __init__(
|
81
|
+
self,
|
82
|
+
port: Optional[int] = None,
|
83
|
+
path: Optional[str] = None,
|
84
|
+
timeout: int = INACTIVITY_TIMEOUT,
|
85
|
+
):
|
50
86
|
self._port = port
|
51
87
|
self._path = path
|
52
88
|
self.timeout = timeout
|
53
89
|
self._remoting_context = RemotingContext()
|
90
|
+
self._remoting_context._recipe_factories["test"] = (
|
91
|
+
lambda recipe_options: Recipe()
|
92
|
+
)
|
54
93
|
self._messenger = RemotingMessenger(
|
55
94
|
self._remoting_context,
|
56
95
|
{
|
57
|
-
|
58
|
-
|
59
|
-
|
96
|
+
"parse-python-source": self.parse_python_source,
|
97
|
+
"parse-python-file": self.parse_python_file,
|
98
|
+
"hello-world": hello_world_handler,
|
99
|
+
"recipe-install": recipe_install_handler,
|
100
|
+
"run-recipe-load-and-visitor": run_recipe_load_and_visitor_handler,
|
101
|
+
"list-projects": list_projects_handler,
|
102
|
+
},
|
60
103
|
)
|
61
104
|
|
62
|
-
def start(self):
|
105
|
+
def start(self) -> None:
|
63
106
|
"""Start the server and listen for connections on the given port."""
|
64
107
|
if self._path:
|
65
108
|
if os.path.exists(self._path):
|
@@ -75,7 +118,7 @@ class Server:
|
|
75
118
|
else:
|
76
119
|
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
|
77
120
|
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
78
|
-
s.bind((
|
121
|
+
s.bind(("localhost", self._port))
|
79
122
|
s.listen(5)
|
80
123
|
print(f"Server listening on port {self._port}")
|
81
124
|
last_activity_time = time.time()
|
@@ -90,7 +133,9 @@ class Server:
|
|
90
133
|
except socket.timeout:
|
91
134
|
current_time = time.time()
|
92
135
|
if current_time - last_activity_time >= self.timeout:
|
93
|
-
print(
|
136
|
+
print(
|
137
|
+
"No new connections for 5 minutes, shutting down server."
|
138
|
+
)
|
94
139
|
break
|
95
140
|
|
96
141
|
def handle_client(self, sock: socket.socket) -> None:
|
@@ -130,62 +175,77 @@ class Server:
|
|
130
175
|
finally:
|
131
176
|
sock.close()
|
132
177
|
|
133
|
-
def parse_python_source(
|
178
|
+
def parse_python_source(
|
179
|
+
self, stream: BytesIO, sock: socket.socket, remoting_ctx: RemotingContext
|
180
|
+
) -> None:
|
134
181
|
remoting_ctx.reset()
|
135
182
|
source = cbor2.load(stream)
|
136
183
|
ctx = InMemoryExecutionContext()
|
137
184
|
ctx.put_message(ExecutionContext.REQUIRE_PRINT_EQUALS_INPUT, False)
|
138
|
-
for cu in
|
139
|
-
|
140
|
-
|
185
|
+
for cu in (
|
186
|
+
PythonParserBuilder()
|
187
|
+
.build()
|
188
|
+
.parse_inputs(
|
189
|
+
[ParserInput(Path("source.py"), None, True, lambda: StringIO(source))],
|
190
|
+
None,
|
191
|
+
ctx,
|
192
|
+
)
|
193
|
+
):
|
141
194
|
response_stream = BytesIO()
|
142
195
|
cbor2.dump(RemotingMessageType.Response, response_stream)
|
143
|
-
cbor2.dump(
|
196
|
+
cbor2.dump(_OK, response_stream)
|
144
197
|
source_stream = BytesIO()
|
145
198
|
remoting_ctx.new_sender_context(source_stream).send_any_tree(cu, None)
|
146
199
|
cbor2.dump(source_stream.getvalue(), response_stream)
|
147
200
|
sock.sendall(response_stream.getvalue())
|
148
201
|
|
149
|
-
def parse_python_file(
|
202
|
+
def parse_python_file(
|
203
|
+
self, stream: BytesIO, sock: socket.socket, remoting_ctx: RemotingContext
|
204
|
+
) -> None:
|
150
205
|
remoting_ctx.reset()
|
151
206
|
path = cbor2.load(stream)
|
152
207
|
ctx = InMemoryExecutionContext()
|
153
208
|
ctx.put_message(ExecutionContext.REQUIRE_PRINT_EQUALS_INPUT, False)
|
154
|
-
for cu in
|
209
|
+
for cu in (
|
210
|
+
PythonParserBuilder()
|
211
|
+
.build()
|
212
|
+
.parse_inputs(
|
155
213
|
[ParserInput(Path(path), None, True, lambda: read_file_contents(path))],
|
156
|
-
None,
|
214
|
+
None,
|
215
|
+
ctx,
|
216
|
+
)
|
217
|
+
):
|
157
218
|
response_stream = BytesIO()
|
158
219
|
cbor2.dump(RemotingMessageType.Response, response_stream)
|
159
|
-
cbor2.dump(
|
220
|
+
cbor2.dump(_OK, response_stream)
|
160
221
|
source_stream = BytesIO()
|
161
222
|
remoting_ctx.new_sender_context(source_stream).send_any_tree(cu, None)
|
162
223
|
cbor2.dump(source_stream.getvalue(), response_stream)
|
163
224
|
sock.sendall(response_stream.getvalue())
|
164
225
|
|
165
226
|
|
166
|
-
def read_file_contents(path):
|
167
|
-
with open(path,
|
227
|
+
def read_file_contents(path: str) -> StringIO:
|
228
|
+
with open(path, "r", newline="", encoding="utf-8") as file:
|
168
229
|
return StringIO(file.read())
|
169
230
|
|
170
231
|
|
171
|
-
def read_data_from_zip():
|
232
|
+
def read_data_from_zip() -> None:
|
172
233
|
# Access the resource within the 'your_package.resources' package
|
173
234
|
# 'data.zip' is the name of the file included
|
174
|
-
with importlib.resources.open_binary(
|
235
|
+
with importlib.resources.open_binary("resources", "rewrite-remote-java.zip") as f:
|
175
236
|
# Open the zip file from the resource file stream
|
176
237
|
with zipfile.ZipFile(f) as zip_file:
|
177
238
|
# List the contents of the zip file
|
178
239
|
print(zip_file.namelist())
|
179
240
|
# Read a specific file inside the zip file (if you know the file name within it)
|
180
|
-
with zip_file.open(
|
241
|
+
with zip_file.open(
|
242
|
+
"rewrite-remote-java-0.2.0-SNAPSHOT/bin/rewrite-remote-java"
|
243
|
+
) as inner_file:
|
181
244
|
data = inner_file.read()
|
182
|
-
print(data.decode(
|
245
|
+
print(data.decode("utf-8"))
|
183
246
|
|
184
247
|
|
185
|
-
|
186
|
-
# read_data_from_zip()
|
187
|
-
|
188
|
-
if __name__ == "__main__":
|
248
|
+
def main() -> Any:
|
189
249
|
sys.setrecursionlimit(2000)
|
190
250
|
port = int(sys.argv[1]) if len(sys.argv) > 1 else 54322
|
191
251
|
timeout = int(sys.argv[2]) if len(sys.argv) > 2 else INACTIVITY_TIMEOUT
|
@@ -193,3 +253,7 @@ if __name__ == "__main__":
|
|
193
253
|
Server(port=port, timeout=timeout).start()
|
194
254
|
# Server(port=find_free_port()).start()
|
195
255
|
# Server(path=tempfile.gettempdir() + '/rewrite-csharp.sock').start()
|
256
|
+
|
257
|
+
|
258
|
+
if __name__ == "__main__":
|
259
|
+
main()
|
@@ -0,0 +1,109 @@
|
|
1
|
+
import typing
|
2
|
+
from typing import Optional
|
3
|
+
from dataclasses import Field
|
4
|
+
from functools import lru_cache
|
5
|
+
from typing import Any, Type
|
6
|
+
|
7
|
+
|
8
|
+
def to_java_type_name_from_value(v: Any) -> Optional[str]:
|
9
|
+
if isinstance(v, bool):
|
10
|
+
return "java.lang.Boolean"
|
11
|
+
elif isinstance(v, int):
|
12
|
+
bit_length = v.bit_length()
|
13
|
+
if bit_length < 32:
|
14
|
+
return "java.lang.Integer"
|
15
|
+
elif bit_length < 64:
|
16
|
+
return "java.lang.Long"
|
17
|
+
else:
|
18
|
+
return "java.math.BigInteger"
|
19
|
+
elif isinstance(v, complex):
|
20
|
+
return "java.lang.String"
|
21
|
+
return to_java_type_name(type(v))
|
22
|
+
|
23
|
+
|
24
|
+
@lru_cache(maxsize=None)
|
25
|
+
def to_java_type_name(t: typing.Type[Any]) -> Optional[str]:
|
26
|
+
if t == bytes:
|
27
|
+
# FIXME can we map this somehow?
|
28
|
+
return None
|
29
|
+
if t == bool:
|
30
|
+
return "java.lang.Boolean"
|
31
|
+
if t == int:
|
32
|
+
return "java.lang.Integer"
|
33
|
+
if t == str:
|
34
|
+
return "java.lang.String"
|
35
|
+
if t == float:
|
36
|
+
return "java.lang.Double"
|
37
|
+
if t == type(None):
|
38
|
+
return "null"
|
39
|
+
if t.__module__.startswith("rewrite.java.support_types"):
|
40
|
+
if t.__name__ == "Space":
|
41
|
+
return "org.openrewrite.java.tree.Space"
|
42
|
+
if t.__name__ == "Comment":
|
43
|
+
return "org.openrewrite.java.tree.Comment"
|
44
|
+
if t.__name__ == "TextComment":
|
45
|
+
return "org.openrewrite.java.tree.TextComment"
|
46
|
+
if t.__name__ == "JLeftPadded":
|
47
|
+
return "org.openrewrite.java.tree.JLeftPadded"
|
48
|
+
if t.__name__ == "JRightPadded":
|
49
|
+
return "org.openrewrite.java.tree.JRightPadded"
|
50
|
+
if t.__name__ == "JContainer":
|
51
|
+
return "org.openrewrite.java.tree.JContainer"
|
52
|
+
if t.__module__.startswith("rewrite.java.markers"):
|
53
|
+
return "org.openrewrite.java.marker." + t.__qualname__
|
54
|
+
if t.__module__.startswith("rewrite.java.tree"):
|
55
|
+
return "org.openrewrite.java.tree.J$" + t.__qualname__.replace(".", "$")
|
56
|
+
if t.__module__.startswith("rewrite.python.support_types"):
|
57
|
+
if t.__name__ == "PyComment":
|
58
|
+
return "org.openrewrite.python.tree.PyComment"
|
59
|
+
if t.__name__ == "PyLeftPadded":
|
60
|
+
return "org.openrewrite.python.tree.PyLeftPadded"
|
61
|
+
if t.__name__ == "PyRightPadded":
|
62
|
+
return "org.openrewrite.python.tree.PyRightPadded"
|
63
|
+
if t.__name__ == "PyContainer":
|
64
|
+
return "org.openrewrite.python.tree.PyContainer"
|
65
|
+
if t.__module__.startswith("rewrite.python.markers"):
|
66
|
+
return "org.openrewrite.python.marker." + t.__qualname__
|
67
|
+
if t.__module__.startswith("rewrite.python.style"):
|
68
|
+
return "org.openrewrite.python.style." + t.__qualname__
|
69
|
+
if t.__module__.startswith("rewrite.python.tree"):
|
70
|
+
return "org.openrewrite.python.tree.Py$" + t.__qualname__.replace(".", "$")
|
71
|
+
if t.__module__.startswith("rewrite.marker"):
|
72
|
+
if t.__name__ == "ParseExceptionResult":
|
73
|
+
return "org.openrewrite.ParseExceptionResult"
|
74
|
+
return "org.openrewrite.marker." + t.__qualname__.replace(".", "$")
|
75
|
+
if t.__module__ == "rewrite.parser" and t.__name__ == "ParseError":
|
76
|
+
return "org.openrewrite.tree.ParseError"
|
77
|
+
if t.__module__.startswith("rewrite.style"):
|
78
|
+
return "org.openrewrite.style." + t.__qualname__
|
79
|
+
if t.__module__.startswith("rewrite.") and t.__module__.endswith(".tree"):
|
80
|
+
model = t.__module__.split(".")[1]
|
81
|
+
return (
|
82
|
+
"org.openrewrite."
|
83
|
+
+ model
|
84
|
+
+ ".tree."
|
85
|
+
+ model.capitalize()
|
86
|
+
+ "$"
|
87
|
+
+ t.__qualname__.replace(".", "$")
|
88
|
+
)
|
89
|
+
return t.__module__ + "." + t.__qualname__
|
90
|
+
# raise NotImplementedError("to_java_type_name: " + str(o))
|
91
|
+
|
92
|
+
|
93
|
+
@lru_cache(maxsize=None)
|
94
|
+
def to_java_field_name(field: Field[Any]) -> str:
|
95
|
+
return __convert_snake_to_camel(field.name[1:])
|
96
|
+
|
97
|
+
|
98
|
+
def __convert_snake_to_camel(field_name: str) -> str:
|
99
|
+
# Remove leading underscore
|
100
|
+
if field_name.startswith("_"):
|
101
|
+
field_name = field_name[1:]
|
102
|
+
|
103
|
+
# Convert snake case to camel case
|
104
|
+
components = field_name.split("_")
|
105
|
+
return components[0] + "".join(x.capitalize() for x in components[1:])
|
106
|
+
|
107
|
+
|
108
|
+
def get_type(type_name: str) -> Type[Any]:
|
109
|
+
raise NotImplementedError("get_type for: " + type_name)
|
@@ -1,17 +0,0 @@
|
|
1
|
-
Metadata-Version: 2.1
|
2
|
-
Name: openrewrite-remote
|
3
|
-
Version: 0.11.0
|
4
|
-
Summary: Remoting functionality for the OpenRewrite library.
|
5
|
-
License: Moderne, Inc. Commercial License
|
6
|
-
Author: Moderne Inc.
|
7
|
-
Author-email: support@moderne.io
|
8
|
-
Requires-Python: >=3.9
|
9
|
-
Classifier: License :: Other/Proprietary License
|
10
|
-
Classifier: Programming Language :: Python :: 3
|
11
|
-
Classifier: Programming Language :: Python :: 3.9
|
12
|
-
Classifier: Programming Language :: Python :: 3.10
|
13
|
-
Classifier: Programming Language :: Python :: 3.11
|
14
|
-
Classifier: Programming Language :: Python :: 3.12
|
15
|
-
Classifier: Programming Language :: Python :: 3.13
|
16
|
-
Requires-Dist: cbor2 (>=5.6.4,<6.0.0)
|
17
|
-
Requires-Dist: openrewrite
|
@@ -1,12 +0,0 @@
|
|
1
|
-
rewrite/remote/__init__.py,sha256=e8QQGZNZpUFcqo1NGQRAJb0il0_Zq0R5CrtM0i5DF9w,245
|
2
|
-
rewrite/remote/client.py,sha256=LBCh8msEv04wex7kenXVwI11i7JB4YvcnSIbf3oJ3pg,1608
|
3
|
-
rewrite/remote/event.py,sha256=9AL5z6D6_GUBUPrNb9WJCHv3RJkID4k2vgiArwIJtmQ,354
|
4
|
-
rewrite/remote/receiver.py,sha256=YLu6YhLCj6aNTeRsHdRFIeC7FY1rp4vGAA5PRze3JO0,15586
|
5
|
-
rewrite/remote/remote_utils.py,sha256=wZhsDEV3qnayy3M98r6M-NMxGeowQ1qW7rFSJR0cY24,10145
|
6
|
-
rewrite/remote/remoting.py,sha256=30tsczUenwEikpTXZE1ghNU8YTl4JU5LoXTgkzDsPgs,13091
|
7
|
-
rewrite/remote/sender.py,sha256=miE7aKTa3RLPoZgYBc0sNnaHlpC7huJuK3Lbp8Kqqo8,18433
|
8
|
-
rewrite/remote/server.py,sha256=k2fuGPR402EYOeNkYDILKq3aRTt0B0I8HZ4PySFURxs,7840
|
9
|
-
rewrite/remote/type_utils.py,sha256=XG3enI43oQF6Bq6c9eaU9bFpBTyL9JJ2qKuwmNCpyHQ,4073
|
10
|
-
openrewrite_remote-0.11.0.dist-info/METADATA,sha256=AI5JsEPUTOZadHnC0GA3QqIu53GgrCGV71iMIJoxE34,659
|
11
|
-
openrewrite_remote-0.11.0.dist-info/WHEEL,sha256=Nq82e9rUAnEjt98J6MlVmMCZb-t9cYE2Ir1kpBmnWfs,88
|
12
|
-
openrewrite_remote-0.11.0.dist-info/RECORD,,
|
rewrite/remote/__init__.py
DELETED
rewrite/remote/client.py
DELETED
@@ -1,40 +0,0 @@
|
|
1
|
-
import socket
|
2
|
-
import tempfile
|
3
|
-
from pathlib import Path
|
4
|
-
|
5
|
-
import rewrite.java.tree as j
|
6
|
-
import rewrite.python.tree as py
|
7
|
-
from rewrite import Markers
|
8
|
-
from rewrite.python import Py
|
9
|
-
from rewrite.java import Space, JavaType
|
10
|
-
|
11
|
-
from rewrite import random_id, Cursor, PrintOutputCapture
|
12
|
-
from rewrite.remote import RemotingContext, RemotePrinterFactory, SenderContext, ReceiverContext
|
13
|
-
from rewrite.remote.python.receiver import PythonReceiver
|
14
|
-
from rewrite.remote.python.sender import PythonSender
|
15
|
-
|
16
|
-
SenderContext.register(Py, lambda: PythonSender())
|
17
|
-
ReceiverContext.register(Py, lambda: PythonReceiver())
|
18
|
-
|
19
|
-
# Path to the Unix domain socket
|
20
|
-
SOCKET_PATH = tempfile.gettempdir() + '/rewrite-java.sock'
|
21
|
-
|
22
|
-
# Create a Unix domain socket
|
23
|
-
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
24
|
-
|
25
|
-
# Connect the socket to the path where the server is listening
|
26
|
-
client.connect(('localhost', 65432))
|
27
|
-
print(f'Connected to {SOCKET_PATH}')
|
28
|
-
|
29
|
-
try:
|
30
|
-
remoting = RemotingContext()
|
31
|
-
remoting.connect(client)
|
32
|
-
RemotePrinterFactory(remoting.client).set_current()
|
33
|
-
|
34
|
-
literal = j.Literal(random_id(), Space.SINGLE_SPACE, Markers.EMPTY, True, 'True', None, JavaType.Primitive())
|
35
|
-
assert_ = py.AssertStatement(random_id(), Space.EMPTY, Markers.EMPTY, [j.JRightPadded(literal, Space.EMPTY, Markers.EMPTY)])
|
36
|
-
cu = py.CompilationUnit(random_id(), Space.EMPTY, Markers.EMPTY, Path('/foo.py'), None, None, False, None, [], [j.JRightPadded(assert_, Space.EMPTY, Markers.EMPTY)], Space.EMPTY)
|
37
|
-
printed = cu.print(Cursor(None, Cursor.ROOT_VALUE), PrintOutputCapture(0))
|
38
|
-
assert printed == 'assert True'
|
39
|
-
finally:
|
40
|
-
client.close()
|
rewrite/remote/type_utils.py
DELETED
@@ -1,101 +0,0 @@
|
|
1
|
-
import typing
|
2
|
-
from dataclasses import Field
|
3
|
-
from functools import lru_cache
|
4
|
-
from typing import Any, Type
|
5
|
-
|
6
|
-
|
7
|
-
def to_java_type_name_from_value(v: Any) -> str:
|
8
|
-
if isinstance(v, bool):
|
9
|
-
return 'java.lang.Boolean'
|
10
|
-
elif isinstance(v, int):
|
11
|
-
bit_length = v.bit_length()
|
12
|
-
if bit_length < 32:
|
13
|
-
return 'java.lang.Integer'
|
14
|
-
elif bit_length < 64:
|
15
|
-
return 'java.lang.Long'
|
16
|
-
else:
|
17
|
-
return 'java.math.BigInteger'
|
18
|
-
elif isinstance(v, complex):
|
19
|
-
return 'java.lang.String'
|
20
|
-
return to_java_type_name(type(v))
|
21
|
-
|
22
|
-
|
23
|
-
@lru_cache(maxsize=None)
|
24
|
-
def to_java_type_name(t: typing.Type) -> str:
|
25
|
-
if t == bytes:
|
26
|
-
# FIXME can we map this somehow?
|
27
|
-
return None
|
28
|
-
if t == bool:
|
29
|
-
return 'java.lang.Boolean'
|
30
|
-
if t == int:
|
31
|
-
return 'java.lang.Integer'
|
32
|
-
if t == str:
|
33
|
-
return 'java.lang.String'
|
34
|
-
if t == float:
|
35
|
-
return 'java.lang.Double'
|
36
|
-
if t == type(None):
|
37
|
-
return 'null'
|
38
|
-
if t.__module__.startswith('rewrite.java.support_types'):
|
39
|
-
if t.__name__ == 'Space':
|
40
|
-
return 'org.openrewrite.java.tree.Space'
|
41
|
-
if t.__name__ == 'Comment':
|
42
|
-
return 'org.openrewrite.java.tree.Comment'
|
43
|
-
if t.__name__ == 'TextComment':
|
44
|
-
return 'org.openrewrite.java.tree.TextComment'
|
45
|
-
if t.__name__ == 'JLeftPadded':
|
46
|
-
return 'org.openrewrite.java.tree.JLeftPadded'
|
47
|
-
if t.__name__ == 'JRightPadded':
|
48
|
-
return 'org.openrewrite.java.tree.JRightPadded'
|
49
|
-
if t.__name__ == 'JContainer':
|
50
|
-
return 'org.openrewrite.java.tree.JContainer'
|
51
|
-
if t.__module__.startswith('rewrite.java.markers'):
|
52
|
-
return 'org.openrewrite.java.marker.' + t.__qualname__
|
53
|
-
if t.__module__.startswith('rewrite.java.tree'):
|
54
|
-
return 'org.openrewrite.java.tree.J$' + t.__qualname__.replace('.', '$')
|
55
|
-
if t.__module__.startswith('rewrite.python.support_types'):
|
56
|
-
if t.__name__ == 'PyComment':
|
57
|
-
return 'org.openrewrite.python.tree.PyComment'
|
58
|
-
if t.__name__ == 'PyLeftPadded':
|
59
|
-
return 'org.openrewrite.python.tree.PyLeftPadded'
|
60
|
-
if t.__name__ == 'PyRightPadded':
|
61
|
-
return 'org.openrewrite.python.tree.PyRightPadded'
|
62
|
-
if t.__name__ == 'PyContainer':
|
63
|
-
return 'org.openrewrite.python.tree.PyContainer'
|
64
|
-
if t.__module__.startswith('rewrite.python.markers'):
|
65
|
-
return 'org.openrewrite.python.marker.' + t.__qualname__
|
66
|
-
if t.__module__.startswith('rewrite.python.style'):
|
67
|
-
return 'org.openrewrite.python.style.' + t.__qualname__
|
68
|
-
if t.__module__.startswith('rewrite.python.tree'):
|
69
|
-
return 'org.openrewrite.python.tree.Py$' + t.__qualname__.replace('.', '$')
|
70
|
-
if t.__module__.startswith('rewrite.marker'):
|
71
|
-
if t.__name__ == 'ParseExceptionResult':
|
72
|
-
return 'org.openrewrite.ParseExceptionResult'
|
73
|
-
return 'org.openrewrite.marker.' + t.__qualname__.replace('.', '$')
|
74
|
-
if t.__module__ == 'rewrite.parser' and t.__name__ == 'ParseError':
|
75
|
-
return 'org.openrewrite.tree.ParseError'
|
76
|
-
if t.__module__.startswith('rewrite.style'):
|
77
|
-
return 'org.openrewrite.style.' + t.__qualname__
|
78
|
-
if t.__module__.startswith('rewrite.') and t.__module__.endswith('.tree'):
|
79
|
-
model = t.__module__.split('.')[1]
|
80
|
-
return 'org.openrewrite.' + model + '.tree.' + model.capitalize() + '$' + t.__qualname__.replace('.', '$')
|
81
|
-
return t.__module__ + '.' + t.__qualname__
|
82
|
-
# raise NotImplementedError("to_java_type_name: " + str(o))
|
83
|
-
|
84
|
-
|
85
|
-
@lru_cache(maxsize=None)
|
86
|
-
def to_java_field_name(field: Field) -> str:
|
87
|
-
return __convert_snake_to_camel(field.name[1:])
|
88
|
-
|
89
|
-
|
90
|
-
def __convert_snake_to_camel(field_name: str) -> str:
|
91
|
-
# Remove leading underscore
|
92
|
-
if field_name.startswith('_'):
|
93
|
-
field_name = field_name[1:]
|
94
|
-
|
95
|
-
# Convert snake case to camel case
|
96
|
-
components = field_name.split('_')
|
97
|
-
return components[0] + ''.join(x.capitalize() for x in components[1:])
|
98
|
-
|
99
|
-
|
100
|
-
def get_type(type_name: str) -> Type:
|
101
|
-
raise NotImplementedError("get_type for: " + type_name)
|