Skip to content

Commit

Permalink
Add support for streams with no project (#37)
Browse files Browse the repository at this point in the history
e.g. s----0000-0000-0000-0002--0003
  • Loading branch information
dkarchmer authored Mar 28, 2018
1 parent 84eb402 commit a9b52a1
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 5 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
### v0.8.13 (2018-03-28)

* Allow IOTileStreamSlug to take a project that is None or ''

### v0.8.12 (2018-03-07)

* Fix typo in IOTileProjectSlug preventing `p--0000-0000` from been accepted as valid.
Expand Down
14 changes: 10 additions & 4 deletions iotile_cloud/utils/gid.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,12 +296,18 @@ def __init__(self, id=None):
if id:
if not isinstance(id, str):
raise ValueError("Variable ID must be int or str")
if len(gid_split(id)) != 4:
parts = gid_split(id)
if len(parts) != 4:
raise ValueError("Stream slug must have three terms: s--<prj>--<dev>--<var>")
self._slug = id
if parts[1] == '':
parts[1] = '0000-0000'
self._slug = gid_join(parts)

def from_parts(self, project, device, variable):
if not isinstance(project, IOTileProjectSlug):
if project == None or project == '':
# It is legal to pass something like `s----1234-5001` as projects are optional
project = IOTileProjectSlug(0)
elif not isinstance(project, IOTileProjectSlug):
project = IOTileProjectSlug(project)
if not isinstance(device, IOTileDeviceSlug):
# Allow 64bits to handle blocks
Expand All @@ -313,7 +319,7 @@ def from_parts(self, project, device, variable):
def get_parts(self):
parts = gid_split(self._slug)
assert(len(parts) == 4)
project = IOTileProjectSlug(parts[1])
project = IOTileProjectSlug(parts[1]) if parts[1] else IOTileProjectSlug(0)
device = IOTileDeviceSlug(parts[2], allow_64bits=True)
variable = IOTileVariableSlug(parts[3], project)
result = {
Expand Down
18 changes: 18 additions & 0 deletions tests/test_gid.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,18 @@ def test_stream_slug(self):
self.assertEqual(str(parts['device']), 'd--0000-0000-0000-0002')
self.assertEqual(str(parts['variable']), 'v--0000-0001--0003')

id = IOTileStreamSlug('s--0000-0000--0000-0000-0000-0002--0003')
self.assertEqual(str(id), 's--0000-0000--0000-0000-0000-0002--0003')

id = IOTileStreamSlug('s----0000-0000-0000-0002--0003')
self.assertEqual(str(id), 's--0000-0000--0000-0000-0000-0002--0003')

parts = id.get_parts()
self.assertEqual(str(parts['project']), 'p--0000-0000')
self.assertEqual(str(parts['device']), 'd--0000-0000-0000-0002')
self.assertEqual(str(parts['variable']), 'v--0000-0000--0003')


def test_stream_from_parts(self):
project = IOTileProjectSlug(5)
device = IOTileDeviceSlug(10)
Expand Down Expand Up @@ -178,6 +190,12 @@ def test_stream_from_parts(self):
id.from_parts(project=0, device=1, variable='5002')
self.assertEqual(str(id), 's--0000-0000--0000-0000-0000-0001--5002')

id = IOTileStreamSlug()
id.from_parts(project='', device=1, variable='5002')
self.assertEqual(str(id), 's--0000-0000--0000-0000-0000-0001--5002')
id.from_parts(project=None, device=1, variable='5002')
self.assertEqual(str(id), 's--0000-0000--0000-0000-0000-0001--5002')

id = IOTileStreamSlug()
with pytest.raises(ValueError):
id.from_parts(project=-1, device=1, variable='5002')
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.12'
version = '0.8.13'

0 comments on commit a9b52a1

Please sign in to comment.