/usr/share/dreampie/dreampielib/common/brine.py is in dreampie 1.1.1-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 | # This file is based on brine.py from RPyC.
# See http://rpyc.wikidot.com/
# and http://sebulbasvn.googlecode.com/svn/tags/rpyc/3.0.6/core/brine.py
# Modified by Noam Yorav-Raphael for DreamPie use.
# Copyright (c) 2005-2009
# Tomer Filiba (tomerfiliba@gmail.com)
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
# Copyright 2010 Noam Yorav-Raphael
#
# This file is part of DreamPie.
#
# DreamPie 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, either version 3 of the License, or
# (at your option) any later version.
#
# DreamPie 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 DreamPie. If not, see <http://www.gnu.org/licenses/>.
"""
brine - a simple, fast and secure object serializer,
optimized for small integers [-48..160), suitable for Python 2/3k communication.
the following types are supported: int (in the unsigned long range), bool,
unicode (In Py2) / str (In Py3), float, slice, complex, tuple(of simple types),
list(of simple types), frozenset(of simple types)
as well as the following singletons: None, NotImplemented, Ellipsis
"""
import sys
py3k = (sys.version_info[0] == 3)
if not py3k:
from cStringIO import StringIO
else:
from io import BytesIO
from struct import Struct
if not py3k:
def b(n):
return chr(n)
empty_bytes = ''
else:
def b(n):
return bytes([n])
empty_bytes = bytes()
# singletons
TAG_NONE = b(0x00)
TAG_EMPTY_STR = b(0x01)
TAG_EMPTY_TUPLE = b(0x02)
TAG_TRUE = b(0x03)
TAG_FALSE = b(0x04)
TAG_NOT_IMPLEMENTED = b(0x05)
TAG_ELLIPSIS = b(0x06)
# types
#TAG_UNICODE = b(0x08) # Removed - STR is unicode.
#TAG_LONG = b(0x09) # Removed
TAG_STR1 = b(0x0a)
TAG_STR2 = b(0x0b)
TAG_STR3 = b(0x0c)
TAG_STR4 = b(0x0d)
TAG_STR_L1 = b(0x0e)
TAG_STR_L4 = b(0x0f)
TAG_TUP1 = b(0x10)
TAG_TUP2 = b(0x11)
TAG_TUP3 = b(0x12)
TAG_TUP4 = b(0x13)
TAG_TUP_L1 = b(0x14)
TAG_TUP_L4 = b(0x15)
TAG_INT_L1 = b(0x16)
TAG_INT_L4 = b(0x17)
TAG_FLOAT = b(0x18)
TAG_SLICE = b(0x19)
TAG_FSET = b(0x1a)
TAG_COMPLEX = b(0x1b)
# List
TAG_EMPTY_LIST = b(0x1c)
TAG_LIST1 = b(0x1d)
TAG_LIST_L1 = b(0x1e)
TAG_LIST_L4 = b(0x1f)
IMM_INTS = dict((i, b(i + 0x50)) for i in range(-0x30, 0xa0))
I1 = Struct("!B")
I4 = Struct("!L")
F8 = Struct("!d")
C16 = Struct("!dd")
_dump_registry = {}
_load_registry = {}
IMM_INTS_LOADER = dict((v, k) for k, v in IMM_INTS.iteritems())
def register(coll, key):
def deco(func):
coll[key] = func
return func
return deco
#===============================================================================
# dumping
#===============================================================================
@register(_dump_registry, type(None))
def _dump_none(_obj, stream):
stream.append(TAG_NONE)
@register(_dump_registry, type(NotImplemented))
def _dump_notimplemeted(_obj, stream):
stream.append(TAG_NOT_IMPLEMENTED)
@register(_dump_registry, type(Ellipsis))
def _dump_ellipsis(_obj, stream):
stream.append(TAG_ELLIPSIS)
@register(_dump_registry, bool)
def _dump_bool(obj, stream):
if obj:
stream.append(TAG_TRUE)
else:
stream.append(TAG_FALSE)
@register(_dump_registry, slice)
def _dump_slice(obj, stream):
stream.append(TAG_SLICE)
_dump((obj.start, obj.stop, obj.step), stream)
@register(_dump_registry, frozenset)
def _dump_frozenset(obj, stream):
stream.append(TAG_FSET)
_dump(tuple(obj), stream)
@register(_dump_registry, int)
def _dump_int(obj, stream):
if obj in IMM_INTS:
stream.append(IMM_INTS[obj])
else:
obj = str(obj)
l = len(obj)
if l < 256:
stream.append(TAG_INT_L1 + I1.pack(l) + obj)
else:
stream.append(TAG_INT_L4 + I4.pack(l) + obj)
#@register(_dump_registry, long)
#def _dump_long(obj, stream):
# stream.append(TAG_LONG)
# _dump_int(obj, stream)
@register(_dump_registry, unicode)
def _dump_str(obj, stream):
obj = obj.encode('utf8')
l = len(obj)
if l == 0:
stream.append(TAG_EMPTY_STR)
elif l == 1:
stream.append(TAG_STR1 + obj)
elif l == 2:
stream.append(TAG_STR2 + obj)
elif l == 3:
stream.append(TAG_STR3 + obj)
elif l == 4:
stream.append(TAG_STR4 + obj)
elif l < 256:
stream.append(TAG_STR_L1 + I1.pack(l) + obj)
else:
stream.append(TAG_STR_L4 + I4.pack(l) + obj)
@register(_dump_registry, float)
def _dump_float(obj, stream):
stream.append(TAG_FLOAT + F8.pack(obj))
@register(_dump_registry, complex)
def _dump_complex(obj, stream):
stream.append(TAG_COMPLEX + C16.pack(obj.real, obj.imag))
#@register(_dump_registry, unicode)
#def _dump_unicode(obj, stream):
# stream.append(TAG_UNICODE)
# _dump_str(obj.encode("utf8"), stream)
@register(_dump_registry, tuple)
def _dump_tuple(obj, stream):
l = len(obj)
if l == 0:
stream.append(TAG_EMPTY_TUPLE)
elif l == 1:
stream.append(TAG_TUP1)
elif l == 2:
stream.append(TAG_TUP2)
elif l == 3:
stream.append(TAG_TUP3)
elif l == 4:
stream.append(TAG_TUP4)
elif l < 256:
stream.append(TAG_TUP_L1 + I1.pack(l))
else:
stream.append(TAG_TUP_L4 + I4.pack(l))
for item in obj:
_dump(item, stream)
@register(_dump_registry, list)
def _dump_list(obj, stream):
l = len(obj)
if l == 0:
stream.append(TAG_EMPTY_LIST)
elif l == 1:
stream.append(TAG_LIST1)
elif l < 256:
stream.append(TAG_LIST_L1 + I1.pack(l))
else:
stream.append(TAG_LIST_L4 + I4.pack(l))
for item in obj:
_dump(item, stream)
def _undumpable(obj, stream):
raise TypeError("cannot dump %r" % (obj,))
def _dump(obj, stream):
_dump_registry.get(type(obj), _undumpable)(obj, stream)
#===============================================================================
# loading
#===============================================================================
@register(_load_registry, TAG_NONE)
def _load_none(_stream):
return None
@register(_load_registry, TAG_NOT_IMPLEMENTED)
def _load_nonimp(_stream):
return NotImplemented
@register(_load_registry, TAG_ELLIPSIS)
def _load_elipsis(_stream):
return Ellipsis
@register(_load_registry, TAG_TRUE)
def _load_true(_stream):
return True
@register(_load_registry, TAG_FALSE)
def _load_false(_stream):
return False
@register(_load_registry, TAG_EMPTY_TUPLE)
def _load_empty_tuple(_stream):
return ()
@register(_load_registry, TAG_EMPTY_LIST)
def _load_empty_list(_stream):
return []
@register(_load_registry, TAG_EMPTY_STR)
def _load_empty_str(_stream):
return u""
#@register(_load_registry, TAG_UNICODE)
#def _load_unicode(stream):
# obj = _load(stream)
# return obj.decode("utf-8")
#@register(_load_registry, TAG_LONG)
#def _load_long(stream):
# obj = _load(stream)
# return long(obj)
@register(_load_registry, TAG_FLOAT)
def _load_float(stream):
return F8.unpack(stream.read(8))[0]
@register(_load_registry, TAG_COMPLEX)
def _load_complex(stream):
real, imag = C16.unpack(stream.read(16))
return complex(real, imag)
@register(_load_registry, TAG_STR1)
def _load_str1(stream):
return stream.read(1).decode('utf8')
@register(_load_registry, TAG_STR2)
def _load_str2(stream):
return stream.read(2).decode('utf8')
@register(_load_registry, TAG_STR3)
def _load_str3(stream):
return stream.read(3).decode('utf8')
@register(_load_registry, TAG_STR4)
def _load_str4(stream):
return stream.read(4).decode('utf8')
@register(_load_registry, TAG_STR_L1)
def _load_str_l1(stream):
l, = I1.unpack(stream.read(1))
return stream.read(l).decode('utf8')
@register(_load_registry, TAG_STR_L4)
def _load_str_l4(stream):
l, = I4.unpack(stream.read(4))
return stream.read(l).decode('utf8')
@register(_load_registry, TAG_TUP1)
def _load_tup1(stream):
return (_load(stream),)
@register(_load_registry, TAG_TUP2)
def _load_tup2(stream):
return (_load(stream), _load(stream))
@register(_load_registry, TAG_TUP3)
def _load_tup3(stream):
return (_load(stream), _load(stream), _load(stream))
@register(_load_registry, TAG_TUP4)
def _load_tup4(stream):
return (_load(stream), _load(stream), _load(stream), _load(stream))
@register(_load_registry, TAG_TUP_L1)
def _load_tup_l1(stream):
l, = I1.unpack(stream.read(1))
return tuple(_load(stream) for i in range(l))
@register(_load_registry, TAG_TUP_L4)
def _load_tup_l4(stream):
l, = I4.unpack(stream.read(4))
return tuple(_load(stream) for i in xrange(l))
@register(_load_registry, TAG_LIST1)
def _load_list1(stream):
return [_load(stream)]
@register(_load_registry, TAG_LIST_L1)
def _load_list_l1(stream):
l, = I1.unpack(stream.read(1))
return list(_load(stream) for i in range(l))
@register(_load_registry, TAG_LIST_L4)
def _load_list_l4(stream):
l, = I4.unpack(stream.read(4))
return list(_load(stream) for i in xrange(l))
@register(_load_registry, TAG_SLICE)
def _load_slice(stream):
start, stop, step = _load(stream)
return slice(start, stop, step)
@register(_load_registry, TAG_FSET)
def _load_frozenset(stream):
return frozenset(_load(stream))
@register(_load_registry, TAG_INT_L1)
def _load_int_l1(stream):
l, = I1.unpack(stream.read(1))
return int(stream.read(l))
@register(_load_registry, TAG_INT_L4)
def _load_int_l4(stream):
l, = I4.unpack(stream.read(4))
return int(stream.read(l))
def _load(stream):
tag = stream.read(1)
if tag in IMM_INTS_LOADER:
return IMM_INTS_LOADER[tag]
return _load_registry.get(tag)(stream)
#===============================================================================
# API
#===============================================================================
def dump(obj):
"""dumps the given object to a byte-string representation"""
stream = []
_dump(obj, stream)
return empty_bytes.join(stream)
def load(data):
"""loads the given byte-string representation to an object"""
if not py3k:
stream = StringIO(data)
else:
stream = BytesIO(data)
return _load(stream)
simple_types = frozenset([type(None), int, long, bool, str, float, unicode,
slice, complex, type(NotImplemented), type(Ellipsis)])
def dumpable(obj):
"""indicates whether the object is dumpable by brine"""
if type(obj) in simple_types:
return True
if type(obj) in (tuple, list, frozenset):
return all(dumpable(item) for item in obj)
return False
if __name__ == "__main__":
x = (u"he", 7, u"llo", 8, (), 900, None, True, Ellipsis, 18.2, 18.2j + 13,
slice(1,2,3), frozenset([5,6,7]), [8,9,10], NotImplemented)
assert dumpable(x)
y = dump(x)
z = load(y)
assert x == z
|