Enhanced ability to read from stdin

This commit is contained in:
Shy 2017-03-25 23:45:42 +01:00
parent a54901b5d6
commit 4bbcffc406

View file

@ -178,23 +178,24 @@ class C4Room:
def _interactive_light_switch(self): def _interactive_light_switch(self):
# Interactively ask for input # Interactively ask for input
print("[{}]".format(self.name)) if sys.stdin.isatty():
print("Please enter 0 or 1 for every lamp:") print("[{}]".format(self.name))
for level in range(len(self.switches)): print("Please enter 0 or 1 for every light:")
print((level * '|') + ",- " + self.switches[level][0]) for level in range(len(self.switches)):
print((level * '|') + ",- " + self.switches[level][0])
# Current state of witches # Current state of switches
state = "" state = ""
req = [] req = []
for t in self.switches: for t in self.switches:
req.append(t[1]) req.append(t[1])
responce = self.c4.pull(req) responce = self.c4.pull(req)
for sw in self.switches: for sw in self.switches:
for r in responce: for r in responce:
if r.topic == sw[1]: if r.topic == sw[1]:
state = state + str(int.from_bytes(r.payload, state = state + str(int.from_bytes(r.payload,
byteorder="little")) byteorder="little"))
print(state) # Present current state print(state) # Present current state
try: try:
userinput = sys.stdin.readline().rstrip('\n') userinput = sys.stdin.readline().rstrip('\n')
@ -208,9 +209,14 @@ class C4Room:
"""Switch lamps in this rooms on and off.""" """Switch lamps in this rooms on and off."""
if not userinput: if not userinput:
userinput = self._interactive_light_switch() userinput = self._interactive_light_switch()
if userinput == "": return
if not userinput.isdecimal():
print("You're not paying attention!", file=sys.stderr)
return
if len(userinput) != len(self.switches): if len(userinput) != len(self.switches):
if int(userinput) <= 15 and len(bin(int(userinput))) <= len(self.switches)+2: if len(bin(int(userinput))) <= len(self.switches)+2:
# +2 because bin() returns something like 'b0...' # +2 because bin() returns something like 'b0...'
# Try to interpret as integer # Try to interpret as integer
binary = bin(int(userinput))[2:] binary = bin(int(userinput))[2:]
@ -677,7 +683,7 @@ class ColorScheme:
cfg_dir = self._get_cfg_dir() cfg_dir = self._get_cfg_dir()
if not cfg_dir: if not cfg_dir:
print("Error: could not load preset!") print("Error: could not load preset!")
return None return
# Expand preset name # Expand preset name
preset = self._expand_preset(preset) preset = self._expand_preset(preset)
@ -687,7 +693,7 @@ class ColorScheme:
fd = open(fn) fd = open(fn)
except OSError: except OSError:
print("Error: could not load preset \"{}\" (file could not be accessed)!".format(preset)) print("Error: could not load preset \"{}\" (file could not be accessed)!".format(preset))
return None return
# Parse the preset file # Parse the preset file
self.mapping = {} self.mapping = {}
@ -764,7 +770,10 @@ class ColorScheme:
# Get current states # Get current states
c4 = C4Interface() c4 = C4Interface()
fd.write("# Preset \"{}\" (auto generated)\n".format(name)) if name == '-':
fd.write("# Preset (auto generated)\n".format(name))
else:
fd.write("# Preset \"{}\" (auto generated)\n".format(name))
fd.write("# Note: \"/master\" topics override every other topic in the room.\n") fd.write("# Note: \"/master\" topics override every other topic in the room.\n")
for room in Wohnzimmer, Plenarsaal, Fnordcenter: for room in Wohnzimmer, Plenarsaal, Fnordcenter:
topics = [] topics = []
@ -1079,15 +1088,19 @@ if __name__ == "__main__":
# Colorscheme # Colorscheme
if args.store_as: if args.store_as:
ColorScheme().store(args.store_as) ColorScheme().store(args.store_as)
presets = {} # Store and reuse initialized presets
if args.w_color: if args.w_color:
preset = ColorScheme(autoinit=args.w_color) if args.w_color not in presets:
if preset: Wohnzimmer().set_colorscheme(preset, args.magic) presets[args.w_color] = ColorScheme(autoinit=args.w_color)
if presets[args.w_color]: Wohnzimmer().set_colorscheme(presets[args.w_color], args.magic)
if args.p_color: if args.p_color:
preset = ColorScheme(autoinit=args.p_color) if args.p_color not in presets:
if preset: Plenarsaal().set_colorscheme(preset, args.magic) presets[args.p_color] = ColorScheme(autoinit=args.p_color)
if presets[args.p_color]: Plenarsaal().set_colorscheme(presets[args.p_color], args.magic)
if args.f_color: if args.f_color:
preset = ColorScheme(autoinit=args.f_color) if args.f_color not in presets:
if preset: Fnordcenter().set_colorscheme(preset, args.magic) presets[args.f_color] = ColorScheme(autoinit=args.f_color)
if presets[args.f_color]: Fnordcenter().set_colorscheme(presets[args.f_color], args.magic)
if args.list_presets: if args.list_presets:
ColorScheme().list_available() ColorScheme().list_available()