~andreserl/pyjuju/maas_provider_distro_series

« back to all changes in this revision

Viewing changes to juju/hooks/cli.py

  • Committer: Jim Baker
  • Date: 2012-09-10 18:28:40 UTC
  • mfrom: (570.2.7 format-2-raw)
  • Revision ID: jim.baker@canonical.com-20120910182840-9rfts18sbp30id5g
merge format-2-raw [r=hazmat][f=1044632]

Modifies format 2 support so that it supports the use of raw strings.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
import argparse
 
2
import base64
 
3
import copy
2
4
import json
3
5
import logging
4
6
import os
223
225
        stream.flush()
224
226
 
225
227
    def format_json(self, result, stream):
226
 
        print >>stream, json.dumps(result)
 
228
        encoded = copy.copy(result)
 
229
        if isinstance(result, dict):
 
230
            for k, v in result.iteritems():
 
231
                # Workaround the fact that JSON does not work with str
 
232
                # values that have high bytes and are not actually UTF-8
 
233
                # encoded; workaround by firt testing whether it can be
 
234
                # decoded as UTF-8, and if not, wrapping as Base64
 
235
                # encoded.
 
236
                if isinstance(v, str):
 
237
                    try:
 
238
                        v.decode("utf8")
 
239
                    except UnicodeDecodeError:
 
240
                        encoded[k] = base64.b64encode(v)
 
241
        json.dump(encoded, stream)
227
242
 
228
243
    def format_smart(self, result, stream):
229
244
        if result is not None:
230
245
            charm_formatter = get_charm_formatter_from_env()
231
 
            print >>stream, charm_formatter.format(result)
 
246
            stream.write(charm_formatter.format_raw(result))
232
247
 
233
248
 
234
249
def parse_log_level(level):