| 152 | |
| 153 | |
| 154 | class StreamedYAMLFormatter(Formatter): |
| 155 | def __init__(self, args, yaml_dumper=None): |
| 156 | super(StreamedYAMLFormatter, self).__init__(args) |
| 157 | self._yaml_dumper = yaml_dumper |
| 158 | if yaml_dumper is None: |
| 159 | self._yaml_dumper = YAMLDumper() |
| 160 | |
| 161 | def __call__(self, command_name, response, stream=None): |
| 162 | if stream is None: |
| 163 | stream = self._get_default_stream() |
| 164 | compat.set_preferred_output_encoding(stream) |
| 165 | response_stream = self._get_response_stream(response) |
| 166 | for response in response_stream: |
| 167 | try: |
| 168 | # For YAML it is ambiguous as to whether the output from the |
| 169 | # stream is N responses in 1 list or N lists each with 1 |
| 170 | # response. We go with the latter so we can reuse our YAML |
| 171 | # dumper |
| 172 | self._yaml_dumper.dump([response], stream) |
| 173 | except OSError: |
| 174 | # If the reading end of our stdout stream has closed the file |
| 175 | # we can just exit. |
| 176 | return |
| 177 | finally: |
| 178 | # flush is needed to avoid the "close failed in file object |
| 179 | # destructor" in python2.x. See: |
| 180 | # http://bugs.python.org/issue11380). |
| 181 | self._flush_stream(stream) |
| 182 | |
| 183 | def _get_response_stream(self, response): |
| 184 | if is_response_paginated(response): |
| 185 | return compat.imap( |
| 186 | self._get_transformed_response_for_output, response |
| 187 | ) |
| 188 | else: |
| 189 | output = self._get_transformed_response_for_output(response) |
| 190 | if output == {}: |
| 191 | # The operation did not have an output so return an empty list |
| 192 | # as the stream so nothing gets printed out. |
| 193 | return [] |
| 194 | return [output] |
| 195 | |
| 196 | |
| 197 | class TableFormatter(FullyBufferedFormatter): |
no outgoing calls