Skip to content

Commit

Permalink
Start refactoring control.py
Browse files Browse the repository at this point in the history
  • Loading branch information
rec committed Oct 3, 2021
1 parent 60d8c10 commit efd948f
Showing 1 changed file with 28 additions and 48 deletions.
76 changes: 28 additions & 48 deletions xled/control.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,7 @@ def check_status(self):
:raises ApplicationError: on application error
:rtype: :class:`~xled.response.ApplicationResponse`
"""
url = urljoin(self.base_url, "status")
response = self.session.get(url)
app_response = ApplicationResponse(response)
required_keys = [u"code"]
assert all(key in app_response.keys() for key in required_keys)
return app_response
return self._get("status", "code")

def delete_movies(self):
"""
Expand All @@ -120,12 +115,7 @@ def delete_movies(self):
:raises ApplicationError: on application error
:rtype: :class:`~xled.response.ApplicationResponse`
"""
url = urljoin(self.base_url, "movies")
response = self.session.delete(url)
app_response = ApplicationResponse(response)
required_keys = [u"code"]
assert all(key in app_response.keys() for key in required_keys)
return app_response
return self._get("movies", "code")

This comment has been minimized.

Copy link
@melange396

melange396 Oct 3, 2021

should this be ._delete() ?

This comment has been minimized.

Copy link
@rec

rec via email Oct 3, 2021

Author Owner

def delete_playlist(self):
"""
Expand All @@ -136,12 +126,7 @@ def delete_playlist(self):
:raises ApplicationError: on application error
:rtype: :class:`~xled.response.ApplicationResponse`
"""
url = urljoin(self.base_url, "playlist")
response = self.session.delete(url)
app_response = ApplicationResponse(response)
required_keys = [u"code"]
assert all(key in app_response.keys() for key in required_keys)
return app_response
return self._delete("playlist", "code")

def firmware_0_update(self, firmware):
"""
Expand All @@ -151,10 +136,7 @@ def firmware_0_update(self, firmware):
:raises ApplicationError: on application error
:rtype: :class:`~xled.response.ApplicationResponse`
"""
url = urljoin(self.base_url, "fw/0/update")
response = self.session.post(url, data=firmware)
app_response = ApplicationResponse(response)
return app_response
return self._post("fw/0/update", data=firmware)

def firmware_1_update(self, firmware):
"""
Expand All @@ -164,10 +146,7 @@ def firmware_1_update(self, firmware):
:raises ApplicationError: on application error
:rtype: :class:`~xled.response.ApplicationResponse`
"""
url = urljoin(self.base_url, "fw/1/update")
response = self.session.post(url, data=firmware)
app_response = ApplicationResponse(response)
return app_response
return self._post("fw/1/update", data=firmware)

def firmware_update(self, stage0_sha1sum, stage1_sha1sum=None):
"""
Expand All @@ -191,10 +170,8 @@ def firmware_update(self, stage0_sha1sum, stage1_sha1sum=None):
"stage0_sha1sum": stage0_sha1sum,
}
}
url = urljoin(self.base_url, "fw/update")
response = self.session.post(url, json=json_payload)
app_response = ApplicationResponse(response)
return app_response

return self._post("fw/update", json=json_payload)

def firmware_version(self):
"""
Expand All @@ -203,12 +180,7 @@ def firmware_version(self):
:raises ApplicationError: on application error
:rtype: :class:`~xled.response.ApplicationResponse`
"""
url = urljoin(self.base_url, "fw/version")
response = self.session.get(url)
app_response = ApplicationResponse(response)
required_keys = [u"version", u"code"]
assert all(key in app_response.keys() for key in required_keys)
return app_response
return self._get("fw/version", u"version", u"code")

def get_brightness(self):
"""
Expand All @@ -217,12 +189,7 @@ def get_brightness(self):
:raises ApplicationError: on application error
:rtype: :class:`~xled.response.ApplicationResponse`
"""
url = urljoin(self.base_url, "led/out/brightness")
response = self.session.get(url)
app_response = ApplicationResponse(response)
required_keys = [u"code", u"mode", u"value"]
assert all(key in app_response.keys() for key in required_keys)
return app_response
return self._get("led/out/brightness", u"code", u"mode", u"value")

def get_device_info(self):
"""
Expand All @@ -231,12 +198,8 @@ def get_device_info(self):
:raises ApplicationError: on application error
:rtype: :class:`~xled.response.ApplicationResponse`
"""
url = urljoin(self.base_url, "gestalt")
response = self.session.get(url)
app_response = ApplicationResponse(response)
required_keys = [u"code"] # and several more, dependent on fw version
assert all(key in app_response.keys() for key in required_keys)
return app_response
return self._get("gestalt", u"code")
# and several more, dependent on fw version

def get_device_name(self):
"""
Expand Down Expand Up @@ -1717,3 +1680,20 @@ def load_movie(self, name):
def set_static_color(self, red, green, blue):
# This function can really be removed now, as there are several fuctions for creating patterns
self.show_pattern(self.make_solid_pattern((red, green, blue)))

def _get(self, tag, *required_keys):
return self._response('get', tag, required_keys)

def _delete(self, tag, *required_keys):
return self._response('delete', tag, required_keys, {})

def _post(self, tag, *required_keys, **kwargs):
return self._response('delete', tag, required_keys, kwargs)

def _response(self, methodname, tag, required_keys, kwargs):
url = urljoin(self.base_url, tag)
method = getattr(self.session, methodname)
response = method(url, **kwargs)
app_response = ApplicationResponse(response)
assert all(key in app_response.keys() for key in required_keys)
return app_response

0 comments on commit efd948f

Please sign in to comment.