Skip to content

Commit 7256118

Browse files
committedJul 7, 2021
Make attributes for dict and list members of json-derived objects
Requested by @davetapley Requires a major version bump
1 parent c383c72 commit 7256118

File tree

2 files changed

+31
-11
lines changed

2 files changed

+31
-11
lines changed
 

‎spypoint/client.py

+23-10
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
from typing import Collection, Dict
15+
from typing import Collection, Dict, List
1616

1717
import requests
1818

@@ -25,29 +25,42 @@
2525
FILTERS_URL = f'{API_BASE}/api/v3/photo/filters'
2626

2727

28-
class Camera:
29-
def __init__(self, id, **kwargs): # noqa
30-
for k, v in kwargs.items():
28+
class _AttrDict:
29+
def __init__(self, d): # noqa
30+
for k, v in d.items():
31+
if isinstance(v, Dict):
32+
v = _AttrDict(v)
33+
elif isinstance(v, List):
34+
v_new = list()
35+
for v_sub in v:
36+
if isinstance(v_sub, dict):
37+
v_new.append(_AttrDict(v_sub))
38+
else:
39+
v_new.append(v_sub)
40+
v = tuple(v_new)
3141
if k and k[0] != '_':
3242
setattr(self, k, v)
43+
44+
45+
class Camera(_AttrDict):
46+
def __init__(self, id, **kwargs): # noqa
47+
super(Camera, self).__init__(kwargs)
3348
self.id = id
3449

3550
def __repr__(self):
3651
return f'Camera({self.id})'
3752

3853

39-
class Photo:
54+
class Photo(_AttrDict):
4055
def __init__(self, **kwargs):
41-
for k, v in kwargs.items():
42-
if k and k[0] != '_':
43-
setattr(self, k, v)
56+
super(Photo, self).__init__(kwargs)
4457

4558
def __repr__(self):
4659
return f'Photo({self.id})' # noqa
4760

4861
def url(self, size='large'):
49-
section = getattr(self, size)
50-
return f'https://{section.get("host")}/{section.get("path")}'
62+
section = getattr(self, size, dict())
63+
return f'https://{getattr(section, "host")}/{getattr(section, "path")}'
5164

5265

5366
class Client:

‎test/test_client.py

+8-1
Original file line numberDiff line numberDiff line change
@@ -144,13 +144,19 @@ def test_tags(self, m: requests_mock.Mocker):
144144
class TestCamera(unittest.TestCase):
145145
tdata = json.loads('''
146146
{
147-
"id": "elided01"
147+
"id": "elided01",
148+
"status": "test_status",
149+
"config": {
150+
"name": "test_camera"
151+
}
148152
}
149153
''')
150154

151155
def test_camera(self):
152156
camera = c.Camera(**self.tdata)
153157
self.assertEqual(self.tdata['id'], camera.id)
158+
self.assertEqual(self.tdata['status'], camera.status)
159+
self.assertEqual(self.tdata['config']['name'], camera.config.name)
154160

155161

156162
class TestPhoto(unittest.TestCase):
@@ -219,4 +225,5 @@ def test_url_large(self):
219225

220226
def _test_url(self, size):
221227
p = c.Photo(**self.tdata)
228+
self.assertEqual(self.tdata['small']['headers'][0]['name'], p.small.headers[0].name)
222229
self.assertEqual(f'https://s3.amazonaws.com/{self.tdata[size]["path"]}', p.url(size))

1 commit comments

Comments
 (1)

rambis1991 commented on Apr 29, 2024

@rambis1991

Sorry I’m not a developer but I do like to Splunk most all of my IOT data. Haven’t used this script yet to pull SPYPOINT pic URLs, but would definitely like to have power / cell signal / firmware status info on my cameras pulled on an hourly basis. Any chance you or the team could tweak the script to pull that data?

Please sign in to comment.