~stephanpampel/landscape-client-charm/landscape-client-charm

« back to all changes in this revision

Viewing changes to hooks/charmhelpers/core/strutils.py

  • Committer: Simon Poirier
  • Date: 2021-03-17 21:08:22 UTC
  • mfrom: (71.1.2 show-charm-source-version)
  • Revision ID: simon.poirier@canonical.com-20210317210822-catzulnroq4kztt2
Add showing charm source version/commit in juju status output

Show diffs side-by-side

added added

removed removed

Lines of Context:
61
61
    if isinstance(value, six.string_types):
62
62
        value = six.text_type(value)
63
63
    else:
64
 
        msg = "Unable to interpret non-string value '%s' as boolean" % (value)
 
64
        msg = "Unable to interpret non-string value '%s' as bytes" % (value)
65
65
        raise ValueError(msg)
66
66
    matches = re.match("([0-9]+)([a-zA-Z]+)", value)
67
 
    if not matches:
68
 
        msg = "Unable to interpret string value '%s' as bytes" % (value)
69
 
        raise ValueError(msg)
70
 
    return int(matches.group(1)) * (1024 ** BYTE_POWER[matches.group(2)])
 
67
    if matches:
 
68
        size = int(matches.group(1)) * (1024 ** BYTE_POWER[matches.group(2)])
 
69
    else:
 
70
        # Assume that value passed in is bytes
 
71
        try:
 
72
            size = int(value)
 
73
        except ValueError:
 
74
            msg = "Unable to interpret string value '%s' as bytes" % (value)
 
75
            raise ValueError(msg)
 
76
    return size
 
77
 
 
78
 
 
79
class BasicStringComparator(object):
 
80
    """Provides a class that will compare strings from an iterator type object.
 
81
    Used to provide > and < comparisons on strings that may not necessarily be
 
82
    alphanumerically ordered.  e.g. OpenStack or Ubuntu releases AFTER the
 
83
    z-wrap.
 
84
    """
 
85
 
 
86
    _list = None
 
87
 
 
88
    def __init__(self, item):
 
89
        if self._list is None:
 
90
            raise Exception("Must define the _list in the class definition!")
 
91
        try:
 
92
            self.index = self._list.index(item)
 
93
        except Exception:
 
94
            raise KeyError("Item '{}' is not in list '{}'"
 
95
                           .format(item, self._list))
 
96
 
 
97
    def __eq__(self, other):
 
98
        assert isinstance(other, str) or isinstance(other, self.__class__)
 
99
        return self.index == self._list.index(other)
 
100
 
 
101
    def __ne__(self, other):
 
102
        return not self.__eq__(other)
 
103
 
 
104
    def __lt__(self, other):
 
105
        assert isinstance(other, str) or isinstance(other, self.__class__)
 
106
        return self.index < self._list.index(other)
 
107
 
 
108
    def __ge__(self, other):
 
109
        return not self.__lt__(other)
 
110
 
 
111
    def __gt__(self, other):
 
112
        assert isinstance(other, str) or isinstance(other, self.__class__)
 
113
        return self.index > self._list.index(other)
 
114
 
 
115
    def __le__(self, other):
 
116
        return not self.__gt__(other)
 
117
 
 
118
    def __str__(self):
 
119
        """Always give back the item at the index so it can be used in
 
120
        comparisons like:
 
121
 
 
122
        s_mitaka = CompareOpenStack('mitaka')
 
123
        s_newton = CompareOpenstack('newton')
 
124
 
 
125
        assert s_newton > s_mitaka
 
126
 
 
127
        @returns: <string>
 
128
        """
 
129
        return self._list[self.index]