Skip to content

Commit

Permalink
Merge pull request #121 from waveform80/cbor-io
Browse files Browse the repository at this point in the history
CBOR I/O
  • Loading branch information
waveform80 committed Mar 4, 2019
2 parents 4712e1b + 1d0e144 commit 2a12432
Show file tree
Hide file tree
Showing 55 changed files with 3,758 additions and 1,725 deletions.
25 changes: 25 additions & 0 deletions LICENSE.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,28 @@ INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.

------------------------------------------------------------------------------

The following copyright and license applies to the included cbor2 module only
(all files under piwheels/cbor2):

Copyright (c) Alex Grönholm

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.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
2 changes: 1 addition & 1 deletion coverage.cfg
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[run]
branch = True
include = piwheels/*
;omit = */bar.py,*/baz.py
omit = piwheels/cbor2/*

[report]
ignore_errors = True
Expand Down
16 changes: 8 additions & 8 deletions docs/slave_protocol.dot
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
digraph slave {
/* Sent messages */
node [shape=house, fontname=Sans, fontsize=10, style=filled, fillcolor="#ffaaaa", penwidth=0];
HELLO1 [label="HELLO"];
HELLO [label="HELLO"];
IDLE;
BUILT;
SENT;
BYE2 [label="BYE"];
BYE [label="BYE"];

/* Received messages */
node [shape=invhouse, fontname=Sans, fontsize=10, style=filled, fillcolor="#aaffaa", penwidth=0];
HELLO2 [label="HELLO"];
ACK [label="ACK"];
BUILD;
SEND;
DONE;
SLEEP;
BYE1 [label="BYE"];
DIE [label="DIE"];

/* Edges */
edge [fontname=Sans, fontsize=10];
HELLO1->HELLO2 [label="[timeout,\npyver,\nabi,\n..."];
HELLO2->IDLE [label="[id]"];
HELLO->ACK [label="[timeout,\npyver,\nabi,\n..."];
ACK->IDLE [label="[id]"];
IDLE->BUILD [label="[package,\nversion]"];
IDLE->SLEEP;
IDLE->BYE1;
IDLE->DIE;
SLEEP->IDLE;
BUILD->BUILT [label="[status,\nduration,\noutput,\n..."];
BUILT->DONE [label="failure"];
Expand All @@ -31,5 +31,5 @@ digraph slave {
SENT->DONE [label="success"];
SENT->SEND [label="failure/more"];
DONE->IDLE;
BYE1->BYE2;
DIE->BYE;
}
2 changes: 1 addition & 1 deletion piwheels/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
__url__ = 'https://www.piwheels.org/'
__platforms__ = 'ALL'

__requires__ = ['configargparse', 'pyzmq']
__requires__ = ['configargparse', 'pyzmq', 'voluptuous']

__extra_requires__ = {
'monitor': ['urwid'],
Expand Down
3 changes: 3 additions & 0 deletions piwheels/cbor2/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from .decoder import load, loads, CBORDecoder, CBORDecodeError # noqa
from .encoder import dump, dumps, CBOREncoder, CBOREncodeError, shareable_encoder # noqa
from .types import CBORTag, CBORSimpleValue, undefined # noqa
109 changes: 109 additions & 0 deletions piwheels/cbor2/compat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
from math import ldexp
import struct
import sys


if sys.version_info.major < 3:
from datetime import tzinfo, timedelta
from binascii import unhexlify

class timezone(tzinfo):
def __init__(self, offset):
self.offset = offset

def utcoffset(self, dt):
return self.offset

def dst(self, dt):
return timedelta(0)

def tzname(self, dt):
return 'UTC+00:00'

def as_unicode(string):
return string.decode('utf-8')

def iteritems(self):
return self.iteritems()

def int2bytes(i):
hexstr = '%x' % i
n = len(hexstr)
pad = ('', '0')[n & 1]
return unhexlify(pad + hexstr)

byte_as_integer = ord
timezone.utc = timezone(timedelta(0))
xrange = xrange # noqa: F821
long = long # noqa: F821
unicode = unicode # noqa: F821
else:
from datetime import timezone

def byte_as_integer(bytestr):
return bytestr[0]

def as_unicode(string):
return string

def iteritems(self):
return self.items()

def int2bytes(i):
bits = i.bit_length()
return i.to_bytes((bits + 7) // 8, 'big')

xrange = range
long = int
unicode = str


if sys.version_info.major >= 3 and sys.version_info.minor >= 6: # pragma: no cover
# Python 3.6 added 16 bit floating point to struct

def pack_float16(value):
pass

def unpack_float16(payload):
pass

else:
def pack_float16(value):
# Based on node-cbor by hildjj
# which was based in turn on Carsten Borman's cn-cbor
try:
u32 = struct.pack('>f', value)
except OverflowError:
return False

u = struct.unpack('>I', u32)[0]
if u & 0x1FFF != 0:
return False

s16 = (u >> 16) & 0x8000
exponent = (u >> 23) & 0xff
mantissa = u & 0x7fffff

if 113 <= exponent <= 142:
s16 += ((exponent - 112) << 10) + (mantissa >> 13)
elif 103 <= exponent < 113:
if mantissa & ((1 << (126 - exponent)) - 1):
return False

s16 += ((mantissa + 0x800000) >> (126 - exponent))
else:
return False

return struct.pack('>BH', 0xf9, s16)

def unpack_float16(payload):
# Code adapted from RFC 7049, appendix D
def decode_single(single):
return struct.unpack("!f", struct.pack("!I", single))[0]

payload = struct.unpack('>H', payload)[0]
value = (payload & 0x7fff) << 13 | (payload & 0x8000) << 16
if payload & 0x7c00 != 0x7c00:
return ldexp(decode_single(value), 112)

return decode_single(value | 0x7f800000)

0 comments on commit 2a12432

Please sign in to comment.