diff --git a/CHANGELOG.md b/CHANGELOG.md index 64a317f..ff0ac90 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/iotile_cloud/utils/gid.py b/iotile_cloud/utils/gid.py index 731c245..9a979a2 100644 --- a/iotile_cloud/utils/gid.py +++ b/iotile_cloud/utils/gid.py @@ -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: @@ -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) @@ -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()]) @@ -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), diff --git a/tests/test_gid.py b/tests/test_gid.py index 28eb3ff..9e88294 100644 --- a/tests/test_gid.py +++ b/tests/test_gid.py @@ -49,6 +49,9 @@ 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') @@ -56,10 +59,11 @@ def test_device_slug(self): 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) diff --git a/version.py b/version.py index c0bfba8..742dbb9 100644 --- a/version.py +++ b/version.py @@ -1 +1 @@ -version = '0.8.10' +version = '0.8.11'