| 41 | |
| 42 | |
| 43 | class CLIDocumentEventHandler: |
| 44 | def __init__(self, help_command): |
| 45 | self.help_command = help_command |
| 46 | self.register(help_command.session, help_command.event_class) |
| 47 | self._arg_groups = self._build_arg_table_groups(help_command) |
| 48 | self._documented_arg_groups = [] |
| 49 | |
| 50 | def _build_arg_table_groups(self, help_command): |
| 51 | arg_groups = {} |
| 52 | for arg in help_command.arg_table.values(): |
| 53 | if arg.group_name is not None: |
| 54 | arg_groups.setdefault(arg.group_name, []).append(arg) |
| 55 | return arg_groups |
| 56 | |
| 57 | def _get_argument_type_name(self, shape, default): |
| 58 | if is_json_value_header(shape): |
| 59 | return 'JSON' |
| 60 | if is_document_type(shape): |
| 61 | return 'document' |
| 62 | if is_streaming_blob_type(shape): |
| 63 | return 'streaming blob' |
| 64 | if is_tagged_union_type(shape): |
| 65 | return 'tagged union structure' |
| 66 | return default |
| 67 | |
| 68 | def _map_handlers(self, session, event_class, mapfn): |
| 69 | for event in DOC_EVENTS: |
| 70 | event_handler_name = event.replace('-', '_') |
| 71 | if hasattr(self, event_handler_name): |
| 72 | event_handler = getattr(self, event_handler_name) |
| 73 | format_string = DOC_EVENTS[event] |
| 74 | num_args = len(format_string.split('.')) - 2 |
| 75 | format_args = (event_class,) + ('*',) * num_args |
| 76 | event_string = event + format_string % format_args |
| 77 | unique_id = event_class + event_handler_name |
| 78 | mapfn(event_string, event_handler, unique_id) |
| 79 | |
| 80 | def register(self, session, event_class): |
| 81 | """ |
| 82 | The default register iterates through all of the |
| 83 | available document events and looks for a corresponding |
| 84 | handler method defined in the object. If it's there, that |
| 85 | handler method will be registered for the all events of |
| 86 | that type for the specified ``event_class``. |
| 87 | """ |
| 88 | self._map_handlers(session, event_class, session.register) |
| 89 | |
| 90 | def unregister(self): |
| 91 | """ |
| 92 | The default unregister iterates through all of the |
| 93 | available document events and looks for a corresponding |
| 94 | handler method defined in the object. If it's there, that |
| 95 | handler method will be unregistered for the all events of |
| 96 | that type for the specified ``event_class``. |
| 97 | """ |
| 98 | self._map_handlers( |
| 99 | self.help_command.session, |
| 100 | self.help_command.event_class, |
no outgoing calls