| 109 | |
| 110 | |
| 111 | class YAMLDumper: |
| 112 | def __init__(self): |
| 113 | self._yaml = YAML(typ='safe') |
| 114 | # Encoding is set to None because we handle the encoding by |
| 115 | # wrapping the stream, so there's no need for the yaml library |
| 116 | # to do it. |
| 117 | self._yaml.encoding = None |
| 118 | self._yaml.representer.default_flow_style = False |
| 119 | |
| 120 | def dump(self, value, stream): |
| 121 | if self._is_json_scalar(value) or isinstance(value, datetime): |
| 122 | # YAML will attempt to disambiguate scalars by ending the stream |
| 123 | # with an elipsis. While this is technically valid YAML, |
| 124 | # it's not particularly useful. Unfortunately there's no |
| 125 | # universal way around this, so instead we just json dump the |
| 126 | # values. Also note that datetimes are explicitly not supported |
| 127 | # - the json dumper will complain if you pass them in. datetime |
| 128 | # values should respect the cli timestamp format, which is |
| 129 | # impossible to do from the Formatter. |
| 130 | json.dump(value, stream, ensure_ascii=False, default=json_encoder) |
| 131 | stream.write('\n') |
| 132 | else: |
| 133 | self._yaml.dump(value, stream) |
| 134 | |
| 135 | def _is_json_scalar(self, value): |
| 136 | if value is None: |
| 137 | return True |
| 138 | return isinstance(value, (int, float, bool, str)) |
| 139 | |
| 140 | |
| 141 | class YAMLFormatter(FullyBufferedFormatter): |