Ole Troan | 5f9dcff | 2016-08-01 04:59:13 +0200 | [diff] [blame] | 1 | # |
| 2 | # Copyright (c) 2016 Cisco and/or its affiliates. |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at: |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | # See the License for the specific language governing permissions and |
| 13 | # limitations under the License. |
| 14 | |
| 15 | # |
| 16 | # Module storing all global variables, shared between main module and plugins |
| 17 | # |
| 18 | import threading |
| 19 | |
| 20 | # |
| 21 | # Global variables |
| 22 | # |
| 23 | results = {} |
| 24 | waiting_for_reply = False |
| 25 | plugins = {} |
| 26 | |
| 27 | class ContextId(object): |
| 28 | def __init__(self): |
| 29 | self.context = 0 |
| 30 | def __call__(self, id): |
| 31 | self.context += 1 |
| 32 | return self.context |
| 33 | get_context = ContextId() |
| 34 | |
| 35 | def waiting_for_reply_clear(): |
| 36 | global waiting_for_reply |
| 37 | waiting_for_reply = False |
| 38 | |
| 39 | def waiting_for_reply_set(): |
| 40 | global waiting_for_reply |
| 41 | waiting_for_reply = True |
| 42 | |
| 43 | def is_waiting_for_reply(): |
| 44 | return waiting_for_reply |
| 45 | |
| 46 | def event_callback_set(callback): |
| 47 | global event_callback |
| 48 | event_callback = callback |
| 49 | |
| 50 | def event_callback_call(r): |
| 51 | global event_callback |
| 52 | event_callback(r) |
| 53 | |
| 54 | def results_event_set(context): |
| 55 | results[context]['e'].set() |
| 56 | |
| 57 | def results_event_clear(context): |
| 58 | results[context]['e'].clear() |
| 59 | |
| 60 | def results_event_wait(context, timeout): |
| 61 | results[context]['e'].wait(timeout) |
| 62 | |
| 63 | def results_set(context, r): |
| 64 | results[context]['r'] = r |
| 65 | |
| 66 | def results_append(context, r): |
| 67 | results[context]['r'].append(r) |
| 68 | |
| 69 | def is_results_context(context): |
| 70 | return context in results |
| 71 | |
| 72 | def is_results_more(context): |
| 73 | return 'm' in results[context] |
| 74 | |
| 75 | def results_more_set(context): |
| 76 | results[context]['m'] = True |
| 77 | |
| 78 | def results_prepare(context): |
| 79 | results[context] = {} |
| 80 | results[context]['e'] = threading.Event() |
| 81 | results[context]['e'].clear() |
| 82 | results[context]['r'] = [] |
| 83 | |
| 84 | def results_get(context): |
| 85 | return results[context]['r'] |
| 86 | |
| 87 | def plugin_register(name, func_table, name_to_id_table, version, msg_id_base_set): |
| 88 | plugins[name] = {} |
| 89 | p = plugins[name] |
| 90 | p['func_table'] = func_table |
| 91 | p['name_to_id_table'] = name_to_id_table |
| 92 | p['version'] = version |
| 93 | p['msg_id_base_set'] = msg_id_base_set |
| 94 | |
| 95 | def plugin_show(): |
| 96 | for p in plugins: |
| 97 | print(p) |