Skip to content

Commit

Permalink
Change IOTileDeviceSlug back to accepting 64bits by default (#35)
Browse files Browse the repository at this point in the history
But add option to check for 48bit limit
  • Loading branch information
dkarchmer authored Mar 4, 2018
1 parent 1e27f54 commit b9eca57
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 9 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
### v0.8.11 (2018-03-04)

* Change IOTileDeviceSlug back to accepting 64bits by default, but add a new allow_64bits option to be turned off
if required to enforce 48bit checks

### v0.8.10 (2018-03-02)

* Fix bug and missing tests in IOTileFleetSlug
Expand Down
16 changes: 10 additions & 6 deletions iotile_cloud/utils/gid.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,17 @@ def __init__(self, id):
class IOTileDeviceSlug(IOTileCloudSlug):
"""Formatted Global Device ID: d--0000-0000-0000-0001"""

def __init__(self, id):
def __init__(self, id, allow_64bits=True):
# For backwards compatibility, allow 64 bit IDs if required
# Meaning that the device may in fact be a data block
hex_count = 16 if allow_64bits else 12

if isinstance(id, IOTileDeviceSlug):
self._slug = id._slug
return

if isinstance(id, int):
if id <= 0 or id >= pow(16, 12):
if id <= 0 or id >= pow(16, hex_count):
raise ValueError('IOTileDeviceSlug: UUID should be greater than zero and less than 16^12')
did = int2did(id)
else:
Expand All @@ -122,9 +126,8 @@ def __init__(self, id):

# Convert to int and back to get rid of anything above 48 bits
id = gid2int(did)
if id <= 0 or id >= pow(16, 12):
if id <= 0 or id >= pow(16, hex_count):
raise ValueError('IOTileDeviceSlug: UUID should be greater than zero and less than 16^12')
did = int2did(id)

self.set_from_single_id_slug('d', 4, did)

Expand Down Expand Up @@ -301,7 +304,8 @@ def from_parts(self, project, device, variable):
if not isinstance(project, IOTileProjectSlug):
project = IOTileProjectSlug(project)
if not isinstance(device, IOTileDeviceSlug):
device = IOTileDeviceSlug(device)
# Allow 64bits to handle blocks
device = IOTileDeviceSlug(device, allow_64bits=True)
if not isinstance(variable, IOTileVariableSlug):
variable = IOTileVariableSlug(variable)
self._slug = gid_join(['s', project.formatted_id(), device.formatted_id(), variable.formatted_local_id()])
Expand All @@ -310,7 +314,7 @@ def get_parts(self):
parts = gid_split(self._slug)
assert(len(parts) == 4)
project = IOTileProjectSlug(parts[1])
device = IOTileDeviceSlug(parts[2])
device = IOTileDeviceSlug(parts[2], allow_64bits=True)
variable = IOTileVariableSlug(parts[3], project)
result = {
'project': str(project),
Expand Down
8 changes: 6 additions & 2 deletions tests/test_gid.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,21 @@ def test_device_slug(self):
id = IOTileDeviceSlug('d--1234')
self.assertEqual(str(id), 'd--0000-0000-0000-1234')

id = IOTileDeviceSlug('d--1234-0000-0000-0001', allow_64bits=True)
self.assertEqual(str(id), 'd--1234-0000-0000-0001')

id = IOTileDeviceSlug('0005')
self.assertEqual(str(id), 'd--0000-0000-0000-0005')
self.assertEqual(id.formatted_id(), '0000-0000-0000-0005')

self.assertRaises(ValueError, IOTileDeviceSlug, 'string')
self.assertRaises(ValueError, IOTileDeviceSlug, 'x--0000-0000-0000-0001')
self.assertRaises(ValueError, IOTileDeviceSlug, '0000-0000-0000-0000')
self.assertRaises(ValueError, IOTileDeviceSlug, 'd--1234-0000-0000-0001') # > 48bts
self.assertRaises(ValueError, IOTileDeviceSlug, 'd--1234-0000-0000-0001', False) # > 48bts
self.assertRaises(ValueError, IOTileDeviceSlug, -5)
self.assertRaises(ValueError, IOTileDeviceSlug, 0)
self.assertRaises(ValueError, IOTileDeviceSlug, pow(16,12))
self.assertRaises(ValueError, IOTileDeviceSlug, pow(16,16))
self.assertRaises(ValueError, IOTileDeviceSlug, pow(16,12), False)

def test_block_slug(self):
id = IOTileBlockSlug(5)
Expand Down
2 changes: 1 addition & 1 deletion version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
version = '0.8.10'
version = '0.8.11'

0 comments on commit b9eca57

Please sign in to comment.