blob: 0342d876ab6d6f0c4c6267412b20f74817c9d8cc [file] [log] [blame]
Zsolt Haraszti46c72002016-10-10 09:55:30 -07001#!/usr/bin/env python
2#
3# Copyright 2016 the original author or authors.
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16#
17import sys
18
19from google.protobuf.compiler import plugin_pb2 as plugin
20from simplejson import dumps
21
22# without this import, http method annotations would not be recognized:
23from google.api import annotations_pb2, http_pb2
24
25from chameleon.protoc_plugins.descriptor_parser import DescriptorParser
26from swagger_template import native_descriptors_to_swagger
27
28
29def generate_code(request, response):
30
31 assert isinstance(request, plugin.CodeGeneratorRequest)
32
33 parser = DescriptorParser()
34 native_data = parser.parse_file_descriptors(request.proto_file,
35 type_tag_name='_type',
36 fold_comments=True)
37 swagger = native_descriptors_to_swagger(native_data)
38
39 # generate the native decoded schema as json
40 # f = response.file.add()
41 # f.name = proto_file.name.replace('.proto', '.native.json')
42 # f.content = dumps(data)
43
44 # generate the real swagger.json file
45 f = response.file.add()
46 f.name = 'swagger.json'
47 f.content = dumps(swagger)
48
49
50if __name__ == '__main__':
51
52 if len(sys.argv) >= 2:
53 # read input from file, to allow troubleshooting
54 with open(sys.argv[1], 'r') as f:
55 data = f.read()
56 else:
57 # read input from stdin
58 data = sys.stdin.read()
59
60 # parse request
61 request = plugin.CodeGeneratorRequest()
62 request.ParseFromString(data)
63
64 # create response object
65 response = plugin.CodeGeneratorResponse()
66
67 # generate the output and the response
68 generate_code(request, response)
69
70 # serialize the response
71 output = response.SerializeToString()
72
73 # write response to stdout
74 sys.stdout.write(output)