Maros Marsalek | 45a42b5 | 2016-04-28 12:29:33 +0200 | [diff] [blame^] | 1 | #!/usr/bin/env python |
| 2 | # |
| 3 | # Copyright (c) 2016 Cisco and/or its affiliates. |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at: |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
| 15 | |
| 16 | import os |
| 17 | import util |
| 18 | from string import Template |
| 19 | |
| 20 | from util import remove_suffix |
| 21 | |
| 22 | callback_suffix = "Callback" |
| 23 | |
| 24 | callback_template = Template(""" |
| 25 | package $base_package.$callback_package; |
| 26 | |
| 27 | /** |
| 28 | * $docs |
| 29 | */ |
| 30 | public interface $cls_name extends $base_package.$callback_package.JVppCallback { |
| 31 | |
| 32 | $callback_method |
| 33 | |
| 34 | } |
| 35 | """) |
| 36 | |
| 37 | global_callback_template = Template(""" |
| 38 | package $base_package.$callback_package; |
| 39 | |
| 40 | /** |
| 41 | * |
| 42 | * |
| 43 | * Global aggregated callback interface |
| 44 | */ |
| 45 | public interface JVppGlobalCallback extends $callbacks { |
| 46 | } |
| 47 | """) |
| 48 | |
| 49 | |
| 50 | def generate_callbacks(func_list, base_package, callback_package, dto_package): |
| 51 | """ Generates callback interfaces """ |
| 52 | print "Generating Callback interfaces" |
| 53 | |
| 54 | if not os.path.exists(callback_package): |
| 55 | raise Exception("%s folder is missing" % callback_package) |
| 56 | |
| 57 | callbacks = [] |
| 58 | for func in func_list: |
| 59 | |
| 60 | if util.is_notification(func['name']) or util.is_ignored(func['name']): |
| 61 | # FIXME handle notifications |
| 62 | continue |
| 63 | |
| 64 | camel_case_name_with_suffix = util.underscore_to_camelcase_upper(func['name']) |
| 65 | if not util.is_reply(camel_case_name_with_suffix): |
| 66 | continue |
| 67 | |
| 68 | camel_case_name = util.remove_reply_suffix(camel_case_name_with_suffix) |
| 69 | callbacks.append("{0}.{1}.{2}".format(base_package, callback_package, camel_case_name + callback_suffix)) |
| 70 | callback_path = os.path.join(callback_package, camel_case_name + callback_suffix + ".java") |
| 71 | callback_file = open(callback_path, 'w') |
| 72 | |
| 73 | reply_type = "%s.%s.%s" % (base_package, dto_package, camel_case_name_with_suffix) |
| 74 | method = "void on{0}({1} reply);".format(camel_case_name_with_suffix, reply_type) |
| 75 | callback_file.write( |
| 76 | callback_template.substitute(docs='Generated from ' + str(func), |
| 77 | cls_name=camel_case_name + callback_suffix, |
| 78 | callback_method=method, |
| 79 | base_package=base_package, |
| 80 | callback_package=callback_package)) |
| 81 | callback_file.flush() |
| 82 | callback_file.close() |
| 83 | |
| 84 | callback_file = open(os.path.join(callback_package, "JVppGlobalCallback.java"), 'w') |
| 85 | callback_file.write(global_callback_template.substitute(callbacks=", ".join(callbacks), |
| 86 | base_package=base_package, |
| 87 | callback_package=callback_package)) |
| 88 | callback_file.flush() |
| 89 | callback_file.close() |