~ack/landscape-client/sources.list-preserve-old-permissions

« back to all changes in this revision

Viewing changes to landscape/lib/bpickle.py

  • Committer: Christopher Armstrong
  • Date: 2008-06-10 10:56:01 UTC
  • Revision ID: radix@twistedmatrix.com-20080610105601-l9qfvqjf88e7j8b6
Import landscape-client into public branch

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
"""
 
2
Copyright (c) 2006, Gustavo Niemeyer <gustavo@niemeyer.net>
 
3
 
 
4
All rights reserved.
 
5
 
 
6
Redistribution and use in source and binary forms, with or without
 
7
modification, are permitted provided that the following conditions are met:
 
8
 
 
9
    * Redistributions of source code must retain the above copyright notice,
 
10
      this list of conditions and the following disclaimer.
 
11
    * Redistributions in binary form must reproduce the above copyright notice,
 
12
      this list of conditions and the following disclaimer in the documentation
 
13
      and/or other materials provided with the distribution.
 
14
    * Neither the name of the copyright holder nor the names of its
 
15
      contributors may be used to endorse or promote products derived from
 
16
      this software without specific prior written permission.
 
17
 
 
18
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 
19
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 
20
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 
21
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 
22
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 
23
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 
24
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 
25
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 
26
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 
27
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 
28
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
29
"""
 
30
 
 
31
 
 
32
dumps_table = {}
 
33
loads_table = {}
 
34
 
 
35
 
 
36
def dumps(obj, _dt=dumps_table):
 
37
    try:
 
38
        return _dt[type(obj)](obj)
 
39
    except KeyError, e:
 
40
        raise ValueError, "Unsupported type: %s" % e
 
41
 
 
42
 
 
43
def loads(str, _lt=loads_table):
 
44
    if not str:
 
45
        raise ValueError, "Can't load empty string"
 
46
    try:
 
47
        return _lt[str[0]](str, 0)[0]
 
48
    except KeyError, e:
 
49
        raise ValueError, "Unknown type character: %s" % e
 
50
    except IndexError:
 
51
        raise ValueError, "Corrupted data"
 
52
 
 
53
def dumps_bool(obj):
 
54
    return "b%d" % int(obj)
 
55
 
 
56
def dumps_int(obj):
 
57
    return "i%s;" % obj
 
58
 
 
59
def dumps_float(obj):
 
60
    return "f%r;" % obj
 
61
 
 
62
def dumps_str(obj):
 
63
    return "s%s:%s" % (len(obj), obj)
 
64
 
 
65
def dumps_unicode(obj):
 
66
    obj = obj.encode("utf-8")
 
67
    return "u%s:%s" % (len(obj), obj)
 
68
 
 
69
def dumps_list(obj, _dt=dumps_table):
 
70
    return "l%s;" % "".join([_dt[type(val)](val) for val in obj])
 
71
 
 
72
def dumps_tuple(obj, _dt=dumps_table):
 
73
    return "t%s;" % "".join([_dt[type(val)](val) for val in obj])
 
74
 
 
75
def dumps_dict(obj, _dt=dumps_table):
 
76
    keys = obj.keys()
 
77
    keys.sort()
 
78
    res = []
 
79
    append = res.append
 
80
    for key in keys:
 
81
        val = obj[key]
 
82
        append(_dt[type(key)](key))
 
83
        append(_dt[type(val)](val))
 
84
    return "d%s;" % "".join(res)
 
85
 
 
86
def dumps_none(obj):
 
87
    return "n"
 
88
 
 
89
def loads_bool(str, pos):
 
90
    return bool(int(str[pos+1])), pos+2
 
91
 
 
92
def loads_int(str, pos):
 
93
    endpos = str.index(";", pos)
 
94
    return int(str[pos+1:endpos]), endpos+1
 
95
 
 
96
def loads_float(str, pos):
 
97
    endpos = str.index(";", pos)
 
98
    return float(str[pos+1:endpos]), endpos+1
 
99
 
 
100
def loads_str(str, pos):
 
101
    startpos = str.index(":", pos)+1
 
102
    endpos = startpos+int(str[pos+1:startpos-1])
 
103
    return str[startpos:endpos], endpos
 
104
 
 
105
def loads_unicode(str, pos):
 
106
    startpos = str.index(":", pos)+1
 
107
    endpos = startpos+int(str[pos+1:startpos-1])
 
108
    return str[startpos:endpos].decode("utf-8"), endpos
 
109
 
 
110
def loads_list(str, pos, _lt=loads_table):
 
111
    pos += 1
 
112
    res = []
 
113
    append = res.append
 
114
    while str[pos] != ";":
 
115
        obj, pos = _lt[str[pos]](str, pos)
 
116
        append(obj)
 
117
    return res, pos+1
 
118
 
 
119
def loads_tuple(str, pos, _lt=loads_table):
 
120
    pos += 1
 
121
    res = []
 
122
    append = res.append
 
123
    while str[pos] != ";":
 
124
        obj, pos = _lt[str[pos]](str, pos)
 
125
        append(obj)
 
126
    return tuple(res), pos+1
 
127
 
 
128
def loads_dict(str, pos, _lt=loads_table):
 
129
    pos += 1
 
130
    res = {}
 
131
    while str[pos] != ";":
 
132
        key, pos = _lt[str[pos]](str, pos)
 
133
        val, pos = _lt[str[pos]](str, pos)
 
134
        res[key] = val
 
135
    return res, pos+1
 
136
 
 
137
def loads_none(str, pos):
 
138
    return None, pos+1
 
139
 
 
140
 
 
141
dumps_table.update({       bool: dumps_bool,
 
142
                            int: dumps_int,
 
143
                           long: dumps_int,
 
144
                          float: dumps_float,
 
145
                            str: dumps_str,
 
146
                        unicode: dumps_unicode,
 
147
                           list: dumps_list,
 
148
                          tuple: dumps_tuple,
 
149
                           dict: dumps_dict,
 
150
                     type(None): dumps_none     })
 
151
 
 
152
loads_table.update({ "b": loads_bool,
 
153
                     "i": loads_int,
 
154
                     "f": loads_float,
 
155
                     "s": loads_str,
 
156
                     "u": loads_unicode,
 
157
                     "l": loads_list,
 
158
                     "t": loads_tuple,
 
159
                     "d": loads_dict,
 
160
                     "n": loads_none     })
 
161