diff options
author | Marek Siarkowicz <mareksiarkowicz@gmail.com> | 2015-06-14 23:26:05 +0200 |
---|---|---|
committer | Marek Siarkowicz <mareksiarkowicz@gmail.com> | 2015-06-14 23:26:05 +0200 |
commit | 43d3e73661ae265d0af314303310ed4dcb1ce780 (patch) | |
tree | 36a2487feeb25f5bd555d95f0a770ffcfe63c620 | |
parent | 1bae288e1e03210b25f7159adb43581f1d18a353 (diff) | |
download | pyParrotZikTCP-43d3e73661ae265d0af314303310ed4dcb1ce780.tar.xz pyParrotZikTCP-43d3e73661ae265d0af314303310ed4dcb1ce780.zip |
Refactor.
Diffstat (limited to '')
-rw-r--r-- | resource_manager.py | 29 |
1 files changed, 13 insertions, 16 deletions
diff --git a/resource_manager.py b/resource_manager.py index b343807..695314b 100644 --- a/resource_manager.py +++ b/resource_manager.py @@ -18,34 +18,31 @@ class ResourceManagerBase(object): return self.fetch(resource) def fetch(self, resource): - assert resource in self.resources, 'Unknown resource {}'.format(resource) - assert 'get' in self.resources[resource], 'Unhandled method' - message = ParrotProtocol.getRequest(resource + '/get') - result = self.send_message(message) + result = self.send_message(self._create_message(resource, 'get')) self.resource_values[resource] = result return result def toggle_on(self, resource): - assert resource in self.resources, 'Unknown resource {}'.format(resource) - assert 'enable' in self.resources[resource], 'Unhandled method' - message = ParrotProtocol.getRequest(resource + '/enable') - self.send_message(message) + self.send_message(self._create_message(resource, 'enable')) self.fetch(resource) def toggle_off(self, resource): - assert resource in self.resources, 'Unknown resource {}'.format(resource) - assert 'disable' in self.resources[resource], 'Unhandled method' - message = ParrotProtocol.getRequest(resource + '/disable') - self.send_message(message) + self.send_message(self._create_message(resource, 'disable')) self.fetch(resource) def set(self, resource, arg): - assert resource in self.resources, 'Unknown resource {}'.format(resource) - assert 'set' in self.resources[resource], 'Unhandled method' - message = ParrotProtocol.setRequest(resource + '/set', str(arg).lower()) - self.send_message(message) + self.send_message(self._create_message(resource, 'set', arg)) self.fetch(resource) + def _create_message(self, resource, method, arg=None): + assert resource in self.resources, 'Unknown resource {}'.format(resource) + assert method in self.resources[resource], 'Unhandled method {} for {}'.format( + method, resource) + if method == 'set': + return ParrotProtocol.setRequest(resource + '/' + method, str(arg).lower()) + else: + return ParrotProtocol.getRequest(resource + '/' + method) + def send_message(self, message): try: self.sock.send(str(message)) |