From 16064921e0858ad735cfa55a02e7e6846bda0745 Mon Sep 17 00:00:00 2001 From: Shy Date: Thu, 25 May 2017 21:22:21 +0200 Subject: [PATCH] Move input parsing into separate function --- c4ctrl.py | 34 +++++++++++++++++++++------------- 1 file changed, 21 insertions(+), 13 deletions(-) diff --git a/c4ctrl.py b/c4ctrl.py index c0c145f..577a687 100755 --- a/c4ctrl.py +++ b/c4ctrl.py @@ -542,8 +542,8 @@ class C4Room: # {{{1 def get_switch_state(self, max_age=5): """ Returns current state of switches as a string of 1s and 0s. - max_age specifies how old (in seconds) a cached responce from a - previously done request may be before it is considered outdated. """ + max_age specifies how old (in seconds) a cached responce must be + before it is considered outdated. """ from time import time @@ -572,17 +572,8 @@ class C4Room: # {{{1 self._switch_state = (state, time()) return state - def light_switch(self, userinput=""): - """ Switch lamps in a room on or off. """ - - if not userinput: - # Derive user input from stdin. - userinput = self._interactive_light_switch() - if userinput == "": return - - if userinput == '-': - print(self.get_switch_state()) - return + def _parse_switch_input(self, userinput): + """ Parse user input to the switch command. """ # Let's support some binary operations! ops = "" # Store operators. @@ -667,6 +658,23 @@ class C4Room: # {{{1 userinput, switch_state)) ops = ops[:-1] + return userinput + + def light_switch(self, userinput=""): + """ Switch lamps in a room on or off. """ + + if not userinput: + # Derive user input from stdin. + userinput = self._interactive_light_switch() + if userinput == "": return + + if userinput == '-': + print(self.get_switch_state()) + return + + userinput = self._parse_switch_input(userinput) + if not userinput: sys.exit(1) + command=[] for i in range(len(self.switches)): # Skip unchanged switches if we happen to know their state.