|
| 1 | +# Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +# or more contributor license agreements. See the NOTICE file |
| 3 | +# distributed with this work for additional information |
| 4 | +# regarding copyright ownership. The ASF licenses this file |
| 5 | +# to you under the Apache License, Version 2.0 (the |
| 6 | +# "License"); you may not use this file except in compliance |
| 7 | +# with the License. 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, |
| 12 | +# software distributed under the License is distributed on an |
| 13 | +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +# KIND, either express or implied. See the License for the |
| 15 | +# specific language governing permissions and limitations |
| 16 | +# under the License. |
| 17 | + |
| 18 | +try: |
| 19 | + from common import grammar |
| 20 | + from marvin.cloudstackAPI import * |
| 21 | + from marvin import cloudstackAPI |
| 22 | +except ImportError, e: |
| 23 | + pass |
| 24 | + |
| 25 | +completions = cloudstackAPI.__all__ |
| 26 | + |
| 27 | + |
| 28 | +def get_api_module(api_name, api_class_strs=[]): |
| 29 | + try: |
| 30 | + api_mod = __import__("marvin.cloudstackAPI.%s" % api_name, |
| 31 | + globals(), locals(), api_class_strs, -1) |
| 32 | + except ImportError, e: |
| 33 | + print "Error: API not found", e |
| 34 | + return None |
| 35 | + return api_mod |
| 36 | + |
| 37 | + |
| 38 | +def main(): |
| 39 | + """ |
| 40 | + cachegen.py creates a precached dictionary for all the available verbs in |
| 41 | + the predefined grammar of cloudmonkey, it dumps the dictionary in an |
| 42 | + importable python module. This way we cheat on the runtime overhead of |
| 43 | + completing commands and help docs. This reduces the overall search and |
| 44 | + cache_miss (computation) complexity from O(n) to O(1) for any valid cmd. |
| 45 | + """ |
| 46 | + # datastructure {'verb': {cmd': ['api', [params], doc, required=[]]}} |
| 47 | + cache_verbs = {} |
| 48 | + for verb in grammar: |
| 49 | + completions_found = filter(lambda x: x.startswith(verb), completions) |
| 50 | + cache_verbs[verb] = {} |
| 51 | + for api_name in completions_found: |
| 52 | + api_cmd_str = "%sCmd" % api_name |
| 53 | + api_mod = get_api_module(api_name, [api_cmd_str]) |
| 54 | + if api_mod is None: |
| 55 | + continue |
| 56 | + try: |
| 57 | + api_cmd = getattr(api_mod, api_cmd_str)() |
| 58 | + required = api_cmd.required |
| 59 | + doc = api_mod.__doc__ |
| 60 | + except AttributeError, e: |
| 61 | + print "Error: API attribute %s not found!" % e |
| 62 | + params = filter(lambda x: '__' not in x and 'required' not in x, |
| 63 | + dir(api_cmd)) |
| 64 | + if len(required) > 0: |
| 65 | + doc += "\nRequired args: %s" % " ".join(required) |
| 66 | + doc += "\nArgs: %s" % " ".join(params) |
| 67 | + api_name_lower = api_name.replace(verb, '').lower() |
| 68 | + cache_verbs[verb][api_name_lower] = [api_name, params, doc, |
| 69 | + required] |
| 70 | + f = open("precache.py", "w") |
| 71 | + f.write("""# Auto-generated code by cachegen.py |
| 72 | +# Licensed to the Apache Software Foundation (ASF) under one |
| 73 | +# or more contributor license agreements. See the NOTICE file |
| 74 | +# distributed with this work for additional information |
| 75 | +# regarding copyright ownership. The ASF licenses this file |
| 76 | +# to you under the Apache License, Version 2.0 (the |
| 77 | +# "License"); you may not use this file except in compliance |
| 78 | +# with the License. You may obtain a copy of the License at |
| 79 | +# |
| 80 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 81 | +# |
| 82 | +# Unless required by applicable law or agreed to in writing, |
| 83 | +# software distributed under the License is distributed on an |
| 84 | +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 85 | +# KIND, either express or implied. See the License for the |
| 86 | +# specific language governing permissions and limitations |
| 87 | +# under the License.""") |
| 88 | + f.write("\nprecached_verbs = %s" % cache_verbs) |
| 89 | + f.close() |
| 90 | + |
| 91 | +if __name__ == "__main__": |
| 92 | + main() |
0 commit comments