net.py: typo fix + catch the right exception
[breadcrumb.git] / code / breadcrumb / server / decoding.py
blob0a321567581125d12fed25bcae4c4c8ab8dfdc3c
1 # -*- coding: utf8 -*-
2 """Functions for decoding crumbs."""
4 # Copyright (C) 2008 Laurens Van Houtven <lvh at laurensvh.be>
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18 import os
19 import sys
20 import struct
21 import logging
23 sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
24 from common.checksum import generate_byte_checksum as generate_checksum
25 from common.consts import CRUMB_FIELDS
27 def decode_crumb(crumb):
28 """Decodes a crumb into a dictionary."""
29 # Unpack the crumb
30 header, content, checksum = crumb[0], crumb[1:-1], crumb[-1]
32 logging.debug("Got crumb: %s" % ':'.join('%02X' % ord(c) for c in crumb))
34 # Checksum
35 if generate_checksum(header + content) != checksum:
36 logging.error("Got invalid checksum, ignoring this crumb...")
37 return
39 keys = []
40 formats = ['<'] # forced little-endianness prevents useless padding
42 fields = zip(unpack_bools(header), CRUMB_FIELDS)
43 for present, field_data in fields:
44 if present: # the field is contained in the crumb
45 attr_name, attr_keys, attr_format = field_data
46 logging.debug("Decoding attribute %s from crumb..." % attr_name)
47 logging.debug("Keys: %s, format: %s" % (attr_keys, attr_format))
49 keys.extend(attr_keys)
50 formats.append(attr_format)
52 logging.debug("Done decoding attributes from the header byte.")
54 format = ''.join(formats)
55 values = struct.unpack(format, content)
57 point = dict(zip(keys, values))
58 logging.debug("Point data: %s" % repr(point))
60 return point
62 def unpack_bools(byte):
63 """Unpacks a byte into a list of eight booleans.
65 The byte is expected to be little-endian.
67 >>> unpackbools('\\x00')
68 [False, False, False, False, False, False, False, False]
70 >>> unpackbools('\\x01')
71 [True, False, False, False, False, False, False, False]
73 >>> unpackbools('\\xff')
74 [True, True, True, True, True, True, True, True]
75 """
76 return [bool((ord(byte) >> i) & 1) for i in xrange(8)]