~andreserl/pyjuju/maas_provider_distro_series

« back to all changes in this revision

Viewing changes to juju/lib/format.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:
42
42
 
43
43
        return data
44
44
 
 
45
    def _parse_value(self, key, value):
 
46
        """Interprets value as a str"""
 
47
        return value
 
48
 
 
49
    def should_delete(self, value):
 
50
        """Whether `value` implies corresponding key should be deleted"""
 
51
        return not value.strip()
 
52
 
45
53
 
46
54
class PythonFormat(BaseFormat):
47
55
    """Supports backwards compatibility through str and JSON encoding."""
52
60
        """Formats `data` using Python str encoding"""
53
61
        return str(data)
54
62
 
55
 
    def _parse_value(self, key, value):
56
 
        """Interprets value as a str"""
57
 
        return value
 
63
    def format_raw(self, data):
 
64
        """Add extra \n seen in Python format, so not truly raw"""
 
65
        return self.format(data) + "\n"
58
66
 
59
67
    # For the old format: 1, using JSON serialization introduces some
60
68
    # subtle issues around Unicode conversion that then later results
69
77
        """Loads data, but also converts str to Unicode"""
70
78
        return json.loads(data)
71
79
 
72
 
    def should_delete(self, value):
73
 
        """Whether `value` implies corresponding key should be deleted"""
74
 
        # In format: 1, all values are strings, but possibly with
75
 
        # spaces. The strip reduces strings consisting only of spaces,
76
 
        # or otherwise empty, to an empty string.
77
 
        return not value.strip()
78
 
 
79
80
 
80
81
class YAMLFormat(BaseFormat):
81
82
    """New format that uses YAML, so no unexpected encoding issues"""
98
99
        # Also remove any extra \n, will still be valid yaml
99
100
        return serialized.rstrip("\n")
100
101
 
101
 
    def _parse_value(self, key, value):
102
 
        # Ensure roundtripping to/from yaml if format: 2; in
103
 
        # particular this ensures that true/false work as desired
104
 
        try:
105
 
            return yaml.safe_load(value)
106
 
        except Exception:
107
 
            raise JujuError("Invalid YAML value (argument:%s)" % key)
 
102
    def format_raw(self, data):
 
103
        """Formats `data` as a raw string if str, otherwise as YAML"""
 
104
        if isinstance(data, str):
 
105
            return data
 
106
        else:
 
107
            return self.format(data)
108
108
 
109
109
    # Use the same format for dump
110
110
    dump = format
113
113
        """Loads data safely, ensuring no Python specific type info leaks"""
114
114
        return yaml.safe_load(data)
115
115
 
116
 
    def should_delete(self, value):
117
 
        """Whether `value` implies corresponding key should be deleted"""
118
 
        # In format: 2, values were already parsed by yaml.safe_load;
119
 
        # in particular, a string consisting only of whitespace is
120
 
        # parsed as None.
121
 
        return value is None
122
 
 
123
116
 
124
117
def is_valid_charm_format(charm_format):
125
118
    """True if `charm_format` is a valid format"""