summaryrefslogtreecommitdiffstats
path: root/ipalib/encoder.py
blob: 044e1e0d3208cb8243fc981b0c8c7f979f968eab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# Authors:
#   Pavel Zuna <pzuna@redhat.com>
#
# Copyright (C) 2009  Red Hat
# see file 'COPYING' for use and warranty information
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; version 2 only
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
"""
This module provides the Encoder base class, that adds encoding/decoding
capabilities to classes extending it. It also defines a set of decorators
designed to automagically encode method arguments and decode their return
values.
"""

class Encoder(object):
    """
    Base class implementing encoding python scalar types to strings
    and vise-versa.
    """
    encode_to = 'utf-8'
    encode_none = False
    encode_dict_keys = False
    encode_dict_keys_postprocess = True
    encode_dict_vals = True
    encode_dict_vals_postprocess = True
    encode_postprocessor = staticmethod(lambda x: x)

    decode_from = 'utf-8'
    decode_none = False
    decode_dict_keys = False
    decode_dict_keys_postprocess = True
    decode_dict_vals = True
    decode_dict_vals_postprocess = True
    decode_dict_vals_table = dict()
    decode_dict_vals_table_keygen = staticmethod(lambda x: x)
    decode_postprocessor = staticmethod(lambda x: x)

    def _decode_dict_val(self, key, val):
        f = self.decode_dict_vals_table.get(
            self.decode_dict_vals_table_keygen(key), self.decode
        )
        return f(val)

    def encode(self, var):
        """
        Encode any python built-in python type variable into `self.encode_to`.

        Compound types have their individual members encoded.

        Returns an encoded copy of 'var'.
        """
        if isinstance(var, basestring):
            return self.encode_postprocessor(var.encode(self.encode_to))
        elif isinstance(var, (bool, float, int, long)):
            return self.encode_postprocessor(str(var).encode(self.encode_to))
        elif isinstance(var, list):
            return [self.encode(m) for m in var]
        elif isinstance(var, tuple):
            return tuple(self.encode(m) for m in var)
        elif isinstance(var, dict):
            if self.encode_dict_keys:
                dct = dict()
                if not self.encode_dict_keys_postprocess:
                    tmp = self.encode_postprocessor
                    self.encode_postprocessor = lambda x: x
                for (k, v) in var.iteritems():
                    dct[self.encode(k)] = v
                if not self.encode_dict_keys_postprocess:
                    self.encode_postprocessor = tmp
            else:
                dct = dict(var)
            if self.encode_dict_vals:
                if not self.encode_dict_vals_postprocess:
                    tmp = self.encode_postprocessor
                    self.encode_postprocessor = lambda x: x
                for (k, v) in dct.iteritems():
                    dct[k] = self.encode(v)
                if not self.encode_dict_vals_postprocess:
                    self.encode_postprocessor = tmp
            return dct
        elif var is None:
            if self.encode_none:
                return self.encode_postprocessor(
                    str(var).encode(self.encode_to)
                )
            return None
        raise TypeError('python built-in type expected, got \'%s\'', type(var))

    def decode(self, var):
        """
        Decode strings in `self.decode_from` into python strings.

        Compound types have their individual members decoded.

        Dictionaries can have their values decoded into other types
        by looking up keys in `self.decode_dict_vals_table`.

        Returns a decoded copy of 'var'.
        """
        if isinstance(var, basestring):
            return self.decode_postprocessor(var.decode(self.decode_from))
        elif isinstance(var, (bool, float, int, long)):
            return self.decode_postprocessor(unicode(var))
        elif isinstance(var, list):
            return [self.decode(m) for m in var]
        elif isinstance(var, tuple):
            return tuple(self.decode(m) for m in var)
        elif isinstance(var, dict):
            if self.decode_dict_keys:
                dct = dict()
                if not self.decode_dict_keys_postprocess:
                    tmp = self.decode_postprocessor
                    self.decode_postprocessor = lambda x: x
                for (k, v) in var.iteritems():
                    dct[self.decode(k)] = v
                if not self.decode_dict_keys_postprocess:
                    self.decode_postprocessor = tmp
            else:
                dct = dict(var)
            if self.decode_dict_vals:
                if not self.decode_dict_vals_postprocess:
                    tmp = self.decode_postprocessor
                    self.decode_postprocessor = lambda x: x
                for (k, v) in dct.iteritems():
                    dct[k] = self._decode_dict_val(k, v)
                if not self.decode_dict_vals_postprocess:
                    self.decode_postprocessor = tmp
            return dct
        elif var is None:
            if self.decode_none:
                return self.decode_postprocessor(
                    str(var).decode(self.decode_from)
                )
            return None
        raise TypeError('python built-in type expected, got \'%s\'', type(var))

## ENCODER METHOD DECORATORS

def encode_args(*outer_args):
    """
    Encode arguments of the decorated method specified by their sequence
    number or name for keyword arguments.

    Example:
    class some_class_that_needs_encoding_capabilities(Encoder):
        ...
        @encode_args(1, 3, 'name'):
        def some_method(
            self, encode_this, dont_encode_this, encode_this_too, **kwargs
        ):
            # if there's going to be a 'name' kwargs it will be encoded
        ...

    This is an Encoder method decorator.
    """
    def decorate(f):
        def new_f(*args, **kwargs):
            assert isinstance(args[0], Encoder), \
                'first argument not Encoder instance'
            new_args = list(args)
            for a in outer_args:
                if isinstance(a, int):
                    new_args[a] = args[0].encode(args[a])
                elif isinstance(a, basestring) and a in kwargs:
                    kwargs[a] = args[0].encode(kwargs[a])
                else:
                    raise TypeError(
                        'encode_args takes a list of ints and basestrings'
                    )
            return f(*new_args, **kwargs)
        new_f.func_name = f.func_name
        return new_f
    return decorate


def decode_retval():
    """
    Decode the return value of the decorated method.

    Example:
    class some_class_that_needs_encoding_capabilities(Encoder):
        ...
        @decode_retval():
        def some_method(self):
            ...
            return this_will_be_decoded
        ...

    This is an Encoder method decorator AND IT HAS TO BE CALLED (use "()")!
    """
    def decorate(f):
        def new_f(*args, **kwargs):
            assert isinstance(args[0], Encoder), \
                'first argument not Encoder instance'
            return args[0].decode(f(*args, **kwargs))
        new_f.func_name = f.func_name
        return new_f
    return decorate