@@ -40,12 +40,13 @@ def py_type(
40
40
message_type = descriptor .type_name .lstrip ("." )
41
41
if message_type .startswith (package ):
42
42
# This is the current package, which has nested types flattened.
43
- message_type = message_type .lstrip (package ).lstrip ("." ).replace ("." , "" )
43
+ message_type = (
44
+ f'"{ message_type .lstrip (package ).lstrip ("." ).replace ("." , "" )} "'
45
+ )
44
46
45
47
if "." in message_type :
46
48
# This is imported from another package. No need
47
49
# to use a forward ref and we need to add the import.
48
- message_type = message_type .strip ('"' )
49
50
parts = message_type .split ("." )
50
51
imports .add (f"from .{ '.' .join (parts [:- 2 ])} import { parts [- 2 ]} " )
51
52
message_type = f"{ parts [- 2 ]} .{ parts [- 1 ]} "
@@ -58,7 +59,7 @@ def py_type(
58
59
# file=sys.stderr,
59
60
# )
60
61
61
- return f'" { message_type } "'
62
+ return message_type
62
63
elif descriptor .type == 12 :
63
64
return "bytes"
64
65
else :
@@ -247,10 +248,28 @@ def generate_code(request, response):
247
248
# Fill response
248
249
f = response .file .add ()
249
250
# print(filename, file=sys.stderr)
250
- f .name = filename + ".py"
251
+ f .name = filename .replace ("." , os .path .sep ) + ".py"
252
+
251
253
# f.content = json.dumps(output, indent=2)
252
254
f .content = template .render (description = output ).rstrip ("\n " ) + "\n "
253
255
256
+ inits = set (["" ])
257
+ for f in response .file :
258
+ # Ensure output paths exist
259
+ print (f .name , file = sys .stderr )
260
+ dirnames = os .path .dirname (f .name )
261
+ if dirnames :
262
+ os .makedirs (dirnames , exist_ok = True )
263
+ base = ""
264
+ for part in dirnames .split (os .path .sep ):
265
+ base = os .path .join (base , part )
266
+ inits .add (base )
267
+
268
+ for base in inits :
269
+ init = response .file .add ()
270
+ init .name = os .path .join (base , "__init__.py" )
271
+ init .content = b""
272
+
254
273
255
274
if __name__ == "__main__" :
256
275
# Read request message from stdin
0 commit comments